Search in sources :

Example 46 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project karaf by apache.

the class GenericType method parametersOf.

static GenericType[] parametersOf(Type type) {
    if (type instanceof Class) {
        Class clazz = (Class) type;
        if (clazz.isArray()) {
            GenericType t = new GenericType(clazz.getComponentType());
            if (t.size() > 0) {
                return new GenericType[] { t };
            } else {
                return EMPTY;
            }
        } else {
            return EMPTY;
        }
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type[] parameters = pt.getActualTypeArguments();
        GenericType[] gts = new GenericType[parameters.length];
        for (int i = 0; i < gts.length; i++) {
            gts[i] = new GenericType(parameters[i]);
        }
        return gts;
    }
    if (type instanceof GenericArrayType) {
        return new GenericType[] { new GenericType(((GenericArrayType) type).getGenericComponentType()) };
    }
    throw new IllegalStateException();
}
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) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 47 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project karaf by apache.

the class GenericType method parametersOf.

static GenericType[] parametersOf(Type type) {
    if (type instanceof Class) {
        Class clazz = (Class) type;
        if (clazz.isArray()) {
            GenericType t = new GenericType(clazz.getComponentType());
            if (t.size() > 0) {
                return new GenericType[] { t };
            } else {
                return EMPTY;
            }
        } else {
            return EMPTY;
        }
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type[] parameters = pt.getActualTypeArguments();
        GenericType[] gts = new GenericType[parameters.length];
        for (int i = 0; i < gts.length; i++) {
            gts[i] = new GenericType(parameters[i]);
        }
        return gts;
    }
    if (type instanceof GenericArrayType) {
        return new GenericType[] { new GenericType(((GenericArrayType) type).getGenericComponentType()) };
    }
    throw new IllegalStateException();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 48 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project geode by apache.

the class OpenTypeConverter method fixType.

private static Type fixType(Type t) {
    if (!(t instanceof GenericArrayType))
        return t;
    GenericArrayType gat = (GenericArrayType) t;
    Type ultimate = ultimateComponentType(gat);
    if (!(ultimate instanceof Class<?>))
        return t;
    Class<?> component = (Class<?>) fixType(gat.getGenericComponentType());
    return Array.newInstance(component, 0).getClass();
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ArrayType(javax.management.openmbean.ArrayType) CompositeType(javax.management.openmbean.CompositeType) OpenType(javax.management.openmbean.OpenType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TabularType(javax.management.openmbean.TabularType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 49 with GenericArrayType

use of java.lang.reflect.GenericArrayType 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);
    }
}
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 50 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project spock by spockframework.

the class GenericTypeReflector method isSuperType.

/**
	 * Checks if the capture of subType is a subtype of superType
	 */
public static boolean isSuperType(Type superType, Type subType) {
    if (superType instanceof ParameterizedType || superType instanceof Class || superType instanceof GenericArrayType) {
        Class<?> superClass = erase(superType);
        Type mappedSubType = getExactSuperType(capture(subType), superClass);
        if (mappedSubType == null) {
            return false;
        } else if (superType instanceof Class<?>) {
            return true;
        } else if (mappedSubType instanceof Class<?>) {
            // class has no parameters, or it's a raw type
            return true;
        } else if (mappedSubType instanceof GenericArrayType) {
            Type superComponentType = getArrayComponentType(superType);
            assert superComponentType != null;
            Type mappedSubComponentType = getArrayComponentType(mappedSubType);
            assert mappedSubComponentType != null;
            return isSuperType(superComponentType, mappedSubComponentType);
        } else {
            assert mappedSubType instanceof ParameterizedType;
            ParameterizedType pMappedSubType = (ParameterizedType) mappedSubType;
            assert pMappedSubType.getRawType() == superClass;
            ParameterizedType pSuperType = (ParameterizedType) superType;
            Type[] superTypeArgs = pSuperType.getActualTypeArguments();
            Type[] subTypeArgs = pMappedSubType.getActualTypeArguments();
            assert superTypeArgs.length == subTypeArgs.length;
            for (int i = 0; i < superTypeArgs.length; i++) {
                if (!contains(superTypeArgs[i], subTypeArgs[i])) {
                    return false;
                }
            }
            // params of the class itself match, so if the owner types are supertypes too, it's a supertype.
            return pSuperType.getOwnerType() == null || isSuperType(pSuperType.getOwnerType(), pMappedSubType.getOwnerType());
        }
    } else if (superType instanceof CaptureType) {
        if (superType.equals(subType))
            return true;
        for (Type lowerBound : ((CaptureType) superType).getLowerBounds()) {
            if (isSuperType(lowerBound, subType)) {
                return true;
            }
        }
        return false;
    } else if (superType instanceof GenericArrayType) {
        return isArraySupertype(superType, subType);
    } else {
        throw new RuntimeException("not implemented: " + superType.getClass());
    }
}
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) 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