本文整理汇总了Java中java.lang.reflect.ParameterizedType类的典型用法代码示例。如果您正苦于以下问题:Java ParameterizedType类的具体用法?Java ParameterizedType怎么用?Java ParameterizedType使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ParameterizedType类属于java.lang.reflect包,在下文中一共展示了ParameterizedType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: canonicalize
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
public static Type canonicalize(Type type) {
if (type instanceof Class) {
Class<?> c = (Class) type;
return c.isArray() ? new GenericArrayTypeImpl(C$Gson$Types.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) {
return new GenericArrayTypeImpl(((GenericArrayType) type).getGenericComponentType());
} else {
if (!(type instanceof WildcardType)) {
return type;
}
WildcardType w = (WildcardType) type;
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds());
}
}
示例2: canonicalize
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
/**
* 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;
}
}
示例3: formatTypeWithoutSpecialCharacter
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
private static String formatTypeWithoutSpecialCharacter(Type type) {
if (type instanceof Class) {
Class clazz = (Class) type;
return clazz.getCanonicalName();
}
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
String typeName = formatTypeWithoutSpecialCharacter(pType.getRawType());
for (Type typeArg : pType.getActualTypeArguments()) {
typeName += "_";
typeName += formatTypeWithoutSpecialCharacter(typeArg);
}
return typeName;
}
if (type instanceof GenericArrayType) {
GenericArrayType gaType = (GenericArrayType) type;
return formatTypeWithoutSpecialCharacter(gaType.getGenericComponentType()) + "_array";
}
throw new AJsoupReaderException("unsupported type: " + type + ", of class " + type.getClass());
}
示例4: CollectionMapping
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
CollectionMapping(Type targetType,
ArrayType<?> openArrayType,
Class<?> openArrayClass,
MXBeanMapping elementMapping) {
super(targetType, openArrayType);
this.elementMapping = elementMapping;
/* Determine the concrete class to be used when converting
back to this Java type. We convert all Lists to ArrayList
and all Sets to TreeSet. (TreeSet because it is a SortedSet,
so works for both Set and SortedSet.) */
Type raw = ((ParameterizedType) targetType).getRawType();
Class<?> c = (Class<?>) raw;
final Class<?> collC;
if (c == List.class)
collC = ArrayList.class;
else if (c == Set.class)
collC = HashSet.class;
else if (c == SortedSet.class)
collC = TreeSet.class;
else { // can't happen
assert(false);
collC = null;
}
collectionClass = Util.cast(collC);
}
示例5: getObjectToMapMarshaller
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
private ArgumentMarshaller getObjectToMapMarshaller(Type type) {
Type localType = type;
if (localType instanceof ParameterizedType) {
localType = ((ParameterizedType) localType).getRawType();
}
if (!(localType instanceof Class)) {
throw new DynamoDbMappingException(
"Cannot convert " + type + " to a class");
}
Class<?> clazz = (Class<?>) localType;
if (StandardAnnotationMaps.of(clazz).attributeType() != DynamoDbAttributeType.M) {
throw new DynamoDbMappingException(
"Cannot marshall type " + type
+ " without a custom marshaler or @DynamoDBDocument "
+ "annotation.");
}
return new ObjectToMapMarshaller(this);
}
示例6: getCreatedClass
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
private Class<?> getCreatedClass(Creator<?> creator) {
String typeName = ((ParameterizedType) creator.getClass()
.getGenericSuperclass()).getActualTypeArguments()[0]
.getTypeName();
int typeParametersIndex = typeName.indexOf('<');
if (typeParametersIndex != -1) {
typeName = typeName.substring(0, typeParametersIndex);
}
Class<?> clazz;
try {
clazz = Class.forName(typeName);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Can not find a Class for '" + typeName + "'.", e);
}
return clazz;
}
示例7: testGetSubtype_recursiveTypeBoundInSubtypeTranslatedAsIs
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
public void testGetSubtype_recursiveTypeBoundInSubtypeTranslatedAsIs() {
class BaseWithTypeVar<T> {}
class Outer<O> {
class Sub<X> extends BaseWithTypeVar<List<X>> {}
class Sub2<Y extends Sub2<Y>> extends BaseWithTypeVar<List<Y>> {}
}
ParameterizedType subtype = (ParameterizedType) new TypeToken<BaseWithTypeVar<List<?>>>() {}
.getSubtype(Outer.Sub.class)
.getType();
assertEquals(Outer.Sub.class, subtype.getRawType());
assertThat(subtype.getActualTypeArguments()[0]).isInstanceOf(WildcardType.class);
ParameterizedType owner = (ParameterizedType) subtype.getOwnerType();
assertEquals(Outer.class, owner.getRawType());
// This returns a strange ? extends Sub2<Y> type, which isn't ideal.
TypeToken<?> unused = new TypeToken<BaseWithTypeVar<List<?>>>() {}.getSubtype(Outer.Sub2.class);
}
示例8: convertToCustomClass
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
/**
* Converts a standard library Java representation of JSON data to an object of the class provided
* through the GenericTypeIndicator
*
* @param object The representation of the JSON data
* @param typeIndicator The indicator providing class of the object to convert to
* @return The POJO object.
*/
public static <T> T convertToCustomClass(Object object, GenericTypeIndicator<T> typeIndicator) {
Class<?> clazz = typeIndicator.getClass();
Type genericTypeIndicatorType = clazz.getGenericSuperclass();
if (genericTypeIndicatorType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericTypeIndicatorType;
if (!parameterizedType.getRawType().equals(GenericTypeIndicator.class)) {
throw new DatabaseException(
"Not a direct subclass of GenericTypeIndicator: " + genericTypeIndicatorType);
}
// We are guaranteed to have exactly one type parameter
Type type = parameterizedType.getActualTypeArguments()[0];
return deserializeToType(object, type);
} else {
throw new DatabaseException(
"Not a direct subclass of GenericTypeIndicator: " + genericTypeIndicatorType);
}
}
示例9: setField
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
private <T> void setField(final T resultObject, final Field field, final Object unmarshalledValue)
throws IllegalAccessException, IOException {
final Class<?> fieldType = field.getType();
final Type genericType = field.getGenericType();
if (fieldType.isPrimitive()) {
reflectionUtil.setField(resultObject, field, unmarshalledValue);
} else if (genericType instanceof ParameterizedType) {
final Class<?>[] actualTypeArguments = reflectionUtil.geClassArgumentsFromGeneric(genericType);
final JavaType typedField;
if (actualTypeArguments.length == 1) {
typedField = objectMapper.getTypeFactory().constructParametricType(fieldType, actualTypeArguments[0]);
} else {
typedField = objectMapper.getTypeFactory()
.constructMapLikeType(fieldType, actualTypeArguments[0], actualTypeArguments[1]);
}
reflectionUtil.setField(resultObject, field, objectMapper.convertValue(unmarshalledValue, typedField));
} else {
reflectionUtil.setField(resultObject, field, objectMapper.convertValue(unmarshalledValue, fieldType));
}
}
示例10: resolveTypeVariable
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
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;
}
示例11: getMapKeyAndValueTypes
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
/**
* Returns a two element array containing this map's key and value types in
* positions 0 and 1 respectively.
*/
public static Type[] getMapKeyAndValueTypes(Type context, Class<?> contextRawType) {
/*
* Work around a problem with the declaration of java.util.Properties. That
* class should extend Hashtable<String, String>, but it's declared to
* extend Hashtable<Object, Object>.
*/
if (context == Properties.class) {
return new Type[] { String.class, String.class }; // TODO: test subclasses of Properties!
}
Type mapType = getSupertype(context, contextRawType, Map.class);
// TODO: strip wildcards?
if (mapType instanceof ParameterizedType) {
ParameterizedType mapParameterizedType = (ParameterizedType) mapType;
return mapParameterizedType.getActualTypeArguments();
}
return new Type[] { Object.class, Object.class };
}
示例12: getTypeParameter
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
private static TypeResult getTypeParameter(Class<?> clazz, Type argType) {
if (argType instanceof Class<?>) {
return new TypeResult((Class<?>) argType, -1, 0);
} else if (argType instanceof ParameterizedType) {
return new TypeResult((Class<?>) ((ParameterizedType) argType).getRawType(), -1, 0);
} else if (argType instanceof GenericArrayType) {
Type arrayElementType = ((GenericArrayType) argType).getGenericComponentType();
TypeResult result = getTypeParameter(clazz, arrayElementType);
result.incrementDimension(1);
return result;
} else {
TypeVariable<?>[] tvs = clazz.getTypeParameters();
for (int i = 0; i < tvs.length; i++) {
if (tvs[i].equals(argType)) {
return new TypeResult(null, i, 0);
}
}
return null;
}
}
示例13: getClassGenricType
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
/**
* 通过反射, 获得Class定义中声明的父类的泛型参数的类型.
* 如无法找到, 返回Object.class.
*
* 如public UserDao extends HibernateDao<User,Long>
*
* @param clazz clazz The class to introspect
* @param index the Index of the generic ddeclaration,start from 0.
* @return the index generic declaration, or Object.class if cannot be determined
*/
public static Class getClassGenricType(final Class clazz, final int index) {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
return Object.class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= params.length || index < 0) {
logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
+ params.length);
return Object.class;
}
if (!(params[index] instanceof Class)) {
logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
return Object.class;
}
return (Class) params[index];
}
示例14: ArrayListTypeFieldDeserializer
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
public ArrayListTypeFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo){
super(clazz, fieldInfo);
Type fieldType = fieldInfo.fieldType;
if (fieldType instanceof ParameterizedType) {
Type argType = ((ParameterizedType) fieldInfo.fieldType).getActualTypeArguments()[0];
if (argType instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) argType;
Type[] upperBounds = wildcardType.getUpperBounds();
if (upperBounds.length == 1) {
argType = upperBounds[0];
}
}
this.itemType = argType;
} else {
this.itemType = Object.class;
}
}
示例15: isTypeMatch
import java.lang.reflect.ParameterizedType; //导入依赖的package包/类
private boolean isTypeMatch(Type type, Type targetType) {
if (type instanceof Class && targetType instanceof Class) {
if (((Class) type).isAssignableFrom((Class) targetType)) {
return true;
}
} else if (type instanceof ParameterizedType && targetType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
ParameterizedType parameterizedTargetType = (ParameterizedType) targetType;
if (isTypeMatch(parameterizedType.getRawType(), ((ParameterizedType) targetType).getRawType())) {
Type[] types = parameterizedType.getActualTypeArguments();
Type[] targetTypes = parameterizedTargetType.getActualTypeArguments();
if (types == null || targetTypes == null || types.length != targetTypes.length) {
return false;
}
int len = types.length;
for (int i = 0; i < len; i++) {
if (!isTypeMatch(types[i], targetTypes[i])) {
return false;
}
}
return true;
}
}
return false;
}