下面列出了java.lang.reflect.GenericArrayType#getGenericComponentType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static Class<?> getArrayComponentType(Type cls) {
if (cls instanceof Class) {
if (((Class<?>)cls).isArray()) {
return ((Class<?>)cls).getComponentType();
}
return (Class<?>)cls;
} else if (cls instanceof ParameterizedType) {
for (Type t2 : ((ParameterizedType)cls).getActualTypeArguments()) {
return getArrayComponentType(t2);
}
} else if (cls instanceof GenericArrayType) {
GenericArrayType gt = (GenericArrayType)cls;
Class<?> ct = (Class<?>) gt.getGenericComponentType();
return Array.newInstance(ct, 0).getClass();
}
return null;
}
/**
* Private helper function that performs some assignability checks for
* the provided GenericArrayType.
*/
private static boolean isAssignableFrom(Type from, GenericArrayType to) {
Type toGenericComponentType = to.getGenericComponentType();
if (toGenericComponentType instanceof ParameterizedType) {
Type t = from;
if (from instanceof GenericArrayType) {
t = ((GenericArrayType) from).getGenericComponentType();
} else if (from instanceof Class<?>) {
Class<?> classType = (Class<?>) from;
while (classType.isArray()) {
classType = classType.getComponentType();
}
t = classType;
}
return isAssignableFrom(t, (ParameterizedType) toGenericComponentType,
new HashMap<String, Type>());
}
// No generic defined on "to"; therefore, return true and let other
// checks determine assignability
return true;
}
public TypeInfo getItemType() {
if (type instanceof Class && ((Class)type).isArray() && !byte[].class.equals(type)) {
Type componentType = ((Class)type).getComponentType();
Type genericComponentType = null;
if (genericType!= null && genericType instanceof GenericArrayType) {
GenericArrayType arrayType = (GenericArrayType) type;
genericComponentType = arrayType.getGenericComponentType();
componentType = arrayType.getGenericComponentType();
}
TypeInfo ti =new TypeInfo(tagName, componentType, annotations);
if (genericComponentType != null) ti.setGenericType(genericComponentType);
for(Annotation anno : annotations) if (anno instanceof XmlElementWrapper) ti.wrapperType = this;
return ti;
}
// if (type instanceof Class && java.util.Collection.class.isAssignableFrom((Class)type)) {
Type t = (genericType != null)? genericType : type;
Type base = Utils.REFLECTION_NAVIGATOR.getBaseClass(t, Collection.class);
if ( base != null) {
return new TypeInfo(tagName, Utils.REFLECTION_NAVIGATOR.getTypeArgument(base,0), annotations);
}
return null;
}
@Override
public boolean supports(Type genericType) {
if (genericType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericType;
if (JAXBElement.class == parameterizedType.getRawType() &&
parameterizedType.getActualTypeArguments().length == 1) {
Type typeArgument = parameterizedType.getActualTypeArguments()[0];
if (typeArgument instanceof Class) {
Class<?> classArgument = (Class<?>) typeArgument;
return ((classArgument.isArray() && Byte.TYPE == classArgument.getComponentType()) ||
isPrimitiveWrapper(classArgument) || isStandardClass(classArgument) ||
supportsInternal(classArgument, false));
}
else if (typeArgument instanceof GenericArrayType) {
GenericArrayType arrayType = (GenericArrayType) typeArgument;
return (Byte.TYPE == arrayType.getGenericComponentType());
}
}
}
else if (genericType instanceof Class) {
Class<?> clazz = (Class<?>) genericType;
return supportsInternal(clazz, this.checkForXmlRootElement);
}
return false;
}
public TypeInfo getItemType() {
// System.out.println("????? TypeInfo " + type);
if (type instanceof Class && ((Class)type).isArray() && !byte[].class.equals(type)) {
Type componentType = ((Class)type).getComponentType();
Type genericComponentType = null;
if (genericType!= null && genericType instanceof GenericArrayType) {
GenericArrayType arrayType = (GenericArrayType) type;
genericComponentType = arrayType.getGenericComponentType();
componentType = arrayType.getGenericComponentType();
}
TypeInfo ti =new TypeInfo(tagName, componentType, annotations);
if (genericComponentType != null) ti.setGenericType(genericComponentType);
return ti;
}
// if (type instanceof Class && java.util.Collection.class.isAssignableFrom((Class)type)) {
Type t = (genericType != null)? genericType : type;
Type base = Utils.REFLECTION_NAVIGATOR.getBaseClass(t, Collection.class);
if ( base != null) {
return new TypeInfo(tagName, Utils.REFLECTION_NAVIGATOR.getTypeArgument(base,0), annotations);
}
return null;
}
/** Extract a class instance from given Type. */
private static Class<?> extractClass(final Class<?> ownerClass, Type arg) {
if (arg instanceof ParameterizedType) {
return extractClass(ownerClass, ((ParameterizedType) arg).getRawType());
} else if (arg instanceof GenericArrayType) {
final GenericArrayType gat = (GenericArrayType) arg;
final Type gt = gat.getGenericComponentType();
final Class<?> componentClass = extractClass(ownerClass, gt);
return Array.newInstance(componentClass, 0).getClass();
} else if (arg instanceof TypeVariable) {
final TypeVariable<?> tv = (TypeVariable<?>) arg;
arg = getTypeVariableMap(ownerClass).get(tv);
if (arg == null) {
arg = extractBoundForTypeVariable(tv);
} else {
arg = extractClass(ownerClass, arg);
}
}
return (arg instanceof Class ? (Class<?>) arg : Object.class);
}
/**
* Private helper function that performs some assignability checks for
* the provided GenericArrayType.
*/
private static boolean isAssignableFrom(Type from, GenericArrayType to) {
Type toGenericComponentType = to.getGenericComponentType();
if (toGenericComponentType instanceof ParameterizedType) {
Type t = from;
if (from instanceof GenericArrayType) {
t = ((GenericArrayType) from).getGenericComponentType();
} else if (from instanceof Class<?>) {
Class<?> classType = (Class<?>) from;
while (classType.isArray()) {
classType = classType.getComponentType();
}
t = classType;
}
return isAssignableFrom(t, (ParameterizedType) toGenericComponentType,
new HashMap<String, Type>());
}
// No generic defined on "to"; therefore, return true and let other
// checks determine assignability
return true;
}
/**
* Private helper function that performs some assignability checks for
* the provided GenericArrayType.
*/
private static boolean isAssignableFrom(Type from, GenericArrayType to) {
Type toGenericComponentType = to.getGenericComponentType();
if (toGenericComponentType instanceof ParameterizedType) {
Type t = from;
if (from instanceof GenericArrayType) {
t = ((GenericArrayType) from).getGenericComponentType();
} else if (from instanceof Class<?>) {
Class<?> classType = (Class<?>) from;
while (classType.isArray()) {
classType = classType.getComponentType();
}
t = classType;
}
return isAssignableFrom(t, (ParameterizedType) toGenericComponentType,
new HashMap<String, Type>());
}
// No generic defined on "to"; therefore, return true and let other
// checks determine assignability
return true;
}
public Type onGenericArray(GenericArrayType g, BinderArg types) {
Type c = visit(g.getGenericComponentType(), types);
if (c == g.getGenericComponentType()) {
return g;
}
return new GenericArrayTypeImpl(c);
}
private static void check2(Type t, String what) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
check(pt.getActualTypeArguments(), "type argument", what);
} else if (t instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) t;
check(tv.getBounds(), "bound", what);
GenericDeclaration gd = tv.getGenericDeclaration();
if (gd instanceof Type)
check((Type) gd, "declaration containing " + what);
} else if (t instanceof WildcardType) {
WildcardType wt = (WildcardType) t;
check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
} else if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
check(c.getGenericInterfaces(), "superinterface", c.toString());
check(c.getGenericSuperclass(), "superclass of " + c);
check(c.getTypeParameters(), "type parameter", c.toString());
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;
Type comp = gat.getGenericComponentType();
if (comp instanceof Class) {
fail("Type " + t + " uses GenericArrayType when plain " +
"array would do, in " + what);
} else
check(comp, "component type of " + what);
} else {
fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
}
}
public Type onGenericArray(GenericArrayType g, BinderArg types) {
Type c = visit(g.getGenericComponentType(), types);
if (c == g.getGenericComponentType()) {
return g;
}
return new GenericArrayTypeImpl(c);
}
private static void check2(Type t, String what) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
check(pt.getActualTypeArguments(), "type argument", what);
} else if (t instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) t;
check(tv.getBounds(), "bound", what);
GenericDeclaration gd = tv.getGenericDeclaration();
if (gd instanceof Type)
check((Type) gd, "declaration containing " + what);
} else if (t instanceof WildcardType) {
WildcardType wt = (WildcardType) t;
check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
} else if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
check(c.getGenericInterfaces(), "superinterface", c.toString());
check(c.getGenericSuperclass(), "superclass of " + c);
check(c.getTypeParameters(), "type parameter", c.toString());
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;
Type comp = gat.getGenericComponentType();
if (comp instanceof Class) {
fail("Type " + t + " uses GenericArrayType when plain " +
"array would do, in " + what);
} else
check(comp, "component type of " + what);
} else {
fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
}
}
private static void check2(Type t, String what) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
check(pt.getActualTypeArguments(), "type argument", what);
} else if (t instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) t;
check(tv.getBounds(), "bound", what);
GenericDeclaration gd = tv.getGenericDeclaration();
if (gd instanceof Type)
check((Type) gd, "declaration containing " + what);
} else if (t instanceof WildcardType) {
WildcardType wt = (WildcardType) t;
check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
} else if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
check(c.getGenericInterfaces(), "superinterface", c.toString());
check(c.getGenericSuperclass(), "superclass of " + c);
check(c.getTypeParameters(), "type parameter", c.toString());
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;
Type comp = gat.getGenericComponentType();
if (comp instanceof Class) {
fail("Type " + t + " uses GenericArrayType when plain " +
"array would do, in " + what);
} else
check(comp, "component type of " + what);
} else {
fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
}
}
public Type onGenericArray(GenericArrayType g, BinderArg types) {
Type c = visit(g.getGenericComponentType(), types);
if (c == g.getGenericComponentType()) {
return g;
}
return new GenericArrayTypeImpl(c);
}
@Override
public boolean equals(Object o) {
if (o instanceof GenericArrayType) {
GenericArrayType that = (GenericArrayType) o;
Type thatComponentType = that.getGenericComponentType();
return genericComponentType.equals(thatComponentType);
} else
return false;
}
@Override
public Object readValueFrom(CsvReader context, Type type) throws Exception {
// TODO 处理null
Iterator<String> in = context.getInputStream();
String check = in.next();
if (StringUtility.isEmpty(check)) {
return null;
}
int length = Integer.valueOf(check);
Class<?> componentClass = null;
Type componentType = null;
if (type instanceof GenericArrayType) {
GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
componentType = genericArrayType.getGenericComponentType();
componentClass = TypeUtility.getRawType(componentType, null);
} else {
Class<?> clazz = TypeUtility.getRawType(type, null);
componentType = clazz.getComponentType();
componentClass = clazz.getComponentType();
}
Object array = Array.newInstance(componentClass, length);
Specification specification = Specification.getSpecification(componentClass);
CsvConverter converter = context.getCsvConverter(specification);
for (int index = 0; index < length; index++) {
Object element = converter.readValueFrom(context, componentType);
Array.set(array, index, element);
}
return array;
}
private static void check2(Type t, String what) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
check(pt.getActualTypeArguments(), "type argument", what);
} else if (t instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) t;
check(tv.getBounds(), "bound", what);
GenericDeclaration gd = tv.getGenericDeclaration();
if (gd instanceof Type)
check((Type) gd, "declaration containing " + what);
} else if (t instanceof WildcardType) {
WildcardType wt = (WildcardType) t;
check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
} else if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
check(c.getGenericInterfaces(), "superinterface", c.toString());
check(c.getGenericSuperclass(), "superclass of " + c);
check(c.getTypeParameters(), "type parameter", c.toString());
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;
Type comp = gat.getGenericComponentType();
if (comp instanceof Class) {
fail("Type " + t + " uses GenericArrayType when plain " +
"array would do, in " + what);
} else
check(comp, "component type of " + what);
} else {
fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
}
}
public int arrayDimension() {
int result = 0;
Type t = gType;
while (true) {
if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
if (c.isArray()) {
t = c.getComponentType();
result++;
} else if (List.class.equals(c)) {
// Raw list type!
result++;
break;
} else {
break;
}
} else if (t instanceof GenericArrayType) {
GenericArrayType g = (GenericArrayType) t;
t = g.getGenericComponentType();
result++;
} else if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
if (List.class.equals(pt.getRawType())) {
t = pt.getActualTypeArguments()[0];
result++;
} else {
break;
}
} else if (t instanceof WildcardType) {
WildcardType wt = (WildcardType) t;
t = wt.getUpperBounds()[0];
} else {
break;
}
}
return result;
}
private Map decodeMap(){
//for Map<K,V>, first to check the type of V
ParameterizedType paramType = (ParameterizedType)field.getGenericType();
Type[] types = paramType.getActualTypeArguments();
boolean isArray = false;
boolean isCollection = false;
boolean isSingle = false;
Class vType = null;
Class elementType = null;
//in JDK6, type[1] of array, is instanceof GenericArrayType
if(types[1] instanceof GenericArrayType){
isArray = true;
GenericArrayType g = (GenericArrayType)types[1];
elementType = (Class)g.getGenericComponentType();
}else if(types[1] instanceof ParameterizedType){
isCollection = true;
ParameterizedType p = (ParameterizedType)types[1];
vType = (Class)p.getRawType();
elementType = (Class)p.getActualTypeArguments()[0];
}else{
//in JDK8, type[1] of array, is a class, not array
Class<?> actualType = FieldUtil.getClassOfType(types[1]);
if(actualType.isArray()){
isArray = true;
elementType = actualType.getComponentType();
}else{
isSingle = true;
}
}
//decode value by different type of V
Map map = (Map)value;
Map result = new HashMap();
for(Object key : map.keySet()){
Object entryValue = map.get(key);
if(entryValue == null){
result.put(key, null);
continue;
}
if(isSingle){
Object embedObj = MapperUtil.fromDBObject((Class)types[1], (DBObject)entryValue);
result.put(key, embedObj);
}else if(isArray){
Object arr = decodeArray(entryValue, elementType);
result.put(key, arr);
}else if(isCollection){
List list = decodeCollection(entryValue, elementType);
if(DataType.isListType(vType) || DataType.isCollectionType(vType)){
result.put(key, list);
}
else if(DataType.isSetType(vType)){
result.put(key, new HashSet(list));
}
else if(DataType.isQueueType(vType)){
result.put(key, new LinkedList(list));
}
}
}
return result;
}
public Class<?> arrayBase() {
if (!isArray()) {
return null;
}
Class<?> result;
Type t = gType;
while (true) {
if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
if (c.isArray()) {
t = c.getComponentType();
} else if (List.class.equals(c)) {
// Raw list type!
result = Object.class;
break;
} else {
result = c;
break;
}
} else if (t instanceof GenericArrayType) {
GenericArrayType g = (GenericArrayType) t;
t = g.getGenericComponentType();
} else if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
if (List.class.equals(pt.getRawType())) {
t = pt.getActualTypeArguments()[0];
} else {
Type rt = pt.getRawType();
if (rt instanceof Class<?>) {
result = (Class<?>) rt;
break;
} else if (rt instanceof TypeVariable) {
t = ((TypeVariable) rt).getBounds()[0];
} else if (rt instanceof WildcardType) {
t = ((WildcardType) rt).getUpperBounds()[0];
} else {
result = null;
break;
}
}
} else if (t instanceof TypeVariable) {
Type type = ((TypeVariable) t).getBounds()[0];
if (type instanceof Class<?>) {
result = (Class<?>) type;
break;
} else if (type instanceof TypeVariable<?>) {
t = type;
}
} else if (t instanceof WildcardType) {
t = ((WildcardType) t).getUpperBounds()[0];
} else {
result = null;
break;
}
}
return result;
}