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);
}
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);
}
});
}
}
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("基类错误无法解析!");
}
}
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("基类错误无法解析!");
}
}
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;
}
Aggregations