Top Description Fields Constructors Methods
com.github.javaparser

public final Class JavaParser

extends Object
Class Inheritance
Imports
com.github.javaparser.ast.CompilationUnit, .ImportDeclaration, .Node, .PackageDeclaration, com.github.javaparser.ast.body.BodyDeclaration, .MethodDeclaration, .Parameter, .TypeDeclaration, com.github.javaparser.ast.expr.*, com.github.javaparser.ast.modules.ModuleDeclaration, .ModuleDirective, com.github.javaparser.ast.stmt.BlockStmt, .ExplicitConstructorInvocationStmt, .Statement, com.github.javaparser.ast.type.ClassOrInterfaceType, .Type, .TypeParameter, java.io.*, java.nio.charset.Charset, java.nio.file.Path

Parse Java source code and creates Abstract Syntax Trees.
Author
Júlio Vilmar Gesser
See Also
StaticJavaParser

Field Summary

Modifier and TypeField and Description
private GeneratedJavaParser
private final ParserConfiguration

Constructor Summary

AccessConstructor and Description
public
JavaParser()

Instantiate the parser with default configuration.

public
JavaParser(ParserConfiguration configuration)

Instantiate the parser.

Method Summary

Modifier and TypeMethod and Description
public ParserConfiguration

Returns:

The configuration for this parser.
getParserConfiguration
()

private GeneratedJavaParser
public <
the subclass of Node that is the result of parsing in the start.
N extends Node
>
ParseResult<N>

Returns:

the parse result, a collection of encountered problems, and some extra data.
parse
(ParseStart<N>
refer to the constants in ParseStart to see what can be parsed.
start
,
Provider
refer to Providers to see how you can read source. The provider will be closed after parsing.
provider
)

Parses source code.

public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parse
(final InputStream
InputStream containing Java source code. It will be closed after parsing.
in
,
Charset
encoding of the source code
encoding
)

Parses the Java code contained in the InputStream and returns a CompilationUnit that represents it.

public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parse
(final InputStream
InputStream containing Java source code. It will be closed after parsing.
in
)

Parses the Java code contained in the InputStream and returns a CompilationUnit that represents it.

public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parse
(final File
File containing Java source code. It will be closed after parsing.
file
,
final Charset
encoding of the source code
encoding
)

Deprecated set the encoding in the ParserConfiguration
Parses the Java code contained in a File and returns a CompilationUnit that represents it.
public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parse
(final File
File containing Java source code. It will be closed after parsing.
file
)

Parses the Java code contained in a File and returns a CompilationUnit that represents it.

public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parse
(final Path
path to a file containing Java source code
path
,
final Charset
encoding of the source code
encoding
)

Deprecated set the encoding in the ParserConfiguration
Parses the Java code contained in a file and returns a CompilationUnit that represents it.
public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parse
(final Path
path to a file containing Java source code
path
)

Parses the Java code contained in a file and returns a CompilationUnit that represents it.

public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parse
(final Reader
the reader containing Java source code. It will be closed after parsing.
reader
)

Parses Java code from a Reader and returns a CompilationUnit that represents it.

public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parse
(String
Java source code
code
)

Parses the Java code contained in code and returns a CompilationUnit that represents it.

public ParseResult<AnnotationExpr>

Returns:

AnnotationExpr representing the Java annotation
parseAnnotation
(final String
String containing Java annotation
annotation
)

Parses the Java annotation contained in a String and returns a AnnotationExpr that represents it.

public ParseResult<BodyDeclaration<?>>

Returns:

BodyDeclaration representing the Java annotation
parseAnnotationBodyDeclaration
(final String
String containing Java body declaration
body
)

Parses the Java annotation body declaration(e.g fields or methods) contained in a String and returns a BodyDeclaration that represents it.

public ParseResult<BlockStmt>

Returns:

BlockStmt representing the Java block
parseBlock
(final String
String containing Java block code
blockStatement
)

Parses the Java block contained in a String and returns a BlockStmt that represents it.

public <T extends BodyDeclaration<?>> ParseResult<T>

Returns:

BodyDeclaration representing the Java interface body
parseBodyDeclaration
(String
the body of a class or interface
body
)

Parses a Java class or interface body declaration(e.g fields or methods) and returns a BodyDeclaration that represents it.

public ParseResult<ClassOrInterfaceType>

Returns:

ClassOrInterfaceType representing the type
parseClassOrInterfaceType
(String
the type name like a.b.c.X or Y
type
)

Parses a Java class or interface type name and returns a ClassOrInterfaceType that represents it.

public ParseResult<ExplicitConstructorInvocationStmt>

Returns:

the AST for the statement.
parseExplicitConstructorInvocationStmt
(String
a statement like super("hello");
statement
)

Parses the this(...) and super(...) statements that may occur at the start of a constructor.

public <T extends Expression> ParseResult<T>

Returns:

Expression representing the Java expression
parseExpression
(final String
String containing Java expression
expression
)

Parses the Java expression contained in a String and returns a Expression that represents it.

public ParseResult<ImportDeclaration>

Returns:

ImportDeclaration representing the Java import declaration
parseImport
(final String
String containing Java import code
importDeclaration
)

Parses the Java import contained in a String and returns a ImportDeclaration that represents it.

public ParseResult<MethodDeclaration>

Returns:

the AST for the method declaration
parseMethodDeclaration
(String
a method declaration like "void foo() {}"
methodDeclaration
)

Parses a method declaration and returns it as a MethodDeclaration.

public ParseResult<ModuleDeclaration>

Returns:

the AST for the module declaration
parseModuleDeclaration
(String
a declaration like "module X {}"
moduleDeclaration
)

Parses a module declaration and returns it as a ModuleDeclaration.

public ParseResult<ModuleDirective>

Returns:

the AST for the module directive
parseModuleDirective
(String
a directive like "opens C;"
moduleDirective
)

Parses a module directive and returns it as a ModuleDirective.

public ParseResult<Name>

Returns:

the AST for the name
parseName
(String
a name like "com.laamella.parameter_source"
qualifiedName
)

Parses a qualified name (one that can have "."s in it) and returns it as a Name.

public ParseResult<PackageDeclaration>

Returns:

the AST for the parameter
parsePackageDeclaration
(String
a declaration like "package com.microsoft.java;"
packageDeclaration
)

Parses a package declaration and returns it as a PackageDeclaration.

public ParseResult<Parameter>

Returns:

the AST for the parameter
parseParameter
(String
a parameter like "int[] x"
parameter
)

Parses a single parameter (a type and a name) and returns it as a Parameter.

public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parseResource
(final String
path to a resource containing Java source code. As resource is accessed through a class loader, a leading "/" is not allowed in pathToResource
path
)

Parses the Java code contained in a resource and returns a CompilationUnit that represents it.

public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parseResource
(final String
path to a resource containing Java source code. As resource is accessed through a class loader, a leading "/" is not allowed in pathToResource
path
,
Charset
encoding of the source code
encoding
)

Deprecated set the encoding in the ParserConfiguration
Parses the Java code contained in a resource and returns a CompilationUnit that represents it.
public ParseResult<CompilationUnit>

Returns:

CompilationUnit representing the Java source code
parseResource
(final ClassLoader
the classLoader that is asked to load the resource
classLoader
,
final String
path to a resource containing Java source code. As resource is accessed through a class loader, a leading "/" is not allowed in pathToResource
path
,
Charset encoding)

Deprecated set the encoding in the ParserConfiguration
Parses the Java code contained in a resource and returns a CompilationUnit that represents it.
public ParseResult<SimpleName>

Returns:

the AST for the name
parseSimpleName
(String
a name like "parameter_source"
name
)

Parses a simple name (one that can NOT have "."s in it) and returns it as a SimpleName.

public ParseResult<Statement>

Returns:

Statement representing the Java statement
parseStatement
(final String
String containing Java statement code
statement
)

Parses the Java statement contained in a String and returns a Statement that represents it.

public ParseResult<Type>

Returns:

ClassOrInterfaceType representing the type
parseType
(String
the type name like a.b.c.X, Y, or int
type
)

Parses a Java type name and returns a Type that represents it.

public ParseResult<TypeDeclaration<?>>

Returns:

the AST for the type declaration
parseTypeDeclaration
(String
a declaration like "class X {}"
typeDeclaration
)

Parses a type declaration and returns it as a TypeDeclaration.

public ParseResult<TypeParameter>

Returns:

the AST for the type parameter
parseTypeParameter
(String
a parameter like "T extends Serializable"
typeParameter
)

Parses a type parameter and returns it as a TypeParameter

public ParseResult<VariableDeclarationExpr>

Returns:

VariableDeclarationExpr representing the type
parseVariableDeclarationExpr
(String
a variable declaration like int x=2;
declaration
)

Parses a variable declaration expression and returns a com.github.javaparser.ast.expr.VariableDeclarationExpr that represents it.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

astParserback to summary
private GeneratedJavaParser astParser
configurationback to summary
private final ParserConfiguration configuration

Constructor Detail

JavaParserback to summary
public JavaParser()

Instantiate the parser with default configuration. Note that parsing can also be done with the static methods StaticJavaParser. Creating an instance will reduce setup time between parsing files.

JavaParserback to summary
public JavaParser(ParserConfiguration configuration)

Instantiate the parser. Note that parsing can also be done with the static methods StaticJavaParser. Creating an instance will reduce setup time between parsing files.

Method Detail

getParserConfigurationback to summary
public ParserConfiguration getParserConfiguration()
Returns:ParserConfiguration

The configuration for this parser.

getParserForProviderback to summary
private GeneratedJavaParser getParserForProvider(Provider provider)
parseback to summary
public <N extends Node> ParseResult<N> parse(ParseStart<N> start, Provider provider)

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...)

Parameters
<N>
the subclass of Node that is the result of parsing in the start.
start:ParseStart<N>

refer to the constants in ParseStart to see what can be parsed.

provider:Provider

refer to Providers to see how you can read source. The provider will be closed after parsing.

Returns:ParseResult<N>

the parse result, a collection of encountered problems, and some extra data.

parseback to summary
public ParseResult<CompilationUnit> parse(final InputStream in, Charset encoding)

Parses the Java code contained in the InputStream and returns a CompilationUnit that represents it.

Parameters
in:InputStream

InputStream containing Java source code. It will be closed after parsing.

encoding:Charset

encoding of the source code

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Exceptions
ParseProblemException:
if the source code has parser errors
parseback to summary
public ParseResult<CompilationUnit> parse(final InputStream in)

Parses the Java code contained in the InputStream and returns a CompilationUnit that represents it.

Parameters
in:InputStream

InputStream containing Java source code. It will be closed after parsing.

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Exceptions
ParseProblemException:
if the source code has parser errors
parseback to summary
public ParseResult<CompilationUnit> parse(final File file, final Charset encoding) throws FileNotFoundException

Deprecated

set the encoding in the ParserConfiguration

Parses the Java code contained in a File and returns a CompilationUnit that represents it.

Parameters
file:File

File containing Java source code. It will be closed after parsing.

encoding:Charset

encoding of the source code

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Annotations
@Deprecated
Exceptions
FileNotFoundException:
the file was not found
ParseProblemException:
if the source code has parser errors
parseback to summary
public ParseResult<CompilationUnit> parse(final File file) throws FileNotFoundException

Parses the Java code contained in a File and returns a CompilationUnit that represents it.

Parameters
file:File

File containing Java source code. It will be closed after parsing.

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Exceptions
FileNotFoundException:
the file was not found
ParseProblemException:
if the source code has parser errors
parseback to summary
public ParseResult<CompilationUnit> parse(final Path path, final Charset encoding) throws IOException

Deprecated

set the encoding in the ParserConfiguration

Parses the Java code contained in a file and returns a CompilationUnit that represents it.

Parameters
path:Path

path to a file containing Java source code

encoding:Charset

encoding of the source code

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Annotations
@Deprecated
Exceptions
IOException:
the path could not be accessed
ParseProblemException:
if the source code has parser errors
parseback to summary
public ParseResult<CompilationUnit> parse(final Path path) throws IOException

Parses the Java code contained in a file and returns a CompilationUnit that represents it.

Parameters
path:Path

path to a file containing Java source code

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Exceptions
IOException:
the path could not be accessed
ParseProblemException:
if the source code has parser errors
parseback to summary
public ParseResult<CompilationUnit> parse(final Reader reader)

Parses Java code from a Reader and returns a CompilationUnit that represents it.

Parameters
reader:Reader

the reader containing Java source code. It will be closed after parsing.

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Exceptions
ParseProblemException:
if the source code has parser errors
parseback to summary
public ParseResult<CompilationUnit> parse(String code)

Parses the Java code contained in code and returns a CompilationUnit that represents it.

Parameters
code:String

Java source code

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Exceptions
ParseProblemException:
if the source code has parser errors
parseAnnotationback to summary
public ParseResult<AnnotationExpr> parseAnnotation(final String annotation)

Parses the Java annotation contained in a String and returns a AnnotationExpr that represents it.

Parameters
annotation:String

String containing Java annotation

Returns:ParseResult<AnnotationExpr>

AnnotationExpr representing the Java annotation

Exceptions
ParseProblemException:
if the source code has parser errors
parseAnnotationBodyDeclarationback to summary
public ParseResult<BodyDeclaration<?>> parseAnnotationBodyDeclaration(final String body)

Parses the Java annotation body declaration(e.g fields or methods) contained in a String and returns a BodyDeclaration that represents it.

Parameters
body:String

String containing Java body declaration

Returns:ParseResult<BodyDeclaration<?>>

BodyDeclaration representing the Java annotation

Exceptions
ParseProblemException:
if the source code has parser errors
parseBlockback to summary
public ParseResult<BlockStmt> parseBlock(final String blockStatement)

Parses the Java block contained in a String and returns a BlockStmt that represents it.

Parameters
blockStatement:String

String containing Java block code

Returns:ParseResult<BlockStmt>

BlockStmt representing the Java block

Exceptions
ParseProblemException:
if the source code has parser errors
parseBodyDeclarationback to summary
public <T extends BodyDeclaration<?>> ParseResult<T> parseBodyDeclaration(String body)

Parses a Java class or interface body declaration(e.g fields or methods) and returns a BodyDeclaration that represents it.

Parameters
body:String

the body of a class or interface

Returns:ParseResult<T>

BodyDeclaration representing the Java interface body

Annotations
@SuppressWarnings:unchecked
Exceptions
ParseProblemException:
if the source code has parser errors
parseClassOrInterfaceTypeback to summary
public ParseResult<ClassOrInterfaceType> parseClassOrInterfaceType(String type)

Parses a Java class or interface type name and returns a ClassOrInterfaceType that represents it.

Parameters
type:String

the type name like a.b.c.X or Y

Returns:ParseResult<ClassOrInterfaceType>

ClassOrInterfaceType representing the type

Exceptions
ParseProblemException:
if the source code has parser errors
parseExplicitConstructorInvocationStmtback to summary
public ParseResult<ExplicitConstructorInvocationStmt> parseExplicitConstructorInvocationStmt(String statement)

Parses the this(...) and super(...) statements that may occur at the start of a constructor.

Parameters
statement:String

a statement like super("hello");

Returns:ParseResult<ExplicitConstructorInvocationStmt>

the AST for the statement.

Exceptions
ParseProblemException:
if the source code has parser errors
parseExpressionback to summary
public <T extends Expression> ParseResult<T> parseExpression(final String expression)

Parses the Java expression contained in a String and returns a Expression that represents it.

Parameters
expression:String

String containing Java expression

Returns:ParseResult<T>

Expression representing the Java expression

Annotations
@SuppressWarnings:unchecked
Exceptions
ParseProblemException:
if the source code has parser errors
parseImportback to summary
public ParseResult<ImportDeclaration> parseImport(final String importDeclaration)

Parses the Java import contained in a String and returns a ImportDeclaration that represents it.

Parameters
importDeclaration:String

String containing Java import code

Returns:ParseResult<ImportDeclaration>

ImportDeclaration representing the Java import declaration

Exceptions
ParseProblemException:
if the source code has parser errors
parseMethodDeclarationback to summary
public ParseResult<MethodDeclaration> parseMethodDeclaration(String methodDeclaration)

Parses a method declaration and returns it as a MethodDeclaration.

Parameters
methodDeclaration:String

a method declaration like "void foo() {}"

Returns:ParseResult<MethodDeclaration>

the AST for the method declaration

Exceptions
ParseProblemException:
if the source code has parser errors
See Also
MethodDeclaration
parseModuleDeclarationback to summary
public ParseResult<ModuleDeclaration> parseModuleDeclaration(String moduleDeclaration)

Parses a module declaration and returns it as a ModuleDeclaration.

Parameters
moduleDeclaration:String

a declaration like "module X {}"

Returns:ParseResult<ModuleDeclaration>

the AST for the module declaration

Exceptions
ParseProblemException:
if the source code has parser errors
See Also
ModuleDeclaration
parseModuleDirectiveback to summary
public ParseResult<ModuleDirective> parseModuleDirective(String moduleDirective)

Parses a module directive and returns it as a ModuleDirective.

Parameters
moduleDirective:String

a directive like "opens C;"

Returns:ParseResult<ModuleDirective>

the AST for the module directive

Exceptions
ParseProblemException:
if the source code has parser errors
See Also
ModuleDirective
parseNameback to summary
public ParseResult<Name> parseName(String qualifiedName)

Parses a qualified name (one that can have "."s in it) and returns it as a Name.

Parameters
qualifiedName:String

a name like "com.laamella.parameter_source"

Returns:ParseResult<Name>

the AST for the name

Exceptions
ParseProblemException:
if the source code has parser errors
parsePackageDeclarationback to summary
public ParseResult<PackageDeclaration> parsePackageDeclaration(String packageDeclaration)

Parses a package declaration and returns it as a PackageDeclaration.

Parameters
packageDeclaration:String

a declaration like "package com.microsoft.java;"

Returns:ParseResult<PackageDeclaration>

the AST for the parameter

Exceptions
ParseProblemException:
if the source code has parser errors
parseParameterback to summary
public ParseResult<Parameter> parseParameter(String parameter)

Parses a single parameter (a type and a name) and returns it as a Parameter.

Parameters
parameter:String

a parameter like "int[] x"

Returns:ParseResult<Parameter>

the AST for the parameter

Exceptions
ParseProblemException:
if the source code has parser errors
parseResourceback to summary
public ParseResult<CompilationUnit> parseResource(final String path) throws IOException

Parses the Java code contained in a resource and returns a CompilationUnit that represents it.

Parameters
path:String

path to a resource containing Java source code. As resource is accessed through a class loader, a leading "/" is not allowed in pathToResource

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Exceptions
IOException:
the path could not be accessed
ParseProblemException:
if the source code has parser errors
parseResourceback to summary
public ParseResult<CompilationUnit> parseResource(final String path, Charset encoding) throws IOException

Deprecated

set the encoding in the ParserConfiguration

Parses the Java code contained in a resource and returns a CompilationUnit that represents it.

Parameters
path:String

path to a resource containing Java source code. As resource is accessed through a class loader, a leading "/" is not allowed in pathToResource

encoding:Charset

encoding of the source code

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Annotations
@Deprecated
Exceptions
IOException:
the path could not be accessed
ParseProblemException:
if the source code has parser errors
parseResourceback to summary
public ParseResult<CompilationUnit> parseResource(final ClassLoader classLoader, final String path, Charset encoding) throws IOException

Deprecated

set the encoding in the ParserConfiguration

Parses the Java code contained in a resource and returns a CompilationUnit that represents it.

Parameters
classLoader:ClassLoader

the classLoader that is asked to load the resource

path:String

path to a resource containing Java source code. As resource is accessed through a class loader, a leading "/" is not allowed in pathToResource

Returns:ParseResult<CompilationUnit>

CompilationUnit representing the Java source code

Annotations
@Deprecated
Exceptions
IOException:
the path could not be accessed
ParseProblemException:
if the source code has parser errors
parseSimpleNameback to summary
public ParseResult<SimpleName> parseSimpleName(String name)

Parses a simple name (one that can NOT have "."s in it) and returns it as a SimpleName.

Parameters
name:String

a name like "parameter_source"

Returns:ParseResult<SimpleName>

the AST for the name

Exceptions
ParseProblemException:
if the source code has parser errors
parseStatementback to summary
public ParseResult<Statement> parseStatement(final String statement)

Parses the Java statement contained in a String and returns a Statement that represents it.

Parameters
statement:String

String containing Java statement code

Returns:ParseResult<Statement>

Statement representing the Java statement

Exceptions
ParseProblemException:
if the source code has parser errors
parseTypeback to summary
public ParseResult<Type> parseType(String type)

Parses a Java type name and returns a Type that represents it.

Parameters
type:String

the type name like a.b.c.X, Y, or int

Returns:ParseResult<Type>

ClassOrInterfaceType representing the type

Exceptions
ParseProblemException:
if the source code has parser errors
parseTypeDeclarationback to summary
public ParseResult<TypeDeclaration<?>> parseTypeDeclaration(String typeDeclaration)

Parses a type declaration and returns it as a TypeDeclaration.

Parameters
typeDeclaration:String

a declaration like "class X {}"

Returns:ParseResult<TypeDeclaration<?>>

the AST for the type declaration

Exceptions
ParseProblemException:
if the source code has parser errors
parseTypeParameterback to summary
public ParseResult<TypeParameter> parseTypeParameter(String typeParameter)

Parses a type parameter and returns it as a TypeParameter

Parameters
typeParameter:String

a parameter like "T extends Serializable"

Returns:ParseResult<TypeParameter>

the AST for the type parameter

Exceptions
ParseProblemException:
if the source code has parser errors
parseVariableDeclarationExprback to summary
public ParseResult<VariableDeclarationExpr> parseVariableDeclarationExpr(String declaration)

Parses a variable declaration expression and returns a com.github.javaparser.ast.expr.VariableDeclarationExpr that represents it.

Parameters
declaration:String

a variable declaration like int x=2;

Returns:ParseResult<VariableDeclarationExpr>

VariableDeclarationExpr representing the type

Exceptions
ParseProblemException:
if the source code has parser errors