Search in sources :

Example 6 with TypeVariable

use of java.lang.reflect.TypeVariable in project okhttp-OkGo by jeasonlzy.

the class TypeUtils method resolve.

public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
    // This implementation is made a little more complicated in an attempt to avoid object-creation.
    while (true) {
        if (toResolve instanceof TypeVariable) {
            TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
            toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
            if (toResolve == typeVariable) {
                return toResolve;
            }
        } else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
            Class<?> original = (Class<?>) toResolve;
            Type componentType = original.getComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : new GenericArrayTypeImpl(newComponentType);
        } else if (toResolve instanceof GenericArrayType) {
            GenericArrayType original = (GenericArrayType) toResolve;
            Type componentType = original.getGenericComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : new GenericArrayTypeImpl(newComponentType);
        } else if (toResolve instanceof ParameterizedType) {
            ParameterizedType original = (ParameterizedType) toResolve;
            Type ownerType = original.getOwnerType();
            Type newOwnerType = resolve(context, contextRawType, ownerType);
            boolean changed = newOwnerType != ownerType;
            Type[] args = original.getActualTypeArguments();
            for (int t = 0, length = args.length; t < length; t++) {
                Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
                if (resolvedTypeArgument != args[t]) {
                    if (!changed) {
                        args = args.clone();
                        changed = true;
                    }
                    args[t] = resolvedTypeArgument;
                }
            }
            return changed ? new ParameterizedTypeImpl(newOwnerType, original.getRawType(), args) : original;
        } else if (toResolve instanceof WildcardType) {
            WildcardType original = (WildcardType) toResolve;
            Type[] originalLowerBound = original.getLowerBounds();
            Type[] originalUpperBound = original.getUpperBounds();
            if (originalLowerBound.length == 1) {
                Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
                if (lowerBound != originalLowerBound[0]) {
                    return new WildcardTypeImpl(new Type[] { Object.class }, new Type[] { lowerBound });
                }
            } else if (originalUpperBound.length == 1) {
                Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
                if (upperBound != originalUpperBound[0]) {
                    return new WildcardTypeImpl(new Type[] { upperBound }, EMPTY_TYPE_ARRAY);
                }
            }
            return original;
        } else {
            return toResolve;
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 7 with TypeVariable

use of java.lang.reflect.TypeVariable in project qi4j-sdk by Qi4j.

the class NamedAssociationModel method bind.

@Override
public void bind(Resolution resolution) throws BindingException {
    builderInfo = new AssociationInfo() {

        @Override
        public boolean isImmutable() {
            return false;
        }

        @Override
        public QualifiedName qualifiedName() {
            return qualifiedName;
        }

        @Override
        public Type type() {
            return type;
        }

        @Override
        public void checkConstraints(Object value) throws ConstraintViolationException {
            NamedAssociationModel.this.checkConstraints(value);
        }
    };
    if (type instanceof TypeVariable) {
        Class mainType = first(resolution.model().types());
        type = Classes.resolveTypeVariable((TypeVariable) type, ((Member) accessor).getDeclaringClass(), mainType);
    }
}
Also used : Type(java.lang.reflect.Type) GenericAssociationInfo(org.qi4j.api.association.GenericAssociationInfo) TypeVariable(java.lang.reflect.TypeVariable) QualifiedName(org.qi4j.api.common.QualifiedName) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) AccessibleObject(java.lang.reflect.AccessibleObject) Member(java.lang.reflect.Member)

Example 8 with TypeVariable

use of java.lang.reflect.TypeVariable in project jersey by jersey.

the class ReflectionHelper method resolveTypeVariable.

private static ClassTypePair resolveTypeVariable(ParameterizedType pt, Class<?> c, final Class<?> dc, final TypeVariable tv, final Map<TypeVariable, Type> map) {
    final Type[] typeArguments = pt.getActualTypeArguments();
    final TypeVariable[] typeParameters = c.getTypeParameters();
    final Map<TypeVariable, Type> subMap = new HashMap<TypeVariable, Type>();
    for (int i = 0; i < typeArguments.length; i++) {
        // Substitute a type variable with the Java class
        final Type typeArgument = typeArguments[i];
        if (typeArgument instanceof TypeVariable) {
            final Type t = map.get(typeArgument);
            subMap.put(typeParameters[i], t);
        } else {
            subMap.put(typeParameters[i], typeArgument);
        }
    }
    if (c == dc) {
        Type t = subMap.get(tv);
        if (t instanceof Class) {
            return ClassTypePair.of((Class) t);
        } else if (t instanceof GenericArrayType) {
            final GenericArrayType gat = (GenericArrayType) t;
            t = gat.getGenericComponentType();
            if (t instanceof Class) {
                c = (Class<?>) t;
                try {
                    return ClassTypePair.of(getArrayForComponentType(c));
                } catch (final Exception ignored) {
                // ignored
                }
                return null;
            } else if (t instanceof ParameterizedType) {
                final Type rt = ((ParameterizedType) t).getRawType();
                if (rt instanceof Class) {
                    c = (Class<?>) rt;
                } else {
                    return null;
                }
                try {
                    return ClassTypePair.of(getArrayForComponentType(c), gat);
                } catch (final Exception e) {
                    return null;
                }
            } else {
                return null;
            }
        } else if (t instanceof ParameterizedType) {
            pt = (ParameterizedType) t;
            if (pt.getRawType() instanceof Class) {
                return ClassTypePair.of((Class<?>) pt.getRawType(), pt);
            } else {
                return null;
            }
        } else {
            return null;
        }
    } else {
        return resolveTypeVariable(c, dc, tv, subMap);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) GenericType(javax.ws.rs.core.GenericType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) GenericArrayType(java.lang.reflect.GenericArrayType) IOException(java.io.IOException)

Example 9 with TypeVariable

use of java.lang.reflect.TypeVariable in project jersey by jersey.

the class ReflectionHelper method resolveGenericType.

/**
     * Resolve generic type parameter(s) of a raw class and it's generic type
     * based on the class that declares the generic type parameter(s) to be resolved
     * and a concrete implementation of the declaring class.
     *
     * @param concreteClass       concrete implementation of the declaring class.
     * @param declaringClass      class declaring the generic type parameter(s) to be
     *                            resolved.
     * @param rawResolvedType     raw class of the generic type to be resolved.
     * @param genericResolvedType generic type information of th type to be resolved.
     * @return a pair of class and the generic type values with the the resolved
     * generic parameter types.
     */
public static ClassTypePair resolveGenericType(final Class concreteClass, final Class declaringClass, final Class rawResolvedType, final Type genericResolvedType) {
    if (genericResolvedType instanceof TypeVariable) {
        final ClassTypePair ct = resolveTypeVariable(concreteClass, declaringClass, (TypeVariable) genericResolvedType);
        if (ct != null) {
            return ct;
        }
    } else if (genericResolvedType instanceof ParameterizedType) {
        final ParameterizedType pt = (ParameterizedType) genericResolvedType;
        final Type[] ptts = pt.getActualTypeArguments();
        boolean modified = false;
        for (int i = 0; i < ptts.length; i++) {
            final ClassTypePair ct = resolveGenericType(concreteClass, declaringClass, (Class) pt.getRawType(), ptts[i]);
            if (ct.type() != ptts[i]) {
                ptts[i] = ct.type();
                modified = true;
            }
        }
        if (modified) {
            final ParameterizedType rpt = new ParameterizedType() {

                @Override
                public Type[] getActualTypeArguments() {
                    return ptts.clone();
                }

                @Override
                public Type getRawType() {
                    return pt.getRawType();
                }

                @Override
                public Type getOwnerType() {
                    return pt.getOwnerType();
                }
            };
            return ClassTypePair.of((Class<?>) pt.getRawType(), rpt);
        }
    } else if (genericResolvedType instanceof GenericArrayType) {
        final GenericArrayType gat = (GenericArrayType) genericResolvedType;
        final ClassTypePair ct = resolveGenericType(concreteClass, declaringClass, null, gat.getGenericComponentType());
        if (gat.getGenericComponentType() != ct.type()) {
            try {
                final Class ac = ReflectionHelper.getArrayForComponentType(ct.rawClass());
                return ClassTypePair.of(ac);
            } catch (final Exception e) {
                LOGGER.log(Level.FINEST, "", e);
            }
        }
    }
    return ClassTypePair.of(rawResolvedType, genericResolvedType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) GenericType(javax.ws.rs.core.GenericType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType) IOException(java.io.IOException)

Example 10 with TypeVariable

use of java.lang.reflect.TypeVariable in project jodd by oblac.

the class ReflectUtil method typeToString.

private static void typeToString(StringBuilder sb, Type type, Set<Type> visited) {
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        final Class<?> rawType = (Class<?>) parameterizedType.getRawType();
        sb.append(rawType.getName());
        boolean first = true;
        for (Type typeArg : parameterizedType.getActualTypeArguments()) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append('<');
            typeToString(sb, typeArg, visited);
            sb.append('>');
        }
    } else if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        sb.append('?');
        // According to JLS(http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.5.1):
        // - Lower and upper can't coexist: (for instance, this is not allowed: <? extends List<String> & super MyInterface>)
        // - Multiple bounds are not supported (for instance, this is not allowed: <? extends List<String> & MyInterface>)
        final Type bound;
        if (wildcardType.getLowerBounds().length != 0) {
            sb.append(" super ");
            bound = wildcardType.getLowerBounds()[0];
        } else {
            sb.append(" extends ");
            bound = wildcardType.getUpperBounds()[0];
        }
        typeToString(sb, bound, visited);
    } else if (type instanceof TypeVariable<?>) {
        TypeVariable<?> typeVariable = (TypeVariable<?>) type;
        sb.append(typeVariable.getName());
        if (!visited.contains(type)) {
            visited.add(type);
            sb.append(" extends ");
            boolean first = true;
            for (Type bound : typeVariable.getBounds()) {
                if (first) {
                    first = false;
                } else {
                    sb.append(" & ");
                }
                typeToString(sb, bound, visited);
            }
            visited.remove(type);
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        typeToString(genericArrayType.getGenericComponentType());
        sb.append(genericArrayType.getGenericComponentType());
        sb.append("[]");
    } else if (type instanceof Class) {
        Class<?> typeClass = (Class<?>) type;
        sb.append(typeClass.getName());
    } else {
        throw new IllegalArgumentException("Unsupported type: " + type);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) 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