Package com.github.javaparser
Class JavaParser
- java.lang.Object
-
- com.github.javaparser.JavaParser
-
public final class JavaParser extends Object
Parse Java source code and creates Abstract Syntax Trees.- Author:
- JĂșlio Vilmar Gesser
- See Also:
StaticJavaParser
-
-
Constructor Summary
Constructors Constructor Description JavaParser()
Instantiate the parser with default configuration.JavaParser(ParserConfiguration configuration)
Instantiate the parser.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description ParserConfiguration
getParserConfiguration()
<N extends Node>
ParseResult<N>parse(ParseStart<N> start, Provider provider)
Parses source code.ParseResult<CompilationUnit>
parse(File file)
ParseResult<CompilationUnit>
parse(File file, Charset encoding)
Deprecated.set the encoding in theParserConfiguration
ParseResult<CompilationUnit>
parse(InputStream in)
ParseResult<CompilationUnit>
parse(InputStream in, Charset encoding)
Parses the Java code contained in theInputStream
and returns aCompilationUnit
that represents it.ParseResult<CompilationUnit>
parse(Reader reader)
Parses Java code from a Reader and returns aCompilationUnit
that represents it.ParseResult<CompilationUnit>
parse(String code)
Parses the Java code contained in code and returns aCompilationUnit
that represents it.ParseResult<CompilationUnit>
parse(Path path)
Parses the Java code contained in a file and returns aCompilationUnit
that represents it.ParseResult<CompilationUnit>
parse(Path path, Charset encoding)
Deprecated.set the encoding in theParserConfiguration
ParseResult<AnnotationExpr>
parseAnnotation(String annotation)
Parses the Java annotation contained in aString
and returns aAnnotationExpr
that represents it.ParseResult<BodyDeclaration<?>>
parseAnnotationBodyDeclaration(String body)
Parses the Java annotation body declaration(e.g fields or methods) contained in aString
and returns aBodyDeclaration
that represents it.ParseResult<ArrayInitializerExpr>
parseArrayInitializerExpr(String arrayInitializerExpr)
Parses an array initializer expression and returns it as ArrayInitializerExpr.ParseResult<BlockStmt>
parseBlock(String blockStatement)
<T extends BodyDeclaration<?>>
ParseResult<T>parseBodyDeclaration(String body)
Parses a Java class or interface body declaration(e.g fields or methods) and returns aBodyDeclaration
that represents it.ParseResult<ClassOrInterfaceType>
parseClassOrInterfaceType(String type)
Parses a Java class or interface type name and returns aClassOrInterfaceType
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>
ParseResult<T>parseExpression(String expression)
Parses the Java expression contained in aString
and returns aExpression
that represents it.ParseResult<ImportDeclaration>
parseImport(String importDeclaration)
Parses the Java import contained in aString
and returns aImportDeclaration
that represents it.ParseResult<MethodDeclaration>
parseMethodDeclaration(String methodDeclaration)
Parses a method declaration and returns it as a MethodDeclaration.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 "."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 theParserConfiguration
ParseResult<CompilationUnit>
parseResource(String path)
Parses the Java code contained in a resource and returns aCompilationUnit
that represents it.ParseResult<CompilationUnit>
parseResource(String path, Charset encoding)
Deprecated.set the encoding in theParserConfiguration
ParseResult<SimpleName>
parseSimpleName(String name)
Parses a simple name (one that can NOT have "."ParseResult<Statement>
parseStatement(String statement)
ParseResult<Type>
parseType(String type)
Parses a Java type name and returns aType
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 TypeParameterParseResult<VariableDeclarationExpr>
parseVariableDeclarationExpr(String declaration)
Parses a variable declaration expression and returns aVariableDeclarationExpr
that represents it.
-
-
-
Constructor Detail
-
JavaParser
public JavaParser()
Instantiate the parser with default configuration. Note that parsing can also be done with the static methodsStaticJavaParser
. Creating an instance will reduce setup time between parsing files.
-
JavaParser
public JavaParser(ParserConfiguration configuration)
Instantiate the parser. Note that parsing can also be done with the static methodsStaticJavaParser
. Creating an instance will reduce setup time between parsing files.
-
-
Method Detail
-
getParserConfiguration
public ParserConfiguration getParserConfiguration()
- Returns:
- The configuration for this parser.
-
parse
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...)- Type Parameters:
N
- the subclass of Node that is the result of parsing in the start.- Parameters:
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.- Returns:
- the parse result, a collection of encountered problems, and some extra data.
-
parse
public ParseResult<CompilationUnit> parse(InputStream in, Charset encoding)
Parses the Java code contained in theInputStream
and returns aCompilationUnit
that represents it.- Parameters:
in
-InputStream
containing Java source code. It will be closed after parsing.encoding
- encoding of the source code- Returns:
- CompilationUnit representing the Java source code
-
parse
public ParseResult<CompilationUnit> parse(InputStream in)
- Parameters:
in
-InputStream
containing Java source code. It will be closed after parsing.- Returns:
- CompilationUnit representing the Java source code
-
parse
@Deprecated public ParseResult<CompilationUnit> parse(File file, Charset encoding) throws FileNotFoundException
Deprecated.set the encoding in theParserConfiguration
Parses the Java code contained in aFile
and returns aCompilationUnit
that represents it.- Parameters:
file
-File
containing Java source code. It will be closed after parsing.encoding
- encoding of the source code- Returns:
- CompilationUnit representing the Java source code
- Throws:
FileNotFoundException
- the file was not found
-
parse
public ParseResult<CompilationUnit> parse(File file) throws FileNotFoundException
- Parameters:
file
-File
containing Java source code. It will be closed after parsing.- Returns:
- CompilationUnit representing the Java source code
- Throws:
FileNotFoundException
- the file was not found
-
parse
@Deprecated public ParseResult<CompilationUnit> parse(Path path, Charset encoding) throws IOException
Deprecated.set the encoding in theParserConfiguration
Parses the Java code contained in a file and returns aCompilationUnit
that represents it.- Parameters:
path
- path to a file containing Java source codeencoding
- encoding of the source code- Returns:
- CompilationUnit representing the Java source code
- Throws:
IOException
- the path could not be accessed
-
parse
public ParseResult<CompilationUnit> parse(Path path) throws IOException
Parses the Java code contained in a file and returns aCompilationUnit
that represents it.- Parameters:
path
- path to a file containing Java source code- Returns:
- CompilationUnit representing the Java source code
- Throws:
IOException
- the path could not be accessed
-
parseResource
public ParseResult<CompilationUnit> parseResource(String path) throws IOException
Parses the Java code contained in a resource and returns aCompilationUnit
that represents it.- Parameters:
path
- path to a resource containing Java source code. As resource is accessed through a class loader, a leading "/" is not allowed in pathToResource- Returns:
- CompilationUnit representing the Java source code
- Throws:
IOException
- the path could not be accessed
-
parseResource
@Deprecated public ParseResult<CompilationUnit> parseResource(String path, Charset encoding) throws IOException
Deprecated.set the encoding in theParserConfiguration
Parses the Java code contained in a resource and returns aCompilationUnit
that represents it.- Parameters:
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 code- Returns:
- CompilationUnit representing the Java source code
- Throws:
IOException
- the path could not be accessed
-
parseResource
@Deprecated public ParseResult<CompilationUnit> parseResource(ClassLoader classLoader, String path, Charset encoding) throws IOException
Deprecated.set the encoding in theParserConfiguration
Parses the Java code contained in a resource and returns aCompilationUnit
that represents it.- Parameters:
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 pathToResource- Returns:
- CompilationUnit representing the Java source code
- Throws:
IOException
- the path could not be accessed
-
parse
public ParseResult<CompilationUnit> parse(Reader reader)
Parses Java code from a Reader and returns aCompilationUnit
that represents it.- Parameters:
reader
- the reader containing Java source code. It will be closed after parsing.- Returns:
- CompilationUnit representing the Java source code
-
parse
public ParseResult<CompilationUnit> parse(String code)
Parses the Java code contained in code and returns aCompilationUnit
that represents it.- Parameters:
code
- Java source code- Returns:
- CompilationUnit representing the Java source code
-
parseBlock
public ParseResult<BlockStmt> parseBlock(String blockStatement)
- Parameters:
blockStatement
-String
containing Java block code- Returns:
- BlockStmt representing the Java block
-
parseStatement
public ParseResult<Statement> parseStatement(String statement)
- Parameters:
statement
-String
containing Java statement code- Returns:
- Statement representing the Java statement
-
parseImport
public ParseResult<ImportDeclaration> parseImport(String importDeclaration)
Parses the Java import contained in aString
and returns aImportDeclaration
that represents it.- Parameters:
importDeclaration
-String
containing Java import code- Returns:
- ImportDeclaration representing the Java import declaration
-
parseExpression
public <T extends Expression> ParseResult<T> parseExpression(String expression)
Parses the Java expression contained in aString
and returns aExpression
that represents it.- Parameters:
expression
-String
containing Java expression- Returns:
- Expression representing the Java expression
-
parseAnnotation
public ParseResult<AnnotationExpr> parseAnnotation(String annotation)
Parses the Java annotation contained in aString
and returns aAnnotationExpr
that represents it.- Parameters:
annotation
-String
containing Java annotation- Returns:
- AnnotationExpr representing the Java annotation
-
parseAnnotationBodyDeclaration
public ParseResult<BodyDeclaration<?>> parseAnnotationBodyDeclaration(String body)
Parses the Java annotation body declaration(e.g fields or methods) contained in aString
and returns aBodyDeclaration
that represents it.- Parameters:
body
-String
containing Java body declaration- Returns:
- BodyDeclaration representing the Java annotation
-
parseBodyDeclaration
public <T extends BodyDeclaration<?>> ParseResult<T> parseBodyDeclaration(String body)
Parses a Java class or interface body declaration(e.g fields or methods) and returns aBodyDeclaration
that represents it.- Parameters:
body
- the body of a class or interface- Returns:
- BodyDeclaration representing the Java interface body
-
parseClassOrInterfaceType
public ParseResult<ClassOrInterfaceType> parseClassOrInterfaceType(String type)
Parses a Java class or interface type name and returns aClassOrInterfaceType
that represents it.- Parameters:
type
- the type name like a.b.c.X or Y- Returns:
- ClassOrInterfaceType representing the type
-
parseType
public ParseResult<Type> parseType(String type)
Parses a Java type name and returns aType
that represents it.- Parameters:
type
- the type name like a.b.c.X, Y, or int- Returns:
- ClassOrInterfaceType representing the type
-
parseVariableDeclarationExpr
public ParseResult<VariableDeclarationExpr> parseVariableDeclarationExpr(String declaration)
Parses a variable declaration expression and returns aVariableDeclarationExpr
that represents it.- Parameters:
declaration
- a variable declaration likeint x=2;
- Returns:
- VariableDeclarationExpr representing the type
-
parseExplicitConstructorInvocationStmt
public ParseResult<ExplicitConstructorInvocationStmt> parseExplicitConstructorInvocationStmt(String statement)
Parses the this(...) and super(...) statements that may occur at the start of a constructor.- Parameters:
statement
- a statement like super("hello");- Returns:
- the AST for the statement.
-
parseName
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
- a name like "com.laamella.parameter_source"- Returns:
- the AST for the name
-
parseSimpleName
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
- a name like "parameter_source"- Returns:
- the AST for the name
-
parseParameter
public ParseResult<Parameter> parseParameter(String parameter)
Parses a single parameter (a type and a name) and returns it as a Parameter.- Parameters:
parameter
- a parameter like "int[] x"- Returns:
- the AST for the parameter
-
parsePackageDeclaration
public ParseResult<PackageDeclaration> parsePackageDeclaration(String packageDeclaration)
Parses a package declaration and returns it as a PackageDeclaration.- Parameters:
packageDeclaration
- a declaration like "package com.microsoft.java;"- Returns:
- the AST for the parameter
-
parseTypeDeclaration
public ParseResult<TypeDeclaration<?>> parseTypeDeclaration(String typeDeclaration)
Parses a type declaration and returns it as a TypeDeclaration.- Parameters:
typeDeclaration
- a declaration like "class X {}"- Returns:
- the AST for the type declaration
-
parseModuleDeclaration
public ParseResult<ModuleDeclaration> parseModuleDeclaration(String moduleDeclaration)
Parses a module declaration and returns it as a ModuleDeclaration.- Parameters:
moduleDeclaration
- a declaration like "module X {}"- Returns:
- the AST for the module declaration
- See Also:
ModuleDeclaration
-
parseModuleDirective
public ParseResult<ModuleDirective> parseModuleDirective(String moduleDirective)
Parses a module directive and returns it as a ModuleDirective.- Parameters:
moduleDirective
- a directive like "opens C;"- Returns:
- the AST for the module directive
- See Also:
ModuleDirective
-
parseTypeParameter
public ParseResult<TypeParameter> parseTypeParameter(String typeParameter)
Parses a type parameter and returns it as a TypeParameter- Parameters:
typeParameter
- a parameter like "T extends Serializable"- Returns:
- the AST for the type parameter
-
parseMethodDeclaration
public ParseResult<MethodDeclaration> parseMethodDeclaration(String methodDeclaration)
Parses a method declaration and returns it as a MethodDeclaration.- Parameters:
methodDeclaration
- a method declaration like "void foo() {}"- Returns:
- the AST for the method declaration
- See Also:
MethodDeclaration
-
parseArrayInitializerExpr
public ParseResult<ArrayInitializerExpr> parseArrayInitializerExpr(String arrayInitializerExpr)
Parses an array initializer expression and returns it as ArrayInitializerExpr.- Parameters:
arrayInitializerExpr
- an array initializer like "{1,2,3}"- Returns:
- the AST for the array initializer expression
- See Also:
ArrayInitializerExpr
-
-