use of java.lang.reflect.WildcardType in project logging-log4j2 by apache.
the class TypeUtil method isAssignable.
/**
* Indicates if two {@link Type}s are assignment compatible.
*
* @param lhs the left hand side to check assignability to
* @param rhs the right hand side to check assignability from
* @return {@code true} if it is legal to assign a variable of type {@code rhs} to a variable of type {@code lhs}
* @see Class#isAssignableFrom(Class)
*/
public static boolean isAssignable(final Type lhs, final Type rhs) {
Objects.requireNonNull(lhs, "No left hand side type provided");
Objects.requireNonNull(rhs, "No right hand side type provided");
if (lhs.equals(rhs)) {
return true;
}
if (Object.class.equals(lhs)) {
// everything is assignable to Object
return true;
}
// raw type on left
if (lhs instanceof Class<?>) {
final Class<?> lhsClass = (Class<?>) lhs;
if (rhs instanceof Class<?>) {
// no generics involved
final Class<?> rhsClass = (Class<?>) rhs;
// possible for primitive types here
return isAssignable(lhsClass, rhsClass);
}
if (rhs instanceof ParameterizedType) {
return isAssignable(lhsClass, getRawType(rhs));
} else if (lhsClass.isArray() && rhs instanceof GenericArrayType) {
// check for compatible array component types
return isAssignable(lhsClass.getComponentType(), ((GenericArrayType) rhs).getGenericComponentType());
} else if (rhs instanceof TypeVariable<?>) {
for (final Type bound : ((TypeVariable<?>) rhs).getBounds()) {
if (isAssignable(lhs, bound)) {
return true;
}
}
} else {
return false;
}
}
// parameterized type on left
if (lhs instanceof ParameterizedType) {
final ParameterizedType lhsType = (ParameterizedType) lhs;
if (rhs instanceof Class<?>) {
return isAssignable(getRawType(lhs), (Class<?>) rhs);
} else if (rhs instanceof ParameterizedType) {
final ParameterizedType rhsType = (ParameterizedType) rhs;
return isParameterizedAssignable(lhsType, rhsType);
} else if (rhs instanceof TypeVariable<?>) {
for (final Type bound : ((TypeVariable<?>) rhs).getBounds()) {
if (isAssignable(lhsType, bound)) {
return true;
}
}
} else {
return false;
}
}
// generic array type on left
if (lhs instanceof GenericArrayType) {
final Type lhsComponentType = ((GenericArrayType) lhs).getGenericComponentType();
if (rhs instanceof Class<?>) {
// raw type on right
final Class<?> rhsClass = (Class<?>) rhs;
if (rhsClass.isArray()) {
return isAssignable(lhsComponentType, rhsClass.getComponentType());
}
} else if (rhs instanceof GenericArrayType) {
return isAssignable(lhsComponentType, ((GenericArrayType) rhs).getGenericComponentType());
}
}
// wildcard type on left
if (lhs instanceof WildcardType) {
return isWildcardAssignable((WildcardType) lhs, rhs);
}
// strange...
return false;
}
use of java.lang.reflect.WildcardType in project logging-log4j2 by apache.
the class TypeUtil method isParameterizedAssignable.
private static boolean isParameterizedAssignable(final ParameterizedType lhs, final ParameterizedType rhs) {
if (lhs.equals(rhs)) {
// that was easy
return true;
}
final Type[] lhsTypeArguments = lhs.getActualTypeArguments();
final Type[] rhsTypeArguments = rhs.getActualTypeArguments();
final int size = lhsTypeArguments.length;
if (rhsTypeArguments.length != size) {
// clearly incompatible types
return false;
}
for (int i = 0; i < size; i++) {
// verify all type arguments are assignable
final Type lhsArgument = lhsTypeArguments[i];
final Type rhsArgument = rhsTypeArguments[i];
if (!lhsArgument.equals(rhsArgument) && !(lhsArgument instanceof WildcardType && isWildcardAssignable((WildcardType) lhsArgument, rhsArgument))) {
return false;
}
}
return true;
}
use of java.lang.reflect.WildcardType in project groovy-core by groovy.
the class Java5 method configureTypeArguments.
private GenericsType[] configureTypeArguments(Type[] ta) {
if (ta.length == 0)
return null;
GenericsType[] gts = new GenericsType[ta.length];
for (int i = 0; i < ta.length; i++) {
ClassNode t = configureType(ta[i]);
if (ta[i] instanceof WildcardType) {
GenericsType[] gen = t.getGenericsTypes();
gts[i] = gen[0];
} else {
gts[i] = new GenericsType(t);
}
}
return gts;
}
use of java.lang.reflect.WildcardType in project beam by apache.
the class ApiSurface method addExposedTypes.
/**
* Adds any types exposed to this set. These will come from the (possibly absent) bounds on the
* wildcard.
*/
private void addExposedTypes(WildcardType type, Class<?> cause) {
visit(type);
for (Type lowerBound : type.getLowerBounds()) {
LOG.debug("Adding exposed types from {}, which is a type lower bound on wildcard type {}", lowerBound, type);
addExposedTypes(lowerBound, cause);
}
for (Type upperBound : type.getUpperBounds()) {
LOG.debug("Adding exposed types from {}, which is a type upper bound on wildcard type {}", upperBound, type);
addExposedTypes(upperBound, cause);
}
}
use of java.lang.reflect.WildcardType in project guava by google.
the class TypeTokenResolutionTest method testWithGenericUpperBoundInWildcard.
public void testWithGenericUpperBoundInWildcard() throws Exception {
WildcardType wildcardType = (WildcardType) new WithGenericBound<String>() {
}.getTargetType("withWildcardUpperBound");
assertEquals(String.class, wildcardType.getUpperBounds()[0]);
}
Aggregations