Class JIntersectionType

java.lang.Object
net.sourceforge.pmd.lang.java.types.JIntersectionType
All Implemented Interfaces:
JTypeMirror, JTypeVisitable

public final class JIntersectionType extends Object implements JTypeMirror
An intersection type. Intersections type act as the greatest lower bound for a set of types.

https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.9

  • Method Details

    • getTypeAnnotations

      public org.pcollections.PSet<SymbolicValue.SymAnnot> getTypeAnnotations()
      Description copied from interface: JTypeMirror
      Return a list of annotations on this type. Annotations can be written on nearly any type (eg @A Out.@B In<@C T>, @A ? extends @B Up).

      For JTypeVar, this includes both the annotations defined on the type var and those defined at use site. For instance

      
          <@A T> void accept(@B T t);
       
      The T type var will have annotation @A in the symbol (AnnotableSymbol.getDeclaredAnnotations()) and in the type var that is in the JMethodSig.getTypeParameters(). In the formal parameter, the type var will have annotations @B @A.
      Specified by:
      getTypeAnnotations in interface JTypeMirror
    • withAnnotations

      public JTypeMirror withAnnotations(org.pcollections.PSet<SymbolicValue.SymAnnot> newTypeAnnots)
      Description copied from interface: JTypeMirror
      Returns a type mirror that is equal to this instance but has different type annotations. Note that some types ignore this method and return themselves without changing. Eg the null type or void type cannot be annotated.
      Specified by:
      withAnnotations in interface JTypeMirror
      Parameters:
      newTypeAnnots - New type annotations (not null)
      Returns:
      A new type, maybe this one
    • getComponents

      public List<JTypeMirror> getComponents()
      Returns the list of components. Their erasure must be pairwise disjoint. If the intersection's superclass is TypeSystem.OBJECT, then it is excluded from this set.
    • getPrimaryBound

      public @NonNull JTypeMirror getPrimaryBound()
      The primary bound of this intersection, which may be a type variable, array type, or class type (not an interface). If all bounds are interfaces, then this returns TypeSystem.OBJECT.
    • getInterfaces

      public @NonNull List<JClassType> getInterfaces()
      Returns all additional bounds on the primary bound, which are necessarily interface types.
    • getInducedClassType

      public @Nullable JClassType getInducedClassType()
      Every intersection type induces a notional class or interface for the purpose of identifying its members. This may be a functional interface. This returns null for the non-implemented cases.

      This is only relevant to check for functional interface parameterization, eg Runnable & Serializable. Do not use this to find out the members of this type, rather, use streamMethods(Predicate) or so.

    • acceptVisitor

      public <T, P> T acceptVisitor(JTypeVisitor<T,P> visitor, P p)
      Description copied from interface: JTypeVisitable
      Accept a type visitor, dispatching on this object's runtime type to the correct method of the visitor.
      Specified by:
      acceptVisitor in interface JTypeVisitable
      Type Parameters:
      T - Type of result of the visitor
      P - Type of data of the visitor
    • streamMethods

      public Stream<JMethodSig> streamMethods(Predicate<? super JMethodSymbol> prefilter)
      Description copied from interface: JTypeMirror
      Returns a stream of method signatures declared in and inherited by this type. Method signatures are created on-demand by this method, they're not reused between calls. This stream does not include constructors.

      Note: streams are a bit impractical when it comes to configuring the filter. Possibly a specialized API should be introduced. We need to support the use cases of the symbol table, ie filter by name + accessibility + staticity, and also possibly use cases for rules, like getting a method from a known signature. See also JClassType.getDeclaredMethod(JExecutableSymbol), which looks like this. Unifying this API would be nice.

      Specified by:
      streamMethods in interface JTypeMirror
      Parameters:
      prefilter - Filter selecting symbols for which a signature should be created and yielded by the stream
    • subst

      public JIntersectionType subst(Function<? super SubstVar,? extends @NonNull JTypeMirror> subst)
      Description copied from interface: JTypeVisitable
      Replace the type variables occurring in the given type by their image by the given function. Substitutions are not applied recursively (ie, is not applied on the result of a substitution).
      Specified by:
      subst in interface JTypeMirror
      Specified by:
      subst in interface JTypeVisitable
      Parameters:
      subst - Substitution function, eg a Substitution
    • getSymbol

      public @Nullable JTypeDeclSymbol getSymbol()
      Description copied from interface: JTypeMirror
      Returns the symbol declaring this type. Reifiable types present a symbol, and some other types too. This method's return value depends on this type:

      Note that type annotations are not reflected on the symbol, but only on the type.

      Specified by:
      getSymbol in interface JTypeMirror
    • getTypeSystem

      public TypeSystem getTypeSystem()
      Description copied from interface: JTypeMirror
      Returns the type system that built this type.
      Specified by:
      getTypeSystem in interface JTypeMirror
    • getErasure

      public JTypeMirror getErasure()
      Description copied from interface: JTypeMirror
      Returns the erasure of this type. Erasure is defined by JLS§4.6, an adapted definition follows:
      1. The erasure of a parameterized type (§4.5) G<T1,...,Tn> is |G|.
      2. The erasure of a nested type T.C is |T|.C.
      3. The erasure of an array type T[] is |T|[].
      4. The erasure of a type variable (§4.4) is the erasure of its upper bound.
      5. The erasure of an intersection type is the erasure of its leftmost component.
      6. The erasure of every other type is the type itself.

      The JVM representation of a type is in general the symbol of its erasure. So to get a Class instance for the runtime representation of a type, you should do t.getErasure().getSymbol(). The erasure procedure gets rid of types that have no symbol (except if t is a wildcard type, or the TypeSystem.NULL_TYPE)

      Specified by:
      getErasure in interface JTypeMirror
    • toString

      public String toString()
      Description copied from interface: JTypeMirror
      The toString of type mirrors prints useful debug information, but shouldn't be relied on anywhere, as it may change anytime. Use TypePrettyPrint to display types.
      Specified by:
      toString in interface JTypeMirror
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Description copied from interface: JTypeMirror
      Returns true if the object is a type equivalent to this one. A few kinds of types use reference identity, like captured type variables, or the null type. A few special types are represented by constants (see TypeSystem). Apart from those, types should always be compared using this method. or TypeOps.isSameType(JTypeMirror, JTypeMirror) (which is null-safe).

      Note that types belonging to different type systems do not test equal. The type system object is global to the analysis though, so this should not ever happen in rules.

      Specified by:
      equals in interface JTypeMirror
      Overrides:
      equals in class Object
      Parameters:
      o -
      Returns:
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object