Class TypesFromReflection

java.lang.Object
net.sourceforge.pmd.lang.java.types.TypesFromReflection

public final class TypesFromReflection extends Object
Builds type mirrors from Type instances.

This is intended as a public API to help rules build types.

  • Method Details

    • fromReflect

      public static JTypeMirror fromReflect(org.apache.commons.lang3.reflect.Typed<?> reflected, TypeSystem ts)
      Builds a type from reflection. This overload expects a ground type, ie it will fail if the given type mentions type variables. This can be used to get a type quickly, eg:
      
      
       // note the anonymous class body
       JTypeMirror streamOfInt = fromReflect(new TypeLiteral<Stream<Integer>>() {}, node.getTypeSystem());
      
       if (node.getTypeMirror().equals(streamOfInt))
         ruleContext.addViolation(node, "Use IntStream instead of Stream<Integer>");
      
       // going the long way:
       TypeSystem ts = node.getTypeSystem();
       JTypeMirror streamOfInt = ts.typeOf(ts.getClassSymbol(Stream.class), false)
                                   .withTypeParameters(listOf(ts.INT.box()));
      
       
      Parameters:
      reflected - A Typed instance, eg a TypeLiteral.
      ts - Type system that will build the type
      Throws:
      IllegalArgumentException - If the given type mentions type variables
      NullPointerException - If the type, or the type system, are null
    • fromReflect

      public static JTypeMirror fromReflect(Type reflected, TypeSystem ts)
    • fromReflect

      public static @Nullable JTypeMirror fromReflect(TypeSystem ts, @NonNull Type reflected, LexicalScope lexicalScope, Substitution subst)
      Builds a type from reflection. This takes care of preserving the identity of type variables.
      Parameters:
      ts - Type system
      reflected - A type instance obtained from reflection
      lexicalScope - An index for the in-scope type variables. All type variables occurring in the type must be referenced.
      subst - Substitution to apply to tvars
      Returns:
      A type, or null if the type system's symbol resolver cannot map the types to its own representation
      Throws:
      IllegalArgumentException - If there are free type variables in the type. Any type variable should be accessible in the lexical scope parameter.
      NullPointerException - If any parameter is null
    • loadType

      public static @Nullable JTypeMirror loadType(TypeSystem ctr, String className)
      Load a class. Supports loading array types like 'java.lang.String[]' and converting a canonical name to a binary name (e.g. 'java.util.Map.Entry' -> 'java.util.Map$Entry').
    • loadType

      public static @Nullable JTypeMirror loadType(TypeSystem ctr, String className, net.sourceforge.pmd.lang.java.symbols.internal.UnresolvedClassStore unresolvedStore)
      Load a class. Supports loading array types like 'java.lang.String[]' and converting a canonical name to a binary name (e.g. 'java.util.Map.Entry' -> 'java.util.Map$Entry'). Types that are not on the classpath may be replaced by placeholder types if the UnresolvedClassStore parameter is non-null.
    • loadSymbol

      public static @Nullable JClassSymbol loadSymbol(TypeSystem ctr, String className)