Class JavaParserUtil
com.github.javaparser.StaticJavaParser that does not leak memory, and it provides some other
methods.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classVisitor that combines String literals in binary "+" expressions to match javac's constant-folding behavior. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.github.javaparser.ParserConfiguration.LanguageLevelThe Language Level to use when parsing if a specific level isn't applied. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclearAnnotations(com.github.javaparser.ast.Node node) Side-effectsnodeby removing all annotations from anywhere inside its subtree.static voidconcatenateAddedStringLiterals(com.github.javaparser.ast.Node node) Side-effects node by combining any added String literals in node's subtree into their concatenation.static com.github.javaparser.ParserConfiguration.LanguageLevelReturns theParserConfiguration.LanguageLevelcorresponding to the current source version.static StringgetFullyQualifiedName(com.github.javaparser.ast.body.TypeDeclaration<?> type, com.github.javaparser.ast.CompilationUnit compilationUnit) Returns the fully qualified name of a type appearing in a given compilation unit.static com.github.javaparser.ast.body.TypeDeclaration<?> getTypeDeclarationByName(com.github.javaparser.ast.CompilationUnit root, String name) Given the compilation unit node for a source file, returns the top level type definition with the given name.static com.github.javaparser.ast.CompilationUnitparseCompilationUnit(File file) Parses the Java code contained in theFileand returns aCompilationUnitthat represents it.static com.github.javaparser.ast.CompilationUnitparseCompilationUnit(InputStream inputStream) Parses the Java code contained in theInputStreamand returns aCompilationUnitthat represents it.static com.github.javaparser.ast.CompilationUnitparseCompilationUnit(String javaSource) Parses the Java code contained in theStringand returns aCompilationUnitthat represents it.static com.github.javaparser.ast.expr.ExpressionparseExpression(String expression) Parses theexpressionand returns anExpressionthat represents it.static com.github.javaparser.ast.expr.ExpressionparseExpression(String expression, com.github.javaparser.ParserConfiguration.LanguageLevel languageLevel) Parses theexpressionand returns anExpressionthat represents it.static com.github.javaparser.ast.StubUnitparseStubUnit(InputStream inputStream) Parses the stub file contained in theInputStreamand returns aStubUnitthat represents it.static com.github.javaparser.ast.StubUnitparseStubUnitForJdk(InputStream inputStream) Parses an annotated-JDK stub file and returns aStubUnitrepresenting it.
-
Field Details
-
DEFAULT_LANGUAGE_LEVEL
public static final com.github.javaparser.ParserConfiguration.LanguageLevel DEFAULT_LANGUAGE_LEVELThe Language Level to use when parsing if a specific level isn't applied. This should be the highest version of Java that the Checker Framework can process. Defined inJavaStubifier.DEFAULT_LANGUAGE_LEVEL, which also generates the annotated JDK and the binary stub files.
-
-
Method Details
-
parseCompilationUnit
public static com.github.javaparser.ast.CompilationUnit parseCompilationUnit(InputStream inputStream) Parses the Java code contained in theInputStreamand returns aCompilationUnitthat represents it.This is like
StaticJavaParser.parse, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-usingStaticJavaParsercauses memory problems because it retains too much memory.- Parameters:
inputStream- the Java source code- Returns:
- CompilationUnit representing the Java source code
- Throws:
com.github.javaparser.ParseProblemException- if the source code has parser errors
-
parseCompilationUnit
public static com.github.javaparser.ast.CompilationUnit parseCompilationUnit(File file) throws FileNotFoundException Parses the Java code contained in theFileand returns aCompilationUnitthat represents it.This is like
StaticJavaParser.parse, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-usingStaticJavaParsercauses memory problems because it retains too much memory.- Parameters:
file- the Java source code- Returns:
- CompilationUnit representing the Java source code
- Throws:
com.github.javaparser.ParseProblemException- if the source code has parser errorsFileNotFoundException- if the file was not found
-
parseCompilationUnit
Parses the Java code contained in theStringand returns aCompilationUnitthat represents it.This is like
StaticJavaParser.parse, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-usingStaticJavaParsercauses memory problems because it retains too much memory.- Parameters:
javaSource- the Java source code- Returns:
- CompilationUnit representing the Java source code
- Throws:
com.github.javaparser.ParseProblemException- if the source code has parser errors
-
parseStubUnit
Parses the stub file contained in theInputStreamand returns aStubUnitthat represents it.This is like
StaticJavaParser.parse, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-usingStaticJavaParsercauses memory problems because it retains too much memory.This variant preserves JavaToken objects on the parsed AST so that
ParseProblemExceptions emitted on malformed user-supplied stubs include precise token-level diagnostics. Callers that parse the annotated JDK and do not need that diagnostic detail should useparseStubUnitForJdk(InputStream)instead, which is measurably faster and allocates less.- Parameters:
inputStream- the stub file- Returns:
- StubUnit representing the stub file
- Throws:
com.github.javaparser.ParseProblemException- if the source code has parser errors
-
parseStubUnitForJdk
Parses an annotated-JDK stub file and returns aStubUnitrepresenting it.Identical to
parseStubUnit(InputStream)except that token retention is disabled. AST nodes still carry theirRange(line/column), soParseProblemExceptions remain identifiable, but the per-token detail used by some JavaParser diagnostics is dropped. This trade-off is appropriate for the annotated JDK, whose stubs are maintained inside the Checker Framework and whose parse errors should never reach an end user; it is not appropriate for user-supplied stub files.JFR profiling of
allNullnessTestsattributes ~6.7% of main-thread time to JavaParser, dominated by the lazy on-demand parsing of JDK class astubs viaAnnotationFileElementTypes#maybeParseEnclosingJdkClass.- Parameters:
inputStream- the JDK stub file- Returns:
- StubUnit representing the JDK stub file
- Throws:
com.github.javaparser.ParseProblemException- if the source code has parser errors
-
parseExpression
Parses theexpressionand returns anExpressionthat represents it.This is like
StaticJavaParser.parseExpression, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-usingStaticJavaParsercauses memory problems because it retains too much memory.- Parameters:
expression- the expression string- Returns:
- the parsed expression
- Throws:
com.github.javaparser.ParseProblemException- if the expression has parser errors
-
parseExpression
public static com.github.javaparser.ast.expr.Expression parseExpression(String expression, com.github.javaparser.ParserConfiguration.LanguageLevel languageLevel) Parses theexpressionand returns anExpressionthat represents it.This is like
StaticJavaParser.parseExpression, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-usingStaticJavaParsercauses memory problems because it retains too much memory.- Parameters:
expression- the expression stringlanguageLevel- the language level to use when parsing the Java source- Returns:
- the parsed expression
- Throws:
com.github.javaparser.ParseProblemException- if the expression has parser errors
-
getTypeDeclarationByName
public static com.github.javaparser.ast.body.TypeDeclaration<?> getTypeDeclarationByName(com.github.javaparser.ast.CompilationUnit root, String name) Given the compilation unit node for a source file, returns the top level type definition with the given name.- Parameters:
root- compilation unit to searchname- name of a top level type declaration inroot- Returns:
- a top level type declaration in
rootnamedname
-
getFullyQualifiedName
public static String getFullyQualifiedName(com.github.javaparser.ast.body.TypeDeclaration<?> type, com.github.javaparser.ast.CompilationUnit compilationUnit) Returns the fully qualified name of a type appearing in a given compilation unit.- Parameters:
type- a type declarationcompilationUnit- the compilation unit containingtype- Returns:
- the fully qualified name of
typeifcompilationUnitcontains a package declaration, or just the name oftypeotherwise
-
clearAnnotations
public static void clearAnnotations(com.github.javaparser.ast.Node node) Side-effectsnodeby removing all annotations from anywhere inside its subtree.- Parameters:
node- a JavaParser Node
-
concatenateAddedStringLiterals
public static void concatenateAddedStringLiterals(com.github.javaparser.ast.Node node) Side-effects node by combining any added String literals in node's subtree into their concatenation. For example, the expression"a" + "b"becomes"ab". This occurs even if, when reading from left to right, the two string literals are not added directly. For example, the expression1 + "a" + "b"parses as(1 + "a") + "b"}, but it is transformed into1 + "ab".This is the same transformation performed by javac automatically. Javac seems to ignore string literals surrounded in parentheses, so this method does as well.
- Parameters:
node- a JavaParser Node
-
getCurrentSourceVersion
public static com.github.javaparser.ParserConfiguration.LanguageLevel getCurrentSourceVersion(ProcessingEnvironment env) Returns theParserConfiguration.LanguageLevelcorresponding to the current source version.- Parameters:
env- processing environment used to get source version- Returns:
- the current source version
-