Package com.github.javaparser
Class StaticJavaParser
java.lang.Object
com.github.javaparser.StaticJavaParser
A simpler, static API than
JavaParser
.-
Method Summary
Modifier and TypeMethodDescriptionstatic ParserConfiguration
Get the configuration for the parse...static CompilationUnit
static CompilationUnit
Deprecated.static CompilationUnit
parse(InputStream in)
static CompilationUnit
parse(InputStream in, Charset encoding)
Deprecated.set the encoding in theParserConfiguration
static CompilationUnit
Parses Java code from a Reader and returns aCompilationUnit
that represents it.static CompilationUnit
Parses the Java code contained in code and returns aCompilationUnit
that represents it.static CompilationUnit
Parses the Java code contained in a file and returns aCompilationUnit
that represents it.static CompilationUnit
Deprecated.set the encoding in theParserConfiguration
static AnnotationExpr
parseAnnotation(String annotation)
Parses the Java annotation contained in aString
and returns aAnnotationExpr
that represents it.static BodyDeclaration<?>
Parses the Java annotation body declaration(e.g fields or methods) contained in aString
and returns aBodyDeclaration
that represents it.static BlockStmt
parseBlock(String blockStatement)
static BodyDeclaration<?>
parseBodyDeclaration(String body)
Parses a Java class or interface body declaration(e.g fields or methods) and returns aBodyDeclaration
that represents it.static ClassOrInterfaceType
parseClassOrInterfaceType(String type)
Parses a Java class or interface type name and returns aClassOrInterfaceType
that represents it.parseExplicitConstructorInvocationStmt(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 aString
and returns aExpression
that represents it.static ImportDeclaration
parseImport(String importDeclaration)
Parses the Java import contained in aString
and returns aImportDeclaration
that represents it.static Javadoc
parseJavadoc(String content)
Parses the content of a JavadocComment and returns aJavadoc
that represents it.static MethodDeclaration
parseMethodDeclaration(String methodDeclaration)
Parses a method declaration and returns it as a MethodDeclaration.static ModuleDeclaration
parseModuleDeclaration(String moduleDeclaration)
Parses a module declaration and returns it as a ModuleDeclaration.static ModuleDirective
parseModuleDirective(String moduleDirective)
Parses a module directive and returns it as a ModuleDirective.static Name
Parses a qualified name (one that can have "."s in it) and returns it as a Name.static PackageDeclaration
parsePackageDeclaration(String packageDeclaration)
Parses a package declaration and returns it as a PackageDeclaration.static Parameter
parseParameter(String parameter)
Parses a single parameter (a type and a name) and returns it as a Parameter.static CompilationUnit
parseResource(ClassLoader classLoader, String path, Charset encoding)
Deprecated.set the encoding in theParserConfiguration
static CompilationUnit
parseResource(String path)
Parses the Java code contained in a resource and returns aCompilationUnit
that represents it.static CompilationUnit
parseResource(String path, Charset encoding)
Deprecated.set the encoding in theParserConfiguration
static SimpleName
parseSimpleName(String name)
Parses a simple name (one that can NOT have "."s in it) and returns it as a SimpleName.static Statement
parseStatement(String statement)
static Type
Parses a Java type name and returns aType
that represents it.static TypeDeclaration<?>
parseTypeDeclaration(String typeDeclaration)
Parses a type declaration and returns it as a TypeDeclaration.static TypeParameter
parseTypeParameter(String typeParameter)
Parses a type parameter and returns it as a TypeParameterstatic VariableDeclarationExpr
parseVariableDeclarationExpr(String declaration)
Parses a variable declaration expression and returns aVariableDeclarationExpr
that represents it.static void
setConfiguration(ParserConfiguration configuration)
Set the configuration for the static parse...
-
Method Details
-
getConfiguration
Get the configuration for the parse... methods. -
setConfiguration
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! -
parse
Deprecated.set the encoding in theParserConfiguration
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
- Throws:
ParseProblemException
- if the source code has parser errors
-
parse
- Parameters:
in
-InputStream
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
@Deprecated public static CompilationUnit parse(File file, Charset encoding) throws FileNotFoundExceptionDeprecated.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:
ParseProblemException
- if the source code has parser errorsFileNotFoundException
- the file was not found
-
parse
- Parameters:
file
-File
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 errorsFileNotFoundException
- the file was not found
-
parse
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 accessedParseProblemException
- if the source code has parser errors
-
parse
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:
ParseProblemException
- if the source code has parser errorsIOException
- the path could not be accessed
-
parseResource
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:
ParseProblemException
- if the source code has parser errorsIOException
- the path could not be accessed
-
parseResource
@Deprecated public static CompilationUnit parseResource(String path, Charset encoding) throws IOExceptionDeprecated.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:
ParseProblemException
- if the source code has parser errorsIOException
- the path could not be accessed
-
parseResource
@Deprecated public static CompilationUnit parseResource(ClassLoader classLoader, String path, Charset encoding) throws IOExceptionDeprecated.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:
ParseProblemException
- if the source code has parser errorsIOException
- the path could not be accessed
-
parse
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
- Throws:
ParseProblemException
- if the source code has parser errors
-
parse
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
- Throws:
ParseProblemException
- if the source code has parser errors
-
parseBlock
- Parameters:
blockStatement
-String
containing Java block code- Returns:
- BlockStmt representing the Java block
- Throws:
ParseProblemException
- if the source code has parser errors
-
parseStatement
- Parameters:
statement
-String
containing Java statement code- Returns:
- Statement representing the Java statement
- Throws:
ParseProblemException
- if the source code has parser errors
-
parseImport
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
- Throws:
ParseProblemException
- if the source code has parser errors
-
parseExpression
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
- Throws:
ParseProblemException
- if the source code has parser errors
-
parseAnnotation
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
- Throws:
ParseProblemException
- if the source code has parser errors
-
parseAnnotationBodyDeclaration
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
- Throws:
ParseProblemException
- if the source code has parser errors
-
parseBodyDeclaration
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
- Throws:
ParseProblemException
- if the source code has parser errors
-
parseClassOrInterfaceType
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
- Throws:
ParseProblemException
- if the source code has parser errors
-
parseType
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
- Throws:
ParseProblemException
- if the source code has parser errors
-
parseVariableDeclarationExpr
Parses a variable declaration expression and returns aVariableDeclarationExpr
that 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
Parses the content of a JavadocComment and returns aJavadoc
that 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
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
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
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
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
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
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
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
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
-
parseMethodDeclaration
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
- Throws:
ParseProblemException
- if the source code has parser errors- See Also:
MethodDeclaration
-
ParserConfiguration