Class TypesFromReflection
java.lang.Object
net.sourceforge.pmd.lang.java.types.TypesFromReflection
Builds type mirrors from
Type instances.
This is intended as a public API to help rules build types.
-
Method Summary
Modifier and TypeMethodDescriptionstatic JTypeMirrorfromReflect(Type reflected, TypeSystem ts) static @Nullable JTypeMirrorfromReflect(TypeSystem ts, @NonNull Type reflected, LexicalScope lexicalScope, Substitution subst) Builds a type from reflection.static JTypeMirrorfromReflect(org.apache.commons.lang3.reflect.Typed<?> reflected, TypeSystem ts) Builds a type from reflection.static @Nullable JClassSymbolloadSymbol(TypeSystem ctr, String className) static @Nullable JTypeMirrorloadType(TypeSystem ctr, String className) Load a class.static @Nullable JTypeMirrorloadType(TypeSystem ctr, String className, net.sourceforge.pmd.lang.java.symbols.internal.UnresolvedClassStore unresolvedStore) Load a class.
-
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- ATypedinstance, eg aTypeLiteral.ts- Type system that will build the type- Throws:
IllegalArgumentException- If the given type mentions type variablesNullPointerException- If the type, or the type system, are null
-
fromReflect
-
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 systemreflected- A type instance obtained from reflectionlexicalScope- 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
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 theUnresolvedClassStoreparameter is non-null. -
loadSymbol
-