use of java.lang.reflect.TypeVariable in project flink by apache.
the class TypeExtractor method createTypeInfoFromInput.
/**
* Finds the type information to a type variable.
*
* It solve the following:
*
* Return the type information for "returnTypeVar" given that "inType" has type information "inTypeInfo".
* Thus "inType" must contain "returnTypeVar" in a "inputTypeHierarchy", otherwise null is returned.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private <IN1> TypeInformation<?> createTypeInfoFromInput(TypeVariable<?> returnTypeVar, ArrayList<Type> inputTypeHierarchy, Type inType, TypeInformation<IN1> inTypeInfo) {
TypeInformation<?> info = null;
// use a factory to find corresponding type information to type variable
final ArrayList<Type> factoryHierarchy = new ArrayList<>(inputTypeHierarchy);
final TypeInfoFactory<?> factory = getClosestFactory(factoryHierarchy, inType);
if (factory != null) {
// the type that defines the factory is last in factory hierarchy
final Type factoryDefiningType = factoryHierarchy.get(factoryHierarchy.size() - 1);
// defining type has generics, the factory need to be asked for a mapping of subtypes to type information
if (factoryDefiningType instanceof ParameterizedType) {
final Type[] typeParams = typeToClass(factoryDefiningType).getTypeParameters();
final Type[] actualParams = ((ParameterizedType) factoryDefiningType).getActualTypeArguments();
// go thru all elements and search for type variables
for (int i = 0; i < actualParams.length; i++) {
final Map<String, TypeInformation<?>> componentInfo = inTypeInfo.getGenericParameters();
final String typeParamName = typeParams[i].toString();
if (!componentInfo.containsKey(typeParamName) || componentInfo.get(typeParamName) == null) {
throw new InvalidTypesException("TypeInformation '" + inTypeInfo.getClass().getSimpleName() + "' does not supply a mapping of TypeVariable '" + typeParamName + "' to corresponding TypeInformation. " + "Input type inference can only produce a result with this information. " + "Please implement method 'TypeInformation.getGenericParameters()' for this.");
}
info = createTypeInfoFromInput(returnTypeVar, factoryHierarchy, actualParams[i], componentInfo.get(typeParamName));
if (info != null) {
break;
}
}
}
} else // the input is a type variable
if (sameTypeVars(inType, returnTypeVar)) {
return inTypeInfo;
} else if (inType instanceof TypeVariable) {
Type resolvedInType = materializeTypeVariable(inputTypeHierarchy, (TypeVariable<?>) inType);
if (resolvedInType != inType) {
info = createTypeInfoFromInput(returnTypeVar, inputTypeHierarchy, resolvedInType, inTypeInfo);
}
} else // input is an array
if (inType instanceof GenericArrayType) {
TypeInformation<?> componentInfo = null;
if (inTypeInfo instanceof BasicArrayTypeInfo) {
componentInfo = ((BasicArrayTypeInfo<?, ?>) inTypeInfo).getComponentInfo();
} else if (inTypeInfo instanceof PrimitiveArrayTypeInfo) {
componentInfo = BasicTypeInfo.getInfoFor(inTypeInfo.getTypeClass().getComponentType());
} else if (inTypeInfo instanceof ObjectArrayTypeInfo) {
componentInfo = ((ObjectArrayTypeInfo<?, ?>) inTypeInfo).getComponentInfo();
}
info = createTypeInfoFromInput(returnTypeVar, inputTypeHierarchy, ((GenericArrayType) inType).getGenericComponentType(), componentInfo);
} else // the input is a tuple
if (inTypeInfo instanceof TupleTypeInfo && isClassType(inType) && Tuple.class.isAssignableFrom(typeToClass(inType))) {
ParameterizedType tupleBaseClass;
// get tuple from possible tuple subclass
while (!(isClassType(inType) && typeToClass(inType).getSuperclass().equals(Tuple.class))) {
inputTypeHierarchy.add(inType);
inType = typeToClass(inType).getGenericSuperclass();
}
inputTypeHierarchy.add(inType);
// we can assume to be parameterized since we
// already did input validation
tupleBaseClass = (ParameterizedType) inType;
Type[] tupleElements = tupleBaseClass.getActualTypeArguments();
// go thru all tuple elements and search for type variables
for (int i = 0; i < tupleElements.length; i++) {
info = createTypeInfoFromInput(returnTypeVar, inputTypeHierarchy, tupleElements[i], ((TupleTypeInfo<?>) inTypeInfo).getTypeAt(i));
if (info != null) {
break;
}
}
} else // the input is a pojo
if (inTypeInfo instanceof PojoTypeInfo && isClassType(inType)) {
// build the entire type hierarchy for the pojo
getTypeHierarchy(inputTypeHierarchy, inType, Object.class);
// determine a field containing the type variable
List<Field> fields = getAllDeclaredFields(typeToClass(inType), false);
for (Field field : fields) {
Type fieldType = field.getGenericType();
if (fieldType instanceof TypeVariable && sameTypeVars(returnTypeVar, materializeTypeVariable(inputTypeHierarchy, (TypeVariable<?>) fieldType))) {
return getTypeOfPojoField(inTypeInfo, field);
} else if (fieldType instanceof ParameterizedType || fieldType instanceof GenericArrayType) {
ArrayList<Type> typeHierarchyWithFieldType = new ArrayList<>(inputTypeHierarchy);
typeHierarchyWithFieldType.add(fieldType);
TypeInformation<?> foundInfo = createTypeInfoFromInput(returnTypeVar, typeHierarchyWithFieldType, fieldType, getTypeOfPojoField(inTypeInfo, field));
if (foundInfo != null) {
return foundInfo;
}
}
}
}
return info;
}
use of java.lang.reflect.TypeVariable in project tomcat by apache.
the class Util method getTypeParameter.
/*
* For a generic parameter, return either the Class used or if the type
* is unknown, the index for the type in definition of the class
*/
private static TypeResult getTypeParameter(Class<?> clazz, Type argType) {
if (argType instanceof Class<?>) {
return new TypeResult((Class<?>) argType, -1, 0);
} else if (argType instanceof ParameterizedType) {
return new TypeResult((Class<?>) ((ParameterizedType) argType).getRawType(), -1, 0);
} else if (argType instanceof GenericArrayType) {
Type arrayElementType = ((GenericArrayType) argType).getGenericComponentType();
TypeResult result = getTypeParameter(clazz, arrayElementType);
result.incrementDimension(1);
return result;
} else {
TypeVariable<?>[] tvs = clazz.getTypeParameters();
for (int i = 0; i < tvs.length; i++) {
if (tvs[i].equals(argType)) {
return new TypeResult(null, i, 0);
}
}
return null;
}
}
use of java.lang.reflect.TypeVariable in project cucumber-jvm by cucumber.
the class Utils method typeArg.
private static Type typeArg(Type type, Class<?> wantedRawType, int index) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type rawType = parameterizedType.getRawType();
if (rawType instanceof Class && wantedRawType.isAssignableFrom((Class) rawType)) {
Type result = parameterizedType.getActualTypeArguments()[index];
if (result instanceof TypeVariable) {
throw new CucumberException("Generic types must be explicit");
}
return result;
} else {
return null;
}
} else {
return null;
}
}
use of java.lang.reflect.TypeVariable in project jetty.project by eclipse.
the class TypeTree method dumpTree.
public static void dumpTree(String indent, Type type) {
if ((type == null) || (type == Object.class)) {
return;
}
if (type instanceof Class<?>) {
Class<?> ctype = (Class<?>) type;
System.out.printf("%s (Class) = %s%n", indent, ctype.getName());
String name = ctype.getName();
if (name.startsWith("java.lang.") || name.startsWith("java.io.")) {
// filter away standard classes from tree (otherwise it will go on infinitely)
return;
}
Type superType = ctype.getGenericSuperclass();
dumpTree(indent + ".genericSuperClass()", superType);
Type[] ifaces = ctype.getGenericInterfaces();
if ((ifaces != null) && (ifaces.length > 0)) {
// System.out.printf("%s.genericInterfaces[].length = %d%n",indent,ifaces.length);
for (int i = 0; i < ifaces.length; i++) {
Type iface = ifaces[i];
dumpTree(indent + ".genericInterfaces[" + i + "]", iface);
}
}
TypeVariable<?>[] typeParams = ctype.getTypeParameters();
if ((typeParams != null) && (typeParams.length > 0)) {
// System.out.printf("%s.typeParameters[].length = %d%n",indent,typeParams.length);
for (int i = 0; i < typeParams.length; i++) {
TypeVariable<?> typeParam = typeParams[i];
dumpTree(indent + ".typeParameters[" + i + "]", typeParam);
}
}
return;
}
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
System.out.printf("%s (ParameterizedType) = %s%n", indent, ReflectUtils.toShortName(ptype));
// dumpTree(indent + ".ownerType()",ptype.getOwnerType());
dumpTree(indent + ".rawType(" + ReflectUtils.toShortName(ptype.getRawType()) + ")", ptype.getRawType());
Type[] args = ptype.getActualTypeArguments();
if (args != null) {
System.out.printf("%s.actualTypeArguments[].length = %d%n", indent, args.length);
for (int i = 0; i < args.length; i++) {
Type arg = args[i];
dumpTree(indent + ".actualTypeArguments[" + i + "]", arg);
}
}
return;
}
if (type instanceof GenericArrayType) {
GenericArrayType gtype = (GenericArrayType) type;
System.out.printf("%s (GenericArrayType) = %s%n", indent, gtype);
return;
}
if (type instanceof TypeVariable<?>) {
TypeVariable<?> tvar = (TypeVariable<?>) type;
System.out.printf("%s (TypeVariable) = %s%n", indent, tvar);
System.out.printf("%s.getName() = %s%n", indent, tvar.getName());
System.out.printf("%s.getGenericDeclaration() = %s%n", indent, tvar.getGenericDeclaration());
return;
}
if (type instanceof WildcardType) {
System.out.printf("%s (WildcardType) = %s%n", indent, type);
return;
}
System.out.printf("%s (?) = %s%n", indent, type);
}
use of java.lang.reflect.TypeVariable in project elasticsearch by elastic.
the class MoreTypes method equals.
/**
* Returns true if {@code a} and {@code b} are equal.
*/
public static boolean equals(Type a, Type b) {
if (a == b) {
// also handles (a == null && b == null)
return true;
} else if (a instanceof Class) {
// Class already specifies equals().
return a.equals(b);
} else if (a instanceof ParameterizedType) {
if (!(b instanceof ParameterizedType)) {
return false;
}
// TODO: save a .clone() call
ParameterizedType pa = (ParameterizedType) a;
ParameterizedType pb = (ParameterizedType) b;
return Objects.equals(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType().equals(pb.getRawType()) && Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());
} else if (a instanceof GenericArrayType) {
if (!(b instanceof GenericArrayType)) {
return false;
}
GenericArrayType ga = (GenericArrayType) a;
GenericArrayType gb = (GenericArrayType) b;
return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
} else if (a instanceof WildcardType) {
if (!(b instanceof WildcardType)) {
return false;
}
WildcardType wa = (WildcardType) a;
WildcardType wb = (WildcardType) b;
return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
} else if (a instanceof TypeVariable) {
if (!(b instanceof TypeVariable)) {
return false;
}
TypeVariable<?> va = (TypeVariable) a;
TypeVariable<?> vb = (TypeVariable) b;
return va.getGenericDeclaration() == vb.getGenericDeclaration() && va.getName().equals(vb.getName());
} else {
// This isn't a type we support. Could be a generic array type, wildcard type, etc.
return false;
}
}
Aggregations