use of java.lang.reflect.TypeVariable in project flink by apache.
the class TypeExtractor method createSubTypesInfo.
/**
* Creates the TypeInformation for all elements of a type that expects a certain number of
* subtypes (e.g. TupleXX).
*
* @param originalType most concrete subclass
* @param definingType type that defines the number of subtypes (e.g. Tuple2 -> 2 subtypes)
* @param typeHierarchy necessary for type inference
* @param in1Type necessary for type inference
* @param in2Type necessary for type inference
* @param lenient decides whether exceptions should be thrown if a subtype can not be determined
* @return array containing TypeInformation of sub types or null if definingType contains
* more subtypes (fields) that defined
*/
private <IN1, IN2> TypeInformation<?>[] createSubTypesInfo(Type originalType, ParameterizedType definingType, ArrayList<Type> typeHierarchy, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, boolean lenient) {
Type[] subtypes = new Type[definingType.getActualTypeArguments().length];
// materialize possible type variables
for (int i = 0; i < subtypes.length; i++) {
final Type actualTypeArg = definingType.getActualTypeArguments()[i];
// materialize immediate TypeVariables
if (actualTypeArg instanceof TypeVariable<?>) {
subtypes[i] = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) actualTypeArg);
} else // class or parameterized type
{
subtypes[i] = actualTypeArg;
}
}
TypeInformation<?>[] subTypesInfo = new TypeInformation<?>[subtypes.length];
for (int i = 0; i < subtypes.length; i++) {
final ArrayList<Type> subTypeHierarchy = new ArrayList<>(typeHierarchy);
subTypeHierarchy.add(subtypes[i]);
// try to derive the type info of the TypeVariable from the immediate base child input as a last attempt
if (subtypes[i] instanceof TypeVariable<?>) {
subTypesInfo[i] = createTypeInfoFromInputs((TypeVariable<?>) subtypes[i], subTypeHierarchy, in1Type, in2Type);
// variable could not be determined
if (subTypesInfo[i] == null && !lenient) {
throw new InvalidTypesException("Type of TypeVariable '" + ((TypeVariable<?>) subtypes[i]).getName() + "' in '" + ((TypeVariable<?>) subtypes[i]).getGenericDeclaration() + "' could not be determined. This is most likely a type erasure problem. " + "The type extraction currently supports types with generic variables only in cases where " + "all variables in the return type can be deduced from the input type(s).");
}
} else {
// create the type information of the subtype or null/exception
try {
subTypesInfo[i] = createTypeInfoWithTypeHierarchy(subTypeHierarchy, subtypes[i], in1Type, in2Type);
} catch (InvalidTypesException e) {
if (lenient) {
subTypesInfo[i] = null;
} else {
throw e;
}
}
}
}
// check that number of fields matches the number of subtypes
if (!lenient) {
Class<?> originalTypeAsClass = null;
if (isClassType(originalType)) {
originalTypeAsClass = typeToClass(originalType);
}
checkNotNull(originalTypeAsClass, "originalType has an unexpected type");
// check if the class we assumed to conform to the defining type so far is actually a pojo because the
// original type contains additional fields.
// check for additional fields.
int fieldCount = countFieldsInClass(originalTypeAsClass);
if (fieldCount > subTypesInfo.length) {
return null;
}
}
return subTypesInfo;
}
use of java.lang.reflect.TypeVariable in project robovm by robovm.
the class TypeVariableImpl method resolve.
void resolve() {
if (formalVar != null) {
return;
}
GenericDeclaration curLayer = declOfVarUser;
TypeVariable var;
while ((var = findFormalVar(curLayer, name)) == null) {
curLayer = nextLayer(curLayer);
if (curLayer == null) {
throw new AssertionError("illegal type variable reference");
}
}
formalVar = (TypeVariableImpl<D>) var;
this.genericDeclaration = formalVar.genericDeclaration;
this.bounds = formalVar.bounds;
}
use of java.lang.reflect.TypeVariable in project robovm by robovm.
the class Types method appendGenericType.
public static void appendGenericType(StringBuilder out, Type type) {
if (type instanceof TypeVariable) {
out.append(((TypeVariable) type).getName());
} else if (type instanceof ParameterizedType) {
out.append(type.toString());
} else if (type instanceof GenericArrayType) {
Type simplified = ((GenericArrayType) type).getGenericComponentType();
appendGenericType(out, simplified);
out.append("[]");
} else if (type instanceof Class) {
Class c = (Class<?>) type;
if (c.isArray()) {
String[] as = c.getName().split("\\[");
int len = as.length - 1;
if (as[len].length() > 1) {
out.append(as[len].substring(1, as[len].length() - 1));
} else {
char ch = as[len].charAt(0);
if (ch == 'I') {
out.append("int");
} else if (ch == 'B') {
out.append("byte");
} else if (ch == 'J') {
out.append("long");
} else if (ch == 'F') {
out.append("float");
} else if (ch == 'D') {
out.append("double");
} else if (ch == 'S') {
out.append("short");
} else if (ch == 'C') {
out.append("char");
} else if (ch == 'Z') {
out.append("boolean");
} else if (ch == 'V') {
out.append("void");
}
}
for (int i = 0; i < len; i++) {
out.append("[]");
}
} else {
out.append(c.getName());
}
}
}
use of java.lang.reflect.TypeVariable in project uavstack by uavorg.
the class FieldInfo method getInheritGenericType.
public static Type getInheritGenericType(Class<?> clazz, TypeVariable<?> tv) {
Type type = null;
GenericDeclaration gd = tv.getGenericDeclaration();
do {
type = clazz.getGenericSuperclass();
if (type == null) {
return null;
}
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
if (ptype.getRawType() == gd) {
TypeVariable<?>[] tvs = gd.getTypeParameters();
Type[] types = ptype.getActualTypeArguments();
for (int i = 0; i < tvs.length; i++) {
if (tvs[i] == tv)
return types[i];
}
return null;
}
}
clazz = TypeUtils.getClass(type);
} while (type != null);
return null;
}
use of java.lang.reflect.TypeVariable in project felix by apache.
the class ConvertingImpl method reifyType.
static Type reifyType(Type typeToReify, Class<?> ownerClass, Type[] typeArgs) {
if (typeToReify instanceof TypeVariable) {
String name = ((TypeVariable<?>) typeToReify).getName();
for (int i = 0; i < ownerClass.getTypeParameters().length; i++) {
TypeVariable<?> typeVariable = ownerClass.getTypeParameters()[i];
if (typeVariable.getName().equals(name)) {
return typeArgs[i];
}
}
// The direct type variable wasn't found, maybe it was already
// bound in this class.
Type currentType = ownerClass;
while (currentType != null) {
if (currentType instanceof Class) {
currentType = ((Class<?>) currentType).getGenericSuperclass();
} else if (currentType instanceof ParameterizedType) {
currentType = ((ParameterizedType) currentType).getRawType();
}
if (currentType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) currentType;
Type rawType = pt.getRawType();
if (rawType instanceof Class) {
return reifyType(typeToReify, (Class<?>) rawType, pt.getActualTypeArguments());
}
}
}
} else if (typeToReify instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) typeToReify;
Type[] parameters = parameterizedType.getActualTypeArguments();
boolean useCopy = false;
final Type[] copiedParameters = new Type[parameters.length];
for (int i = 0; i < parameters.length; i++) {
copiedParameters[i] = reifyType(parameters[i], ownerClass, typeArgs);
useCopy |= copiedParameters[i] != parameters[i];
}
if (useCopy) {
return new ParameterizedType() {
@Override
public Type getRawType() {
return parameterizedType.getRawType();
}
@Override
public Type getOwnerType() {
return parameterizedType.getOwnerType();
}
@Override
public Type[] getActualTypeArguments() {
return Arrays.copyOf(copiedParameters, copiedParameters.length);
}
};
}
} else if (typeToReify instanceof GenericArrayType) {
GenericArrayType type = (GenericArrayType) typeToReify;
Type genericComponentType = type.getGenericComponentType();
final Type reifiedType = reifyType(genericComponentType, ownerClass, typeArgs);
if (reifiedType != genericComponentType) {
return new GenericArrayType() {
@Override
public Type getGenericComponentType() {
return reifiedType;
}
};
}
}
return typeToReify;
}
Aggregations