Class JavaParserUtil


  • public class JavaParserUtil
    extends java.lang.Object
    Utility methods for working with JavaParser. It is a replacement for StaticJavaParser that does not leak memory, and it provides some other methods.
    • Field Summary

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

      Constructors 
      Constructor Description
      JavaParserUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clearAnnotations​(com.github.javaparser.ast.Node node)
      Side-effects node by removing all annotations from anywhere inside its subtree.
      static void concatenateAddedStringLiterals​(com.github.javaparser.ast.Node node)
      Side-effects node by combining any added String literals in node's subtree into their concatenation.
      static com.github.javaparser.ParserConfiguration.LanguageLevel getCurrentSourceVersion​(javax.annotation.processing.ProcessingEnvironment env)
      Returns the ParserConfiguration.LanguageLevel corresponding to the current source version.
      static java.lang.String getFullyQualifiedName​(com.github.javaparser.ast.body.TypeDeclaration<?> type, com.github.javaparser.ast.CompilationUnit compilationUnit)
      Returns the fully qualified name of a type appearing in a given compilation unit.
      static com.github.javaparser.ast.body.TypeDeclaration<?> getTypeDeclarationByName​(com.github.javaparser.ast.CompilationUnit root, java.lang.String name)
      Given the compilation unit node for a source file, returns the top level type definition with the given name.
      static com.github.javaparser.ast.CompilationUnit parseCompilationUnit​(java.io.File file)
      Parses the Java code contained in the File and returns a CompilationUnit that represents it.
      static com.github.javaparser.ast.CompilationUnit parseCompilationUnit​(java.io.InputStream inputStream)
      Parses the Java code contained in the InputStream and returns a CompilationUnit that represents it.
      static com.github.javaparser.ast.CompilationUnit parseCompilationUnit​(java.lang.String javaSource)
      Parses the Java code contained in the String and returns a CompilationUnit that represents it.
      static com.github.javaparser.ast.expr.Expression parseExpression​(java.lang.String expression)
      Parses the expression and returns an Expression that represents it.
      static com.github.javaparser.ast.expr.Expression parseExpression​(java.lang.String expression, com.github.javaparser.ParserConfiguration.LanguageLevel languageLevel)
      Parses the expression and returns an Expression that represents it.
      static com.github.javaparser.ast.StubUnit parseStubUnit​(java.io.InputStream inputStream)
      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 Detail

      • 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.
    • Constructor Detail

      • JavaParserUtil

        public JavaParserUtil()
    • Method Detail

      • parseCompilationUnit

        public static com.github.javaparser.ast.CompilationUnit parseCompilationUnit​(java.io.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​(java.io.File file)
                                                                              throws java.io.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
        java.io.FileNotFoundException - if the file was not found
      • parseCompilationUnit

        public static com.github.javaparser.ast.CompilationUnit parseCompilationUnit​(java.lang.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.

        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​(java.io.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​(java.lang.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​(java.lang.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
      • getTypeDeclarationByName

        public static com.github.javaparser.ast.body.TypeDeclaration<?> getTypeDeclarationByName​(com.github.javaparser.ast.CompilationUnit root,
                                                                                                 java.lang.String name)
        Given the compilation unit node for a source file, returns the top level type definition with the given name.
        Parameters:
        root - compilation unit to search
        name - name of a top level type declaration in root
        Returns:
        a top level type declaration in root named name
      • getFullyQualifiedName

        public static java.lang.String getFullyQualifiedName​(com.github.javaparser.ast.body.TypeDeclaration<?> type,
                                                             com.github.javaparser.ast.CompilationUnit compilationUnit)
        Returns the fully qualified name of a type appearing in a given compilation unit.
        Parameters:
        type - a type declaration
        compilationUnit - the compilation unit containing type
        Returns:
        the fully qualified name of type if compilationUnit contains a package declaration, or just the name of type otherwise
      • clearAnnotations

        public static void clearAnnotations​(com.github.javaparser.ast.Node node)
        Side-effects node by removing all annotations from anywhere inside its subtree.
        Parameters:
        node - a JavaParser Node
      • concatenateAddedStringLiterals

        public static void concatenateAddedStringLiterals​(com.github.javaparser.ast.Node node)
        Side-effects node by combining any added String literals in node's subtree into their concatenation. For example, the expression "a" + "b" becomes "ab". This occurs even if, when reading from left to right, the two string literals are not added directly. For example, the expression 1 + "a" + "b" parses as (1 + "a") + "b"}, but it is transformed into 1 + "ab".

        This is the same transformation performed by javac automatically. Javac seems to ignore string literals surrounded in parentheses, so this method does as well.

        Parameters:
        node - a JavaParser Node
      • getCurrentSourceVersion

        public static com.github.javaparser.ParserConfiguration.LanguageLevel getCurrentSourceVersion​(javax.annotation.processing.ProcessingEnvironment env)
        Returns the ParserConfiguration.LanguageLevel corresponding to the current source version.
        Parameters:
        env - processing environment used to get source version
        Returns:
        the current source version