Search in sources :

Example 1 with ClassTypePair

use of org.glassfish.jersey.internal.util.collection.ClassTypePair in project jersey by jersey.

the class Parameter method create.

/**
     * Create a parameter model.
     *
     * @param concreteClass   concrete resource method handler implementation class.
     * @param declaringClass  declaring class of the method the parameter belongs to or field that this parameter represents.
     * @param encodeByDefault flag indicating whether the parameter should be encoded by default or not. Note that a presence
     *                        of {@link Encoded} annotation in the list of the parameter {@code annotations} will override any
     *                        value set in the flag to {@code true}.
     * @param rawType         raw Java parameter type.
     * @param type            generic Java parameter type.
     * @param annotations     parameter annotations.
     * @return new parameter model.
     */
@SuppressWarnings("unchecked")
public static Parameter create(Class concreteClass, Class declaringClass, boolean encodeByDefault, Class<?> rawType, Type type, Annotation[] annotations) {
    if (null == annotations) {
        return null;
    }
    Annotation paramAnnotation = null;
    Parameter.Source paramSource = null;
    String paramName = null;
    boolean paramEncoded = encodeByDefault;
    String paramDefault = null;
    /**
         * Create a parameter from the list of annotations. Unknown annotated
         * parameters are also supported, and in such a cases the last
         * unrecognized annotation is taken to be that associated with the
         * parameter.
         */
    for (Annotation annotation : annotations) {
        if (ANNOTATION_HELPER_MAP.containsKey(annotation.annotationType())) {
            ParamAnnotationHelper helper = ANNOTATION_HELPER_MAP.get(annotation.annotationType());
            paramAnnotation = annotation;
            paramSource = helper.getSource();
            paramName = helper.getValueOf(annotation);
        } else if (Encoded.class == annotation.annotationType()) {
            paramEncoded = true;
        } else if (DefaultValue.class == annotation.annotationType()) {
            paramDefault = ((DefaultValue) annotation).value();
        } else {
            // Take latest unknown annotation, but don't override known annotation
            if ((paramAnnotation == null) || (paramSource == Source.UNKNOWN)) {
                paramAnnotation = annotation;
                paramSource = Source.UNKNOWN;
                paramName = getValue(annotation);
            }
        }
    }
    if (paramAnnotation == null) {
        paramSource = Parameter.Source.ENTITY;
    }
    ClassTypePair ct = ReflectionHelper.resolveGenericType(concreteClass, declaringClass, rawType, type);
    if (paramSource == Source.BEAN_PARAM) {
        return new BeanParameter(annotations, paramAnnotation, paramName, ct.rawClass(), ct.type(), paramEncoded, paramDefault);
    } else {
        return new Parameter(annotations, paramAnnotation, paramSource, paramName, ct.rawClass(), ct.type(), paramEncoded, paramDefault);
    }
}
Also used : Encoded(javax.ws.rs.Encoded) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) Annotation(java.lang.annotation.Annotation)

Example 2 with ClassTypePair

use of org.glassfish.jersey.internal.util.collection.ClassTypePair 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 3 with ClassTypePair

use of org.glassfish.jersey.internal.util.collection.ClassTypePair 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 4 with ClassTypePair

use of org.glassfish.jersey.internal.util.collection.ClassTypePair in project jersey by jersey.

the class MultivaluedParameterExtractorFactory method process.

@SuppressWarnings("unchecked")
private MultivaluedParameterExtractor<?> process(final ParamConverterFactory paramConverterFactory, final String defaultValue, final Class<?> rawType, final Type type, final Annotation[] annotations, final String parameterName) {
    // Try to find a converter that support rawType and type at first.
    // E.g. if someone writes a converter that support List<Integer> this approach should precede the next one.
    ParamConverter<?> converter = paramConverterFactory.getConverter(rawType, type, annotations);
    if (converter != null) {
        try {
            return new SingleValueExtractor(converter, parameterName, defaultValue);
        } catch (final ExtractorException e) {
            throw e;
        } catch (final Exception e) {
            throw new ProcessingException(LocalizationMessages.ERROR_PARAMETER_TYPE_PROCESSING(rawType), e);
        }
    }
    // Check whether the rawType is the type of the collection supported.
    if (rawType == List.class || rawType == Set.class || rawType == SortedSet.class) {
        // Get the generic type of the list. If none is found default to String.
        final List<ClassTypePair> typePairs = ReflectionHelper.getTypeArgumentAndClass(type);
        final ClassTypePair typePair = (typePairs.size() == 1) ? typePairs.get(0) : null;
        if (typePair == null || typePair.rawClass() == String.class) {
            return StringCollectionExtractor.getInstance(rawType, parameterName, defaultValue);
        } else {
            converter = paramConverterFactory.getConverter(typePair.rawClass(), typePair.type(), annotations);
            if (converter == null) {
                return null;
            }
            try {
                return CollectionExtractor.getInstance(rawType, converter, parameterName, defaultValue);
            } catch (final ExtractorException e) {
                throw e;
            } catch (final Exception e) {
                throw new ProcessingException(LocalizationMessages.ERROR_PARAMETER_TYPE_PROCESSING(rawType), e);
            }
        }
    }
    // Check primitive types.
    if (rawType == String.class) {
        return new SingleStringValueExtractor(parameterName, defaultValue);
    } else if (rawType == Character.class) {
        return new PrimitiveCharacterExtractor(parameterName, defaultValue, PrimitiveMapper.primitiveToDefaultValueMap.get(rawType));
    } else if (rawType.isPrimitive()) {
        // Convert primitive to wrapper class
        final Class<?> wrappedRaw = PrimitiveMapper.primitiveToClassMap.get(rawType);
        if (wrappedRaw == null) {
            // Primitive type not supported
            return null;
        }
        if (wrappedRaw == Character.class) {
            return new PrimitiveCharacterExtractor(parameterName, defaultValue, PrimitiveMapper.primitiveToDefaultValueMap.get(wrappedRaw));
        }
        // Check for static valueOf(String)
        final Method valueOf = AccessController.doPrivileged(ReflectionHelper.getValueOfStringMethodPA(wrappedRaw));
        if (valueOf != null) {
            try {
                return new PrimitiveValueOfExtractor(valueOf, parameterName, defaultValue, PrimitiveMapper.primitiveToDefaultValueMap.get(wrappedRaw));
            } catch (final Exception e) {
                throw new ProcessingException(LocalizationMessages.DEFAULT_COULD_NOT_PROCESS_METHOD(defaultValue, valueOf));
            }
        }
    }
    return null;
}
Also used : ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) Method(java.lang.reflect.Method) SortedSet(java.util.SortedSet) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) ProcessingException(javax.ws.rs.ProcessingException) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) ProcessingException(javax.ws.rs.ProcessingException)

Example 5 with ClassTypePair

use of org.glassfish.jersey.internal.util.collection.ClassTypePair in project jersey by jersey.

the class ReflectionHelper method getParameterizedClassArguments.

/**
     * Get the parameterized class arguments for a declaring class that
     * declares a generic interface type.
     *
     * @param p the declaring class
     * @return the parameterized class arguments, or null if the generic
     * interface type is not a parameterized type.
     */
public static Class[] getParameterizedClassArguments(final DeclaringClassInterfacePair p) {
    if (p.genericInterface instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) p.genericInterface;
        final Type[] as = pt.getActualTypeArguments();
        final Class[] cas = new Class[as.length];
        for (int i = 0; i < as.length; i++) {
            final Type a = as[i];
            if (a instanceof Class) {
                cas[i] = (Class) a;
            } else if (a instanceof ParameterizedType) {
                pt = (ParameterizedType) a;
                cas[i] = (Class) pt.getRawType();
            } else if (a instanceof TypeVariable) {
                final TypeVariable tv = (TypeVariable) a;
                final ClassTypePair ctp = resolveTypeVariable(p.concreteClass, p.declaringClass, tv);
                cas[i] = (ctp != null) ? ctp.rawClass() : (Class<?>) (tv.getBounds()[0]);
            } else if (a instanceof GenericArrayType) {
                final GenericArrayType gat = (GenericArrayType) a;
                final Type t = gat.getGenericComponentType();
                if (t instanceof Class) {
                    cas[i] = getArrayForComponentType((Class<?>) t);
                }
            }
        }
        return cas;
    } else {
        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) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

ClassTypePair (org.glassfish.jersey.internal.util.collection.ClassTypePair)9 Type (java.lang.reflect.Type)5 GenericArrayType (java.lang.reflect.GenericArrayType)4 ParameterizedType (java.lang.reflect.ParameterizedType)4 WildcardType (java.lang.reflect.WildcardType)4 GenericType (javax.ws.rs.core.GenericType)4 TypeVariable (java.lang.reflect.TypeVariable)3 ParamConverter (javax.ws.rs.ext.ParamConverter)3 ParamConverterProvider (javax.ws.rs.ext.ParamConverterProvider)3 Annotation (java.lang.annotation.Annotation)2 Function (com.google.common.base.Function)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Optional (java.util.Optional)1 SortedSet (java.util.SortedSet)1 Nullable (javax.annotation.Nullable)1 Inject (javax.inject.Inject)1 Singleton (javax.inject.Singleton)1 Encoded (javax.ws.rs.Encoded)1