Java Code Examples for java.lang.reflect.GenericArrayType#getGenericComponentType()

The following examples show how to use java.lang.reflect.GenericArrayType#getGenericComponentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeToken.java    From GVGAI_GYM with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: TypeToken.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: TypeInfo.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
 
Example 4
Source File: Jaxb2Marshaller.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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;
}
 
Example 5
Source File: TypeToken.java    From GVGAI_GYM with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: JAXBSchemaInitializer.java    From cxf with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 7
Source File: TypeInfo.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
 
Example 8
Source File: GenericTypeResolver.java    From geowave with Apache License 2.0 6 votes vote down vote up
/** 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);
}
 
Example 9
Source File: TestPlainArrayNotGeneric.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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() + ")");
    }
}
 
Example 10
Source File: PropertyType.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
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;
    }
 
Example 11
Source File: TestPlainArrayNotGeneric.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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() + ")");
    }
}
 
Example 12
Source File: ArrayConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 13
Source File: GenericArrayTypeImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 14
Source File: ReflectionNavigator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public Type onGenericArray(GenericArrayType g, BinderArg types) {
    Type c = visit(g.getGenericComponentType(), types);
    if (c == g.getGenericComponentType()) {
        return g;
    }

    return new GenericArrayTypeImpl(c);
}
 
Example 15
Source File: TestPlainArrayNotGeneric.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
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() + ")");
    }
}
 
Example 16
Source File: ReflectionNavigator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public Type onGenericArray(GenericArrayType g, BinderArg types) {
    Type c = visit(g.getGenericComponentType(), types);
    if (c == g.getGenericComponentType()) {
        return g;
    }

    return new GenericArrayTypeImpl(c);
}
 
Example 17
Source File: TestPlainArrayNotGeneric.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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() + ")");
    }
}
 
Example 18
Source File: ReflectionNavigator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public Type onGenericArray(GenericArrayType g, BinderArg types) {
    Type c = visit(g.getGenericComponentType(), types);
    if (c == g.getGenericComponentType()) {
        return g;
    }

    return new GenericArrayTypeImpl(c);
}
 
Example 19
Source File: PropertyType.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 20
Source File: EmbedListDecoder.java    From bugu-mongo with Apache License 2.0 4 votes vote down vote up
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;
}