public final class JavaParser
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private GeneratedJavaParser |
astParser |
private CommentsInserter |
commentsInserter |
private ParserConfiguration |
configuration |
private static ParserConfiguration |
staticConfiguration |
Constructor and Description |
---|
JavaParser()
Instantiate the parser with default configuration.
|
JavaParser(ParserConfiguration configuration)
Instantiate the parser.
|
Modifier and Type | Method and Description |
---|---|
ParserConfiguration |
getParserConfiguration()
Get the non-static configuration for this parser.
|
private GeneratedJavaParser |
getParserForProvider(Provider provider) |
static ParserConfiguration |
getStaticConfiguration()
Get the configuration for the static parse...
|
static CompilationUnit |
parse(java.io.File file)
Parses the Java code contained in a
File and returns a
CompilationUnit that represents it.Note: Uses UTF-8 encoding |
static CompilationUnit |
parse(java.io.File file,
java.nio.charset.Charset encoding)
Parses the Java code contained in a
File and returns a
CompilationUnit that represents it. |
static CompilationUnit |
parse(java.io.InputStream in)
Parses the Java code contained in the
InputStream and returns a
CompilationUnit that represents it.Note: Uses UTF-8 encoding |
static CompilationUnit |
parse(java.io.InputStream in,
java.nio.charset.Charset encoding)
Parses the Java code contained in the
InputStream and returns a
CompilationUnit that represents it. |
<N extends Node> |
parse(ParseStart<N> start,
Provider provider)
Parses source code.
|
static CompilationUnit |
parse(java.nio.file.Path path)
Parses the Java code contained in a file and returns a
CompilationUnit that represents it.Note: Uses UTF-8 encoding |
static CompilationUnit |
parse(java.nio.file.Path path,
java.nio.charset.Charset encoding)
Parses the Java code contained in a file and returns a
CompilationUnit that represents it. |
static CompilationUnit |
parse(java.io.Reader reader)
Parses Java code from a Reader and returns a
CompilationUnit that represents it. |
static CompilationUnit |
parse(java.lang.String code)
Parses the Java code contained in code and returns a
CompilationUnit that represents it. |
static AnnotationExpr |
parseAnnotation(java.lang.String annotation)
Parses the Java annotation contained in a
String and returns a
AnnotationExpr that represents it. |
static BodyDeclaration<?> |
parseAnnotationBodyDeclaration(java.lang.String body)
Parses the Java annotation body declaration(e.g fields or methods) contained in a
String and returns a BodyDeclaration that represents it. |
static BlockStmt |
parseBlock(java.lang.String blockStatement)
Parses the Java block contained in a
String and returns a
BlockStmt that represents it. |
static BodyDeclaration<?> |
parseBodyDeclaration(java.lang.String body)
Parses a Java class or interface body declaration(e.g fields or methods) and returns a
BodyDeclaration that represents it. |
static BodyDeclaration<?> |
parseClassBodyDeclaration(java.lang.String body)
Deprecated.
just use parseBodyDeclaration now.
|
static ClassOrInterfaceType |
parseClassOrInterfaceType(java.lang.String type)
Parses a Java class or interface type name and returns a
ClassOrInterfaceType that represents it. |
static ExplicitConstructorInvocationStmt |
parseExplicitConstructorInvocationStmt(java.lang.String statement)
Parses the this(...) and super(...) statements that may occur at the start of a constructor.
|
static <T extends Expression> |
parseExpression(java.lang.String expression)
Parses the Java expression contained in a
String and returns a
Expression that represents it. |
static ImportDeclaration |
parseImport(java.lang.String importDeclaration)
Parses the Java import contained in a
String and returns a
ImportDeclaration that represents it. |
static BodyDeclaration<?> |
parseInterfaceBodyDeclaration(java.lang.String body)
Deprecated.
just use parseBodyDeclaration now.
|
static Javadoc |
parseJavadoc(java.lang.String content)
Parses the content of a JavadocComment and returns a
Javadoc that
represents it. |
static Name |
parseName(java.lang.String qualifiedName)
Parses a qualified name (one that can have "."s in it) and returns it as a Name.
|
static PackageDeclaration |
parsePackageDeclaration(java.lang.String packageDeclaration)
Parses a package declaration and returns it as a PackageDeclaration.
|
static Parameter |
parseParameter(java.lang.String parameter)
Parses a single parameter (a type and a name) and returns it as a Parameter.
|
static CompilationUnit |
parseResource(java.lang.ClassLoader classLoader,
java.lang.String path,
java.nio.charset.Charset encoding)
Parses the Java code contained in a resource and returns a
CompilationUnit that represents it. |
static CompilationUnit |
parseResource(java.lang.String path)
Parses the Java code contained in a resource and returns a
CompilationUnit that represents it.Note: Uses UTF-8 encoding |
static CompilationUnit |
parseResource(java.lang.String path,
java.nio.charset.Charset encoding)
Parses the Java code contained in a resource and returns a
CompilationUnit that represents it. |
static Statement |
parseStatement(java.lang.String statement)
Parses the Java statement contained in a
String and returns a
Statement that represents it. |
static Type |
parseType(java.lang.String type)
Parses a Java type name and returns a
Type that represents it. |
static VariableDeclarationExpr |
parseVariableDeclarationExpr(java.lang.String declaration)
Parses a variable declaration expression and returns a
VariableDeclarationExpr
that represents it. |
static void |
setStaticConfiguration(ParserConfiguration staticConfiguration)
Set the configuration for the static parse...
|
private static <T extends Node> |
simplifiedParse(ParseStart<T> context,
Provider provider) |
private final CommentsInserter commentsInserter
private final ParserConfiguration configuration
private GeneratedJavaParser astParser
private static ParserConfiguration staticConfiguration
public JavaParser()
public JavaParser(ParserConfiguration configuration)
public static ParserConfiguration getStaticConfiguration()
public static void setStaticConfiguration(ParserConfiguration staticConfiguration)
public ParserConfiguration getParserConfiguration()
private GeneratedJavaParser getParserForProvider(Provider provider)
public <N extends Node> ParseResult<N> parse(ParseStart<N> start, Provider provider)
N
- the subclass of Node that is the result of parsing in the start.start
- refer to the constants in ParseStart to see what can be parsed.provider
- refer to Providers to see how you can read source. The provider will be closed after parsing.public static CompilationUnit parse(java.io.InputStream in, java.nio.charset.Charset encoding)
InputStream
and returns a
CompilationUnit
that represents it.in
- InputStream
containing Java source code. It will be closed after parsing.encoding
- encoding of the source codeParseProblemException
- if the source code has parser errorspublic static CompilationUnit parse(java.io.InputStream in)
InputStream
and returns a
CompilationUnit
that represents it.in
- InputStream
containing Java source code. It will be closed after parsing.ParseProblemException
- if the source code has parser errorspublic static CompilationUnit parse(java.io.File file, java.nio.charset.Charset encoding) throws java.io.FileNotFoundException
File
and returns a
CompilationUnit
that represents it.file
- File
containing Java source code. It will be closed after parsing.encoding
- encoding of the source codeParseProblemException
- if the source code has parser errorsjava.io.FileNotFoundException
- the file was not foundpublic static CompilationUnit parse(java.io.File file) throws java.io.FileNotFoundException
File
and returns a
CompilationUnit
that represents it.file
- File
containing Java source code. It will be closed after parsing.ParseProblemException
- if the source code has parser errorsjava.io.FileNotFoundException
- the file was not foundpublic static CompilationUnit parse(java.nio.file.Path path, java.nio.charset.Charset encoding) throws java.io.IOException
CompilationUnit
that represents it.path
- path to a file containing Java source codeencoding
- encoding of the source codejava.io.IOException
- the path could not be accessedParseProblemException
- if the source code has parser errorspublic static CompilationUnit parse(java.nio.file.Path path) throws java.io.IOException
CompilationUnit
that represents it.path
- path to a file containing Java source codeParseProblemException
- if the source code has parser errorsjava.io.IOException
- the path could not be accessedpublic static CompilationUnit parseResource(java.lang.String path) throws java.io.IOException
CompilationUnit
that represents it.path
- path to a resource containing Java source code. As resource is accessed through a class loader, a
leading "/" is not allowed in pathToResourceParseProblemException
- if the source code has parser errorsjava.io.IOException
- the path could not be accessedpublic static CompilationUnit parseResource(java.lang.String path, java.nio.charset.Charset encoding) throws java.io.IOException
CompilationUnit
that represents it.path
- path to a resource containing Java source code. As resource is accessed through a class loader, a
leading "/" is not allowed in pathToResourceencoding
- encoding of the source codeParseProblemException
- if the source code has parser errorsjava.io.IOException
- the path could not be accessedpublic static CompilationUnit parseResource(java.lang.ClassLoader classLoader, java.lang.String path, java.nio.charset.Charset encoding) throws java.io.IOException
CompilationUnit
that represents it.classLoader
- the classLoader that is asked to load the resourcepath
- path to a resource containing Java source code. As resource is accessed through a class loader, a
leading "/" is not allowed in pathToResourceParseProblemException
- if the source code has parser errorsjava.io.IOException
- the path could not be accessedpublic static CompilationUnit parse(java.io.Reader reader)
CompilationUnit
that represents it.reader
- the reader containing Java source code. It will be closed after parsing.ParseProblemException
- if the source code has parser errorspublic static CompilationUnit parse(java.lang.String code)
CompilationUnit
that represents it.code
- Java source codeParseProblemException
- if the source code has parser errorspublic static BlockStmt parseBlock(java.lang.String blockStatement)
String
and returns a
BlockStmt
that represents it.blockStatement
- String
containing Java block codeParseProblemException
- if the source code has parser errorspublic static Statement parseStatement(java.lang.String statement)
String
and returns a
Statement
that represents it.statement
- String
containing Java statement codeParseProblemException
- if the source code has parser errorsprivate static <T extends Node> T simplifiedParse(ParseStart<T> context, Provider provider)
public static ImportDeclaration parseImport(java.lang.String importDeclaration)
String
and returns a
ImportDeclaration
that represents it.importDeclaration
- String
containing Java import codeParseProblemException
- if the source code has parser errorspublic static <T extends Expression> T parseExpression(java.lang.String expression)
String
and returns a
Expression
that represents it.expression
- String
containing Java expressionParseProblemException
- if the source code has parser errorspublic static AnnotationExpr parseAnnotation(java.lang.String annotation)
String
and returns a
AnnotationExpr
that represents it.annotation
- String
containing Java annotationParseProblemException
- if the source code has parser errorspublic static BodyDeclaration<?> parseAnnotationBodyDeclaration(java.lang.String body)
String
and returns a BodyDeclaration
that represents it.body
- String
containing Java body declarationParseProblemException
- if the source code has parser errors@Deprecated public static BodyDeclaration<?> parseClassBodyDeclaration(java.lang.String body)
BodyDeclaration
that represents it.body
- the body of a classParseProblemException
- if the source code has parser errors@Deprecated public static BodyDeclaration<?> parseInterfaceBodyDeclaration(java.lang.String body)
BodyDeclaration
that represents it.body
- the body of an interfaceParseProblemException
- if the source code has parser errorspublic static BodyDeclaration<?> parseBodyDeclaration(java.lang.String body)
BodyDeclaration
that represents it.body
- the body of a class or interfaceParseProblemException
- if the source code has parser errorspublic static ClassOrInterfaceType parseClassOrInterfaceType(java.lang.String type)
ClassOrInterfaceType
that represents it.type
- the type name like a.b.c.X or YParseProblemException
- if the source code has parser errorspublic static Type parseType(java.lang.String type)
Type
that represents it.type
- the type name like a.b.c.X, Y, or intParseProblemException
- if the source code has parser errorspublic static VariableDeclarationExpr parseVariableDeclarationExpr(java.lang.String declaration)
VariableDeclarationExpr
that represents it.declaration
- a variable declaration like int x=2;
ParseProblemException
- if the source code has parser errorspublic static Javadoc parseJavadoc(java.lang.String content)
Javadoc
that
represents it.content
- a variable declaration like content of my javadoc\n * second line\n * third line
ParseProblemException
- if the source code has parser errorspublic static ExplicitConstructorInvocationStmt parseExplicitConstructorInvocationStmt(java.lang.String statement)
statement
- a statement like super("hello");ParseProblemException
- if the source code has parser errorspublic static Name parseName(java.lang.String qualifiedName)
qualifiedName
- a name like "com.laamella.parameter_source"ParseProblemException
- if the source code has parser errorspublic static Parameter parseParameter(java.lang.String parameter)
parameter
- a parameter like "int[] x"ParseProblemException
- if the source code has parser errorspublic static PackageDeclaration parsePackageDeclaration(java.lang.String packageDeclaration)
packageDeclaration
- a declaration like "package com.microsoft.java;"ParseProblemException
- if the source code has parser errors