https://github.com/col-e/sourcesolver
A lightweight wrapper on the javac API to support resolving class and member references in a String of Java source code.
https://github.com/col-e/sourcesolver
Last synced: 9 months ago
JSON representation
A lightweight wrapper on the javac API to support resolving class and member references in a String of Java source code.
- Host: GitHub
- URL: https://github.com/col-e/sourcesolver
- Owner: Col-E
- License: mit
- Created: 2024-11-24T08:47:02.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-04-09T00:37:57.000Z (9 months ago)
- Last Synced: 2025-04-15T03:15:05.146Z (9 months ago)
- Language: Java
- Homepage:
- Size: 639 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Source-Solver
A lightweight wrapper on the `javac` API to support resolving class and member references in a `String` of Java source code.
## Example use
```java
// Provide some Java source code
String sourceCode = Files.readString(Paths.get("Checkbox.java"));
int index = sourceCode.indexOf("void processItemEvent(") + 15;
// Create a parser and parse the source into the Source-Solver model
Parser parser = new Parser();
CompilationUnitModel model = parser.parse(sourceCode);
// Create an entry-pool which provides information about class structures to Source-Solver.
// You are expected to provide your own ClassEntry instances into the pool.
// You can find an example of how to do this in the test classes of this repository.
EntryPool pool = new BasicEntryPool();
loadClassesIntoPool(pool);
// Create a resolver targeting the given compilation unit and resolve at some index
// into the 'sourceCode' string.
Resolver resolver = new BasicResolver(model, pool);
Resolution resolution = resolver.resolveAt(index);
// Check for an instance of the expected resolution type to get more information about the resolved
// class or member reference at the given index.
if (resolution instanceof MethodResolution methodResolution) { ... }
```
## Requirements
You will need to provide access to the following modules when using this library:
```
jdk.compiler/com.sun.tools.javac.parser
jdk.compiler/com.sun.tools.javac.tree
jdk.compiler/com.sun.tools.javac.util
```
Because the `jdk.compiler` module does not have exports for these packages except to other internal modules, we cannot provide you a `module-info` to do this automatically. Sorry.