Interface JElementSymbol
-
- All Known Subinterfaces:
AnnotableSymbol
,net.sourceforge.pmd.lang.java.symbols.BoundToNode<N>
,JAccessibleElementSymbol
,JClassSymbol
,JConstructorSymbol
,JExecutableSymbol
,JFieldSymbol
,JFormalParamSymbol
,JLocalVariableSymbol
,JMethodSymbol
,JTypeDeclSymbol
,JTypeParameterOwnerSymbol
,JTypeParameterSymbol
,JVariableSymbol
public interface JElementSymbol
Represents a named program element that can be referred to by simple name. Abstracts over whether the declaration is in the analysed file or not, using reflection when it's not.This type hierarchy is probably not directly relevant to users writing rules. It's mostly intended to unify the representation of type resolution and symbol analysis.
- Since:
- 7.0.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description <R,P>
RacceptVisitor(SymbolVisitor<R,P> visitor, P param)
Dispatch to the appropriate visit method of the visitor and returns its result.boolean
equals(Object o)
Two symbols representing the same program element should be equal.String
getSimpleName()
Gets the name with which this declaration may be referred to, eg the name of the method, or the simple name of the class.TypeSystem
getTypeSystem()
Returns the type system that created this symbol.default boolean
isUnresolved()
Returns true if this symbol is a placeholder, created to fill-in an unresolved reference.default boolean
nameEquals(@NonNull String name)
Returns true if the simple name of this symbol is the same as the given name.default @Nullable JavaNode
tryGetNode()
Returns the node that declares this symbol.
-
-
-
Method Detail
-
getSimpleName
String getSimpleName()
Gets the name with which this declaration may be referred to, eg the name of the method, or the simple name of the class.- Returns:
- the name
-
nameEquals
default boolean nameEquals(@NonNull String name)
Returns true if the simple name of this symbol is the same as the given name.- Parameters:
name
- Simple name- Throws:
NullPointerException
- If the parameter is null
-
getTypeSystem
TypeSystem getTypeSystem()
Returns the type system that created this symbol. The symbol uses this instance to create new types, for example to reflect its superclass.
-
isUnresolved
default boolean isUnresolved()
Returns true if this symbol is a placeholder, created to fill-in an unresolved reference. Depending on the type of this symbol, this may be:- An unresolved class (more details on
JTypeDeclSymbol.isUnresolved()
) - An unresolved field
- An unresolved method or constructor. Note that we cheat and
represent them only with the constant
TypeSystem.UNRESOLVED_METHOD.getSymbol()
, which may not match its usage site in either name, formal parameters, location, etc.
We try to recover some information about the missing symbol from the references we found, currently this includes only the number of type parameters of an unresolved class.
Rules should care about unresolved symbols to avoid false positives or logic errors. The equivalent for types is
TypeSystem.UNKNOWN
.The following symbols are never unresolved, because they are lexically scoped:
- type parameters
- local variables
- formal parameters
- local classes, anonymous classes
- An unresolved class (more details on
-
tryGetNode
default @Nullable JavaNode tryGetNode()
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.
-
equals
boolean equals(Object o)
Two symbols representing the same program element should be equal. So eg twoJClassSymbol
, even if their implementation class is different, should compare publicly observable properties (their binary name is enough).#hashCode()
must of course be consistent with this contract.Symbols should only be compared using this method, never with
==
, because their unicity is not guaranteed.
-
acceptVisitor
<R,P> R acceptVisitor(SymbolVisitor<R,P> visitor, P param)
Dispatch to the appropriate visit method of the visitor and returns its result.
-
-