Class StaticJavaParserUtil

java.lang.Object
org.checkerframework.framework.util.StaticJavaParserUtil

public final class StaticJavaParserUtil extends Object
Utility methods for working with JavaParser. It is a replacement for com.github.javaparser.StaticJavaParser that does not leak memory. Also see JavaParserUtil.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.github.javaparser.ParserConfiguration.LanguageLevel
    The Language Level to use when parsing if a specific level isn't applied.
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.github.javaparser.ast.CompilationUnit
    Parses the Java code contained in the File and returns a CompilationUnit that represents it.
    static com.github.javaparser.ast.CompilationUnit
    Parses the Java code contained in the InputStream and returns a CompilationUnit that represents it.
    static com.github.javaparser.ast.CompilationUnit
    Parses the Java code contained in the String and returns a CompilationUnit that represents it.
    static com.github.javaparser.ast.expr.Expression
    parseExpression(String expression)
    Parses the expression and returns an Expression that represents it.
    static com.github.javaparser.ast.expr.Expression
    parseExpression(String expression, com.github.javaparser.ParserConfiguration.LanguageLevel languageLevel)
    Parses the expression and returns an Expression that represents it.
    static com.github.javaparser.ast.StubUnit
    Parses the stub file contained in the InputStream and returns a StubUnit that represents it.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_LANGUAGE_LEVEL

      public static final com.github.javaparser.ParserConfiguration.LanguageLevel DEFAULT_LANGUAGE_LEVEL
      The Language Level to use when parsing if a specific level isn't applied. This should be the highest version of Java that the Checker Framework can process.
  • Method Details

    • parseCompilationUnit

      public static com.github.javaparser.ast.CompilationUnit parseCompilationUnit(InputStream inputStream)
      Parses the Java code contained in the InputStream and returns a CompilationUnit that represents it.

      This is like StaticJavaParser.parse, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-using StaticJavaParser causes memory problems because it retains too much memory.

      Parameters:
      inputStream - the Java source code
      Returns:
      CompilationUnit representing the Java source code
      Throws:
      com.github.javaparser.ParseProblemException - if the source code has parser errors
    • parseCompilationUnit

      public static com.github.javaparser.ast.CompilationUnit parseCompilationUnit(File file) throws FileNotFoundException
      Parses the Java code contained in the File and returns a CompilationUnit that represents it.

      This is like StaticJavaParser.parse, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-using StaticJavaParser causes memory problems because it retains too much memory.

      Parameters:
      file - the Java source code
      Returns:
      CompilationUnit representing the Java source code
      Throws:
      com.github.javaparser.ParseProblemException - if the source code has parser errors
      FileNotFoundException - if the file was not found
    • parseCompilationUnit

      public static com.github.javaparser.ast.CompilationUnit parseCompilationUnit(String javaSource)
      Parses the Java code contained in the String and returns a CompilationUnit that represents it.

      This is like StaticJavaParser.parse, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-using StaticJavaParser causes memory problems because it retains too much memory.

      Unlike the other parseCompilationUnit methods, this one does not preprocess unicode escapes, so that the line and column of every node is a position in javaSource itself. When JavaParser preprocesses unicode escapes, it maps each node's position back to a position in the unpreprocessed text, but that mapping is off by one character per backslash escape: after a backslash that does not start a unicode escape, JavaParser pushes the following character back onto its input but counts it twice. A caller such as InsertAjavaAnnotations that converts a position into an offset into javaSource would compute the wrong offset.

      Parameters:
      javaSource - the Java source code
      Returns:
      CompilationUnit representing the Java source code
      Throws:
      com.github.javaparser.ParseProblemException - if the source code has parser errors
    • parseStubUnit

      public static com.github.javaparser.ast.StubUnit parseStubUnit(InputStream inputStream)
      Parses the stub file contained in the InputStream and returns a StubUnit that represents it.

      This is like StaticJavaParser.parse, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-using StaticJavaParser causes memory problems because it retains too much memory.

      Parameters:
      inputStream - the stub file
      Returns:
      StubUnit representing the stub file
      Throws:
      com.github.javaparser.ParseProblemException - if the source code has parser errors
    • parseExpression

      public static com.github.javaparser.ast.expr.Expression parseExpression(String expression)
      Parses the expression and returns an Expression that represents it.

      This is like StaticJavaParser.parseExpression, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-using StaticJavaParser causes memory problems because it retains too much memory.

      Parameters:
      expression - the expression string
      Returns:
      the parsed expression
      Throws:
      com.github.javaparser.ParseProblemException - if the expression has parser errors
    • parseExpression

      public static com.github.javaparser.ast.expr.Expression parseExpression(String expression, com.github.javaparser.ParserConfiguration.LanguageLevel languageLevel)
      Parses the expression and returns an Expression that represents it.

      This is like StaticJavaParser.parseExpression, but it does not lead to memory leaks because it creates a new instance of JavaParser each time it is invoked. Re-using StaticJavaParser causes memory problems because it retains too much memory.

      Parameters:
      expression - the expression string
      languageLevel - the language level to use when parsing the Java source
      Returns:
      the parsed expression
      Throws:
      com.github.javaparser.ParseProblemException - if the expression has parser errors