Search in sources :

Example 86 with WildcardType

use of java.lang.reflect.WildcardType in project stanbol by apache.

the class JerseyUtils method testType.

// /**
// * Getter for a Service from the {@link ServletContext} by using the
// * {@link Class#getName()} as key for {@link ServletContext#getAttribute(String)}.
// * In case the Service can not be found a {@link WebApplicationException} is
// * thrown with the message that the Service is currently not available.
// * @param <T> The type of the Service
// * @param service the Service interface
// * @param context the context used to search the service
// * @return the Service instance
// * @throws WebApplicationException in case the service instance was not found
// * in the parsed servlet context
// * @throws IllegalArgumentException if <code>null</code> is parsed as
// * service or context
// */
// @SuppressWarnings("unchecked")
// public static <T> T getService(Class<T> service, ServletContext context) throws WebApplicationException, IllegalArgumentException {
// if(service == null){
// throw new IllegalArgumentException("The parsed ServiceInterface MUST NOT be NULL!");
// }
// if(context == null){
// throw new IllegalArgumentException("The parsed ServletContext MUST NOT be NULL");
// }
// T serviceInstance = (T) context.getAttribute(service.getName());
// if(serviceInstance == null){
// throw new WebApplicationException(new IllegalStateException(
// "The "+service.getSimpleName()+" Service is currently not available " +
// "(full name= "+service+"| " +
// "servlet context name = "+context.getServletContextName()+")"),
// Response.Status.INTERNAL_SERVER_ERROR);
// }
// return serviceInstance;
// }
/**
 * Tests if a generic type (may be &lt;?&gt;, &lt;? extends {required}&gt;
 * or &lt;? super {required}&gt;) is compatible with the required one.
 * TODO: Should be moved to an utility class
 * @param required the required class the generic type MUST BE compatible with
 * @param genericType the required class
 * @return if the generic type is compatible with the required class
 */
public static boolean testType(Class<?> required, Type type) {
    // for the examples let assume that a Set is the raw type and the
    // requested generic type is a Representation with the following class
    // hierarchy:
    // Object
    // -> Representation
    // -> RdfRepresentation
    // -> InMemoryRepresentation
    // -> InputStream
    // -> Collection<T>
    boolean typeOK = false;
    // types.add(type);
    if (type instanceof Class<?>) {
        typeOK = required.isAssignableFrom((Class<?>) type);
        type = ((Class<?>) type).getGenericSuperclass();
    } else if (type instanceof WildcardType) {
        // In cases <? super {class}>, <? extends {class}, <?>
        WildcardType wildcardSetType = (WildcardType) type;
        if (wildcardSetType.getLowerBounds().length > 0) {
            Type lowerBound = wildcardSetType.getLowerBounds()[0];
            // OK
            // Set<? super RdfRepresentation>
            // Set<? super Representation>
            // NOT OK
            // Set<? super InputStream>
            // Set<? super Collection<Representation>>
            typeOK = lowerBound instanceof Class<?> && required.isAssignableFrom((Class<?>) lowerBound);
        } else if (wildcardSetType.getUpperBounds().length > 0) {
            Type upperBound = wildcardSetType.getUpperBounds()[0];
            // OK
            // Set<? extends Representation>
            // Set<? extends Object>
            // NOT OK
            // Set<? extends RdfRepresentation>
            // Set<? extends InputStream>
            // Set<? extends Collection<Representation>
            typeOK = upperBound instanceof Class<?> && ((Class<?>) upperBound).isAssignableFrom(required);
        } else {
            // no upper nor lower bound
            // Set<?>
            typeOK = true;
        }
    } else if (required.isArray() && type instanceof GenericArrayType) {
        // In case the required type is an array we need also to support
        // possible generic Array specifications
        GenericArrayType arrayType = (GenericArrayType) type;
        typeOK = testType(required.getComponentType(), arrayType.getGenericComponentType());
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pType = ((ParameterizedType) type);
        typeOK = pType.getRawType() instanceof Class<?> && required.isAssignableFrom((Class<?>) pType.getRawType());
        type = null;
    } else {
        // GenericArrayType but !required.isArray() -> incompatible
        // TypeVariable -> no variables define -> incompatible
        typeOK = false;
    // type = null; //end
    }
    // }
    return typeOK;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) PatternType(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint.PatternType) MediaType(javax.ws.rs.core.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 87 with WildcardType

use of java.lang.reflect.WildcardType in project QuickAndroid by ImKarl.

the class $Type$Types method resolve.

public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
    // 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 ? newParameterizedTypeWithOwner(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 88 with WildcardType

use of java.lang.reflect.WildcardType in project QuickAndroid by ImKarl.

the class $Type$Types method equals.

/**
 * Returns true if {@code a} and {@code b} are equal.
 */
public 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;
        }
        // TODO: save a .clone() call
        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 {
        // wildcard type, etc.
        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 89 with WildcardType

use of java.lang.reflect.WildcardType in project SmartAndroidSource by jaychou2012.

the class $Gson$Types method equals.

/**
 * Returns true if {@code a} and {@code b} are equal.
 */
@SuppressWarnings("unchecked")
public 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;
        }
        // TODO: save a .clone() call
        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 {
        // wildcard type, etc.
        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 90 with WildcardType

use of java.lang.reflect.WildcardType in project SmartAndroidSource by jaychou2012.

the class $Gson$Types method resolve.

@SuppressWarnings("unchecked")
public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
    // 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 ? newParameterizedTypeWithOwner(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)

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