Analyse, transform and generate your Java code base.
In its simplest form, the JavaParser library allows you to interact with Java source code as a Java object representation in a Java environment. More formally we refer to this object representation as an Abstract Syntax Tree (AST).
/** * Parses source code. * It takes the source code from a Provider. * The start indicates what can be found in the source code (compilation unit, block, import...) * * @param start refer to the constants in ParseStart to see what can be parsed. * @param provider refer to Providers to see how you can read source. The provider will be closed after parsing. * @param <N> the subclass of Node that is the result of parsing in the start. * @return the parse result, a collection of encountered problems, and some extra data. */ public <N extends Node> ParseResult<N> parse(ParseStart<N> start, Provider provider) { assertNotNull(start); assertNotNull(provider); final GeneratedJavaParser parser = getParserForProvider(provider); try { N resultNode = start.parse(parser); ParseResult<N> result = new ParseResult<>(resultNode, parser.problems, parser.getTokens(), parser.getCommentsCollection());
/***************************************** * THE JAVA LANGUAGE GRAMMAR STARTS HERE * *****************************************/
/* * Program structuring syntax follows. */ final public CompilationUnit CompilationUnit() throws ParseException {PackageDeclaration pakage = null; NodeList<ImportDeclaration> imports = emptyList(); ImportDeclaration in = null; NodeList<TypeDeclaration<?>> types = emptyList(); ModifierHolder modifier; TypeDeclaration<?> tn = null; ModuleDeclaration module = null; try { label_1: while (true) { if (jj_2_1(2)) { ; } else { break label_1; } jj_consume_token(SEMICOLON); } if (jj_2_2(2147483647)) { pakage = PackageDeclaration(); } else { ; } label_2: while (true) { switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) { case ABSTRACT: case CLASS: case _DEFAULT: case ENUM: case FINAL: case IMPORT: case INTERFACE: case NATIVE: case PRIVATE: case PROTECTED: case PUBLIC: case STATIC: case STRICTFP: case SYNCHRONIZED: case TRANSIENT: case VOLATILE: case OPEN: case MODULE: case TRANSITIVE: case SEMICOLON: case AT:{ ; break; } default: jj_la1[0] = jj_gen; break label_2; } switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) { case IMPORT:{ in = ImportDeclaration(); imports = add(imports, in); break; } case ABSTRACT: case CLASS: case _DEFAULT: case ENUM: case FINAL: case INTERFACE: case NATIVE: case PRIVATE: case PROTECTED: case PUBLIC: case STATIC: case STRICTFP: case SYNCHRONIZED: case TRANSIENT: case VOLATILE: case OPEN: case MODULE: case TRANSITIVE: case SEMICOLON: case AT:{ modifier = Modifiers(); switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) { case CLASS: case INTERFACE:{ tn = ClassOrInterfaceDeclaration(modifier); types = add(types, tn); break; } case ENUM:{ tn = EnumDeclaration(modifier); types = add(types, tn); break; } case AT:{ tn = AnnotationTypeDeclaration(modifier); types = add(types, tn); break; } case OPEN: case MODULE:{ module = ModuleDeclaration(modifier); break; } case SEMICOLON:{ jj_consume_token(SEMICOLON); break; } default: jj_la1[1] = jj_gen; jj_consume_token(-1); throw new ParseException(); } break; } default: jj_la1[2] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) { case 0:{ jj_consume_token(0); break; } case CTRL_Z:{ jj_consume_token(CTRL_Z); break; } default: jj_la1[3] = jj_gen; jj_consume_token(-1); throw new ParseException(); } return new CompilationUnit(range(token_source.getHomeToken(), token()), pakage, imports, types, module); } catch (ParseException e) { recover(EOF, e); final CompilationUnit compilationUnit = new CompilationUnit(range(token_source.getHomeToken(), token()), null, new NodeList<ImportDeclaration>(), new NodeList<TypeDeclaration<?>>(), null); compilationUnit.setParsed(UNPARSABLE); return compilationUnit; } }