Package org.plumelib.javacparse
Class JavacParse
java.lang.Object
org.plumelib.javacparse.JavacParse
This class contains static methods that parse Java code.
Internally, this class calls the javac parser from the JDK.
-
Method Summary
Modifier and TypeMethodDescriptionstatic JavacParseResult<CompilationUnitTree>parseCompilationUnit(String javaCode) Parse a Java file's contents.static JavacParseResult<CompilationUnitTree>parseCompilationUnit(JavaFileObject source) Parse the contents of a JavaFileObject.static ExpressionTreeparseExpression(String expressionSource) Parses the given Java expression string, such as "foo.bar()" or "1 + 2".static JavacParseResult<ExpressionTree>parseExpression(JavaFileObject source) Deprecated.may parse a prefix rather than the whole stringstatic JavacParseResult<CompilationUnitTree>Parse a Java file.static MethodTreeparseMethod(String methodSource) Parses the given Java method or annotation type element.static JavacParseResult<ClassTree>parseTypeDeclaration(String classSource) Parses the given Java type declaration (class, interface, enum, record, etc.).static TreeparseTypeUse(String typeSource) Parses the given Java type use, such as "int", "String", or "List<? extends Number>".static JavacParseResult<ExpressionTree>parseTypeUse(JavaFileObject source) Deprecated.may parse a prefix rather than the whole string
-
Method Details
-
parseFile
Parse a Java file.- Parameters:
filename- the file to parse- Returns:
- a (parsed) compilation unit, which may include parse errors
- Throws:
IOException- if there is trouble reading the file
-
parseCompilationUnit
Parse a Java file's contents.- Parameters:
javaCode- the contents of a Java file- Returns:
- a (parsed) compilation unit, which may include parse errors
-
parseTypeDeclaration
Parses the given Java type declaration (class, interface, enum, record, etc.).- Parameters:
classSource- the string representation of a Java type declaration- Returns:
- the parsed type declaration
- Throws:
IllegalArgumentException- if the source is not parsable as a type declaration or contains a top-level ";"
-
parseMethod
Parses the given Java method or annotation type element.- Parameters:
methodSource- the string representation of a Java method or annotation type element- Returns:
- the parsed method
- Throws:
IllegalArgumentException- if the method source does not parse
-
parseExpression
Parses the given Java expression string, such as "foo.bar()" or "1 + 2".- Parameters:
expressionSource- the string representation of a Java expression- Returns:
- the parsed expression
- Throws:
IllegalArgumentException- if the expression source does not parse
-
parseTypeUse
Parses the given Java type use, such as "int", "String", or "List<? extends Number>".- Parameters:
typeSource- the string representation of a Java type use- Returns:
- the parsed type use
- Throws:
IllegalArgumentException- if the type source does not parse
-
parseCompilationUnit
public static JavacParseResult<CompilationUnitTree> parseCompilationUnit(JavaFileObject source) throws IOException Parse the contents of a JavaFileObject.- Parameters:
source- a JavaFileObject- Returns:
- a (parsed) compilation unit, which may include parse errors
- Throws:
IOException- if there is trouble reading the file
-
parseExpression
@Deprecated public static JavacParseResult<ExpressionTree> parseExpression(JavaFileObject source) throws IOException Deprecated.may parse a prefix rather than the whole stringParse a Java expression.Warning: If the prefix of the string is a Java expression, this may return the result of parsing that prefix, even if the whole string is not an expression. For example, it parses "Hello this is nonsense." without error as an identifier "Hello", but it parses "1 +" into a parse error. Therefore, this routine is not appropriate for most uses.
- Parameters:
source- a JavaFileObject- Returns:
- a (parsed) expression, possibly an ErroneousTree
- Throws:
IOException- if there is trouble reading the file
-
parseTypeUse
@Deprecated public static JavacParseResult<ExpressionTree> parseTypeUse(JavaFileObject source) throws IOException Deprecated.may parse a prefix rather than the whole stringParse a type use.Warning: If the prefix of the string is a Java type, this may return the result of parsing that prefix, even if the whole string is not a type. For example, it parses "Foo bar baz" without error as the type "Foo". Therefore, this routine is not appropriate for most uses; prefer
parseTypeUse(String).- Parameters:
source- a JavaFileObject- Returns:
- a (parsed) type use, possibly an ErroneousTree
- Throws:
IOException- if there is trouble reading the file
-