Search in sources :

Example 6 with ClassTypePair

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

the class ReflectionHelper method getParameterizedTypeArguments.

/**
     * Get the parameterized type arguments for a declaring class that
     * declares a generic interface type.
     *
     * @param p the declaring class
     * @return the parameterized type arguments, or null if the generic
     * interface type is not a parameterized type.
     */
public static Type[] getParameterizedTypeArguments(final DeclaringClassInterfacePair p) {
    if (p.genericInterface instanceof ParameterizedType) {
        final ParameterizedType pt = (ParameterizedType) p.genericInterface;
        final Type[] as = pt.getActualTypeArguments();
        final Type[] ras = new Type[as.length];
        for (int i = 0; i < as.length; i++) {
            final Type a = as[i];
            if (a instanceof Class) {
                ras[i] = a;
            } else if (a instanceof ParameterizedType) {
                ras[i] = a;
            } else if (a instanceof TypeVariable) {
                final ClassTypePair ctp = resolveTypeVariable(p.concreteClass, p.declaringClass, (TypeVariable) a);
                if (ctp == null) {
                    throw new IllegalArgumentException(LocalizationMessages.ERROR_RESOLVING_GENERIC_TYPE_VALUE(p.genericInterface, p.concreteClass));
                }
                ras[i] = ctp.type();
            }
        }
        return ras;
    } 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)

Example 7 with ClassTypePair

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

the class OptionalParamConverterProvider method getConverter.

@Override
public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) {
    final List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(genericType);
    final ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
    if (ctp == null || ctp.rawClass() == String.class) {
        return new ParamConverter<T>() {

            @Override
            public T fromString(final String value) {
                return rawType.cast(Optional.fromNullable(value));
            }

            @Override
            public String toString(final T value) throws IllegalArgumentException {
                return value.toString();
            }
        };
    }
    final Set<ParamConverterProvider> converterProviders = Providers.getProviders(injectionManager, ParamConverterProvider.class);
    for (ParamConverterProvider provider : converterProviders) {
        @SuppressWarnings("unchecked") final ParamConverter<?> converter = provider.getConverter(ctp.rawClass(), ctp.type(), annotations);
        if (converter != null) {
            return new ParamConverter<T>() {

                @Override
                public T fromString(final String value) {
                    return rawType.cast(Optional.fromNullable(value).transform((Function<String, Object>) s -> converter.fromString(value)));
                }

                @Override
                public String toString(final T value) throws IllegalArgumentException {
                    return value.toString();
                }
            };
        }
    }
    return null;
}
Also used : Function(com.google.common.base.Function) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) ParamConverter(javax.ws.rs.ext.ParamConverter) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider)

Example 8 with ClassTypePair

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

the class OptionalParamConverterProvider method getConverter.

/**
     * {@inheritDoc}
     */
@Override
public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) {
    if (Optional.class.equals(rawType)) {
        final List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(genericType);
        final ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
        if (ctp == null || ctp.rawClass() == String.class) {
            return new ParamConverter<T>() {

                @Override
                public T fromString(final String value) {
                    return rawType.cast(Optional.ofNullable(value));
                }

                @Override
                public String toString(final T value) {
                    return value.toString();
                }
            };
        }
        for (ParamConverterProvider provider : Providers.getProviders(locator, ParamConverterProvider.class)) {
            final ParamConverter<?> converter = provider.getConverter(ctp.rawClass(), ctp.type(), annotations);
            if (converter != null) {
                return new ParamConverter<T>() {

                    @Override
                    public T fromString(final String value) {
                        return rawType.cast(Optional.ofNullable(value).map(s -> converter.fromString(value)));
                    }

                    @Override
                    public String toString(final T value) {
                        return value.toString();
                    }
                };
            }
        }
    }
    return null;
}
Also used : Inject(javax.inject.Inject) ParamConverter(javax.ws.rs.ext.ParamConverter) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) List(java.util.List) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) Optional(java.util.Optional) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Providers(org.glassfish.jersey.internal.inject.Providers) Singleton(javax.inject.Singleton) ReflectionHelper(org.glassfish.jersey.internal.util.ReflectionHelper) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) ParamConverter(javax.ws.rs.ext.ParamConverter) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider)

Example 9 with ClassTypePair

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

the class OptionalParamConverterProvider method getConverter.

/**
 * {@inheritDoc}
 */
@Override
@Nullable
public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) {
    if (Optional.class.equals(rawType)) {
        final List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(genericType);
        final ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
        if (ctp == null || ctp.rawClass() == String.class) {
            return new ParamConverter<T>() {

                @Override
                public T fromString(final String value) {
                    return rawType.cast(Optional.fromNullable(value));
                }

                @Override
                public String toString(final T value) {
                    return value.toString();
                }
            };
        }
        for (ParamConverterProvider provider : Providers.getProviders(manager, ParamConverterProvider.class)) {
            final ParamConverter<?> converter = provider.getConverter(ctp.rawClass(), ctp.type(), annotations);
            if (converter != null) {
                return new ParamConverter<T>() {

                    @Override
                    public T fromString(final String value) {
                        final Object convertedValue = value == null ? null : converter.fromString(value);
                        return rawType.cast(Optional.fromNullable(convertedValue));
                    }

                    @Override
                    public String toString(final T value) {
                        return value.toString();
                    }
                };
            }
        }
    }
    return null;
}
Also used : ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) ParamConverter(javax.ws.rs.ext.ParamConverter) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) Nullable(javax.annotation.Nullable)

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