Search in sources :

Example 96 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project moshi by square.

the class Types method equals.

/** Returns true if {@code a} and {@code b} are equal. */
static boolean equals(Type a, Type b) {
    if (a == b) {
        // Also handles (a == null && b == null).
        return true;
    } else if (a instanceof Class) {
        // Class already specifies equals().
        return a.equals(b);
    } else if (a instanceof ParameterizedType) {
        if (!(b instanceof ParameterizedType))
            return false;
        ParameterizedType pa = (ParameterizedType) a;
        ParameterizedType pb = (ParameterizedType) b;
        Type[] aTypeArguments = pa instanceof ParameterizedTypeImpl ? ((ParameterizedTypeImpl) pa).typeArguments : pa.getActualTypeArguments();
        Type[] bTypeArguments = pb instanceof ParameterizedTypeImpl ? ((ParameterizedTypeImpl) pb).typeArguments : pb.getActualTypeArguments();
        return equal(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType().equals(pb.getRawType()) && Arrays.equals(aTypeArguments, bTypeArguments);
    } else if (a instanceof GenericArrayType) {
        if (!(b instanceof GenericArrayType))
            return false;
        GenericArrayType ga = (GenericArrayType) a;
        GenericArrayType gb = (GenericArrayType) b;
        return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
    } else if (a instanceof WildcardType) {
        if (!(b instanceof WildcardType))
            return false;
        WildcardType wa = (WildcardType) a;
        WildcardType wb = (WildcardType) b;
        return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
    } else if (a instanceof TypeVariable) {
        if (!(b instanceof TypeVariable))
            return false;
        TypeVariable<?> va = (TypeVariable<?>) a;
        TypeVariable<?> vb = (TypeVariable<?>) b;
        return va.getGenericDeclaration() == vb.getGenericDeclaration() && va.getName().equals(vb.getName());
    } else {
        // This isn't a supported type.
        return false;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 97 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project retrofit by square.

the class Utils method equals.

/** Returns true if {@code a} and {@code b} are equal. */
static boolean equals(Type a, Type b) {
    if (a == b) {
        // Also handles (a == null && b == null).
        return true;
    } else if (a instanceof Class) {
        // Class already specifies equals().
        return a.equals(b);
    } else if (a instanceof ParameterizedType) {
        if (!(b instanceof ParameterizedType))
            return false;
        ParameterizedType pa = (ParameterizedType) a;
        ParameterizedType pb = (ParameterizedType) b;
        return equal(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType().equals(pb.getRawType()) && Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());
    } else if (a instanceof GenericArrayType) {
        if (!(b instanceof GenericArrayType))
            return false;
        GenericArrayType ga = (GenericArrayType) a;
        GenericArrayType gb = (GenericArrayType) b;
        return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
    } else if (a instanceof WildcardType) {
        if (!(b instanceof WildcardType))
            return false;
        WildcardType wa = (WildcardType) a;
        WildcardType wb = (WildcardType) b;
        return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
    } else if (a instanceof TypeVariable) {
        if (!(b instanceof TypeVariable))
            return false;
        TypeVariable<?> va = (TypeVariable<?>) a;
        TypeVariable<?> vb = (TypeVariable<?>) b;
        return va.getGenericDeclaration() == vb.getGenericDeclaration() && va.getName().equals(vb.getName());
    } else {
        // This isn't a type we support!
        return false;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 98 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project retrofit by square.

the class Utils method getRawType.

static Class<?> getRawType(Type type) {
    if (type == null)
        throw new NullPointerException("type == null");
    if (type instanceof Class<?>) {
        // Type is a normal class.
        return (Class<?>) type;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        // I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but
        // suspects some pathological case related to nested classes exists.
        Type rawType = parameterizedType.getRawType();
        if (!(rawType instanceof Class))
            throw new IllegalArgumentException();
        return (Class<?>) rawType;
    }
    if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        return Array.newInstance(getRawType(componentType), 0).getClass();
    }
    if (type instanceof TypeVariable) {
        // type that's more general than necessary is okay.
        return Object.class;
    }
    if (type instanceof WildcardType) {
        return getRawType(((WildcardType) type).getUpperBounds()[0]);
    }
    throw new IllegalArgumentException("Expected a Class, ParameterizedType, or " + "GenericArrayType, but <" + type + "> is of type " + type.getClass().getName());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 99 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project spring-framework by spring-projects.

the class TypeUtils method isAssignable.

/**
	 * Check if the right-hand side type may be assigned to the left-hand side
	 * type following the Java generics rules.
	 * @param lhsType the target type
	 * @param rhsType the value type that should be assigned to the target type
	 * @return true if rhs is assignable to lhs
	 */
public static boolean isAssignable(Type lhsType, Type rhsType) {
    Assert.notNull(lhsType, "Left-hand side type must not be null");
    Assert.notNull(rhsType, "Right-hand side type must not be null");
    // all types are assignable to themselves and to class Object
    if (lhsType.equals(rhsType) || Object.class == lhsType) {
        return true;
    }
    if (lhsType instanceof Class) {
        Class<?> lhsClass = (Class<?>) lhsType;
        // just comparing two classes
        if (rhsType instanceof Class) {
            return ClassUtils.isAssignable(lhsClass, (Class<?>) rhsType);
        }
        if (rhsType instanceof ParameterizedType) {
            Type rhsRaw = ((ParameterizedType) rhsType).getRawType();
            // a parameterized type is always assignable to its raw class type
            if (rhsRaw instanceof Class) {
                return ClassUtils.isAssignable(lhsClass, (Class<?>) rhsRaw);
            }
        } else if (lhsClass.isArray() && rhsType instanceof GenericArrayType) {
            Type rhsComponent = ((GenericArrayType) rhsType).getGenericComponentType();
            return isAssignable(lhsClass.getComponentType(), rhsComponent);
        }
    }
    // parameterized types are only assignable to other parameterized types and class types
    if (lhsType instanceof ParameterizedType) {
        if (rhsType instanceof Class) {
            Type lhsRaw = ((ParameterizedType) lhsType).getRawType();
            if (lhsRaw instanceof Class) {
                return ClassUtils.isAssignable((Class<?>) lhsRaw, (Class<?>) rhsType);
            }
        } else if (rhsType instanceof ParameterizedType) {
            return isAssignable((ParameterizedType) lhsType, (ParameterizedType) rhsType);
        }
    }
    if (lhsType instanceof GenericArrayType) {
        Type lhsComponent = ((GenericArrayType) lhsType).getGenericComponentType();
        if (rhsType instanceof Class) {
            Class<?> rhsClass = (Class<?>) rhsType;
            if (rhsClass.isArray()) {
                return isAssignable(lhsComponent, rhsClass.getComponentType());
            }
        } else if (rhsType instanceof GenericArrayType) {
            Type rhsComponent = ((GenericArrayType) rhsType).getGenericComponentType();
            return isAssignable(lhsComponent, rhsComponent);
        }
    }
    if (lhsType instanceof WildcardType) {
        return isAssignable((WildcardType) lhsType, rhsType);
    }
    return false;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 100 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project morphia by mongodb.

the class MappedField method discoverType.

@SuppressWarnings("unchecked")
protected void discoverType(final Mapper mapper) {
    if (genericType instanceof TypeVariable) {
        realType = extractTypeVariable((TypeVariable) genericType);
    } else if (genericType instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) genericType;
        final Type[] types = pt.getActualTypeArguments();
        realType = toClass(pt);
        for (Type type : types) {
            if (type instanceof ParameterizedType) {
                typeParameters.add(new EphemeralMappedField((ParameterizedType) type, this, mapper));
            } else {
                if (type instanceof WildcardType) {
                    type = ((WildcardType) type).getUpperBounds()[0];
                }
                typeParameters.add(new EphemeralMappedField(type, this, mapper));
            }
        }
    } else if (genericType instanceof WildcardType) {
        final WildcardType wildcardType = (WildcardType) genericType;
        final Type[] types = wildcardType.getUpperBounds();
        realType = toClass(types[0]);
    } else if (genericType instanceof Class) {
        realType = (Class) genericType;
    } else if (genericType instanceof GenericArrayType) {
        final Type genericComponentType = ((GenericArrayType) genericType).getGenericComponentType();
        if (genericComponentType instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) genericComponentType;
            realType = toClass(genericType);
            final Type[] types = pt.getActualTypeArguments();
            for (Type type : types) {
                if (type instanceof ParameterizedType) {
                    typeParameters.add(new EphemeralMappedField((ParameterizedType) type, this, mapper));
                } else {
                    if (type instanceof WildcardType) {
                        type = ((WildcardType) type).getUpperBounds()[0];
                    }
                    typeParameters.add(new EphemeralMappedField(type, this, mapper));
                }
            }
        } else {
            if (genericComponentType instanceof TypeVariable) {
                realType = toClass(genericType);
            } else {
                realType = (Class) genericComponentType;
            }
        }
    }
    if (Object.class.equals(realType) || Object[].class.equals(realType)) {
        if (LOG.isWarningEnabled()) {
            LOG.warning(format("Parameterized types are treated as untyped Objects. See field '%s' on %s", field.getName(), field.getDeclaringClass()));
        }
    }
    if (realType == null) {
        throw new MappingException(format("A type could not be found for the field %s.%s", getType(), getField()));
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) DBObject(com.mongodb.DBObject) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

GenericArrayType (java.lang.reflect.GenericArrayType)137 ParameterizedType (java.lang.reflect.ParameterizedType)131 Type (java.lang.reflect.Type)105 TypeVariable (java.lang.reflect.TypeVariable)78 WildcardType (java.lang.reflect.WildcardType)76 Field (java.lang.reflect.Field)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Method (java.lang.reflect.Method)4 Collection (java.util.Collection)4 Test (org.junit.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ElementType (java.lang.annotation.ElementType)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 GenericType (javax.ws.rs.core.GenericType)3 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)3 XmlType (javax.xml.bind.annotation.XmlType)3 Holder (javax.xml.ws.Holder)3 EqualsTester (com.google.common.testing.EqualsTester)2