Class JavaParserUtil

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

public final class JavaParserUtil extends Object
Utility methods for working with JavaParser. Also see StaticJavaParserUtil.
  • Method Details

    • resolveTypeNameAsTypeElement

      public static @Nullable TypeElement resolveTypeNameAsTypeElement(Elements elements, com.github.javaparser.ast.type.ClassOrInterfaceType type)
      Returns the element for the given JavaParser type, whose name is resolved in the scope of the type declarations and the compilation unit that contain it. Returns null if the name cannot be resolved: it names a type variable, a local class, a member of a local or anonymous class, or a type that is not on the classpath. To learn whether the name names a type variable, and which one, call resolveTypeNameAsTypeParameter(Elements, ClassOrInterfaceType, Map).

      A client that resolves many names should call resolveTypeNameAsTypeElement(Elements, ClassOrInterfaceType, Map), which memoizes the name lookups.

      Parameters:
      elements - used for looking up names
      type - a JavaParser class or interface type
      Returns:
      the element for type, or null if it cannot be determined
    • resolveTypeNameAsTypeElement

      public static @Nullable TypeElement resolveTypeNameAsTypeElement(Elements elements, com.github.javaparser.ast.type.ClassOrInterfaceType type, Map<String,@Nullable TypeElement> cache)
      Returns the element for the given JavaParser type, whose name is resolved in the scope of the type declarations and the compilation unit that contain it. Returns null if the name cannot be resolved: it names a type variable, a local class, a member of a local or anonymous class, or a type that is not on the classpath. To learn whether the name names a type variable, and which one, call resolveTypeNameAsTypeParameter(Elements, ClassOrInterfaceType, Map).

      Resolving one name looks up many candidate names, most of which name no type, so a client that resolves many names should pass the same cache to each call. A cache should not be reused across annotation processing rounds or across Elements instances: a name that names no type in one round might name a generated type in a later round.

      Parameters:
      elements - used for looking up names
      type - a JavaParser class or interface type
      cache - maps a name to the type it names, or to null if it names no type; this method both reads and writes it. It must permit null values, so it cannot be a ConcurrentHashMap; this method is not thread-safe.
      Returns:
      the element for type, or null if it cannot be determined
    • resolveTypeNameAsTypeParameter

      public static @Nullable com.github.javaparser.ast.type.TypeParameter resolveTypeNameAsTypeParameter(Elements elements, com.github.javaparser.ast.type.ClassOrInterfaceType type)
      Returns the declaration of the type variable that the given JavaParser type names, or null if the name does not name a type variable. The name is resolved in the scope of the type declarations and the compilation unit that contain it, so the result is null if a type declaration shadows a type variable of the same name.

      A client that resolves many names should call resolveTypeNameAsTypeParameter(Elements, ClassOrInterfaceType, Map), which memoizes the name lookups.

      Parameters:
      elements - used for looking up names
      type - a JavaParser class or interface type
      Returns:
      the declaration of the type variable that type names, or null if type does not name a type variable
    • resolveTypeNameAsTypeParameter

      public static @Nullable com.github.javaparser.ast.type.TypeParameter resolveTypeNameAsTypeParameter(Elements elements, com.github.javaparser.ast.type.ClassOrInterfaceType type, Map<String,@Nullable TypeElement> cache)
      Returns the declaration of the type variable that the given JavaParser type names, or null if the name does not name a type variable. The name is resolved in the scope of the type declarations and the compilation unit that contain it, so the result is null if a type declaration shadows a type variable of the same name.

      A name with more than one component, as in T.Inner, never names a type variable, because a type variable has no member types.

      Parameters:
      elements - used for looking up names
      type - a JavaParser class or interface type
      cache - maps a name to the type it names, or to null if it names no type; this method both reads and writes it
      Returns:
      the declaration of the type variable that type names, or null if type does not name a type variable
    • resolveTypeName

      public static JavaParserUtil.ResolvedTypeName resolveTypeName(Elements elements, com.github.javaparser.ast.type.ClassOrInterfaceType type, Map<String,@Nullable TypeElement> cache)
      Returns what the given JavaParser type's name refers to: a type, a type variable, or neither. The name is resolved in the scope of the type declarations and the compilation unit that contain it.

      Resolving a name walks the enclosing scopes and searches supertypes, which is far more work than the cache avoids. A client that needs to know both whether the name names a type and whether it names a type variable should call this method once, rather than calling both resolveTypeNameAsTypeElement(Elements, ClassOrInterfaceType, Map) and resolveTypeNameAsTypeParameter(Elements, ClassOrInterfaceType, Map), each of which repeats the walk.

      Parameters:
      elements - used for looking up names
      type - a JavaParser class or interface type
      cache - maps a name to the type it names, or to null if it names no type; this method both reads and writes it. See resolveTypeNameAsTypeElement(Elements, ClassOrInterfaceType, Map) for restrictions on it.
      Returns:
      what the name of type refers to
    • getTypeDeclarationByName

      public static com.github.javaparser.ast.body.TypeDeclaration<?> getTypeDeclarationByName(com.github.javaparser.ast.CompilationUnit root, 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 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
    • typeKindForPrimitive

      public static TypeKind typeKindForPrimitive(com.github.javaparser.ast.type.PrimitiveType primitiveType)
      Returns the TypeKind that corresponds to the given JavaParser primitive type.
      Parameters:
      primitiveType - a JavaParser primitive type
      Returns:
      the TypeKind for primitiveType
    • typeToTypeMirror

      public static @Nullable TypeMirror typeToTypeMirror(Elements elements, Types types, com.github.javaparser.ast.type.Type type, Map<String,@Nullable TypeElement> cache)
      Returns the TypeMirror for the given JavaParser type, or null if it cannot be determined. It cannot be determined for a use of a type variable, an intersection type, a union type, var, a wildcard, a type parameter declaration, or a type that is not on the classpath.
      Parameters:
      elements - used for looking up names
      types - used for creating types
      type - a JavaParser type
      cache - maps a name to the type it names, or to null if it names no type; this method both reads and writes it. See resolveTypeNameAsTypeElement(Elements, ClassOrInterfaceType, Map) for restrictions on it.
      Returns:
      the TypeMirror for type, or null if it cannot be determined
    • 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

      @Deprecated public static com.github.javaparser.ParserConfiguration.LanguageLevel getCurrentSourceVersion(ProcessingEnvironment env)
      Deprecated.
      Does not seem to be used
      Returns the ParserConfiguration.LanguageLevel corresponding to the current source version.
      Parameters:
      env - processing environment used to get source version
      Returns:
      the current source version