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 boolean
annotationAppliesTo
(ElementType elementType) Return whether annotations of this annotation type apply to the given construct, as per theTarget
annotation.default org.pcollections.PSet
<String> Return the simple names of all annotation attributes.default @Nullable RetentionPolicy
Returns the retention policy of this annotation, if this is an annotation symbol.@Nullable JTypeDeclSymbol
Returns the component symbol, returns null if this is not an array.@NonNull String
Returns the binary name of this type, as specified by the JLS: the JLS.@Nullable String
Returns the simple name of this class, as specified byClass.getCanonicalName()
.Returns the constructors declared by this class.default @Nullable JClassSymbol
getDeclaredClass
(String name) Returns a class with the given name defined in this class.Returns the member classes declared directly in this class.default @Nullable JFieldSymbol
getDeclaredField
(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 SymbolicValue
getDefaultAnnotationAttributeValue
(String attrName) Return the default value of the attribute if this is an annotation type with a default.@Nullable JExecutableSymbol
Returns the method or constructor this symbol is declared in, if it represents a local class declaration or an anonymous class declaration.default JTypeParameterOwnerSymbol
Returns theenclosing method
or theenclosing class
, in that order of priority.default @NonNull List
<JFieldSymbol> Returns a list with all enum constants.default @NonNull JClassSymbol
Returns the toplevel class containing this class.default List
<JClassSymbol> Return the list of permitted subclasses or subinterfaces, as defined in thepermits
clause of a sealed class or interface.default @NonNull List
<JRecordComponentSymbol> Returns a list with all record components.@Nullable JClassSymbol
Returns the superclass symbol if it exists.@Nullable JClassType
getSuperclassType
(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 boolean
boolean
boolean
boolean
isArray()
default boolean
isClass()
This returns true if this is not an interface, primitive or array.boolean
isEnum()
default boolean
isFinal()
Return true if this type is final, that is, does not admit subtypes.boolean
boolean
boolean
isRecord()
default boolean
isSealed()
Return true if this type is sealed.default ASTTypeDeclaration
Returns the node that declares this symbol.Methods inherited from interface net.sourceforge.pmd.lang.java.symbols.AnnotableSymbol
getDeclaredAnnotation, getDeclaredAnnotations, isAnnotationPresent
Methods inherited from interface net.sourceforge.pmd.lang.java.symbols.JAccessibleElementSymbol
getEnclosingClass, getModifiers, getPackageName, isStatic
Methods inherited from interface net.sourceforge.pmd.lang.java.symbols.JElementSymbol
equals, getTypeSystem, nameEquals
Methods inherited from interface net.sourceforge.pmd.lang.java.symbols.JTypeDeclSymbol
getSimpleName, isInterface, isUnresolved
Methods 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:JTypeParameterOwnerSymbol
Returns theenclosing method
or theenclosing class
, in that order of priority.- Specified by:
getEnclosingTypeParameterOwner
in 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 singleint
parameter.- 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 length
field.- 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 thepermits
clause 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-sealed
modifier 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
Return whether annotations of this annotation type apply to the given construct, as per theTarget
annotation. Return false if this is not an annotation. -
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:JElementSymbol
Dispatch to the appropriate visit method of the visitor and returns its result.- Specified by:
acceptVisitor
in interfaceJElementSymbol
-
tryGetNode
Description copied from interface:JElementSymbol
Returns 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:
tryGetNode
in interfaceJElementSymbol
-