Search in sources :

Example 36 with WildcardType

use of java.lang.reflect.WildcardType in project jdbi by jdbi.

the class Types method newArrayType.

static Type newArrayType(Type componentType) {
    if (componentType instanceof WildcardType) {
        WildcardType wildcard = (WildcardType) componentType;
        Type[] lowerBounds = wildcard.getLowerBounds();
        checkArgument(lowerBounds.length <= 1, "Wildcard cannot have more than one lower bounds.");
        if (lowerBounds.length == 1) {
            return supertypeOf(newArrayType(lowerBounds[0]));
        } else {
            Type[] upperBounds = wildcard.getUpperBounds();
            checkArgument(upperBounds.length == 1, "Wildcard should have only one upper bound.");
            return subtypeOf(newArrayType(upperBounds[0]));
        }
    }
    return JavaVersion.CURRENT.newArrayType(componentType);
}
Also used : WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 37 with WildcardType

use of java.lang.reflect.WildcardType in project tikxml by Tickaroo.

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 38 with WildcardType

use of java.lang.reflect.WildcardType in project tikxml by Tickaroo.

the class Types method resolve.

static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
    // This implementation is made a little more complicated in an attempt to avoid object-creation.
    while (true) {
        if (toResolve instanceof TypeVariable) {
            TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
            toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
            if (toResolve == typeVariable)
                return toResolve;
        } else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
            Class<?> original = (Class<?>) toResolve;
            Type componentType = original.getComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : arrayOf(newComponentType);
        } else if (toResolve instanceof GenericArrayType) {
            GenericArrayType original = (GenericArrayType) toResolve;
            Type componentType = original.getGenericComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : arrayOf(newComponentType);
        } else if (toResolve instanceof ParameterizedType) {
            ParameterizedType original = (ParameterizedType) toResolve;
            Type ownerType = original.getOwnerType();
            Type newOwnerType = resolve(context, contextRawType, ownerType);
            boolean changed = newOwnerType != ownerType;
            Type[] args = original.getActualTypeArguments();
            for (int t = 0, length = args.length; t < length; t++) {
                Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
                if (resolvedTypeArgument != args[t]) {
                    if (!changed) {
                        args = args.clone();
                        changed = true;
                    }
                    args[t] = resolvedTypeArgument;
                }
            }
            return changed ? new ParameterizedTypeImpl(newOwnerType, original.getRawType(), args) : original;
        } else if (toResolve instanceof WildcardType) {
            WildcardType original = (WildcardType) toResolve;
            Type[] originalLowerBound = original.getLowerBounds();
            Type[] originalUpperBound = original.getUpperBounds();
            if (originalLowerBound.length == 1) {
                Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
                if (lowerBound != originalLowerBound[0]) {
                    return supertypeOf(lowerBound);
                }
            } else if (originalUpperBound.length == 1) {
                Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
                if (upperBound != originalUpperBound[0]) {
                    return subtypeOf(upperBound);
                }
            }
            return original;
        } else {
            return toResolve;
        }
    }
}
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 39 with WildcardType

use of java.lang.reflect.WildcardType in project core by weld.

the class Reflections method getRawType.

@SuppressWarnings("unchecked")
public static <T> Class<T> getRawType(Type type) {
    if (type instanceof Class<?>) {
        return (Class<T>) type;
    }
    if (type instanceof ParameterizedType) {
        if (((ParameterizedType) type).getRawType() instanceof Class<?>) {
            return (Class<T>) ((ParameterizedType) type).getRawType();
        }
    }
    if (type instanceof TypeVariable<?>) {
        TypeVariable<?> variable = (TypeVariable<?>) type;
        Type[] bounds = variable.getBounds();
        return getBound(bounds);
    }
    if (type instanceof WildcardType) {
        WildcardType wildcard = (WildcardType) type;
        return getBound(wildcard.getUpperBounds());
    }
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        Class<?> rawType = getRawType(genericArrayType.getGenericComponentType());
        if (rawType != null) {
            return (Class<T>) Array.newInstance(rawType, 0).getClass();
        }
    }
    return null;
}
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 40 with WildcardType

use of java.lang.reflect.WildcardType in project core by weld.

the class FastProcessAnnotatedTypeResolver method process.

private void process(ContainerLifecycleEventObserverMethod<?> observer, Type observedType) throws UnsupportedObserverMethodException {
    if (Object.class.equals(observedType)) {
        // void observe(Object event)
        catchAllObservers.add(observer);
    } else if (ProcessAnnotatedType.class.equals(observedType)) {
        // void observe(ProcessAnnotatedType event)
        catchAllObservers.add(observer);
    } else if (observedType instanceof ParameterizedType) {
        ParameterizedType type = (ParameterizedType) observedType;
        if (ProcessAnnotatedType.class.equals(type.getRawType())) {
            Type typeParameter = type.getActualTypeArguments()[0];
            if (typeParameter instanceof Class<?>) {
                this.observers.put(observer, new ExactTypePredicate(Reflections.getRawType(typeParameter)));
            } else if (typeParameter instanceof ParameterizedType) {
                /*
                     * The event type always has the form of ProcessAnnotatedType<X> where X is a raw type.
                     * Therefore, no event will ever match an observer with type ProcessAnnotatedType<Foo<Y>> no matter
                     * what Y is. This would be because primarily because parameterized are invariant. Event for an exact match
                     * of the raw type, Foo raw event type is not assignable to Foo<?> parameterized type according to CDI assignability rules.
                     */
                return;
            } else if (typeParameter instanceof WildcardType) {
                // void observe(ProcessAnnotatedType<?> event)
                WildcardType wildCard = (WildcardType) typeParameter;
                checkBounds(observer, wildCard.getUpperBounds());
                this.observers.put(observer, CompositePredicate.assignable(Types.getRawTypes(wildCard.getUpperBounds())));
            } else if (typeParameter instanceof TypeVariable<?>) {
                // <T> void observe(ProcessAnnotatedType<T> event)
                TypeVariable<?> variable = (TypeVariable<?>) typeParameter;
                checkBounds(observer, variable.getBounds());
                this.observers.put(observer, CompositePredicate.assignable(Types.getRawTypes(variable.getBounds())));
            }
        }
    } else if (observedType instanceof TypeVariable<?>) {
        defaultRules(observer, observedType);
    }
}
Also used : ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable)

Aggregations

WildcardType (java.lang.reflect.WildcardType)236 ParameterizedType (java.lang.reflect.ParameterizedType)208 Type (java.lang.reflect.Type)180 GenericArrayType (java.lang.reflect.GenericArrayType)160 TypeVariable (java.lang.reflect.TypeVariable)134 Test (org.junit.Test)24 GenericClass (org.evosuite.utils.generic.GenericClass)14 Method (java.lang.reflect.Method)10 WildcardTypeImpl (org.evosuite.utils.generic.WildcardTypeImpl)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 CaptureType (com.googlecode.gentyref.CaptureType)7 JSONException (com.alibaba.fastjson.JSONException)5 JSONType (com.alibaba.fastjson.annotation.JSONType)5 CatalogType (org.spongepowered.api.CatalogType)4 GenericDeclaration (java.lang.reflect.GenericDeclaration)3 HashSet (java.util.HashSet)3 List (java.util.List)3 MediaType (javax.ws.rs.core.MediaType)3