Class Substitution

java.lang.Object
net.sourceforge.pmd.lang.java.types.Substitution
All Implemented Interfaces:
Function<SubstVar,JTypeMirror>

public final class Substitution extends Object
A function from SubstVars to types. Applying it to a type replaces the occurrences of some variables with other types. This can be done with TypeOps.subst(JTypeMirror, Function).
  • Field Details

    • EMPTY

      public static final Substitution EMPTY
      The empty substitution maps every type variable to itself.
  • Method Details

    • isEmptySubst

      public static boolean isEmptySubst(Function<?,?> m)
    • apply

      public @NonNull JTypeMirror apply(@NonNull SubstVar var)
      Returns the type with which the given variable should be replaced.
    • andThen

      public Substitution andThen(Substitution other)
      Returns a composed substitution that first applies this substitution to its input, and then applies the after substitution to the result.

      Given two substitutions S1, S2 and a type t:
      subst(subst(t, S1), S2) == subst(t, S1.andThen(S2))

      For example:

      
        S1 = [ T -> A<U> ]
        S2 = [ U -> B<V> ]
      
        subst(List<T>, S1)    = List<A<U>>
        subst(List<A<U>>, S2) = List<A<B<V>>>
      
        S1.andThen(S2) = [ T -> A<B<V>>, U -> B<V> ]
       
      Parameters:
      other - the function to apply after this function is applied
      Returns:
      a composed substitution
      Throws:
      NullPointerException - if other is null
    • plus

      public Substitution plus(SubstVar from, JTypeMirror to)
      Maps the given variable to the given type. This does not apply this substitution to the type mirror.
    • mapping

      public static Substitution mapping(List<? extends SubstVar> from, List<? extends JTypeMirror> to)
      Builds a substitution where the mapping from vars to types is defined by the correspondence between the two lists.

      If there are no vars to be mapped, then no substitution is returned even though some types might have been supplied.

      Throws:
      IllegalArgumentException - If the two lists are of different lengths
      NullPointerException - If any of the two lists is null
    • erasing

      public static Substitution erasing(List<? extends JTypeVar> tparams)
      Returns a substitution that replaces the given type variables with their erasure.
      Parameters:
      tparams - Type variables to erase
    • getMap

      protected Map<@NonNull SubstVar,@NonNull JTypeMirror> getMap()
    • isEmpty

      public boolean isEmpty()
    • toString

      public String toString()
      Overrides:
      toString in class Object