Class JavacParse
java.lang.Object
org.checkerframework.javacutil.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 JavacParseResult
<ExpressionTree> parseExpression
(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 JavacParseResult
<MethodTree> parseMethod
(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 JavacParseResult
<ExpressionTree> parseTypeUse
(String typeUseSource) Parses the given Java type use.static JavacParseResult
<ExpressionTree> parseTypeUse
(JavaFileObject source) Parse a type use.
-
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
-
parseMethod
Parses the given Java method or annotation type element.- Parameters:
methodSource
- the string representation of a Java expression- Returns:
- the parsed expression
-
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
-
parseTypeUse
Parses the given Java type use.- Parameters:
typeUseSource
- the string representation of a Java type use- Returns:
- the parsed type use
-
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
public static JavacParseResult<ExpressionTree> parseTypeUse(JavaFileObject source) throws IOException Parse a type use.- Parameters:
source
- a JavaFileObject- Returns:
- a (parsed) type use, possibly an ErroneousTree
- Throws:
IOException
- if there is trouble reading the file
-