/**
* Returns a type that is functionally equal but not necessarily equal according to {@link
* Object#equals(Object) Object.equals()}.
*/
static Type canonicalize(Type type) {
if (type instanceof Class) {
Class<?> c = (Class<?>) type;
return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c;
} else if (type instanceof ParameterizedType) {
if (type instanceof ParameterizedTypeImpl) return type;
ParameterizedType p = (ParameterizedType) type;
return new ParameterizedTypeImpl(p.getOwnerType(),
p.getRawType(), p.getActualTypeArguments());
} else if (type instanceof GenericArrayType) {
if (type instanceof GenericArrayTypeImpl) return type;
GenericArrayType g = (GenericArrayType) type;
return new GenericArrayTypeImpl(g.getGenericComponentType());
} else if (type instanceof WildcardType) {
if (type instanceof WildcardTypeImpl) return type;
WildcardType w = (WildcardType) type;
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds());
} else {
return type; // This type is unsupported!
}
}
java类java.lang.reflect.GenericArrayType的实例源码
Types.java 文件源码
项目:inspector
阅读 42
收藏 0
点赞 0
评论 0
ClassReflectUtil.java 文件源码
项目:tasfe-framework
阅读 50
收藏 0
点赞 0
评论 0
public static Class getGenericClass(ParameterizedType parameterizedType, int i) {
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) {
// 处理多级泛型
System.out.println("111111");
return (Class) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) {
// 处理数组泛型
return (Class) ((GenericArrayType) genericClass).getGenericComponentType();
} else if (genericClass instanceof TypeVariable) {
// 处理泛型擦拭对象
System.out.println("33333333");
return (Class) getClass(((TypeVariable) genericClass).getBounds()[0], 0);
} else {
System.out.println("444444");
return (Class) genericClass;
}
}
$Gson$Types.java 文件源码
项目:lams
阅读 43
收藏 0
点赞 0
评论 0
/**
* Returns a type that is functionally equal but not necessarily equal
* according to {@link Object#equals(Object) Object.equals()}. The returned
* type is {@link java.io.Serializable}.
*/
public static Type canonicalize(Type type) {
if (type instanceof Class) {
Class<?> c = (Class<?>) type;
return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c;
} else if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) type;
return new ParameterizedTypeImpl(p.getOwnerType(),
p.getRawType(), p.getActualTypeArguments());
} else if (type instanceof GenericArrayType) {
GenericArrayType g = (GenericArrayType) type;
return new GenericArrayTypeImpl(g.getGenericComponentType());
} else if (type instanceof WildcardType) {
WildcardType w = (WildcardType) type;
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds());
} else {
// type is either serializable as-is or unsupported
return type;
}
}
InjectorImpl.java 文件源码
项目:businessworks
阅读 51
收藏 0
点赞 0
评论 0
/**
* Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's
* a bit awkward because we have to pull out the inner type in the type literal.
*/
private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding(
Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException {
Type typeLiteralType = key.getTypeLiteral().getType();
if (!(typeLiteralType instanceof ParameterizedType)) {
throw errors.cannotInjectRawTypeLiteral().toException();
}
ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType;
Type innerType = parameterizedType.getActualTypeArguments()[0];
// this is unforunate. We don't support building TypeLiterals for type variable like 'T'. If
// this proves problematic, we can probably fix TypeLiteral to support type variables
if (!(innerType instanceof Class)
&& !(innerType instanceof GenericArrayType)
&& !(innerType instanceof ParameterizedType)) {
throw errors.cannotInjectTypeLiteralOf(innerType).toException();
}
@SuppressWarnings("unchecked") // by definition, innerType == T, so this is safe
TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType);
InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<TypeLiteral<T>>(
Initializables.of(value));
return new InstanceBindingImpl<TypeLiteral<T>>(this, key, SourceProvider.UNKNOWN_SOURCE,
factory, ImmutableSet.<InjectionPoint>of(), value);
}
HandlerMethodInvoker.java 文件源码
项目:lams
阅读 45
收藏 0
点赞 0
评论 0
private Class<?> getHttpEntityType(MethodParameter methodParam) {
Assert.isAssignable(HttpEntity.class, methodParam.getParameterType());
ParameterizedType type = (ParameterizedType) methodParam.getGenericParameterType();
if (type.getActualTypeArguments().length == 1) {
Type typeArgument = type.getActualTypeArguments()[0];
if (typeArgument instanceof Class) {
return (Class<?>) typeArgument;
}
else if (typeArgument instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) typeArgument).getGenericComponentType();
if (componentType instanceof Class) {
// Surely, there should be a nicer way to do this
Object array = Array.newInstance((Class<?>) componentType, 0);
return array.getClass();
}
}
}
throw new IllegalArgumentException(
"HttpEntity parameter (" + methodParam.getParameterName() + ") is not parameterized");
}
Utils.java 文件源码
项目:GitHub
阅读 42
收藏 0
点赞 0
评论 0
static boolean hasUnresolvableType(Type type) {
if (type instanceof Class<?>) {
return false;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
if (hasUnresolvableType(typeArgument)) {
return true;
}
}
return false;
}
if (type instanceof GenericArrayType) {
return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType());
}
if (type instanceof TypeVariable) {
return true;
}
if (type instanceof WildcardType) {
return true;
}
String className = type == null ? "null" : type.getClass().getName();
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + className);
}
TypeResolver.java 文件源码
项目:googles-monorepo-demo
阅读 42
收藏 0
点赞 0
评论 0
/**
* Resolves all type variables in {@code type} and all downstream types and returns a
* corresponding type with type variables resolved.
*/
public Type resolveType(Type type) {
checkNotNull(type);
if (type instanceof TypeVariable) {
return typeTable.resolve((TypeVariable<?>) type);
} else if (type instanceof ParameterizedType) {
return resolveParameterizedType((ParameterizedType) type);
} else if (type instanceof GenericArrayType) {
return resolveGenericArrayType((GenericArrayType) type);
} else if (type instanceof WildcardType) {
return resolveWildcardType((WildcardType) type);
} else {
// if Class<?>, no resolution needed, we are done.
return type;
}
}
C$Gson$Types.java 文件源码
项目:boohee_v5.6
阅读 41
收藏 0
点赞 0
评论 0
public static Class<?> getRawType(Type type) {
if (type instanceof Class) {
return (Class) type;
}
if (type instanceof ParameterizedType) {
Type rawType = ((ParameterizedType) type).getRawType();
C$Gson$Preconditions.checkArgument(rawType instanceof Class);
return (Class) rawType;
} else if (type instanceof GenericArrayType) {
return Array.newInstance(C$Gson$Types.getRawType(((GenericArrayType) type)
.getGenericComponentType()), 0).getClass();
} else {
if (type instanceof TypeVariable) {
return Object.class;
}
if (type instanceof WildcardType) {
return C$Gson$Types.getRawType(((WildcardType) type).getUpperBounds()[0]);
}
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or " +
"GenericArrayType, but <" + type + "> is of type " + (type == null ? "null" :
type.getClass().getName()));
}
}
Field.java 文件源码
项目:NanoUI
阅读 43
收藏 0
点赞 0
评论 0
/** If the type of the field is parameterized, returns the Class object representing the parameter type at the specified index,
* null otherwise. */
public Class getElementType (int index) {
Type genericType = field.getGenericType();
if (genericType instanceof ParameterizedType) {
Type[] actualTypes = ((ParameterizedType)genericType).getActualTypeArguments();
if (actualTypes.length - 1 >= index) {
Type actualType = actualTypes[index];
if (actualType instanceof Class)
return (Class)actualType;
else if (actualType instanceof ParameterizedType)
return (Class)((ParameterizedType)actualType).getRawType();
else if (actualType instanceof GenericArrayType) {
Type componentType = ((GenericArrayType)actualType).getGenericComponentType();
if (componentType instanceof Class) return ArrayReflection.newInstance((Class)componentType, 0).getClass();
}
}
}
return null;
}
MoreTypes.java 文件源码
项目:elasticsearch_my
阅读 40
收藏 0
点赞 0
评论 0
/**
* Returns the hashCode of {@code type}.
*/
public static int hashCode(Type type) {
if (type instanceof Class) {
// Class specifies hashCode().
return type.hashCode();
} else if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) type;
return Arrays.hashCode(p.getActualTypeArguments())
^ p.getRawType().hashCode()
^ hashCodeOrZero(p.getOwnerType());
} else if (type instanceof GenericArrayType) {
return hashCode(((GenericArrayType) type).getGenericComponentType());
} else if (type instanceof WildcardType) {
WildcardType w = (WildcardType) type;
return Arrays.hashCode(w.getLowerBounds()) ^ Arrays.hashCode(w.getUpperBounds());
} else {
// This isn't a type we support. Probably a type variable
return hashCodeOrZero(type);
}
}
MQTTUtils.java 文件源码
项目:thingplug-sdk-android
阅读 42
收藏 0
点赞 0
评论 0
static boolean hasUnresolvableType(Type type) {
if (type instanceof Class<?>) {
return false;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
if (hasUnresolvableType(typeArgument)) {
return true;
}
}
return false;
}
if (type instanceof GenericArrayType) {
return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType());
}
if (type instanceof TypeVariable) {
return true;
}
if (type instanceof WildcardType) {
return true;
}
String className = type == null ? "null" : type.getClass().getName();
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + className);
}
TypeResolver.java 文件源码
项目:guava-mock
阅读 45
收藏 0
点赞 0
评论 0
/**
* Resolves all type variables in {@code type} and all downstream types and returns a
* corresponding type with type variables resolved.
*/
public Type resolveType(Type type) {
checkNotNull(type);
if (type instanceof TypeVariable) {
return typeTable.resolve((TypeVariable<?>) type);
} else if (type instanceof ParameterizedType) {
return resolveParameterizedType((ParameterizedType) type);
} else if (type instanceof GenericArrayType) {
return resolveGenericArrayType((GenericArrayType) type);
} else if (type instanceof WildcardType) {
return resolveWildcardType((WildcardType) type);
} else {
// if Class<?>, no resolution needed, we are done.
return type;
}
}
ClassUtils.java 文件源码
项目:dubbox-hystrix
阅读 52
收藏 0
点赞 0
评论 0
public static Class<?> getGenericClass(Class<?> cls, int i) {
try {
ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) { // 处理多级泛型
return (Class<?>) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) { // 处理数组泛型
return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
} else if (genericClass != null) {
return (Class<?>) genericClass;
}
} catch (Throwable e) {
}
if (cls.getSuperclass() != null) {
return getGenericClass(cls.getSuperclass(), i);
} else {
throw new IllegalArgumentException(cls.getName() + " generic type undefined!");
}
}
ClassUtil.java 文件源码
项目:AndroidBasicLibs
阅读 51
收藏 0
点赞 0
评论 0
public static Class getGenericClass(ParameterizedType parameterizedType, int i) {
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) { // 处理多级泛型
return (Class) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) { // 处理数组泛型
return (Class) ((GenericArrayType) genericClass).getGenericComponentType();
} else if (genericClass instanceof TypeVariable) { // 处理泛型擦拭对象
return (Class) getClass(((TypeVariable) genericClass).getBounds()[0], 0);
} else {
return (Class) genericClass;
}
}
Utils.java 文件源码
项目:super-volley
阅读 46
收藏 0
点赞 0
评论 0
static boolean hasUnresolvableType(Type type) {
if (type instanceof Class<?>) {
return false;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
if (hasUnresolvableType(typeArgument)) {
return true;
}
}
return false;
}
if (type instanceof GenericArrayType) {
return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType());
}
if (type instanceof TypeVariable) {
return true;
}
if (type instanceof WildcardType) {
return true;
}
String className = type == null ? "null" : type.getClass().getName();
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + className);
}
b.java 文件源码
项目:letv
阅读 46
收藏 0
点赞 0
评论 0
public static Type a(Type type) {
if (type instanceof Class) {
c cVar;
Class cls = (Class) type;
if (cls.isArray()) {
cVar = new c(a(cls.getComponentType()));
} else {
Object obj = cls;
}
return cVar;
} else if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
return new d(parameterizedType.getOwnerType(), parameterizedType.getRawType(), parameterizedType.getActualTypeArguments());
} else if (type instanceof GenericArrayType) {
return new c(((GenericArrayType) type).getGenericComponentType());
} else {
if (!(type instanceof WildcardType)) {
return type;
}
WildcardType wildcardType = (WildcardType) type;
return new e(wildcardType.getUpperBounds(), wildcardType.getLowerBounds());
}
}
MQTTUtils.java 文件源码
项目:thingplug-sdk-android
阅读 55
收藏 0
点赞 0
评论 0
static boolean hasUnresolvableType(Type type) {
if (type instanceof Class<?>) {
return false;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
if (hasUnresolvableType(typeArgument)) {
return true;
}
}
return false;
}
if (type instanceof GenericArrayType) {
return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType());
}
if (type instanceof TypeVariable) {
return true;
}
if (type instanceof WildcardType) {
return true;
}
String className = type == null ? "null" : type.getClass().getName();
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + className);
}
InjectorImpl.java 文件源码
项目:Elasticsearch
阅读 52
收藏 0
点赞 0
评论 0
/**
* Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's
* a bit awkward because we have to pull out the inner type in the type literal.
*/
private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding(
Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException {
Type typeLiteralType = key.getTypeLiteral().getType();
if (!(typeLiteralType instanceof ParameterizedType)) {
throw errors.cannotInjectRawTypeLiteral().toException();
}
ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType;
Type innerType = parameterizedType.getActualTypeArguments()[0];
// this is unforunate. We don't support building TypeLiterals for type variable like 'T'. If
// this proves problematic, we can probably fix TypeLiteral to support type variables
if (!(innerType instanceof Class)
&& !(innerType instanceof GenericArrayType)
&& !(innerType instanceof ParameterizedType)) {
throw errors.cannotInjectTypeLiteralOf(innerType).toException();
}
@SuppressWarnings("unchecked") // by definition, innerType == T, so this is safe
TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType);
InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<>(
Initializables.of(value));
return new InstanceBindingImpl<>(this, key, SourceProvider.UNKNOWN_SOURCE,
factory, ImmutableSet.<InjectionPoint>of(), value);
}
ClassUtils.java 文件源码
项目:eXperDB-DB2PG
阅读 50
收藏 0
点赞 0
评论 0
static public Class<?> getClass(Type type) throws ClassNotFoundException {
if (type instanceof Class) {
return (Class<?>) type;
} else if (type instanceof ParameterizedType) {
return getClass(((ParameterizedType) type).getRawType());
} else if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
Class<?> componentClass = getClass(componentType);
if (componentClass != null) {
return Array.newInstance(componentClass, 0).getClass();
}
}
String className = getClassName(type);
if (className == null || className.isEmpty()) {
return null;
}
return Class.forName(className);
}
$Gson$Types.java 文件源码
项目:odoo-work
阅读 50
收藏 0
点赞 0
评论 0
/**
* Returns a type that is functionally equal but not necessarily equal
* according to {@link Object#equals(Object) Object.equals()}. The returned
* type is {@link Serializable}.
*/
public static Type canonicalize(Type type) {
if (type instanceof Class) {
Class<?> c = (Class<?>) type;
return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c;
} else if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) type;
return new ParameterizedTypeImpl(p.getOwnerType(),
p.getRawType(), p.getActualTypeArguments());
} else if (type instanceof GenericArrayType) {
GenericArrayType g = (GenericArrayType) type;
return new GenericArrayTypeImpl(g.getGenericComponentType());
} else if (type instanceof WildcardType) {
WildcardType w = (WildcardType) type;
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds());
} else {
// type is either serializable as-is or unsupported
return type;
}
}
DefaultMXBeanMappingFactory.java 文件源码
项目:OpenJSharp
阅读 54
收藏 0
点赞 0
评论 0
@Override
final Object fromNonNullOpenValue(Object openValue)
throws InvalidObjectException {
final Object[] openArray = (Object[]) openValue;
final Type javaType = getJavaType();
final Object[] valueArray;
final Type componentType;
if (javaType instanceof GenericArrayType) {
componentType =
((GenericArrayType) javaType).getGenericComponentType();
} else if (javaType instanceof Class<?> &&
((Class<?>) javaType).isArray()) {
componentType = ((Class<?>) javaType).getComponentType();
} else {
throw new IllegalArgumentException("Not an array: " +
javaType);
}
valueArray = (Object[]) Array.newInstance((Class<?>) componentType,
openArray.length);
for (int i = 0; i < openArray.length; i++)
valueArray[i] = elementMapping.fromOpenValue(openArray[i]);
return valueArray;
}
TypeToken.java 文件源码
项目:odoo-work
阅读 67
收藏 0
点赞 0
评论 0
/**
* Check if this type is assignable from the given Type.
*
* @deprecated this implementation may be inconsistent with javac for types
* with wildcards.
*/
@Deprecated
public boolean isAssignableFrom(Type from) {
if (from == null) {
return false;
}
if (type.equals(from)) {
return true;
}
if (type instanceof Class<?>) {
return rawType.isAssignableFrom($Gson$Types.getRawType(from));
} else if (type instanceof ParameterizedType) {
return isAssignableFrom(from, (ParameterizedType) type,
new HashMap<String, Type>());
} else if (type instanceof GenericArrayType) {
return rawType.isAssignableFrom($Gson$Types.getRawType(from))
&& isAssignableFrom(from, (GenericArrayType) type);
} else {
throw buildUnexpectedTypeError(
type, Class.class, ParameterizedType.class, GenericArrayType.class);
}
}
ReflectionUtils.java 文件源码
项目:gitplex-mit
阅读 48
收藏 0
点赞 0
评论 0
/**
* Get the underlying class for a type.
* <p>
* @param type
* the type to get class from
* @return
* the underlying class, or <tt>null</tt> if the type is a variable type
*/
private static Class<?> getClass(Type type) {
if (type instanceof Class) {
return (Class<?>) type;
} else if (type instanceof ParameterizedType) {
return getClass(((ParameterizedType) type).getRawType());
} else if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type)
.getGenericComponentType();
Class<?> componentClass = getClass(componentType);
if (componentClass != null) {
return Array.newInstance(componentClass, 0).getClass();
} else {
return null;
}
} else {
return null;
}
}
TypeToken.java 文件源码
项目:googles-monorepo-demo
阅读 49
收藏 0
点赞 0
评论 0
/**
* Returns true if this type is a subtype of the given {@code type}. "Subtype" is defined
* according to
* <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.5.1">the rules for
* type arguments</a> introduced with Java generics.
*
* @since 19.0
*/
public final boolean isSubtypeOf(Type supertype) {
checkNotNull(supertype);
if (supertype instanceof WildcardType) {
// if 'supertype' is <? super Foo>, 'this' can be:
// Foo, SubFoo, <? extends Foo>.
// if 'supertype' is <? extends Foo>, nothing is a subtype.
return any(((WildcardType) supertype).getLowerBounds()).isSupertypeOf(runtimeType);
}
// if 'this' is wildcard, it's a suptype of to 'supertype' if any of its "extends"
// bounds is a subtype of 'supertype'.
if (runtimeType instanceof WildcardType) {
// <? super Base> is of no use in checking 'from' being a subtype of 'to'.
return any(((WildcardType) runtimeType).getUpperBounds()).isSubtypeOf(supertype);
}
// if 'this' is type variable, it's a subtype if any of its "extends"
// bounds is a subtype of 'supertype'.
if (runtimeType instanceof TypeVariable) {
return runtimeType.equals(supertype)
|| any(((TypeVariable<?>) runtimeType).getBounds()).isSubtypeOf(supertype);
}
if (runtimeType instanceof GenericArrayType) {
return of(supertype).isSupertypeOfArray((GenericArrayType) runtimeType);
}
// Proceed to regular Type subtype check
if (supertype instanceof Class) {
return this.someRawTypeIsSubclassOf((Class<?>) supertype);
} else if (supertype instanceof ParameterizedType) {
return this.isSubtypeOfParameterizedType((ParameterizedType) supertype);
} else if (supertype instanceof GenericArrayType) {
return this.isSubtypeOfArrayType((GenericArrayType) supertype);
} else { // to instanceof TypeVariable
return false;
}
}
MQTTUtils.java 文件源码
项目:thingplug-sdk-android
阅读 48
收藏 0
点赞 0
评论 0
static Class<?> getRawType(Type type) {
if (type == null) throw new NullPointerException("type == null");
if (type instanceof Class<?>) {
// Type is a normal class.
return (Class<?>) type;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
// I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but
// suspects some pathological case related to nested classes exists.
Type rawType = parameterizedType.getRawType();
if (!(rawType instanceof Class)) throw new IllegalArgumentException();
return (Class<?>) rawType;
}
if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
return Array.newInstance(getRawType(componentType), 0).getClass();
}
if (type instanceof TypeVariable) {
// We could use the variable's bounds, but that won't work if there are multiple. Having a raw
// type that's more general than necessary is okay.
return Object.class;
}
if (type instanceof WildcardType) {
return getRawType(((WildcardType) type).getUpperBounds()[0]);
}
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + type.getClass().getName());
}
ParameterizedTypeUtil.java 文件源码
项目:GitHub
阅读 40
收藏 0
点赞 0
评论 0
private static Type getTrueType(
Type type,
TypeVariable<?>[] typeVariables,
Type[] actualTypes) {
if (type instanceof TypeVariable<?>) {
TypeVariable<?> tv = (TypeVariable<?>) type;
String name = tv.getName();
if (actualTypes != null) {
for (int i = 0; i < typeVariables.length; i++) {
if (name.equals(typeVariables[i].getName())) {
return actualTypes[i];
}
}
}
return tv;
// }else if (type instanceof Class<?>) {
// return type;
} else if (type instanceof GenericArrayType) {
Type ct = ((GenericArrayType) type).getGenericComponentType();
if (ct instanceof Class<?>) {
return Array.newInstance((Class<?>) ct, 0).getClass();
}
}
return type;
}
JavaObjectDeserializer.java 文件源码
项目:GitHub
阅读 55
收藏 0
点赞 0
评论 0
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
if (componentType instanceof TypeVariable) {
TypeVariable<?> componentVar = (TypeVariable<?>) componentType;
componentType = componentVar.getBounds()[0];
}
List<Object> list = new ArrayList<Object>();
parser.parseArray(componentType, list);
Class<?> componentClass;
if (componentType instanceof Class) {
componentClass = (Class<?>) componentType;
Object[] array = (Object[]) Array.newInstance(componentClass, list.size());
list.toArray(array);
return (T) array;
} else {
return (T) list.toArray();
}
}
if (type instanceof Class && type != Object.class && type != Serializable.class) {
return (T) parser.parseObject(type);
}
return (T) parser.parse(fieldName);
}
TypeUtils.java 文件源码
项目:boohee_v5.6
阅读 50
收藏 0
点赞 0
评论 0
public static Type unwrap(Type type) {
if (!(type instanceof GenericArrayType)) {
return type;
}
Type componentType = ((GenericArrayType) type).getGenericComponentType();
if (componentType == Byte.TYPE) {
return byte[].class;
}
if (componentType == Character.TYPE) {
return char[].class;
}
return type;
}
TypeUtils.java 文件源码
项目:GitHub
阅读 60
收藏 0
点赞 0
评论 0
public static Type checkPrimitiveArray(GenericArrayType genericArrayType) {
Type clz = genericArrayType;
Type genericComponentType = genericArrayType.getGenericComponentType();
String prefix = "[";
while (genericComponentType instanceof GenericArrayType) {
genericComponentType = ((GenericArrayType) genericComponentType)
.getGenericComponentType();
prefix += prefix;
}
if (genericComponentType instanceof Class<?>) {
Class<?> ck = (Class<?>) genericComponentType;
if (ck.isPrimitive()) {
try {
if (ck == boolean.class) {
clz = Class.forName(prefix + "Z");
} else if (ck == char.class) {
clz = Class.forName(prefix + "C");
} else if (ck == byte.class) {
clz = Class.forName(prefix + "B");
} else if (ck == short.class) {
clz = Class.forName(prefix + "S");
} else if (ck == int.class) {
clz = Class.forName(prefix + "I");
} else if (ck == long.class) {
clz = Class.forName(prefix + "J");
} else if (ck == float.class) {
clz = Class.forName(prefix + "F");
} else if (ck == double.class) {
clz = Class.forName(prefix + "D");
}
} catch (ClassNotFoundException e) {
}
}
}
return clz;
}
Utils.java 文件源码
项目:GitHub
阅读 48
收藏 0
点赞 0
评论 0
static Class<?> getRawType(Type type) {
checkNotNull(type, "type == null");
if (type instanceof Class<?>) {
// Type is a normal class.
return (Class<?>) type;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
// I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but
// suspects some pathological case related to nested classes exists.
Type rawType = parameterizedType.getRawType();
if (!(rawType instanceof Class)) throw new IllegalArgumentException();
return (Class<?>) rawType;
}
if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
return Array.newInstance(getRawType(componentType), 0).getClass();
}
if (type instanceof TypeVariable) {
// We could use the variable's bounds, but that won't work if there are multiple. Having a raw
// type that's more general than necessary is okay.
return Object.class;
}
if (type instanceof WildcardType) {
return getRawType(((WildcardType) type).getUpperBounds()[0]);
}
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + type.getClass().getName());
}