use of java.lang.reflect.GenericArrayType in project karaf by apache.
the class GenericType method parametersOf.
static GenericType[] parametersOf(Type type) {
if (type instanceof Class) {
Class clazz = (Class) type;
if (clazz.isArray()) {
GenericType t = new GenericType(clazz.getComponentType());
if (t.size() > 0) {
return new GenericType[] { t };
} else {
return EMPTY;
}
} else {
return EMPTY;
}
}
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type[] parameters = pt.getActualTypeArguments();
GenericType[] gts = new GenericType[parameters.length];
for (int i = 0; i < gts.length; i++) {
gts[i] = new GenericType(parameters[i]);
}
return gts;
}
if (type instanceof GenericArrayType) {
return new GenericType[] { new GenericType(((GenericArrayType) type).getGenericComponentType()) };
}
throw new IllegalStateException();
}
use of java.lang.reflect.GenericArrayType in project karaf by apache.
the class GenericType method parametersOf.
static GenericType[] parametersOf(Type type) {
if (type instanceof Class) {
Class clazz = (Class) type;
if (clazz.isArray()) {
GenericType t = new GenericType(clazz.getComponentType());
if (t.size() > 0) {
return new GenericType[] { t };
} else {
return EMPTY;
}
} else {
return EMPTY;
}
}
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type[] parameters = pt.getActualTypeArguments();
GenericType[] gts = new GenericType[parameters.length];
for (int i = 0; i < gts.length; i++) {
gts[i] = new GenericType(parameters[i]);
}
return gts;
}
if (type instanceof GenericArrayType) {
return new GenericType[] { new GenericType(((GenericArrayType) type).getGenericComponentType()) };
}
throw new IllegalStateException();
}
use of java.lang.reflect.GenericArrayType in project geode by apache.
the class OpenTypeConverter method fixType.
private static Type fixType(Type t) {
if (!(t instanceof GenericArrayType))
return t;
GenericArrayType gat = (GenericArrayType) t;
Type ultimate = ultimateComponentType(gat);
if (!(ultimate instanceof Class<?>))
return t;
Class<?> component = (Class<?>) fixType(gat.getGenericComponentType());
return Array.newInstance(component, 0).getClass();
}
use of java.lang.reflect.GenericArrayType in project spock by spockframework.
the class GenericTypeReflector method getExactDirectSuperTypes.
/**
* Returns the direct supertypes of the given type. Resolves type parameters.
*/
private static Type[] getExactDirectSuperTypes(Type type) {
if (type instanceof ParameterizedType || type instanceof Class) {
Class<?> clazz;
if (type instanceof ParameterizedType) {
clazz = (Class<?>) ((ParameterizedType) type).getRawType();
} else {
clazz = (Class<?>) type;
if (clazz.isArray())
return getArrayExactDirectSuperTypes(clazz);
}
Type[] superInterfaces = clazz.getGenericInterfaces();
Type superClass = clazz.getGenericSuperclass();
Type[] result;
int resultIndex;
if (superClass == null) {
result = new Type[superInterfaces.length];
resultIndex = 0;
} else {
result = new Type[superInterfaces.length + 1];
resultIndex = 1;
result[0] = mapTypeParameters(superClass, type);
}
for (Type superInterface : superInterfaces) {
result[resultIndex++] = mapTypeParameters(superInterface, type);
}
return result;
} else if (type instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) type;
return tv.getBounds();
} else if (type instanceof WildcardType) {
// But it does happen if the upper bound of a type variable contains a wildcard
return ((WildcardType) type).getUpperBounds();
} else if (type instanceof CaptureType) {
return ((CaptureType) type).getUpperBounds();
} else if (type instanceof GenericArrayType) {
return getArrayExactDirectSuperTypes(type);
} else {
throw new RuntimeException("not implemented type: " + type);
}
}
use of java.lang.reflect.GenericArrayType in project spock by spockframework.
the class GenericTypeReflector method isSuperType.
/**
* Checks if the capture of subType is a subtype of superType
*/
public static boolean isSuperType(Type superType, Type subType) {
if (superType instanceof ParameterizedType || superType instanceof Class || superType instanceof GenericArrayType) {
Class<?> superClass = erase(superType);
Type mappedSubType = getExactSuperType(capture(subType), superClass);
if (mappedSubType == null) {
return false;
} else if (superType instanceof Class<?>) {
return true;
} else if (mappedSubType instanceof Class<?>) {
// class has no parameters, or it's a raw type
return true;
} else if (mappedSubType instanceof GenericArrayType) {
Type superComponentType = getArrayComponentType(superType);
assert superComponentType != null;
Type mappedSubComponentType = getArrayComponentType(mappedSubType);
assert mappedSubComponentType != null;
return isSuperType(superComponentType, mappedSubComponentType);
} else {
assert mappedSubType instanceof ParameterizedType;
ParameterizedType pMappedSubType = (ParameterizedType) mappedSubType;
assert pMappedSubType.getRawType() == superClass;
ParameterizedType pSuperType = (ParameterizedType) superType;
Type[] superTypeArgs = pSuperType.getActualTypeArguments();
Type[] subTypeArgs = pMappedSubType.getActualTypeArguments();
assert superTypeArgs.length == subTypeArgs.length;
for (int i = 0; i < superTypeArgs.length; i++) {
if (!contains(superTypeArgs[i], subTypeArgs[i])) {
return false;
}
}
// params of the class itself match, so if the owner types are supertypes too, it's a supertype.
return pSuperType.getOwnerType() == null || isSuperType(pSuperType.getOwnerType(), pMappedSubType.getOwnerType());
}
} else if (superType instanceof CaptureType) {
if (superType.equals(subType))
return true;
for (Type lowerBound : ((CaptureType) superType).getLowerBounds()) {
if (isSuperType(lowerBound, subType)) {
return true;
}
}
return false;
} else if (superType instanceof GenericArrayType) {
return isArraySupertype(superType, subType);
} else {
throw new RuntimeException("not implemented: " + superType.getClass());
}
}
Aggregations