Class JavacParse

java.lang.Object
org.plumelib.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
      Throws:
      IllegalArgumentException - if the source is not parsable as a type declaration or contains a top-level ";"
    • parseMethod

      public static MethodTree parseMethod(String methodSource)
      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

      public static 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
      Throws:
      IllegalArgumentException - if the expression source does not parse
    • parseTypeUse

      public static Tree parseTypeUse(String typeSource)
      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 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

      @Deprecated public static JavacParseResult<ExpressionTree> parseTypeUse(JavaFileObject source) throws IOException
      Deprecated.
      may parse a prefix rather than the whole string
      Parse 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