use of java.lang.reflect.WildcardType in project fastjson by alibaba.
the class TypeUtils method getCollectionItemType.
public static Type getCollectionItemType(Type fieldType) {
Type itemType = null;
Class<?> clazz = null;
if (fieldType instanceof ParameterizedType) {
Type actualTypeArgument = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
if (actualTypeArgument instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) actualTypeArgument;
Type[] upperBounds = wildcardType.getUpperBounds();
if (upperBounds.length == 1) {
actualTypeArgument = upperBounds[0];
}
}
itemType = actualTypeArgument;
} else if (//
fieldType instanceof Class<?> && !(clazz = (Class<?>) fieldType).getName().startsWith("java.")) {
Type superClass = clazz.getGenericSuperclass();
itemType = TypeUtils.getCollectionItemType(superClass);
}
if (itemType == null) {
itemType = Object.class;
}
return itemType;
}
use of java.lang.reflect.WildcardType in project aries by apache.
the class ReferenceDependency method getBindType.
private BindType getBindType(Type type) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = cast(type);
Type rawType = parameterizedType.getRawType();
if (Instance.class.isAssignableFrom(cast(rawType))) {
_instance = true;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
return getBindType(actualTypeArguments[0]);
} else if (Map.class.isAssignableFrom(cast(rawType))) {
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
Type first = actualTypeArguments[0];
Type second = actualTypeArguments[1];
if (!(first instanceof ParameterizedType) && String.class.isAssignableFrom(cast(first))) {
if ((!(second instanceof ParameterizedType) && (second == Object.class)) || (second instanceof WildcardType)) {
return BindType.SERVICE_PROPERTIES;
}
}
return BindType.SERVICE;
} else if (ServiceReference.class.isAssignableFrom(cast(rawType))) {
return BindType.SERVICE_REFERENCE;
}
return BindType.SERVICE;
} else if (ServiceReference.class.isAssignableFrom(cast(type))) {
return BindType.SERVICE_REFERENCE;
}
return BindType.SERVICE;
}
use of java.lang.reflect.WildcardType 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.WildcardType in project spock by spockframework.
the class GenericTypeReflector method capture.
/**
* Applies capture conversion to the given type.
*/
public static Type capture(Type type) {
VarMap varMap = new VarMap();
List<CaptureTypeImpl> toInit = new ArrayList<CaptureTypeImpl>();
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Class<?> clazz = (Class<?>) pType.getRawType();
Type[] arguments = pType.getActualTypeArguments();
TypeVariable<?>[] vars = clazz.getTypeParameters();
Type[] capturedArguments = new Type[arguments.length];
assert arguments.length == vars.length;
for (int i = 0; i < arguments.length; i++) {
Type argument = arguments[i];
if (argument instanceof WildcardType) {
CaptureTypeImpl captured = new CaptureTypeImpl((WildcardType) argument, vars[i]);
argument = captured;
toInit.add(captured);
}
capturedArguments[i] = argument;
varMap.add(vars[i], argument);
}
for (CaptureTypeImpl captured : toInit) {
captured.init(varMap);
}
Type ownerType = (pType.getOwnerType() == null) ? null : capture(pType.getOwnerType());
return new ParameterizedTypeImpl(clazz, capturedArguments, ownerType);
} else {
return type;
}
}
use of java.lang.reflect.WildcardType in project robovm by robovm.
the class OldGenericReflectionCornerCases method testMultipleBoundedWildcardUnEquality.
@SuppressWarnings("unchecked")
public void testMultipleBoundedWildcardUnEquality() throws Exception {
Class<? extends MultipleBoundedWildcardUnEquality> clazz = MultipleBoundedWildcardUnEquality.class;
// new WildcardEquality<Object>().wildcardEquality(new Pair<String,
// Integer>());
Method method = clazz.getDeclaredMethod("multipleBoundedWildcardUnEquality", Pair.class);
TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
assertLenghtOne(typeParameters);
TypeVariable<?> typeParameter = typeParameters[0];
Type[] typeParameterBounds = typeParameter.getBounds();
assertEquals(2, typeParameterBounds.length);
assertEquals(Object.class, typeParameterBounds[0]);
assertInstanceOf(ParameterizedType.class, typeParameterBounds[1]);
ParameterizedType parameterizedType = (ParameterizedType) typeParameterBounds[1];
assertEquals(Comparable.class, parameterizedType.getRawType());
Type[] typeArguments = parameterizedType.getActualTypeArguments();
assertLenghtOne(typeArguments);
assertInstanceOf(ParameterizedType.class, typeArguments[0]);
ParameterizedType type = (ParameterizedType) typeArguments[0];
assertEquals(typeParameter, type.getActualTypeArguments()[0]);
assertEquals(MultipleBoundedWildcardUnEquality.class, type.getRawType());
Type[] parameterTypes = method.getGenericParameterTypes();
assertLenghtOne(parameterTypes);
Type parameter = parameterTypes[0];
assertInstanceOf(ParameterizedType.class, parameter);
ParameterizedType paramType = (ParameterizedType) parameter;
Type[] actualTypeArguments = paramType.getActualTypeArguments();
assertEquals(2, actualTypeArguments.length);
Type firstArgument = actualTypeArguments[0];
assertInstanceOf(WildcardType.class, firstArgument);
WildcardType firstWildcardArgument = (WildcardType) firstArgument;
Type secondArgument = actualTypeArguments[1];
assertInstanceOf(WildcardType.class, secondArgument);
WildcardType secondWildcardArgument = (WildcardType) secondArgument;
assertNotEquals(firstWildcardArgument, secondWildcardArgument);
Type[] firstWildcardArgumentUpperBounds = firstWildcardArgument.getUpperBounds();
assertLenghtOne(firstWildcardArgumentUpperBounds);
Type firstWildcardArgumentUpperBoundsType = firstWildcardArgumentUpperBounds[0];
Type[] secondWildcardArgumentLowerBounds = secondWildcardArgument.getLowerBounds();
assertLenghtOne(secondWildcardArgumentLowerBounds);
Type secondWildcardArgumentLoweroundsType = secondWildcardArgumentLowerBounds[0];
assertEquals(firstWildcardArgumentUpperBoundsType, secondWildcardArgumentLoweroundsType);
}
Aggregations