Skip to content

Commit e7a1e2a

Browse files
committed
Code Refactoring
1 parent 32c65b7 commit e7a1e2a

8 files changed

Lines changed: 84 additions & 84 deletions

File tree

‎src/main/java/liquidjava/mcp/runtime/AnalysisKey.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
import java.util.HexFormat;
1313
import java.util.List;
1414
import java.util.stream.Stream;
15-
import liquidjava.mcp.utils.Utils;
15+
import liquidjava.mcp.utils.PathUtils;
1616

1717
public record AnalysisKey(Path path, boolean debug, List<Source> sources) {
1818
private record Source(Path path, String hash) {}
1919

2020
public static AnalysisKey read(String input, boolean debug) throws IOException {
21-
Path path = Utils.canonicalPath(input);
21+
Path path = PathUtils.canonicalPath(input);
2222
List<Path> files = sourceFiles(path);
2323
List<Source> sources = new ArrayList<>();
2424
for (Path file : files) {

‎src/main/java/liquidjava/mcp/tools/context/ContextMapper.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77
import java.util.Map;
88

9+
import liquidjava.mcp.utils.PathUtils;
910
import liquidjava.mcp.utils.Utils;
1011
import liquidjava.processor.context.AliasWrapper;
1112
import liquidjava.processor.context.Context;
@@ -24,7 +25,7 @@ private ContextMapper() {}
2425
static Map<String, Object> locals(ContextRequest request) {
2526
ContextHistory history = ContextHistory.getInstance();
2627
List<Range> scopes = history.getFileScopes().entrySet().stream()
27-
.filter(entry -> Utils.samePath(entry.getKey(), request.file()))
28+
.filter(entry -> PathUtils.samePath(entry.getKey(), request.file()))
2829
.flatMap(entry -> entry.getValue().stream())
2930
.map(Range::parse)
3031
.toList();
@@ -73,7 +74,7 @@ private static List<Map<String, Object>> aliases(Context context) {
7374
private static List<Map<String, Object>> ghosts(Context context, String file) {
7475
return context.getGhostStates().stream()
7576
.filter(state -> state.getParent() == null)
76-
.filter(state -> file == null || state.getFile() != null && Utils.samePath(state.getFile(), file))
77+
.filter(state -> file == null || state.getFile() != null && PathUtils.samePath(state.getFile(), file))
7778
.sorted(Comparator.comparing(GhostFunction::getQualifiedName))
7879
.map(ContextMapper::ghost)
7980
.distinct()
@@ -83,7 +84,7 @@ private static List<Map<String, Object>> ghosts(Context context, String file) {
8384
private static List<Map<String, Object>> states(Context context, String file) {
8485
return context.getGhostStates().stream()
8586
.filter(state -> state.getParent() != null)
86-
.filter(state -> file == null || state.getFile() != null && Utils.samePath(state.getFile(), file))
87+
.filter(state -> file == null || state.getFile() != null && PathUtils.samePath(state.getFile(), file))
8788
.sorted(Comparator.comparing(GhostState::getQualifiedName))
8889
.map(ContextMapper::ghost)
8990
.toList();
@@ -93,7 +94,7 @@ private static boolean visible(RefinedVariable variable, String file, Range curs
9394
if (variable.getPlacementInCode() == null) return false;
9495
SourcePosition position = variable.getPlacementInCode().getPosition();
9596
if (position == null || !position.isValidPosition() || position.getFile() == null) return false;
96-
if (!Utils.samePath(position.getFile().toString(), file)) return false;
97+
if (!PathUtils.samePath(position.getFile().toString(), file)) return false;
9798

9899
Range declaration = Range.from(position);
99100
if (!declaration.startsBefore(cursor)) return false;

‎src/main/java/liquidjava/mcp/tools/context/ContextRequest.java‎

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,17 @@
11
package liquidjava.mcp.tools.context;
22

3-
import java.io.IOException;
43
import java.nio.file.Files;
5-
import java.nio.file.InvalidPathException;
6-
import java.nio.file.NoSuchFileException;
74
import java.nio.file.Path;
85
import java.util.Map;
9-
import liquidjava.mcp.utils.Utils;
6+
import liquidjava.mcp.utils.PathUtils;
107

118
public record ContextRequest(String path, String file, Integer line, Integer column) {
129
public ContextRequest {
13-
if (path == null || path.isBlank())
14-
throw new IllegalArgumentException("path must be a nonblank string");
15-
Path verificationPath;
16-
try {
17-
verificationPath = Utils.canonicalPath(path);
18-
path = verificationPath.toString();
19-
} catch (InvalidPathException e) {
20-
throw new IllegalArgumentException("invalid path: " + e.getReason(), e);
21-
} catch (NoSuchFileException e) {
22-
throw new IllegalArgumentException("The path " + path + " was not found", e);
23-
} catch (IOException e) {
24-
throw new IllegalArgumentException("Cannot resolve path " + path + ": " + e.getMessage(), e);
25-
}
10+
Path verificationPath = PathUtils.requireExisting(path);
11+
path = verificationPath.toString();
2612

2713
if (file != null) {
28-
if (file.isBlank())
29-
throw new IllegalArgumentException("file must be a nonblank string");
30-
Path sourceFile;
31-
try {
32-
sourceFile = Utils.canonicalPath(file);
33-
} catch (IOException e) {
34-
throw new IllegalArgumentException("file must be an existing Java source file", e);
35-
}
36-
if (!Files.isRegularFile(sourceFile) || !sourceFile.getFileName().toString().endsWith(".java"))
37-
throw new IllegalArgumentException("file must be an existing Java source file");
14+
Path sourceFile = PathUtils.requireExistingJavaFile(file, "file");
3815
boolean belongsToPath = Files.isDirectory(verificationPath)
3916
? sourceFile.startsWith(verificationPath)
4017
: sourceFile.equals(verificationPath);

‎src/main/java/liquidjava/mcp/tools/context/ContractRequest.java‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
package liquidjava.mcp.tools.context;
22

3-
import java.io.IOException;
4-
import java.nio.file.InvalidPathException;
5-
import java.nio.file.NoSuchFileException;
63
import java.util.Map;
7-
import liquidjava.mcp.utils.Utils;
4+
import liquidjava.mcp.utils.PathUtils;
85

96
public record ContractRequest(String path, String className, String signature) {
107
public ContractRequest {
11-
if (path == null || path.isBlank())
12-
throw new IllegalArgumentException("path must be a nonblank string");
13-
try {
14-
path = Utils.canonicalPath(path).toString();
15-
} catch (InvalidPathException e) {
16-
throw new IllegalArgumentException("invalid path: " + e.getReason(), e);
17-
} catch (NoSuchFileException e) {
18-
throw new IllegalArgumentException("The path " + path + " was not found", e);
19-
} catch (IOException e) {
20-
throw new IllegalArgumentException("Cannot resolve path " + path + ": " + e.getMessage(), e);
21-
}
8+
path = PathUtils.requireExisting(path).toString();
229
if (className != null && className.isBlank())
2310
throw new IllegalArgumentException("className must be a nonblank string");
2411
if (signature != null && signature.isBlank())

‎src/main/java/liquidjava/mcp/tools/fsm/StateMachineRequest.java‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
package liquidjava.mcp.tools.fsm;
22

3-
import java.nio.file.Files;
4-
import java.nio.file.InvalidPathException;
5-
import java.nio.file.Path;
63
import java.util.Map;
4+
import liquidjava.mcp.utils.PathUtils;
75

86
public record StateMachineRequest(String path) {
97
public StateMachineRequest {
10-
if (path == null || path.isBlank())
11-
throw new IllegalArgumentException("path must be a nonblank string");
12-
try {
13-
Path source = Path.of(path).toAbsolutePath().normalize();
14-
if (!Files.isRegularFile(source) || !source.getFileName().toString().endsWith(".java"))
15-
throw new IllegalArgumentException("path must be an existing Java source file");
16-
} catch (InvalidPathException e) {
17-
throw new IllegalArgumentException("invalid path: " + e.getReason(), e);
18-
}
8+
PathUtils.requireExistingJavaFile(path, "path");
199
}
2010

2111
public static StateMachineRequest fromArguments(Map<String, Object> arguments) {

‎src/main/java/liquidjava/mcp/tools/verification/VerifyRequest.java‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
package liquidjava.mcp.tools.verification;
22

3-
import java.nio.file.InvalidPathException;
4-
import java.nio.file.Path;
53
import java.util.Map;
4+
import liquidjava.mcp.utils.PathUtils;
65

76
public record VerifyRequest(String path, boolean debug) {
87
public VerifyRequest {
9-
if (path == null || path.isBlank())
10-
throw new IllegalArgumentException("path must be a nonblank string");
11-
try {
12-
Path.of(path);
13-
} catch (InvalidPathException e) {
14-
throw new IllegalArgumentException("invalid path: " + e.getReason(), e);
15-
}
8+
PathUtils.requireValid(path, "path");
169
}
1710

1811
public static VerifyRequest fromArguments(Map<String, Object> arguments) {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package liquidjava.mcp.utils;
2+
3+
import java.io.IOException;
4+
import java.io.UncheckedIOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.InvalidPathException;
7+
import java.nio.file.NoSuchFileException;
8+
import java.nio.file.Path;
9+
10+
public final class PathUtils {
11+
private PathUtils() {}
12+
13+
public static Path requireValid(String value, String name) {
14+
requireNonBlank(value, name);
15+
try {
16+
return Path.of(value);
17+
} catch (InvalidPathException e) {
18+
throw new IllegalArgumentException("invalid path: " + e.getReason(), e);
19+
}
20+
}
21+
22+
public static Path requireExisting(String value) {
23+
requireNonBlank(value, "path");
24+
try {
25+
return canonicalPath(value);
26+
} catch (InvalidPathException e) {
27+
throw new IllegalArgumentException("invalid path: " + e.getReason(), e);
28+
} catch (NoSuchFileException e) {
29+
throw new IllegalArgumentException("The path " + value + " was not found", e);
30+
} catch (IOException e) {
31+
throw new IllegalArgumentException("Cannot resolve path " + value + ": " + e.getMessage(), e);
32+
}
33+
}
34+
35+
public static Path requireExistingJavaFile(String value, String name) {
36+
requireNonBlank(value, name);
37+
Path source;
38+
try {
39+
source = canonicalPath(value);
40+
} catch (InvalidPathException e) {
41+
throw new IllegalArgumentException("invalid path: " + e.getReason(), e);
42+
} catch (IOException e) {
43+
throw new IllegalArgumentException(name + " must be an existing Java source file", e);
44+
}
45+
if (!Files.isRegularFile(source) || !source.getFileName().toString().endsWith(".java"))
46+
throw new IllegalArgumentException(name + " must be an existing Java source file");
47+
return source;
48+
}
49+
50+
public static Path canonicalPath(String path) throws IOException {
51+
return Path.of(path).toRealPath();
52+
}
53+
54+
public static boolean samePath(String left, String right) {
55+
try {
56+
return canonicalPath(left).equals(canonicalPath(right));
57+
} catch (IOException e) {
58+
throw new UncheckedIOException(e);
59+
}
60+
}
61+
62+
private static String requireNonBlank(String value, String name) {
63+
if (value == null || value.isBlank())
64+
throw new IllegalArgumentException(name + " must be a nonblank string");
65+
return value;
66+
}
67+
}
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package liquidjava.mcp.utils;
22

33
import java.io.ByteArrayOutputStream;
4-
import java.io.IOException;
5-
import java.io.UncheckedIOException;
64
import java.nio.charset.StandardCharsets;
7-
import java.nio.file.Path;
85
import java.util.Map;
96
import spoon.reflect.cu.SourcePosition;
107

@@ -23,16 +20,4 @@ public static Map<String, Object> mapPosition(SourcePosition position) {
2320
public static String stripAnsi(ByteArrayOutputStream bytes) {
2421
return Regex.ANSI_ESCAPE.matcher(bytes.toString(StandardCharsets.UTF_8)).replaceAll("");
2522
}
26-
27-
public static Path canonicalPath(String path) throws IOException {
28-
return Path.of(path).toRealPath();
29-
}
30-
31-
public static boolean samePath(String left, String right) {
32-
try {
33-
return canonicalPath(left).equals(canonicalPath(right));
34-
} catch (IOException e) {
35-
throw new UncheckedIOException(e);
36-
}
37-
}
3823
}

0 commit comments

Comments
 (0)