Search in sources :

Example 71 with Type

use of java.lang.reflect.Type 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 72 with Type

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

the class ReflectionHelper method getRawClass.

/**
     * Given the type parameter gets the raw type represented by the type, or null if this has no associated raw class.
     *
     * @param type the type to find the raw class on.
     * @return the raw class associated with this type.
     */
public static Class<?> getRawClass(Type type) {
    if (type == null)
        return null;
    if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        if (!(componentType instanceof ParameterizedType) && !(componentType instanceof Class)) {
            // type variable is not supported
            return null;
        }
        Class<?> rawComponentClass = getRawClass(componentType);
        String forNameName = "[L" + rawComponentClass.getName() + ";";
        try {
            return Class.forName(forNameName);
        } catch (Throwable th) {
            // ignore, but return null
            return null;
        }
    }
    if (type instanceof Class) {
        return (Class<?>) type;
    }
    if (type instanceof ParameterizedType) {
        Type rawType = ((ParameterizedType) type).getRawType();
        if (rawType instanceof Class) {
            return (Class<?>) rawType;
        }
    }
    return null;
}
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) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 73 with Type

use of java.lang.reflect.Type 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 74 with Type

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

the class ReflectionHelper method resolveTypeVariable.

private static ClassTypePair resolveTypeVariable(final Class<?> c, final Class<?> dc, final TypeVariable tv, final Map<TypeVariable, Type> map) {
    final Type[] gis = c.getGenericInterfaces();
    for (final Type gi : gis) {
        if (gi instanceof ParameterizedType) {
            // process pt of interface
            final ParameterizedType pt = (ParameterizedType) gi;
            final ClassTypePair ctp = resolveTypeVariable(pt, (Class<?>) pt.getRawType(), dc, tv, map);
            if (ctp != null) {
                return ctp;
            }
        }
    }
    final Type gsc = c.getGenericSuperclass();
    if (gsc instanceof ParameterizedType) {
        // process pt of class
        final ParameterizedType pt = (ParameterizedType) gsc;
        return resolveTypeVariable(pt, c.getSuperclass(), dc, tv, map);
    } else if (gsc instanceof Class) {
        return resolveTypeVariable(c.getSuperclass(), dc, tv, map);
    }
    return null;
}
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)

Example 75 with Type

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

the class Providers method getAllRankedProviders.

/**
     * Get the iterable of all {@link RankedProvider providers} (custom and default) registered for the given service provider
     * contract in the underlying {@link InjectionManager injection manager} container.
     *
     * @param <T>             service provider contract Java type.
     * @param injectionManager underlying injection manager.
     * @param contract        service provider contract.
     * @return iterable of all available ranked service providers for the contract. Return value is never null.
     */
public static <T> Iterable<RankedProvider<T>> getAllRankedProviders(InjectionManager injectionManager, Class<T> contract) {
    List<ServiceHolder<T>> providers = getServiceHolders(injectionManager, contract, CustomAnnotationLiteral.INSTANCE);
    providers.addAll(getServiceHolders(injectionManager, contract));
    LinkedHashMap<ServiceHolder<T>, RankedProvider<T>> providerMap = new LinkedHashMap<>();
    for (ServiceHolder<T> provider : providers) {
        if (!providerMap.containsKey(provider)) {
            Set<Type> contractTypes = provider.getContractTypes();
            Class<?> implementationClass = provider.getImplementationClass();
            boolean proxyGenerated = true;
            for (Type ct : contractTypes) {
                if (((Class<?>) ct).isAssignableFrom(implementationClass)) {
                    proxyGenerated = false;
                    break;
                }
            }
            Set<Type> contracts = proxyGenerated ? contractTypes : null;
            providerMap.put(provider, new RankedProvider<>(provider.getInstance(), provider.getRank(), contracts));
        }
    }
    return providerMap.values();
}
Also used : LinkedHashMap(java.util.LinkedHashMap) RuntimeType(javax.ws.rs.RuntimeType) Type(java.lang.reflect.Type) RankedProvider(org.glassfish.jersey.model.internal.RankedProvider)

Aggregations

Type (java.lang.reflect.Type)6423 ParameterizedType (java.lang.reflect.ParameterizedType)1761 ProgressRequestBody (io.kubernetes.client.ProgressRequestBody)722 ProgressResponseBody (io.kubernetes.client.ProgressResponseBody)722 GenericArrayType (java.lang.reflect.GenericArrayType)690 WildcardType (java.lang.reflect.WildcardType)571 Test (org.junit.Test)512 ArrayList (java.util.ArrayList)427 Method (java.lang.reflect.Method)416 TypeVariable (java.lang.reflect.TypeVariable)337 List (java.util.List)335 Map (java.util.Map)289 Gson (com.google.gson.Gson)231 V1Status (io.kubernetes.client.models.V1Status)228 V1Status (io.kubernetes.client.openapi.models.V1Status)224 HashMap (java.util.HashMap)204 Field (java.lang.reflect.Field)163 Annotation (java.lang.annotation.Annotation)160 IOException (java.io.IOException)140 Collection (java.util.Collection)111