Class JavaParserUtil
StaticJavaParserUtil.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordWhat a type name refers to: a type, a type variable, or neither.static classVisitor that combines added String literals, seeconcatenateAddedStringLiterals(com.github.javaparser.ast.Node). -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclearAnnotations(com.github.javaparser.ast.Node node) Side-effectsnodeby removing all annotations from anywhere inside its subtree.static voidconcatenateAddedStringLiterals(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.LanguageLevelDeprecated.Does not seem to be usedstatic StringgetFullyQualifiedName(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, String name) Given the compilation unit node for a source file, returns the top level type definition with the given name.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.static @Nullable TypeElementresolveTypeNameAsTypeElement(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.static @Nullable TypeElementresolveTypeNameAsTypeElement(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.static @Nullable com.github.javaparser.ast.type.TypeParameterresolveTypeNameAsTypeParameter(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.static @Nullable com.github.javaparser.ast.type.TypeParameterresolveTypeNameAsTypeParameter(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.static TypeKindtypeKindForPrimitive(com.github.javaparser.ast.type.PrimitiveType primitiveType) Returns theTypeKindthat corresponds to the given JavaParser primitive type.static @Nullable TypeMirrortypeToTypeMirror(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.
-
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, callresolveTypeNameAsTypeParameter(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 namestype- 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, callresolveTypeNameAsTypeParameter(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
Elementsinstances: a name that names no type in one round might name a generated type in a later round.- Parameters:
elements- used for looking up namestype- a JavaParser class or interface typecache- 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 aConcurrentHashMap; 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 namestype- a JavaParser class or interface type- Returns:
- the declaration of the type variable that
typenames, or null iftypedoes 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 namestype- a JavaParser class or interface typecache- 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
typenames, or null iftypedoes 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)andresolveTypeNameAsTypeParameter(Elements, ClassOrInterfaceType, Map), each of which repeats the walk.- Parameters:
elements- used for looking up namestype- a JavaParser class or interface typecache- maps a name to the type it names, or to null if it names no type; this method both reads and writes it. SeeresolveTypeNameAsTypeElement(Elements, ClassOrInterfaceType, Map)for restrictions on it.- Returns:
- what the name of
typerefers 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 searchname- name of a top level type declaration inroot- Returns:
- a top level type declaration in
rootnamedname
-
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 declarationcompilationUnit- the compilation unit containingtype- Returns:
- the fully qualified name of
typeifcompilationUnitcontains a package declaration, or just the name oftypeotherwise
-
typeKindForPrimitive
public static TypeKind typeKindForPrimitive(com.github.javaparser.ast.type.PrimitiveType primitiveType) Returns theTypeKindthat corresponds to the given JavaParser primitive type.- Parameters:
primitiveType- a JavaParser primitive type- Returns:
- the
TypeKindforprimitiveType
-
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 namestypes- used for creating typestype- a JavaParser typecache- maps a name to the type it names, or to null if it names no type; this method both reads and writes it. SeeresolveTypeNameAsTypeElement(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-effectsnodeby 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 expression1 + "a" + "b"parses as(1 + "a") + "b"}, but it is transformed into1 + "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 usedReturns theParserConfiguration.LanguageLevelcorresponding to the current source version.- Parameters:
env- processing environment used to get source version- Returns:
- the current source version
-