JavaParser-AST-Inspector
JavaParser-AST-Inspector copied to clipboard
initial inclusion of a live template
This "live template" creates a complete boilerplate for parsing and handling String parse input.
-
While I'm not 100% on the actual content of the template and it can probably be improved (suggestions/issues/pull requests welcome!), it's a start showing it can be done! :+1: :grin:
-
As an aside, it highlights how much the
StaticJavaParserhides from you and the logic paths/branches that are not handled/able to be handled if using it...
Here is a sample of the output (note it will auto-format based on your editor settings):
String inputToParse = "class X {}";
JavaParser javaParser = new JavaParser();
ParseResult<CompilationUnit> parseResult = javaParser.parse(inputToParse);
if (parseResult.isSuccessful()) {
if (parseResult.getResult().isPresent()) {
CompilationUnit compilationUnit = parseResult.getResult().get();
// Do things with the compilation unit
} else {
System.out.println("Warning: Parsing was successful, but no CompilationUnit found. ");
}
} else {
System.out.println("Warning: Parse was unsuccessful. Attempts to inspect/alter the AST may be possible, but may lead to unintended outputs.");
if (parseResult.getResult().isPresent()) {
CompilationUnit compilationUnitWithParseError = parseResult.getResult().get();
// Do things with the compilation unit (with parse error)
} else {
System.out.println("Warning: Parsing was successful, but no CompilationUnit found. ");
}
}
Here's a screenshot showing the places where input is prompted for.

Here's a screenshot showing how it reacts to parsing an Expression instead of a full CompilationUnit.
Note the auto-suggested variable names update based on the previous selections.

Here's a screenshot showing how it reacts to using a different variable name
Note here that I manually tweak it to expr instead of expression, and further down the variable has updated to exprWithParseError.

closes #210