Package com.github.javaparser
Class StaticJavaParser
- java.lang.Object
-
- com.github.javaparser.StaticJavaParser
-
public final class StaticJavaParser extends java.lang.ObjectA simpler, static API thanJavaParser.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.ThreadLocal<ParserConfiguration>localConfiguration
-
Constructor Summary
Constructors Modifier Constructor Description privateStaticJavaParser()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static ParserConfigurationgetConfiguration()Deprecated.usegetParserConfiguration()insteadstatic ParserConfigurationgetParserConfiguration()Get the configuration for the parse...private static <T extends Node>
ThandleResult(ParseResult<T> result)Deprecated.private static JavaParsernewParser()private static JavaParserAdapternewParserAdapted()static CompilationUnitparse(java.io.File file)static CompilationUnitparse(java.io.File file, java.nio.charset.Charset encoding)Deprecated.set the encoding in theParserConfigurationstatic CompilationUnitparse(java.io.InputStream in)static CompilationUnitparse(java.io.InputStream in, java.nio.charset.Charset encoding)Deprecated.set the encoding in theParserConfigurationstatic CompilationUnitparse(java.io.Reader reader)Parses Java code from a Reader and returns aCompilationUnitthat represents it.static CompilationUnitparse(java.lang.String code)Parses the Java code contained in code and returns aCompilationUnitthat represents it.static CompilationUnitparse(java.nio.file.Path path)Parses the Java code contained in a file and returns aCompilationUnitthat represents it.static CompilationUnitparse(java.nio.file.Path path, java.nio.charset.Charset encoding)Deprecated.set the encoding in theParserConfigurationstatic AnnotationExprparseAnnotation(java.lang.String annotation)Parses the Java annotation contained in aStringand returns aAnnotationExprthat represents it.static BodyDeclaration<?>parseAnnotationBodyDeclaration(java.lang.String body)Parses the Java annotation body declaration(e.g fields or methods) contained in aStringand returns aBodyDeclarationthat represents it.static ArrayInitializerExprparseArrayInitializerExpr(java.lang.String arrayInitializerExpr)Parses an array initializer expression and returns it as ArrayInitializerExpr.static BlockStmtparseBlock(java.lang.String blockStatement)Parses the Java block contained in aStringand returns aBlockStmtthat represents it.static BodyDeclaration<?>parseBodyDeclaration(java.lang.String body)Parses a Java class or interface body declaration(e.g fields or methods) and returns aBodyDeclarationthat represents it.static ClassOrInterfaceTypeparseClassOrInterfaceType(java.lang.String type)Parses a Java class or interface type name and returns aClassOrInterfaceTypethat represents it.static ExplicitConstructorInvocationStmtparseExplicitConstructorInvocationStmt(java.lang.String statement)Parses the this(...) and super(...) statements that may occur at the start of a constructor.static <T extends Expression>
TparseExpression(java.lang.String expression)Parses the Java expression contained in aStringand returns aExpressionthat represents it.static ImportDeclarationparseImport(java.lang.String importDeclaration)Parses the Java import contained in aStringand returns aImportDeclarationthat represents it.static JavadocparseJavadoc(java.lang.String content)Parses the content of a JavadocComment and returns aJavadocthat represents it.static MethodDeclarationparseMethodDeclaration(java.lang.String methodDeclaration)Parses a method declaration and returns it as a MethodDeclaration.static ModuleDeclarationparseModuleDeclaration(java.lang.String moduleDeclaration)Parses a module declaration and returns it as a ModuleDeclaration.static ModuleDirectiveparseModuleDirective(java.lang.String moduleDirective)Parses a module directive and returns it as a ModuleDirective.static NameparseName(java.lang.String qualifiedName)Parses a qualified name (one that can have "."s in it) and returns it as a Name.static PackageDeclarationparsePackageDeclaration(java.lang.String packageDeclaration)Parses a package declaration and returns it as a PackageDeclaration.static ParameterparseParameter(java.lang.String parameter)Parses a single parameter (a type and a name) and returns it as a Parameter.static CompilationUnitparseResource(java.lang.ClassLoader classLoader, java.lang.String path, java.nio.charset.Charset encoding)Deprecated.set the encoding in theParserConfigurationstatic CompilationUnitparseResource(java.lang.String path)Parses the Java code contained in a resource and returns aCompilationUnitthat represents it.static CompilationUnitparseResource(java.lang.String path, java.nio.charset.Charset encoding)Deprecated.set the encoding in theParserConfigurationstatic SimpleNameparseSimpleName(java.lang.String name)Parses a simple name (one that can NOT have "."s in it) and returns it as a SimpleName.static StatementparseStatement(java.lang.String statement)Parses the Java statement contained in aStringand returns aStatementthat represents it.static TypeparseType(java.lang.String type)Parses a Java type name and returns aTypethat represents it.static TypeDeclaration<?>parseTypeDeclaration(java.lang.String typeDeclaration)Parses a type declaration and returns it as a TypeDeclaration.static TypeParameterparseTypeParameter(java.lang.String typeParameter)Parses a type parameter and returns it as a TypeParameterstatic VariableDeclarationExprparseVariableDeclarationExpr(java.lang.String declaration)Parses a variable declaration expression and returns aVariableDeclarationExprthat represents it.static voidsetConfiguration(ParserConfiguration configuration)Set the configuration for the static parse...
-
-
-
Field Detail
-
localConfiguration
private static final java.lang.ThreadLocal<ParserConfiguration> localConfiguration
-
-
Method Detail
-
getConfiguration
@Deprecated public static ParserConfiguration getConfiguration()
Deprecated.usegetParserConfiguration()insteadGet the configuration for the parse... methods. Deprecated method.
-
getParserConfiguration
public static ParserConfiguration getParserConfiguration()
Get the configuration for the parse... methods.
-
setConfiguration
public static void setConfiguration(@NotNull 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!
-
parse
@Deprecated public static CompilationUnit parse(@NotNull java.io.InputStream in, @NotNull java.nio.charset.Charset encoding)
Deprecated.set the encoding in theParserConfigurationParses 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(@NotNull java.io.InputStream in)
- 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
@Deprecated public static CompilationUnit parse(@NotNull java.io.File file, @NotNull java.nio.charset.Charset encoding) throws java.io.FileNotFoundException
Deprecated.set the encoding in theParserConfigurationParses 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 errorsjava.io.FileNotFoundException- the file was not found
-
parse
public static CompilationUnit parse(@NotNull java.io.File file) throws java.io.FileNotFoundException
- 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 errorsjava.io.FileNotFoundException- the file was not found
-
parse
@Deprecated public static CompilationUnit parse(@NotNull java.nio.file.Path path, @NotNull java.nio.charset.Charset encoding) throws java.io.IOException
Deprecated.set the encoding in theParserConfigurationParses 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:
java.io.IOException- the path could not be accessedParseProblemException- if the source code has parser errors
-
parse
public static CompilationUnit parse(@NotNull java.nio.file.Path path) throws java.io.IOException
Parses the Java code contained in a file and returns aCompilationUnitthat 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 errorsjava.io.IOException- the path could not be accessed
-
parseResource
public static CompilationUnit parseResource(@NotNull java.lang.String path) throws java.io.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 pathToResource- Returns:
- CompilationUnit representing the Java source code
- Throws:
ParseProblemException- if the source code has parser errorsjava.io.IOException- the path could not be accessed
-
parseResource
@Deprecated public static CompilationUnit parseResource(@NotNull java.lang.String path, @NotNull java.nio.charset.Charset encoding) throws java.io.IOException
Deprecated.set the encoding in theParserConfigurationParses 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 errorsjava.io.IOException- the path could not be accessed
-
parseResource
@Deprecated public static CompilationUnit parseResource(@NotNull java.lang.ClassLoader classLoader, @NotNull java.lang.String path, @NotNull java.nio.charset.Charset encoding) throws java.io.IOException
Deprecated.set the encoding in theParserConfigurationParses 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 errorsjava.io.IOException- the path could not be accessed
-
parse
public static CompilationUnit parse(@NotNull java.io.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(@NotNull java.lang.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(@NotNull java.lang.String blockStatement)
Parses the Java block contained in aStringand returns aBlockStmtthat represents it.- 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(@NotNull java.lang.String statement)
Parses the Java statement contained in aStringand returns aStatementthat represents it.- 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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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
-
parseBodyDeclaration
public static BodyDeclaration<?> parseBodyDeclaration(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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(@NotNull java.lang.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
-
parseMethodDeclaration
public static MethodDeclaration parseMethodDeclaration(@NotNull java.lang.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
- Throws:
ParseProblemException- if the source code has parser errors- See Also:
MethodDeclaration
-
parseArrayInitializerExpr
public static ArrayInitializerExpr parseArrayInitializerExpr(@NotNull java.lang.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
- Throws:
ParseProblemException- if the source code has parser errors- See Also:
ArrayInitializerExpr
-
newParser
private static JavaParser newParser()
-
newParserAdapted
private static JavaParserAdapter newParserAdapted()
-
handleResult
@Deprecated private static <T extends Node> T handleResult(ParseResult<T> result)
Deprecated.
-
-