Search in sources :

Example 36 with Type

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

the class HtmlJsonProvider method isWriteable.

@Override
public boolean isWriteable(Class clazz, Type type, Annotation[] antns, MediaType mt) {
    if (!mt.isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
        return false;
    }
    if (clazz.isArray()) {
        return Models.isModel(clazz.getComponentType());
    }
    if (java.util.List.class.isAssignableFrom(clazz)) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            Type[] args = pt.getActualTypeArguments();
            if (args.length == 1 && args[0] instanceof Class) {
                return Models.isModel((Class<?>) args[0]);
            }
        }
    }
    return Models.isModel(clazz);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(javax.ws.rs.core.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 37 with Type

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

the class CdiComponentProvider method processAnnotatedType.

@SuppressWarnings("unused")
private void processAnnotatedType(@Observes final //                                              PathParam.class})
ProcessAnnotatedType processAnnotatedType) {
    final AnnotatedType<?> annotatedType = processAnnotatedType.getAnnotatedType();
    // if one of the JAX-RS annotations is present in the currently seen class, add it to the "whitelist"
    if (containsJaxRsConstructorInjection(annotatedType) || containsJaxRsFieldInjection(annotatedType) || containsJaxRsMethodInjection(annotatedType)) {
        jaxrsInjectableTypes.add(annotatedType.getBaseType());
    }
    if (customHk2TypesProvider != null) {
        final Type baseType = annotatedType.getBaseType();
        if (customHk2TypesProvider.getHk2Types().contains(baseType)) {
            processAnnotatedType.veto();
            jerseyVetoedTypes.add(baseType);
        }
    }
    if (containsJaxRsParameterizedCtor(annotatedType)) {
        processAnnotatedType.setAnnotatedType(new AnnotatedType() {

            @Override
            public Class getJavaClass() {
                return annotatedType.getJavaClass();
            }

            @Override
            public Set<AnnotatedConstructor> getConstructors() {
                final Set<AnnotatedConstructor> result = new HashSet<>();
                for (final AnnotatedConstructor c : annotatedType.getConstructors()) {
                    result.add(enrichedConstructor(c));
                }
                return result;
            }

            @Override
            public Set getMethods() {
                return annotatedType.getMethods();
            }

            @Override
            public Set getFields() {
                return annotatedType.getFields();
            }

            @Override
            public Type getBaseType() {
                return annotatedType.getBaseType();
            }

            @Override
            public Set<Type> getTypeClosure() {
                return annotatedType.getTypeClosure();
            }

            @Override
            public <T extends Annotation> T getAnnotation(final Class<T> annotationType) {
                return annotatedType.getAnnotation(annotationType);
            }

            @Override
            public Set<Annotation> getAnnotations() {
                return annotatedType.getAnnotations();
            }

            @Override
            public boolean isAnnotationPresent(final Class<? extends Annotation> annotationType) {
                return annotatedType.isAnnotationPresent(annotationType);
            }
        });
    }
}
Also used : ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) Type(java.lang.reflect.Type) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Set(java.util.Set) HashSet(java.util.HashSet) AnnotatedConstructor(javax.enterprise.inject.spi.AnnotatedConstructor)

Example 38 with Type

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

the class NewsCallback method convertSuccess.

/**
     * 这里的数据解析是根据 http://apistore.baidu.com/apiworks/servicedetail/688.html 返回的数据来写的
     * 实际使用中,自己服务器返回的数据格式和上面网站肯定不一样,所以以下是参考代码,根据实际情况自己改写
     */
@Override
public T convertSuccess(Response response) throws Exception {
    //以下代码是通过泛型解析实际参数,泛型必须传
    Type genType = getClass().getGenericSuperclass();
    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
    Type type = params[0];
    if (!(type instanceof ParameterizedType))
        throw new IllegalStateException("没有填写泛型参数");
    JsonReader jsonReader = new JsonReader(response.body().charStream());
    Type rawType = ((ParameterizedType) type).getRawType();
    if (rawType == NewsResponse.class) {
        NewsResponse newsResponse = Convert.fromJson(jsonReader, type);
        if (newsResponse.showapi_res_code == 0) {
            response.close();
            //noinspection unchecked
            return (T) newsResponse;
        } else {
            response.close();
            throw new IllegalStateException(newsResponse.showapi_res_error);
        }
    } else {
        response.close();
        throw new IllegalStateException("基类错误无法解析!");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonReader(com.google.gson.stream.JsonReader) NewsResponse(com.lzy.demo.model.NewsResponse)

Example 39 with Type

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

the class NewsConvert method convertSuccess.

@Override
public T convertSuccess(Response response) throws Exception {
    //以下代码是通过泛型解析实际参数,泛型必须传
    Type genType = getClass().getGenericSuperclass();
    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
    Type type = params[0];
    if (!(type instanceof ParameterizedType))
        throw new IllegalStateException("没有填写泛型参数");
    JsonReader jsonReader = new JsonReader(response.body().charStream());
    Type rawType = ((ParameterizedType) type).getRawType();
    if (rawType == NewsResponse.class) {
        NewsResponse newsResponse = Convert.fromJson(jsonReader, type);
        if (newsResponse.showapi_res_code == 0) {
            response.close();
            //noinspection unchecked
            return (T) newsResponse;
        } else {
            response.close();
            throw new IllegalStateException(newsResponse.showapi_res_error);
        }
    } else {
        response.close();
        throw new IllegalStateException("基类错误无法解析!");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonReader(com.google.gson.stream.JsonReader) NewsResponse(com.lzy.demo.model.NewsResponse)

Example 40 with Type

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

the class TypeUtils method resolveTypeVariable.

private static Type resolveTypeVariable(Type context, Class<?> contextRawType, TypeVariable<?> unknown) {
    Class<?> declaredByRaw = declaringClassOf(unknown);
    // We can't reduce this further.
    if (declaredByRaw == null)
        return unknown;
    Type declaredBy = getGenericSupertype(context, contextRawType, declaredByRaw);
    if (declaredBy instanceof ParameterizedType) {
        int index = indexOf(declaredByRaw.getTypeParameters(), unknown);
        return ((ParameterizedType) declaredBy).getActualTypeArguments()[index];
    }
    return unknown;
}
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)

Aggregations

Type (java.lang.reflect.Type)6477 ParameterizedType (java.lang.reflect.ParameterizedType)1798 ProgressRequestBody (io.kubernetes.client.ProgressRequestBody)722 ProgressResponseBody (io.kubernetes.client.ProgressResponseBody)722 GenericArrayType (java.lang.reflect.GenericArrayType)711 WildcardType (java.lang.reflect.WildcardType)580 Test (org.junit.Test)513 ArrayList (java.util.ArrayList)429 Method (java.lang.reflect.Method)424 TypeVariable (java.lang.reflect.TypeVariable)350 List (java.util.List)342 Map (java.util.Map)296 Gson (com.google.gson.Gson)230 V1Status (io.kubernetes.client.models.V1Status)228 V1Status (io.kubernetes.client.openapi.models.V1Status)224 HashMap (java.util.HashMap)211 Field (java.lang.reflect.Field)170 Annotation (java.lang.annotation.Annotation)161 IOException (java.io.IOException)146 Collection (java.util.Collection)116