Skip to content

Commit be1008a

Browse files
committed
refactor: simplify core models and session utilities
1 parent f5544b6 commit be1008a

9 files changed

Lines changed: 25 additions & 41 deletions

File tree

src/main/java/dev/whitedev/jpi/agent/AgentOptions.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ public static AgentOptions parse(String value) {
2525
String host = required(values, "host");
2626
String token = required(values, "token");
2727
int port = Integer.parseInt(required(values, "port"));
28-
if (!InetAddress.getByName(host).isLoopbackAddress()) {
28+
InetAddress address = InetAddress.getByName(host);
29+
if (!address.isLoopbackAddress()) {
2930
throw new IllegalArgumentException("Agent only accepts a loopback host");
3031
}
3132
if (port < 1 || port > 65535 || token.length() < 16) {
3233
throw new IllegalArgumentException("Invalid agent port or token");
3334
}
34-
return new AgentOptions(InetAddress.getByName(host), port, token);
35+
return new AgentOptions(address, port, token);
3536
} catch (Exception exception) {
3637
if (exception instanceof IllegalArgumentException) throw (IllegalArgumentException) exception;
3738
throw new IllegalArgumentException("Invalid agent options", exception);

src/main/java/dev/whitedev/jpi/attach/CommandLineTokenizer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ public static List<String> parse(String commandLine) {
2121
} else if (value == '"') {
2222
quoted = !quoted;
2323
} else if (Character.isWhitespace(value) && !quoted) {
24-
add(result, current);
24+
flush(result, current);
2525
} else {
2626
current.append(value);
2727
}
2828
}
2929
if (escaping) current.append('\\');
3030
if (quoted) throw new IllegalArgumentException("Unclosed quote in arguments");
31-
add(result, current);
31+
flush(result, current);
3232
return result;
3333
}
3434

35-
private static void add(List<String> result, StringBuilder value) {
35+
private static void flush(List<String> result, StringBuilder value) {
3636
if (value.length() == 0) return;
3737
result.add(value.toString());
3838
value.setLength(0);

src/main/java/dev/whitedev/jpi/attach/InspectorSession.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ public final class InspectorSession implements Closeable {
2525
public JvmDescriptor target() { return target; }
2626

2727
public synchronized byte[] request(Operation operation, String payload) throws IOException {
28-
if (closed) throw new IOException("Inspector session is closed");
28+
ensureOpen();
2929
WireProtocol.writeRequest(output, operation, WireProtocol.utf8(payload == null ? "" : payload));
3030
WireProtocol.Response response = WireProtocol.readResponse(input);
3131
if (!response.success()) throw new IOException(response.text());
3232
return response.payload();
3333
}
3434

35+
private void ensureOpen() throws IOException {
36+
if (closed) throw new IOException("Inspector session is closed");
37+
}
38+
3539
public String requestText(Operation operation, String payload) throws IOException {
3640
return WireProtocol.text(request(operation, payload));
3741
}
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package dev.whitedev.jpi.attach;
22

3-
public final class JvmDescriptor {
4-
private final String id;
5-
private final String displayName;
6-
7-
public JvmDescriptor(String id, String displayName) {
8-
this.id = id;
9-
this.displayName = displayName == null || displayName.trim().isEmpty() ? "<unknown>" : displayName;
3+
public record JvmDescriptor(String id, String displayName) {
4+
public JvmDescriptor {
5+
displayName = displayName == null || displayName.isBlank() ? "<unknown>" : displayName;
106
}
117

12-
public String id() { return id; }
13-
public String displayName() { return displayName; }
148
@Override public String toString() { return id + " - " + displayName; }
159
}

src/main/java/dev/whitedev/jpi/attach/JvmDiscovery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import com.sun.tools.attach.VirtualMachine;
44
import com.sun.tools.attach.VirtualMachineDescriptor;
5-
import java.lang.management.ManagementFactory;
5+
66
import java.util.ArrayList;
77
import java.util.Comparator;
88
import java.util.List;
99

1010
public final class JvmDiscovery {
1111
public List<JvmDescriptor> discover() {
12-
String ownPid = ManagementFactory.getRuntimeMXBean().getName().split("@", 2)[0];
12+
String ownPid = Long.toString(ProcessHandle.current().pid());
1313
List<JvmDescriptor> result = new ArrayList<>();
1414
for (VirtualMachineDescriptor descriptor : VirtualMachine.list()) {
1515
if (!descriptor.id().equals(ownPid)) result.add(new JvmDescriptor(descriptor.id(), descriptor.displayName()));

src/main/java/dev/whitedev/jpi/decompile/DecompilerEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public enum DecompilerEngine {
1010

1111
DecompilerEngine(String displayName, String... resources) {
1212
this.displayName = displayName;
13-
this.resources = resources;
13+
this.resources = resources.clone();
1414
}
1515

1616
String[] resources() {

src/main/java/dev/whitedev/jpi/export/SessionSnapshotExporter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ public static Path export(InspectorSession session, Path output) throws IOExcept
3535

3636
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(destination), StandardCharsets.UTF_8)) {
3737
for (Map.Entry<String, String> report : reports.entrySet()) {
38-
zip.putNextEntry(new ZipEntry(report.getKey()));
39-
zip.write(report.getValue().getBytes(StandardCharsets.UTF_8));
40-
zip.closeEntry();
38+
writeEntry(zip, report);
4139
}
4240
}
4341
return destination;
4442
}
43+
44+
private static void writeEntry(ZipOutputStream zip, Map.Entry<String, String> report) throws IOException {
45+
zip.putNextEntry(new ZipEntry(report.getKey()));
46+
zip.write(report.getValue().getBytes(StandardCharsets.UTF_8));
47+
zip.closeEntry();
48+
}
4549
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
package dev.whitedev.jpi.nativeaccess;
22

3-
public final class MemoryMatch {
4-
private final long address;
5-
private final String value;
6-
private final int byteSize;
7-
public MemoryMatch(long address, String value, int byteSize) { this.address = address; this.value = value; this.byteSize = byteSize; }
8-
public long address() { return address; }
9-
public String value() { return value; }
10-
public int byteSize() { return byteSize; }
3+
public record MemoryMatch(long address, String value, int byteSize) {
114
}

tools/check-no-comments.ps1

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)