Class ClassTypingContext

java.lang.Object
spoon.support.visitor.ClassTypingContext
All Implemented Interfaces:
GenericTypeAdapter

public class ClassTypingContext extends Object
Helper class created from type X or reference to X. It provides access to actual type arguments of any super type of type X adapted to type X.
Example:
 //reference to `ArrayList` with actual type argument `Integer`
 CtTypeReference arrayListRef = ... //ArrayList<Integer>
 //type java.util.List with type parameter `E`
 CtType list = ... //List<E>
 //adapting of type parameter `E` to scope of arrayListRef
 CtTypeReference typeParamE_adaptedTo_arrayListRef = new ClassTypingContext(arrayListRef).adaptType(list.getFormalCtTypeParameters().get(0))
 //the value of `E` in scope of arrayListRef is `Integer`
 assertEquals(Integer.class.getName(), typeParamE_adaptedTo_arrayListRef.getQualifiedName());
 
  • Constructor Details

    • ClassTypingContext

      public ClassTypingContext(CtTypeReference<?> typeReference)
      Parameters:
      typeReference - CtTypeReference whose actual type arguments are used for resolving of input type parameters
    • ClassTypingContext

      public ClassTypingContext(CtType<?> type)
      Parameters:
      type - CtType whose formal type parameters are transformed to CtTypeReferences, which plays role of actual type arguments, used for resolving of input type parameters
  • Method Details

    • getAdaptationScope

      public CtType<?> getAdaptationScope()
      Returns:
      the scope of this type adapter
    • isSubtypeOf

      public boolean isSubtypeOf(CtTypeReference<?> superTypeRef)
      detects if `superTypeRef` is a super type of the type or type reference, which was send to constructor of this instance. It takes into account the actual type arguments of this type and `superTypeRef` So for example:
       CtTypeReference listInteger = ...//List<Integer>
       CtTypeReference listString = ...//List<Integer>
       assertFalse(new ClassTypingContext(listInteger).isSubtypeOf(listString))
       CtTypeReference listExtendsNumber = ...//List<? extends Number>
       assertTrue(new ClassTypingContext(listInteger).isSubtypeOf(listExtendsNumber))
       
      Parameters:
      superTypeRef - the reference
      Returns:
      true if this type (including actual type arguments) is a subtype of superTypeRef
    • resolveActualTypeArgumentsOf

      public List<CtTypeReference<?>> resolveActualTypeArgumentsOf(CtTypeReference<?> typeRef)
      resolve actual type argument values of the provided type reference
      Parameters:
      typeRef - the reference to the type whose actual type argument values has to be resolved in scope of `scope` type
      Returns:
      actual type arguments of `typeRef` in scope of `scope` element or null if typeRef is not a super type of `scope`
    • isOverriding

      public boolean isOverriding(CtMethod<?> thisMethod, CtMethod<?> thatMethod)
      thisMethod overrides thatMethod if 1) thisMethod class is a subclass of thatMethod class 2) thisMethod is a subsignature of thatMethod See http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.1
      Parameters:
      thisMethod - - the scope method
      thatMethod - - to be checked method
      Returns:
      true if thisMethod overrides thatMethod
    • isSubSignature

      public boolean isSubSignature(CtMethod<?> thisMethod, CtMethod<?> thatMethod)
      isSubsignature is defined as an oriented relation between two methods as defined in See https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.2 thisMethod is subsignature of thatMethod if either A) thisMethod is same signature like thatMethod B) thisMethod is same signature like type erasure of thatMethod
      Parameters:
      thisMethod - - the scope method to be checked with
      thatMethod - - the checked method
      Returns:
      true if thisMethod is subsignature of thatMethod
    • isSameSignature

      public boolean isSameSignature(CtExecutable<?> thisExecutable, CtMethod<?> thatExecutable)
      Two methods are considered as having the same signature if they have the same name and argument types See https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.2
      Parameters:
      thisExecutable - - the scope method to be checked with
      thatExecutable - - the checked method
      Returns:
      true if this method and thatMethod has same signature
    • getEnclosingGenericTypeAdapter

      public ClassTypingContext getEnclosingGenericTypeAdapter()
      Returns:
      the GenericTypeAdapter, which adapts generic types of enclosing type
    • createEnclosingHierarchy

      protected ClassTypingContext createEnclosingHierarchy(CtType<?> enclosingType)
      might be used to create custom chain of super type hierarchies
    • createEnclosingHierarchy

      protected ClassTypingContext createEnclosingHierarchy(CtTypeReference<?> enclosingTypeRef)
      might be used to create custom chain of super type hierarchies
    • adaptTypeParameter

      protected CtTypeReference<?> adaptTypeParameter(CtTypeParameter typeParam)
      adapts `typeParam` to the CtTypeReference of scope of this ClassTypingContext In can be CtTypeParameterReference again - depending actual type arguments of this ClassTypingContext.
      Parameters:
      typeParam - to be resolved CtTypeParameter
      Returns:
      CtTypeReference or CtTypeParameterReference adapted to scope of this ClassTypingContext or null if `typeParam` cannot be adapted to target `scope`
    • adaptType

      public CtTypeReference<?> adaptType(CtTypeInformation type)
      Description copied from interface: GenericTypeAdapter
      adapts `type` to the CtTypeReference of the scope of this GenericTypeAdapter This mapping function is able to resolve CtTypeParameter of:
      A) input type or any super class or any enclosing class of input type or it's super class
      B) super interfaces of input type or super interfaces of it's super classes.
      The type reference is adapted recursive including all it's actual type arguments and bounds.
      Specified by:
      adaptType in interface GenericTypeAdapter
      Parameters:
      type - to be adapted type
      Returns:
      CtTypeReference adapted to scope of this ClassTypingContext or null if type cannot be adapted to this `scope`.