Top Description Fields Constructors Methods
com.github.javaparser

public final Class StaticJavaParser

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, com.github.javaparser.javadoc.Javadoc, java.io.*, java.nio.charset.Charset, java.nio.file.Path

A simpler, static API than JavaParser.

Field Summary

Modifier and TypeField and Description
private static ParserConfiguration

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ParserConfiguration
getConfiguration()

Get the configuration for the parse...

private static <T extends Node> T
private static JavaParser
public static 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
)

Deprecated set the encoding in the ParserConfiguration
Parses the Java code contained in the InputStream and returns a CompilationUnit that represents it.
public static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static BodyDeclaration<?>

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 static 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 static 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 static <T extends Expression> 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 static 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 static Javadoc

Returns:

Javadoc representing the content of the comment
parseJavadoc
(String
a variable declaration like content of my javadoc\n * second line\n * third line
content
)

Parses the content of a JavadocComment and returns a Javadoc that represents it.

public static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static VariableDeclarationExpr

Returns:

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

Parses a variable declaration expression and returns a VariableDeclarationExpr that represents it.

public static void
setConfiguration(ParserConfiguration configuration)

Set the configuration for the static parse...

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

configurationback to summary
private static ParserConfiguration configuration

Constructor Detail

StaticJavaParserback to summary
private StaticJavaParser()

Method Detail

getConfigurationback to summary
public static ParserConfiguration getConfiguration()

Get the configuration for the parse... methods.

handleResultback to summary
private static <T extends Node> T handleResult(ParseResult<T> result)
newParserback to summary
private static JavaParser newParser()
parseback to summary
public static CompilationUnit parse(final InputStream in, Charset encoding)

Deprecated

set the encoding in the ParserConfiguration

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:CompilationUnit

CompilationUnit representing the Java source code

Annotations
@Deprecated
Exceptions
ParseProblemException:
if the source code has parser errors
parseback to summary
public static 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:CompilationUnit

CompilationUnit representing the Java source code

Exceptions
ParseProblemException:
if the source code has parser errors
parseback to summary
public static 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: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 static 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: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 static 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: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 static 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: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 static 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:CompilationUnit

CompilationUnit representing the Java source code

Exceptions
ParseProblemException:
if the source code has parser errors
parseback to summary
public static 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:CompilationUnit

CompilationUnit representing the Java source code

Exceptions
ParseProblemException:
if the source code has parser errors
parseAnnotationback to summary
public static 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:AnnotationExpr

AnnotationExpr representing the Java annotation

Exceptions
ParseProblemException:
if the source code has parser errors
parseAnnotationBodyDeclarationback to summary
public static 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:BodyDeclaration<?>

BodyDeclaration representing the Java annotation

Exceptions
ParseProblemException:
if the source code has parser errors
parseBlockback to summary
public static 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:BlockStmt

BlockStmt representing the Java block

Exceptions
ParseProblemException:
if the source code has parser errors
parseBodyDeclarationback to summary
public static BodyDeclaration<?> 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:BodyDeclaration<?>

BodyDeclaration representing the Java interface body

Exceptions
ParseProblemException:
if the source code has parser errors
parseClassOrInterfaceTypeback to summary
public static 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:ClassOrInterfaceType

ClassOrInterfaceType representing the type

Exceptions
ParseProblemException:
if the source code has parser errors
parseExplicitConstructorInvocationStmtback to summary
public static 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:ExplicitConstructorInvocationStmt

the AST for the statement.

Exceptions
ParseProblemException:
if the source code has parser errors
parseExpressionback to summary
public static <T extends Expression> 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:T

Expression representing the Java expression

Exceptions
ParseProblemException:
if the source code has parser errors
parseImportback to summary
public static 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:ImportDeclaration

ImportDeclaration representing the Java import declaration

Exceptions
ParseProblemException:
if the source code has parser errors
parseJavadocback to summary
public static Javadoc parseJavadoc(String content)

Parses the content of a JavadocComment and returns a Javadoc that represents it.

Parameters
content:String

a variable declaration like content of my javadoc\n * second line\n * third line

Returns:Javadoc

Javadoc representing the content of the comment

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

Parses a method declaration and returns it as a MethodDeclaration.

Parameters
methodDeclaration:String

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

Returns:MethodDeclaration

the AST for the method declaration

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

Parses a module declaration and returns it as a ModuleDeclaration.

Parameters
moduleDeclaration:String

a declaration like "module X {}"

Returns:ModuleDeclaration

the AST for the module declaration

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

Parses a module directive and returns it as a ModuleDirective.

Parameters
moduleDirective:String

a directive like "opens C;"

Returns:ModuleDirective

the AST for the module directive

Exceptions
ParseProblemException:
if the source code has parser errors
See Also
ModuleDirective
parseNameback to summary
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:String

a name like "com.laamella.parameter_source"

Returns:Name

the AST for the name

Exceptions
ParseProblemException:
if the source code has parser errors
parsePackageDeclarationback to summary
public static 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:PackageDeclaration

the AST for the parameter

Exceptions
ParseProblemException:
if the source code has parser errors
parseParameterback to summary
public static 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:Parameter

the AST for the parameter

Exceptions
ParseProblemException:
if the source code has parser errors
parseResourceback to summary
public static 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: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 static 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: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 static 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: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 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:String

a name like "parameter_source"

Returns:SimpleName

the AST for the name

Exceptions
ParseProblemException:
if the source code has parser errors
parseStatementback to summary
public static 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:Statement

Statement representing the Java statement

Exceptions
ParseProblemException:
if the source code has parser errors
parseTypeback to summary
public static 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:Type

ClassOrInterfaceType representing the type

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

Parses a type declaration and returns it as a TypeDeclaration.

Parameters
typeDeclaration:String

a declaration like "class X {}"

Returns:TypeDeclaration<?>

the AST for the type declaration

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

Parses a type parameter and returns it as a TypeParameter

Parameters
typeParameter:String

a parameter like "T extends Serializable"

Returns:TypeParameter

the AST for the type parameter

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

Parses a variable declaration expression and returns a VariableDeclarationExpr that represents it.

Parameters
declaration:String

a variable declaration like int x=2;

Returns:VariableDeclarationExpr

VariableDeclarationExpr representing the type

Exceptions
ParseProblemException:
if the source code has parser errors
setConfigurationback to summary
public static void setConfiguration(ParserConfiguration configuration)

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!