Interface JElementSymbol

All Known Subinterfaces:
AnnotableSymbol, net.sourceforge.pmd.lang.java.symbols.BoundToNode<N>, JAccessibleElementSymbol, JClassSymbol, JConstructorSymbol, JExecutableSymbol, JFieldSymbol, JFormalParamSymbol, JLocalVariableSymbol, JMethodSymbol, JModuleSymbol, JRecordComponentSymbol, 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

    Modifier and Type
    Method
    Description
    <R, P> R
    acceptVisitor(SymbolVisitor<R,P> visitor, P param)
    Dispatch to the appropriate visit method of the visitor and returns its result.
    boolean
    Two symbols representing the same program element should be equal.
    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 type system that created this symbol.
    default boolean
    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
    Returns the node that declares this symbol.
  • Method Details

    • 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:

      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:

    • tryGetNode

      default @Nullable JavaNode tryGetNode()
      Returns the node that declares this symbol. Eg for JMethodSymbol, it's an ASTMethodDeclaration. 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 two JClassSymbol, 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.

      Overrides:
      equals in class Object
      Parameters:
      o - Comparand
      Returns:
      True if the other is a symbol for the same program element
    • acceptVisitor

      <R, P> R acceptVisitor(SymbolVisitor<R,P> visitor, P param)
      Dispatch to the appropriate visit method of the visitor and returns its result.