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
-
-
Constructor Summary
Constructors Constructor Description JavaParser()Instantiate the parser with default configuration.JavaParser(ParserConfiguration configuration)Instantiate the parser.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description ParserConfigurationgetParserConfiguration()Get the non-static configuration for this parser.static ParserConfigurationgetStaticConfiguration()Get the configuration for the static parse...<N extends Node>
ParseResult<N>parse(ParseStart<N> start, Provider provider)Parses source code.static CompilationUnitparse(File file)Parses the Java code contained in aFileand returns aCompilationUnitthat represents it.
Note: Uses UTF-8 encodingstatic CompilationUnitparse(File file, Charset encoding)Parses the Java code contained in aFileand returns aCompilationUnitthat represents it.static CompilationUnitparse(InputStream in)Parses the Java code contained in theInputStreamand returns aCompilationUnitthat represents it.
Note: Uses UTF-8 encodingstatic CompilationUnitparse(InputStream in, Charset encoding)Parses the Java code contained in theInputStreamand returns aCompilationUnitthat represents it.static CompilationUnitparse(Reader reader)Parses Java code from a Reader and returns aCompilationUnitthat represents it.static CompilationUnitparse(String code)Parses the Java code contained in code and returns aCompilationUnitthat represents it.static CompilationUnitparse(Path path)Parses the Java code contained in a file and returns aCompilationUnitthat represents it.
Note: Uses UTF-8 encodingstatic CompilationUnitparse(Path path, Charset encoding)Parses the Java code contained in a file and returns aCompilationUnitthat represents it.static AnnotationExprparseAnnotation(String annotation)Parses the Java annotation contained in aStringand returns aAnnotationExprthat represents it.static BodyDeclaration<?>parseAnnotationBodyDeclaration(String body)Parses the Java annotation body declaration(e.g fields or methods) contained in aStringand returns aBodyDeclarationthat represents it.static BlockStmtparseBlock(String blockStatement)static BodyDeclaration<?>parseBodyDeclaration(String body)Parses a Java class or interface body declaration(e.g fields or methods) and returns aBodyDeclarationthat represents it.static BodyDeclaration<?>parseClassBodyDeclaration(String body)Deprecated.just use parseBodyDeclaration now.static ClassOrInterfaceTypeparseClassOrInterfaceType(String type)Parses a Java class or interface type name and returns aClassOrInterfaceTypethat represents it.static ExplicitConstructorInvocationStmtparseExplicitConstructorInvocationStmt(String statement)Parses the this(...) and super(...) statements that may occur at the start of a constructor.static <T extends Expression>
TparseExpression(String expression)Parses the Java expression contained in aStringand returns aExpressionthat represents it.static ImportDeclarationparseImport(String importDeclaration)Parses the Java import contained in aStringand returns aImportDeclarationthat represents it.static BodyDeclaration<?>parseInterfaceBodyDeclaration(String body)Deprecated.just use parseBodyDeclaration now.static JavadocparseJavadoc(String content)Parses the content of a JavadocComment and returns aJavadocthat represents it.static ModuleDeclarationparseModuleDeclaration(String moduleDeclaration)Parses a module declaration and returns it as a ModuleDeclaration.static ModuleDirectiveparseModuleDirective(String moduleDirective)Parses a module directive and returns it as a ModuleDirective.static NameparseName(String qualifiedName)Parses a qualified name (one that can have "."s in it) and returns it as a Name.static PackageDeclarationparsePackageDeclaration(String packageDeclaration)Parses a package declaration and returns it as a PackageDeclaration.static ParameterparseParameter(String parameter)Parses a single parameter (a type and a name) and returns it as a Parameter.static CompilationUnitparseResource(ClassLoader classLoader, String path, Charset encoding)Parses the Java code contained in a resource and returns aCompilationUnitthat represents it.static CompilationUnitparseResource(String path)Parses the Java code contained in a resource and returns aCompilationUnitthat represents it.
Note: Uses UTF-8 encodingstatic CompilationUnitparseResource(String path, Charset encoding)Parses the Java code contained in a resource and returns aCompilationUnitthat represents it.static SimpleNameparseSimpleName(String name)Parses a simple name (one that can NOT have "."s in it) and returns it as a SimpleName.static StatementparseStatement(String statement)static TypeparseType(String type)Parses a Java type name and returns aTypethat represents it.static TypeDeclaration<?>parseTypeDeclaration(String typeDeclaration)Parses a type declaration and returns it as a TypeDeclaration.static TypeParameterparseTypeParameter(String typeParameter)Parses a type parameter and returns it as a TypeParameterstatic VariableDeclarationExprparseVariableDeclarationExpr(String declaration)Parses a variable declaration expression and returns aVariableDeclarationExprthat represents it.static voidsetStaticConfiguration(ParserConfiguration staticConfiguration)Set the configuration for the static parse...
-
-
-
Constructor Detail
-
JavaParser
public JavaParser()
Instantiate the parser with default configuration. Note that parsing can also be done with the static methods on this class. 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 methods on this class. Creating an instance will reduce setup time between parsing files.
-
-
Method Detail
-
getStaticConfiguration
public static ParserConfiguration getStaticConfiguration()
Get the configuration for the static parse... methods. This is a STATIC field, so modifying it will directly change how all static parse... methods work!
-
setStaticConfiguration
public static void setStaticConfiguration(ParserConfiguration staticConfiguration)
Set the configuration for the static parse... methods. This is a STATIC field, so modifying it will directly change how all static parse... methods work!
-
getParserConfiguration
public ParserConfiguration getParserConfiguration()
Get the non-static configuration for this parser.- Returns:
- The non-static 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 static CompilationUnit parse(InputStream in, Charset encoding)
Parses the Java code contained in theInputStreamand returns aCompilationUnitthat represents it.- Parameters:
in-InputStreamcontaining Java source code. It will be closed after parsing.encoding- encoding of the source code- Returns:
- CompilationUnit representing the Java source code
- Throws:
ParseProblemException- if the source code has parser errors
-
parse
public static CompilationUnit parse(InputStream in)
Parses the Java code contained in theInputStreamand returns aCompilationUnitthat represents it.
Note: Uses UTF-8 encoding- Parameters:
in-InputStreamcontaining Java source code. It will be closed after parsing.- Returns:
- CompilationUnit representing the Java source code
- Throws:
ParseProblemException- if the source code has parser errors
-
parse
public static CompilationUnit parse(File file, Charset encoding) throws FileNotFoundException
Parses the Java code contained in aFileand returns aCompilationUnitthat represents it.- Parameters:
file-Filecontaining Java source code. It will be closed after parsing.encoding- encoding of the source code- Returns:
- CompilationUnit representing the Java source code
- Throws:
ParseProblemException- if the source code has parser errorsFileNotFoundException- the file was not found
-
parse
public static CompilationUnit parse(File file) throws FileNotFoundException
Parses the Java code contained in aFileand returns aCompilationUnitthat represents it.
Note: Uses UTF-8 encoding- Parameters:
file-Filecontaining Java source code. It will be closed after parsing.- Returns:
- CompilationUnit representing the Java source code
- Throws:
ParseProblemException- if the source code has parser errorsFileNotFoundException- the file was not found
-
parse
public static CompilationUnit parse(Path path, Charset encoding) throws IOException
Parses the Java code contained in a file and returns aCompilationUnitthat 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 accessedParseProblemException- if the source code has parser errors
-
parse
public static CompilationUnit parse(Path path) throws IOException
Parses the Java code contained in a file and returns aCompilationUnitthat represents it.
Note: Uses UTF-8 encoding- Parameters:
path- path to a file containing Java source code- Returns:
- CompilationUnit representing the Java source code
- Throws:
ParseProblemException- if the source code has parser errorsIOException- the path could not be accessed
-
parseResource
public static CompilationUnit parseResource(String path) throws IOException
Parses the Java code contained in a resource and returns aCompilationUnitthat represents it.
Note: Uses UTF-8 encoding- 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:
ParseProblemException- if the source code has parser errorsIOException- the path could not be accessed
-
parseResource
public static CompilationUnit parseResource(String path, Charset encoding) throws IOException
Parses the Java code contained in a resource and returns aCompilationUnitthat 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:
ParseProblemException- if the source code has parser errorsIOException- the path could not be accessed
-
parseResource
public static CompilationUnit parseResource(ClassLoader classLoader, String path, Charset encoding) throws IOException
Parses the Java code contained in a resource and returns aCompilationUnitthat 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:
ParseProblemException- if the source code has parser errorsIOException- the path could not be accessed
-
parse
public static CompilationUnit parse(Reader reader)
Parses Java code from a Reader and returns aCompilationUnitthat represents it.- Parameters:
reader- the reader containing Java source code. It will be closed after parsing.- Returns:
- CompilationUnit representing the Java source code
- Throws:
ParseProblemException- if the source code has parser errors
-
parse
public static CompilationUnit parse(String code)
Parses the Java code contained in code and returns aCompilationUnitthat represents it.- Parameters:
code- Java source code- Returns:
- CompilationUnit representing the Java source code
- Throws:
ParseProblemException- if the source code has parser errors
-
parseBlock
public static BlockStmt parseBlock(String blockStatement)
- Parameters:
blockStatement-Stringcontaining Java block code- Returns:
- BlockStmt representing the Java block
- Throws:
ParseProblemException- if the source code has parser errors
-
parseStatement
public static Statement parseStatement(String statement)
- Parameters:
statement-Stringcontaining Java statement code- Returns:
- Statement representing the Java statement
- Throws:
ParseProblemException- if the source code has parser errors
-
parseImport
public static ImportDeclaration parseImport(String importDeclaration)
Parses the Java import contained in aStringand returns aImportDeclarationthat represents it.- Parameters:
importDeclaration-Stringcontaining Java import code- Returns:
- ImportDeclaration representing the Java import declaration
- Throws:
ParseProblemException- if the source code has parser errors
-
parseExpression
public static <T extends Expression> T parseExpression(String expression)
Parses the Java expression contained in aStringand returns aExpressionthat represents it.- Parameters:
expression-Stringcontaining Java expression- Returns:
- Expression representing the Java expression
- Throws:
ParseProblemException- if the source code has parser errors
-
parseAnnotation
public static AnnotationExpr parseAnnotation(String annotation)
Parses the Java annotation contained in aStringand returns aAnnotationExprthat represents it.- Parameters:
annotation-Stringcontaining Java annotation- Returns:
- AnnotationExpr representing the Java annotation
- Throws:
ParseProblemException- if the source code has parser errors
-
parseAnnotationBodyDeclaration
public static BodyDeclaration<?> parseAnnotationBodyDeclaration(String body)
Parses the Java annotation body declaration(e.g fields or methods) contained in aStringand returns aBodyDeclarationthat represents it.- Parameters:
body-Stringcontaining Java body declaration- Returns:
- BodyDeclaration representing the Java annotation
- Throws:
ParseProblemException- if the source code has parser errors
-
parseClassBodyDeclaration
@Deprecated public static BodyDeclaration<?> parseClassBodyDeclaration(String body)
Deprecated.just use parseBodyDeclaration now.Parses a Java class body declaration(e.g fields or methods) and returns aBodyDeclarationthat represents it.- Parameters:
body- the body of a class- Returns:
- BodyDeclaration representing the Java class body
- Throws:
ParseProblemException- if the source code has parser errors
-
parseInterfaceBodyDeclaration
@Deprecated public static BodyDeclaration<?> parseInterfaceBodyDeclaration(String body)
Deprecated.just use parseBodyDeclaration now.Parses a Java interface body declaration(e.g fields or methods) and returns aBodyDeclarationthat represents it.- Parameters:
body- the body of an interface- Returns:
- BodyDeclaration representing the Java interface body
- Throws:
ParseProblemException- if the source code has parser errors
-
parseBodyDeclaration
public static BodyDeclaration<?> parseBodyDeclaration(String body)
Parses a Java class or interface body declaration(e.g fields or methods) and returns aBodyDeclarationthat represents it.- Parameters:
body- the body of a class or interface- Returns:
- BodyDeclaration representing the Java interface body
- Throws:
ParseProblemException- if the source code has parser errors
-
parseClassOrInterfaceType
public static ClassOrInterfaceType parseClassOrInterfaceType(String type)
Parses a Java class or interface type name and returns aClassOrInterfaceTypethat represents it.- Parameters:
type- the type name like a.b.c.X or Y- Returns:
- ClassOrInterfaceType representing the type
- Throws:
ParseProblemException- if the source code has parser errors
-
parseType
public static Type parseType(String type)
Parses a Java type name and returns aTypethat represents it.- Parameters:
type- the type name like a.b.c.X, Y, or int- Returns:
- ClassOrInterfaceType representing the type
- Throws:
ParseProblemException- if the source code has parser errors
-
parseVariableDeclarationExpr
public static VariableDeclarationExpr parseVariableDeclarationExpr(String declaration)
Parses a variable declaration expression and returns aVariableDeclarationExprthat represents it.- Parameters:
declaration- a variable declaration likeint x=2;- Returns:
- VariableDeclarationExpr representing the type
- Throws:
ParseProblemException- if the source code has parser errors
-
parseJavadoc
public static Javadoc parseJavadoc(String content)
Parses the content of a JavadocComment and returns aJavadocthat represents it.- Parameters:
content- a variable declaration likecontent of my javadoc\n * second line\n * third line- Returns:
- Javadoc representing the content of the comment
- Throws:
ParseProblemException- if the source code has parser errors
-
parseExplicitConstructorInvocationStmt
public static 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.
- Throws:
ParseProblemException- if the source code has parser errors
-
parseName
public static 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
- Throws:
ParseProblemException- if the source code has parser errors
-
parseSimpleName
public static 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
- Throws:
ParseProblemException- if the source code has parser errors
-
parseParameter
public static 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
- Throws:
ParseProblemException- if the source code has parser errors
-
parsePackageDeclaration
public static 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
- Throws:
ParseProblemException- if the source code has parser errors
-
parseTypeDeclaration
public static 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
- Throws:
ParseProblemException- if the source code has parser errors
-
parseModuleDeclaration
public static 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
- Throws:
ParseProblemException- if the source code has parser errors- See Also:
ModuleDeclaration
-
parseModuleDirective
public static 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
- Throws:
ParseProblemException- if the source code has parser errors- See Also:
ModuleDirective
-
parseTypeParameter
public static 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
- Throws:
ParseProblemException- if the source code has parser errors
-
-