use of java.lang.reflect.ParameterizedType in project guava by hceylan.
the class TypeTokenTest method testGetSupertype_fullyGenericType.
public void testGetSupertype_fullyGenericType() {
ParameterizedType expectedType = Types.newParameterizedType(Map.class, ListMap.class.getTypeParameters()[0], Types.newParameterizedType(List.class, ListMap.class.getTypeParameters()[1]));
assertEquals(expectedType, TypeToken.of(ListMap.class).getSupertype(Map.class).getType());
}
use of java.lang.reflect.ParameterizedType in project platform_frameworks_base by android.
the class TypeReference method toString.
private static void toString(Type type, StringBuilder out) {
if (type == null) {
return;
} else if (type instanceof TypeVariable<?>) {
// T
out.append(((TypeVariable<?>) type).getName());
} else if (type instanceof Class<?>) {
Class<?> klass = (Class<?>) type;
out.append(klass.getName());
toString(klass.getTypeParameters(), out);
} else if (type instanceof ParameterizedType) {
// "Foo<T1, T2, T3, ... Tn>"
ParameterizedType p = (ParameterizedType) type;
out.append(((Class<?>) p.getRawType()).getName());
toString(p.getActualTypeArguments(), out);
} else if (type instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) type;
toString(gat.getGenericComponentType(), out);
out.append("[]");
} else {
// WildcardType, BoundedType
// TODO:
out.append(type.toString());
}
}
use of java.lang.reflect.ParameterizedType in project flink by apache.
the class TypeExtractor method analyzePojo.
@SuppressWarnings("unchecked")
protected <OUT, IN1, IN2> TypeInformation<OUT> analyzePojo(Class<OUT> clazz, ArrayList<Type> typeHierarchy, ParameterizedType parameterizedType, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type) {
if (!Modifier.isPublic(clazz.getModifiers())) {
LOG.info("Class " + clazz.getName() + " is not public, cannot treat it as a POJO type. Will be handled as GenericType");
return new GenericTypeInfo<OUT>(clazz);
}
// add the hierarchy of the POJO itself if it is generic
if (parameterizedType != null) {
getTypeHierarchy(typeHierarchy, parameterizedType, Object.class);
} else // create a type hierarchy, if the incoming only contains the most bottom one or none.
if (typeHierarchy.size() <= 1) {
getTypeHierarchy(typeHierarchy, clazz, Object.class);
}
List<Field> fields = getAllDeclaredFields(clazz, false);
if (fields.size() == 0) {
LOG.info("No fields detected for " + clazz + ". Cannot be used as a PojoType. Will be handled as GenericType");
return new GenericTypeInfo<OUT>(clazz);
}
List<PojoField> pojoFields = new ArrayList<PojoField>();
for (Field field : fields) {
Type fieldType = field.getGenericType();
if (!isValidPojoField(field, clazz, typeHierarchy)) {
LOG.info(clazz + " is not a valid POJO type");
return null;
}
try {
ArrayList<Type> fieldTypeHierarchy = new ArrayList<Type>(typeHierarchy);
fieldTypeHierarchy.add(fieldType);
TypeInformation<?> ti = createTypeInfoWithTypeHierarchy(fieldTypeHierarchy, fieldType, in1Type, in2Type);
pojoFields.add(new PojoField(field, ti));
} catch (InvalidTypesException e) {
Class<?> genericClass = Object.class;
if (isClassType(fieldType)) {
genericClass = typeToClass(fieldType);
}
pojoFields.add(new PojoField(field, new GenericTypeInfo<OUT>((Class<OUT>) genericClass)));
}
}
CompositeType<OUT> pojoType = new PojoTypeInfo<OUT>(clazz, pojoFields);
//
// Validate the correctness of the pojo.
// returning "null" will result create a generic type information.
//
List<Method> methods = getAllDeclaredMethods(clazz);
for (Method method : methods) {
if (method.getName().equals("readObject") || method.getName().equals("writeObject")) {
LOG.info(clazz + " contains custom serialization methods we do not call.");
return null;
}
}
// Try retrieving the default constructor, if it does not have one
// we cannot use this because the serializer uses it.
Constructor defaultConstructor = null;
try {
defaultConstructor = clazz.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
LOG.info(clazz + " is abstract or an interface, having a concrete " + "type can increase performance.");
} else {
LOG.info(clazz + " must have a default constructor to be used as a POJO.");
return null;
}
}
if (defaultConstructor != null && !Modifier.isPublic(defaultConstructor.getModifiers())) {
LOG.info("The default constructor of " + clazz + " should be Public to be used as a POJO.");
return null;
}
// everything is checked, we return the pojo
return pojoType;
}
use of java.lang.reflect.ParameterizedType 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.ParameterizedType in project flink by apache.
the class Serializers method recursivelyRegisterGenericType.
private static void recursivelyRegisterGenericType(Type fieldType, ExecutionConfig config, Set<Class<?>> alreadySeen) {
if (fieldType instanceof ParameterizedType) {
// field has generics
ParameterizedType parameterizedFieldType = (ParameterizedType) fieldType;
for (Type t : parameterizedFieldType.getActualTypeArguments()) {
if (TypeExtractionUtils.isClassType(t)) {
recursivelyRegisterType(TypeExtractionUtils.typeToClass(t), config, alreadySeen);
}
}
recursivelyRegisterGenericType(parameterizedFieldType.getRawType(), config, alreadySeen);
} else if (fieldType instanceof GenericArrayType) {
GenericArrayType genericArrayType = (GenericArrayType) fieldType;
recursivelyRegisterGenericType(genericArrayType.getGenericComponentType(), config, alreadySeen);
} else if (fieldType instanceof Class) {
Class<?> clazz = (Class<?>) fieldType;
recursivelyRegisterType(clazz, config, alreadySeen);
}
}
Aggregations