public class JavaParserUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
JavaParserUtil.StringLiteralConcatenateVisitor
Visitor that combines added String literals, see
concatenateAddedStringLiterals(com.github.javaparser.ast.Node) . |
Modifier and Type | Field and Description |
---|---|
static com.github.javaparser.ParserConfiguration.LanguageLevel |
DEFAULT_LANGUAGE_LEVEL
The Language Level to use when parsing if a specific level isn't applied.
|
Constructor and Description |
---|
JavaParserUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
clearAnnotations(com.github.javaparser.ast.Node node)
Side-effects
node by removing all annotations from anywhere inside its subtree. |
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.
|
static com.github.javaparser.ParserConfiguration.LanguageLevel |
getCurrentSourceVersion(ProcessingEnvironment env)
Returns the
ParserConfiguration.LanguageLevel corresponding to
the current source version. |
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.
|
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.CompilationUnit |
parseCompilationUnit(File file)
Parses the Java code contained in the
File and returns a CompilationUnit that
represents it. |
static com.github.javaparser.ast.CompilationUnit |
parseCompilationUnit(InputStream inputStream)
Parses the Java code contained in the
InputStream and returns a CompilationUnit
that represents it. |
static com.github.javaparser.ast.CompilationUnit |
parseCompilationUnit(String javaSource)
Parses the Java code contained in the
String and returns a CompilationUnit that
represents it. |
static com.github.javaparser.ast.expr.Expression |
parseExpression(String expression)
Parses the
expression and returns an Expression that represents it. |
static com.github.javaparser.ast.expr.Expression |
parseExpression(String expression,
com.github.javaparser.ParserConfiguration.LanguageLevel languageLevel)
Parses the
expression and returns an Expression that represents it. |
static com.github.javaparser.ast.StubUnit |
parseStubUnit(InputStream inputStream)
Parses the stub file contained in the
InputStream and returns a StubUnit that
represents it. |
public static com.github.javaparser.ParserConfiguration.LanguageLevel DEFAULT_LANGUAGE_LEVEL
public static com.github.javaparser.ast.CompilationUnit parseCompilationUnit(InputStream inputStream)
InputStream
and returns a CompilationUnit
that 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-using StaticJavaParser
causes memory problems because it retains too much memory.
inputStream
- the Java source codecom.github.javaparser.ParseProblemException
- if the source code has parser errorspublic static com.github.javaparser.ast.CompilationUnit parseCompilationUnit(File file) throws FileNotFoundException
File
and returns a CompilationUnit
that
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-using StaticJavaParser
causes memory problems because it retains too much memory.
file
- the Java source codecom.github.javaparser.ParseProblemException
- if the source code has parser errorsFileNotFoundException
- if the file was not foundpublic static com.github.javaparser.ast.CompilationUnit parseCompilationUnit(String javaSource)
String
and returns a CompilationUnit
that
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-using StaticJavaParser
causes memory problems because it retains too much memory.
javaSource
- the Java source codecom.github.javaparser.ParseProblemException
- if the source code has parser errorspublic static com.github.javaparser.ast.StubUnit parseStubUnit(InputStream inputStream)
InputStream
and returns a StubUnit
that
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-using StaticJavaParser
causes memory problems because it retains too much memory.
inputStream
- the stub filecom.github.javaparser.ParseProblemException
- if the source code has parser errorspublic static com.github.javaparser.ast.expr.Expression parseExpression(String expression)
expression
and returns an Expression
that 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-using StaticJavaParser
causes memory problems because it retains too much memory.
expression
- the expression stringcom.github.javaparser.ParseProblemException
- if the expression has parser errorspublic static com.github.javaparser.ast.expr.Expression parseExpression(String expression, com.github.javaparser.ParserConfiguration.LanguageLevel languageLevel)
expression
and returns an Expression
that 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-using StaticJavaParser
causes memory problems because it retains too much memory.
expression
- the expression stringlanguageLevel
- the language level to use when parsing the Java sourcecom.github.javaparser.ParseProblemException
- if the expression has parser errorspublic static com.github.javaparser.ast.body.TypeDeclaration<?> getTypeDeclarationByName(com.github.javaparser.ast.CompilationUnit root, String name)
root
- compilation unit to searchname
- name of a top level type declaration in root
root
named name
public static String getFullyQualifiedName(com.github.javaparser.ast.body.TypeDeclaration<?> type, com.github.javaparser.ast.CompilationUnit compilationUnit)
type
- a type declarationcompilationUnit
- the compilation unit containing type
type
if compilationUnit
contains a package
declaration, or just the name of type
otherwisepublic static void clearAnnotations(com.github.javaparser.ast.Node node)
node
by removing all annotations from anywhere inside its subtree.node
- a JavaParser Nodepublic static void concatenateAddedStringLiterals(com.github.javaparser.ast.Node node)
"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 expression 1 + "a" + "b"
parses as (1 + "a") + "b"
}, but it is
transformed into 1 + "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.
node
- a JavaParser Nodepublic static com.github.javaparser.ParserConfiguration.LanguageLevel getCurrentSourceVersion(ProcessingEnvironment env)
ParserConfiguration.LanguageLevel
corresponding to
the current source version.env
- processing environment used to get source version