Interface JClassSymbol
- All Superinterfaces:
AnnotableSymbol,JAccessibleElementSymbol,JElementSymbol,JTypeDeclSymbol,JTypeParameterOwnerSymbol
Class instance. This is not a type, it's
the *declaration* of a type. For example, a class symbol representing
a generic class can provide access to the formal type parameters, but
the symbol does not represent a specific parametrization of a type.
Class symbols represent the full range of types represented by Class:
classes, interfaces, arrays, and primitives. This excludes type variables,
intersection types, parameterized types, wildcard types, etc., which are only
compile-time constructs.
Class symbols are used to back JClassType, JArrayType,
and JPrimitiveType. See JTypeMirror.getSymbol().
- Since:
- 7.0.0
-
Field Summary
Fields inherited from interface net.sourceforge.pmd.lang.java.symbols.JAccessibleElementSymbol
PRIMITIVE_PACKAGE -
Method Summary
Modifier and TypeMethodDescriptiondefault <R,P> R acceptVisitor(SymbolVisitor<R, P> visitor, P param) Dispatch to the appropriate visit method of the visitor and returns its result.default booleanannotationAppliesTo(ElementType elementType) Deprecated.Since 7.17.0.default OptionalBoolannotationAppliesToContext(ElementType elementType, LanguageVersion javaVersion) Return whether annotations of this annotation type apply to the given construct, as per theTargetannotation.default org.pcollections.PSet<String>Return the simple names of all annotation attributes.default @Nullable RetentionPolicyReturns the retention policy of this annotation, if this is an annotation symbol.@Nullable JTypeDeclSymbolReturns the component symbol, returns null if this is not an array.@NonNull StringReturns the binary name of this type, as specified by the JLS: the JLS.@Nullable StringReturns the simple name of this class, as specified byClass.getCanonicalName().Returns the constructors declared by this class.default @Nullable JClassSymbolgetDeclaredClass(String name) Returns a class with the given name defined in this class.Returns the member classes declared directly in this class.default @Nullable JFieldSymbolgetDeclaredField(String name) Returns a field with the given name defined in this class.Returns the fields declared directly in this class.Returns the methods declared directly in this class.default @Nullable SymbolicValuegetDefaultAnnotationAttributeValue(String attrName) Return the default value of the attribute if this is an annotation type with a default.@Nullable JExecutableSymbolReturns the method or constructor this symbol is declared in, if it represents a local class declaration or an anonymous class declaration.default JTypeParameterOwnerSymbolReturns theenclosing methodor theenclosing class, in that order of priority.default @NonNull List<JFieldSymbol>Returns a list with all enum constants.default @NonNull JClassSymbolReturns the toplevel class containing this class.default List<JClassSymbol>Return the list of permitted subclasses or subinterfaces, as defined in thepermitsclause of a sealed class or interface.default @NonNull List<JRecordComponentSymbol>Returns a list with all record components.@Nullable JClassSymbolReturns the superclass symbol if it exists.@Nullable JClassTypegetSuperclassType(Substitution substitution) Returns the superclass type, under the given substitution.Returns the direct super-interfaces of this class or interface symbol.getSuperInterfaceTypes(Substitution substitution) Returns the list of super interface types, under the given substitution.default booleanbooleanbooleanbooleanisArray()default booleanisClass()This returns true if this is not an interface, primitive or array.booleanisEnum()default booleanisFinal()Return true if this type is final, that is, does not admit subtypes.booleanbooleanbooleanisRecord()default booleanisSealed()Return true if this type is sealed.default OptionalBoolReturn whether this is an annotation type, and can apply to type use.default ASTTypeDeclarationReturns the node that declares this symbol.Methods inherited from interface net.sourceforge.pmd.lang.java.symbols.AnnotableSymbol
getDeclaredAnnotation, getDeclaredAnnotations, isAnnotationPresentMethods inherited from interface net.sourceforge.pmd.lang.java.symbols.JAccessibleElementSymbol
getEnclosingClass, getModifiers, getPackageName, isStaticMethods inherited from interface net.sourceforge.pmd.lang.java.symbols.JElementSymbol
equals, getTypeSystem, nameEqualsMethods inherited from interface net.sourceforge.pmd.lang.java.symbols.JTypeDeclSymbol
getSimpleName, isInterface, isUnresolvedMethods inherited from interface net.sourceforge.pmd.lang.java.symbols.JTypeParameterOwnerSymbol
getLexicalScope, getTypeParameterCount, getTypeParameters, isGeneric
-
Method Details
-
getBinaryName
@NonNull String getBinaryName()Returns the binary name of this type, as specified by the JLS: the JLS. For array types this returns the binary name of the component followed by "[]". This differs fromClass.getName(), which for array types outputs an internal name.For example:
whereasint.class.getName() == "int" int[].class.getName() == "[I" String.class.getName() == "java.lang.String" String[].class.getName() == "[Ljava.lang.String;"symbolOf(int.class).getBinaryName() == "int" symbolOf(int[].class).getBinaryName() == "int[]" symbolOf(String.class).getBinaryName() == "java.lang.String" symbolOf(String[].class).getBinaryName() == "java.lang.String[]" -
getCanonicalName
@Nullable String getCanonicalName()Returns the simple name of this class, as specified byClass.getCanonicalName(). -
getEnclosingMethod
@Nullable JExecutableSymbol getEnclosingMethod()Returns the method or constructor this symbol is declared in, if it represents a local class declaration or an anonymous class declaration.Notice, that this returns null also if this class is local to a class or instance initializer, a field initializer, and some other special circumstances.
- See Also:
-
getEnclosingTypeParameterOwner
Description copied from interface:JTypeParameterOwnerSymbolReturns theenclosing methodor theenclosing class, in that order of priority.- Specified by:
getEnclosingTypeParameterOwnerin interfaceJTypeParameterOwnerSymbol
-
getDeclaredClasses
List<JClassSymbol> getDeclaredClasses()Returns the member classes declared directly in this class.- See Also:
-
getDeclaredClass
Returns a class with the given name defined in this class. -
getDeclaredMethods
List<JMethodSymbol> getDeclaredMethods()Returns the methods declared directly in this class. This excludes bridges and other synthetic methods.For an array type T[], to the difference of
Class, this method returns a one-element list with theObject.clone()method, as if declared like so:public final T[] clone() {...}.- See Also:
-
getConstructors
List<JConstructorSymbol> getConstructors()Returns the constructors declared by this class. This excludes synthetic constructors.For an array type T[], and to the difference of
Class, this should return a one-element list with a constructor having the same modifiers as the array type, and a singleintparameter.- See Also:
-
getDeclaredFields
List<JFieldSymbol> getDeclaredFields()Returns the fields declared directly in this class. This excludes synthetic fields.For arrays, and to the difference of
Class, this should return a one-element list with thepublic final int lengthfield.- See Also:
-
getDeclaredField
Returns a field with the given name defined in this class. -
getEnumConstants
Returns a list with all enum constants. If this symbol does not represent an enum, returns an empty list. The returned list is a subset ofgetDeclaredFields(). The order of fields denotes the normal order of enum constants. -
getRecordComponents
Returns a list with all record components. If this symbol does not represent a record, returns an empty list. The order of values denotes the normal order of components. -
getSuperInterfaceTypes
Returns the list of super interface types, under the given substitution. -
getSuperclassType
Returns the superclass type, under the given substitution. -
getSuperclass
@Nullable JClassSymbol getSuperclass() -
getSuperInterfaces
List<JClassSymbol> getSuperInterfaces()Returns the direct super-interfaces of this class or interface symbol. -
isAbstract
default boolean isAbstract() -
getArrayComponent
@Nullable JTypeDeclSymbol getArrayComponent()Returns the component symbol, returns null if this is not an array. -
isArray
boolean isArray() -
isPrimitive
boolean isPrimitive() -
isEnum
boolean isEnum() -
isRecord
boolean isRecord() -
isAnnotation
boolean isAnnotation() -
isLocalClass
boolean isLocalClass() -
isAnonymousClass
boolean isAnonymousClass() -
getPermittedSubtypes
Return the list of permitted subclasses or subinterfaces, as defined in thepermitsclause of a sealed class or interface. If this class is sealed but has no permits clause, the permitted subtypes are inferred from the types in the compilation unit. If the class is not sealed, returns an empty list.Note that an enum class for which some constants declare a body is technically implicitly sealed, and implicitly permits only the anonymous classes for those enum constants. For consistency, this method will return only symbols that have a canonical name, and therefore always return an empty list for enums.
- See Also:
-
isSealed
default boolean isSealed()Return true if this type is sealed. Then it has a non-empty list of permitted subclasses (or it is a compile-time error). Note that there is no trace of the non-sealed modifier in class files. A class must have thenon-sealedmodifier if it is not sealed, not final, and has a sealed supertype.Note that an enum class for which some constants declare a body is technically implicitly sealed, and implicitly permits only the anonymous classes for those enum constants. For consistency with
getPermittedSubtypes(), we treat such enums as not sealed.- See Also:
-
isFinal
default boolean isFinal()Return true if this type is final, that is, does not admit subtypes. Note that array types have both modifiers final and abstract. Note also that enum classes may be non-final if they have constants that declare an anonymous body. -
getAnnotationAttributeNames
Return the simple names of all annotation attributes. If this is not an annotation type, return an empty set. -
getDefaultAnnotationAttributeValue
Return the default value of the attribute if this is an annotation type with a default. Return null if this is not an annotation type, if there is no such attribute, or the attribute has no default value. If the name is in the attribute name set, then the null return value can only mean that the attribute exists but has no default value.- Parameters:
attrName- Attribute name
-
getAnnotationRetention
Returns the retention policy of this annotation, if this is an annotation symbol. Otherwise returns null. -
annotationAppliesTo
Deprecated.Since 7.17.0. UseannotationAppliesToContext(ElementType, LanguageVersion)Return whether annotations of this annotation type apply to the given construct, as per theTargetannotation. Return false if this is not an annotation. -
annotationAppliesToContext
default OptionalBool annotationAppliesToContext(ElementType elementType, LanguageVersion javaVersion) Return whether annotations of this annotation type apply to the given construct, as per theTargetannotation. Return false if this is not an annotation. May return unknown if we are not sure.- Since:
- 7.17.0
-
mayBeTypeAnnotation
Return whether this is an annotation type, and can apply to type use. Whether a specific annotation of this type is a type annotation also depends on the context where it appears, as this annotation type may be applicable to other contexts thanElementType.TYPE_USE.- Since:
- 7.17.0
-
isClass
default boolean isClass()This returns true if this is not an interface, primitive or array. Note that this includes in particular records and enums. -
getNestRoot
Returns the toplevel class containing this class. If this is a toplevel class, returns this. -
acceptVisitor
Description copied from interface:JElementSymbolDispatch to the appropriate visit method of the visitor and returns its result.- Specified by:
acceptVisitorin interfaceJElementSymbol
-
tryGetNode
Description copied from interface:JElementSymbolReturns the node that declares this symbol. Eg forJMethodSymbol, it's anASTMethodDeclaration. Will only return non-null if the symbol is declared in the file currently being analysed.- Specified by:
tryGetNodein interfaceJElementSymbol
-