Class JavacParse

java.lang.Object
org.checkerframework.javacutil.javacparse.JavacParse

public final class JavacParse extends Object
This class contains static methods that parse Java code.

Internally, this class calls the javac parser from the JDK.

  • Method Details

    • parseFile

      public static JavacParseResult<CompilationUnitTree> parseFile(String filename) throws IOException
      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

      public static JavacParseResult<CompilationUnitTree> parseCompilationUnit(String javaCode)
      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

      public static JavacParseResult<ClassTree> parseTypeDeclaration(String classSource)
      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

      public static JavacParseResult<MethodTree> parseMethod(String methodSource)
      Parses the given Java method or annotation type element.
      Parameters:
      methodSource - the string representation of a Java expression
      Returns:
      the parsed expression
    • parseExpression

      public static JavacParseResult<ExpressionTree> parseExpression(String expressionSource)
      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

      public static JavacParseResult<ExpressionTree> parseTypeUse(String typeUseSource)
      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 string
      Parse 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