Skip to content

Commit 4530a3c

Browse files
cursoragentsimbo1905
andcommitted
Issue #128 Add runtime-compiled JsonPath
Co-authored-by: simbo1905 <simbo1905@60hertz.com>
1 parent 9b6838d commit 4530a3c

File tree

8 files changed

+802
-9
lines changed

8 files changed

+802
-9
lines changed

json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPath.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ static List<JsonValue> query(String path, JsonValue json) {
7777
///
7878
/// This method is idempotent: if the receiver is already compiled, it returns itself.
7979
/// Implementations that do not support runtime compilation may return themselves.
80-
JsonPath compile();
80+
default JsonPath compile() {
81+
return JsonPathCompiler.compile(this);
82+
}
8183

8284
/// Returns a (potentially) runtime-compiled version of the provided JsonPath.
8385
static JsonPath compile(JsonPath path) {
8486
Objects.requireNonNull(path, "path must not be null");
85-
return path.compile();
87+
return JsonPathCompiler.compile(path);
8688
}
8789
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package json.java21.jsonpath;
2+
3+
/// Marker for `JsonPath` implementations backed by a `JsonPathAst.Root`.
4+
///
5+
/// This is intentionally package-private so tests in this package can inspect internals,
6+
/// while keeping the public API surface small.
7+
interface JsonPathAstBacked {
8+
JsonPathAst.Root ast();
9+
}
10+

json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathAstPath.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// Package-private AST-backed `JsonPath` implementation.
1010
///
1111
/// This is the behavior-preserving interpreter that walks `JsonPathAst` at runtime.
12-
final class JsonPathAstPath implements JsonPath {
12+
final class JsonPathAstPath implements JsonPath, JsonPathAstBacked {
1313

1414
private static final Logger LOG = Logger.getLogger(JsonPathAstPath.class.getName());
1515

@@ -26,17 +26,12 @@ public List<JsonValue> query(JsonValue json) {
2626
return JsonPathAstInterpreter.evaluate(ast, json);
2727
}
2828

29-
@Override
30-
public JsonPath compile() {
31-
// Compiler-backed implementation is added later; keep behavior-preserving no-op for now.
32-
return this;
33-
}
34-
3529
@Override
3630
public String toString() {
3731
return JsonPathAstInterpreter.reconstruct(ast);
3832
}
3933

34+
@Override
4035
JsonPathAst.Root ast() {
4136
return ast;
4237
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package json.java21.jsonpath;
2+
3+
/// Marker for `JsonPath` implementations that are already runtime-compiled.
4+
interface JsonPathCompiled extends JsonPath {
5+
}
6+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package json.java21.jsonpath;
2+
3+
import jdk.sandbox.java.util.json.JsonValue;
4+
5+
import java.util.List;
6+
import java.util.Objects;
7+
8+
/// A `JsonPath` backed by a runtime-compiled generated class.
9+
///
10+
/// This wrapper intentionally does not retain the AST; it only retains the generated Java source
11+
/// (for diagnostics/debugging) and delegates evaluation to the compiled instance.
12+
final class JsonPathCompiledPath implements JsonPathCompiled {
13+
14+
private final String expression;
15+
private final String generatedClassName;
16+
private final String javaSource;
17+
private final JsonPath delegate;
18+
19+
JsonPathCompiledPath(String expression, String generatedClassName, String javaSource, JsonPath delegate) {
20+
this.expression = Objects.requireNonNull(expression, "expression must not be null");
21+
this.generatedClassName = Objects.requireNonNull(generatedClassName, "generatedClassName must not be null");
22+
this.javaSource = Objects.requireNonNull(javaSource, "javaSource must not be null");
23+
this.delegate = Objects.requireNonNull(delegate, "delegate must not be null");
24+
}
25+
26+
@Override
27+
public List<JsonValue> query(JsonValue json) {
28+
return delegate.query(json);
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return expression;
34+
}
35+
36+
String generatedClassName() {
37+
return generatedClassName;
38+
}
39+
40+
String javaSource() {
41+
return javaSource;
42+
}
43+
}
44+

0 commit comments

Comments
 (0)