public final class JavaParser extends Object
StaticJavaParser
Constructor and Description |
---|
JavaParser()
Instantiate the parser with default configuration.
|
JavaParser(ParserConfiguration configuration)
Instantiate the parser.
|
Modifier and Type | Method and Description |
---|---|
ParserConfiguration |
getParserConfiguration() |
ParseResult<CompilationUnit> |
parse(File file)
|
ParseResult<CompilationUnit> |
parse(File file,
Charset encoding)
Deprecated.
set the encoding in the
ParserConfiguration |
ParseResult<CompilationUnit> |
parse(InputStream in)
|
ParseResult<CompilationUnit> |
parse(InputStream in,
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.
|
ParseResult<CompilationUnit> |
parse(Path path)
Parses the Java code contained in a file and returns a
CompilationUnit that represents it. |
ParseResult<CompilationUnit> |
parse(Path path,
Charset encoding)
Deprecated.
set the encoding in the
ParserConfiguration |
ParseResult<CompilationUnit> |
parse(Reader reader)
Parses Java code from a Reader and returns a
CompilationUnit that represents it. |
ParseResult<CompilationUnit> |
parse(String code)
Parses the Java code contained in code and returns a
CompilationUnit that represents it. |
ParseResult<AnnotationExpr> |
parseAnnotation(String annotation)
Parses the Java annotation contained in a
String and returns a
AnnotationExpr that represents it. |
ParseResult<BodyDeclaration<?>> |
parseAnnotationBodyDeclaration(String body)
Parses the Java annotation body declaration(e.g fields or methods) contained in a
String and returns a BodyDeclaration that represents it. |
ParseResult<BlockStmt> |
parseBlock(String blockStatement)
|
ParseResult<BodyDeclaration<?>> |
parseBodyDeclaration(String body)
Parses a Java class or interface body declaration(e.g fields or methods) and returns a
BodyDeclaration that represents it. |
ParseResult<ClassOrInterfaceType> |
parseClassOrInterfaceType(String type)
Parses a Java class or interface type name and returns a
ClassOrInterfaceType that represents it. |
ParseResult<ExplicitConstructorInvocationStmt> |
parseExplicitConstructorInvocationStmt(String statement)
Parses the this(...) and super(...) statements that may occur at the start of a constructor.
|
<T extends Expression> |
parseExpression(String expression)
Parses the Java expression contained in a
String and returns a
Expression that represents it. |
ParseResult<ImportDeclaration> |
parseImport(String importDeclaration)
Parses the Java import contained in a
String and returns a
ImportDeclaration that represents it. |
ParseResult<ModuleDeclaration> |
parseModuleDeclaration(String moduleDeclaration)
Parses a module declaration and returns it as a ModuleDeclaration.
|
ParseResult<ModuleDirective> |
parseModuleDirective(String moduleDirective)
Parses a module directive and returns it as a ModuleDirective.
|
ParseResult<Name> |
parseName(String qualifiedName)
Parses a qualified name (one that can have "."s in it) and returns it as a Name.
|
ParseResult<PackageDeclaration> |
parsePackageDeclaration(String packageDeclaration)
Parses a package declaration and returns it as a PackageDeclaration.
|
ParseResult<Parameter> |
parseParameter(String parameter)
Parses a single parameter (a type and a name) and returns it as a Parameter.
|
ParseResult<CompilationUnit> |
parseResource(ClassLoader classLoader,
String path,
Charset encoding)
Deprecated.
set the encoding in the
ParserConfiguration |
ParseResult<CompilationUnit> |
parseResource(String path)
Parses the Java code contained in a resource and returns a
CompilationUnit that represents it. |
ParseResult<CompilationUnit> |
parseResource(String path,
Charset encoding)
Deprecated.
set the encoding in the
ParserConfiguration |
ParseResult<SimpleName> |
parseSimpleName(String name)
Parses a simple name (one that can NOT have "."s in it) and returns it as a SimpleName.
|
ParseResult<Statement> |
parseStatement(String statement)
|
ParseResult<Type> |
parseType(String type)
Parses a Java type name and returns a
Type that represents it. |
ParseResult<TypeDeclaration<?>> |
parseTypeDeclaration(String typeDeclaration)
Parses a type declaration and returns it as a TypeDeclaration.
|
ParseResult<TypeParameter> |
parseTypeParameter(String typeParameter)
Parses a type parameter and returns it as a TypeParameter
|
ParseResult<VariableDeclarationExpr> |
parseVariableDeclarationExpr(String declaration)
Parses a variable declaration expression and returns a
VariableDeclarationExpr
that represents it. |
public JavaParser()
StaticJavaParser
.
Creating an instance will reduce setup time between parsing files.public JavaParser(ParserConfiguration configuration)
StaticJavaParser
.
Creating an instance will reduce setup time between parsing files.public ParserConfiguration getParserConfiguration()
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 ParseResult<CompilationUnit> parse(InputStream in, 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 ParseResult<CompilationUnit> parse(InputStream in)
in
- InputStream
containing Java source code. It will be closed after parsing.ParseProblemException
- if the source code has parser errors@Deprecated public ParseResult<CompilationUnit> parse(File file, Charset encoding) throws FileNotFoundException
ParserConfiguration
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 errorsFileNotFoundException
- the file was not foundpublic ParseResult<CompilationUnit> parse(File file) throws FileNotFoundException
file
- File
containing Java source code. It will be closed after parsing.ParseProblemException
- if the source code has parser errorsFileNotFoundException
- the file was not found@Deprecated public ParseResult<CompilationUnit> parse(Path path, Charset encoding) throws IOException
ParserConfiguration
CompilationUnit
that represents it.path
- path to a file containing Java source codeencoding
- encoding of the source codeIOException
- the path could not be accessedParseProblemException
- if the source code has parser errorspublic ParseResult<CompilationUnit> parse(Path path) throws IOException
CompilationUnit
that represents it.path
- path to a file containing Java source codeParseProblemException
- if the source code has parser errorsIOException
- the path could not be accessedpublic ParseResult<CompilationUnit> parseResource(String path) throws 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 errorsIOException
- the path could not be accessed@Deprecated public ParseResult<CompilationUnit> parseResource(String path, Charset encoding) throws IOException
ParserConfiguration
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 errorsIOException
- the path could not be accessed@Deprecated public ParseResult<CompilationUnit> parseResource(ClassLoader classLoader, String path, Charset encoding) throws IOException
ParserConfiguration
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 errorsIOException
- the path could not be accessedpublic ParseResult<CompilationUnit> parse(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 ParseResult<CompilationUnit> parse(String code)
CompilationUnit
that represents it.code
- Java source codeParseProblemException
- if the source code has parser errorspublic ParseResult<BlockStmt> parseBlock(String blockStatement)
blockStatement
- String
containing Java block codeParseProblemException
- if the source code has parser errorspublic ParseResult<Statement> parseStatement(String statement)
statement
- String
containing Java statement codeParseProblemException
- if the source code has parser errorspublic ParseResult<ImportDeclaration> parseImport(String importDeclaration)
String
and returns a
ImportDeclaration
that represents it.importDeclaration
- String
containing Java import codeParseProblemException
- if the source code has parser errorspublic <T extends Expression> ParseResult<T> parseExpression(String expression)
String
and returns a
Expression
that represents it.expression
- String
containing Java expressionParseProblemException
- if the source code has parser errorspublic ParseResult<AnnotationExpr> parseAnnotation(String annotation)
String
and returns a
AnnotationExpr
that represents it.annotation
- String
containing Java annotationParseProblemException
- if the source code has parser errorspublic ParseResult<BodyDeclaration<?>> parseAnnotationBodyDeclaration(String body)
String
and returns a BodyDeclaration
that represents it.body
- String
containing Java body declarationParseProblemException
- if the source code has parser errorspublic ParseResult<BodyDeclaration<?>> parseBodyDeclaration(String body)
BodyDeclaration
that represents it.body
- the body of a class or interfaceParseProblemException
- if the source code has parser errorspublic ParseResult<ClassOrInterfaceType> parseClassOrInterfaceType(String type)
ClassOrInterfaceType
that represents it.type
- the type name like a.b.c.X or YParseProblemException
- if the source code has parser errorspublic ParseResult<Type> parseType(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 ParseResult<VariableDeclarationExpr> parseVariableDeclarationExpr(String declaration)
VariableDeclarationExpr
that represents it.declaration
- a variable declaration like int x=2;
ParseProblemException
- if the source code has parser errorspublic ParseResult<ExplicitConstructorInvocationStmt> parseExplicitConstructorInvocationStmt(String statement)
statement
- a statement like super("hello");ParseProblemException
- if the source code has parser errorspublic ParseResult<Name> parseName(String qualifiedName)
qualifiedName
- a name like "com.laamella.parameter_source"ParseProblemException
- if the source code has parser errorspublic ParseResult<SimpleName> parseSimpleName(String name)
name
- a name like "parameter_source"ParseProblemException
- if the source code has parser errorspublic ParseResult<Parameter> parseParameter(String parameter)
parameter
- a parameter like "int[] x"ParseProblemException
- if the source code has parser errorspublic ParseResult<PackageDeclaration> parsePackageDeclaration(String packageDeclaration)
packageDeclaration
- a declaration like "package com.microsoft.java;"ParseProblemException
- if the source code has parser errorspublic ParseResult<TypeDeclaration<?>> parseTypeDeclaration(String typeDeclaration)
typeDeclaration
- a declaration like "class X {}"ParseProblemException
- if the source code has parser errorspublic ParseResult<ModuleDeclaration> parseModuleDeclaration(String moduleDeclaration)
moduleDeclaration
- a declaration like "module X {}"ParseProblemException
- if the source code has parser errorsModuleDeclaration
public ParseResult<ModuleDirective> parseModuleDirective(String moduleDirective)
moduleDirective
- a directive like "opens C;"ParseProblemException
- if the source code has parser errorsModuleDirective
public ParseResult<TypeParameter> parseTypeParameter(String typeParameter)
typeParameter
- a parameter like "T extends Serializable"ParseProblemException
- if the source code has parser errorsCopyright © 2007–2019. All rights reserved.