Search in sources :

Example 41 with TypeVariable

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;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) TypeExtractionUtils.isClassType(org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType) Type(java.lang.reflect.Type) CompositeType(org.apache.flink.api.common.typeutils.CompositeType) ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) ArrayList(java.util.ArrayList) InvalidTypesException(org.apache.flink.api.common.functions.InvalidTypesException) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation)

Example 42 with TypeVariable

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;
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 43 with TypeVariable

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());
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 44 with TypeVariable

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 45 with TypeVariable

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

TypeVariable (java.lang.reflect.TypeVariable)424 ParameterizedType (java.lang.reflect.ParameterizedType)342 Type (java.lang.reflect.Type)341 GenericArrayType (java.lang.reflect.GenericArrayType)251 WildcardType (java.lang.reflect.WildcardType)191 Method (java.lang.reflect.Method)63 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)23 GenericDeclaration (java.lang.reflect.GenericDeclaration)19 Test (org.junit.Test)16 Field (java.lang.reflect.Field)15 List (java.util.List)13 Map (java.util.Map)11 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)11 TypeExtractionUtils.isClassType (org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType)11 Member (java.lang.reflect.Member)10 MediaType (javax.ws.rs.core.MediaType)10 CaptureType (com.googlecode.gentyref.CaptureType)8 ElementType (java.lang.annotation.ElementType)8 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)8