Example usage for java.lang.reflect WildcardType getLowerBounds

List of usage examples for java.lang.reflect WildcardType getLowerBounds

Introduction

In this page you can find the example usage for java.lang.reflect WildcardType getLowerBounds.

Prototype

Type[] getLowerBounds();

Source Link

Document

Returns an array of Type objects representing the lower bound(s) of this type variable.

Usage

From source file:org.eclipse.wb.internal.swing.databinding.model.generic.GenericUtils.java

private static String resolveTypeName(Type type) {
    if (type instanceof Class<?>) {
        Class<?> rawType = (Class<?>) type;
        return convertPrimitiveType(ReflectionUtils.getFullyQualifiedName(rawType, false));
    }/*from  ww  w.j  av  a  2 s.  c o m*/
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Class<?> rawType = (Class<?>) parameterizedType.getRawType();
        StringBuffer fullName = new StringBuffer();
        fullName.append(CoreUtils.getClassName(rawType));
        fullName.append("<");
        Type[] types = parameterizedType.getActualTypeArguments();
        for (int i = 0; i < types.length; i++) {
            if (i > 0) {
                fullName.append(", ");
            }
            fullName.append(resolveTypeName(types[i]));
        }
        fullName.append(">");
        return fullName.toString();
    }
    if (type instanceof GenericArrayType) {
        StringBuffer fullName = new StringBuffer();
        Type elementType = null;
        GenericArrayType arrayType = (GenericArrayType) type;
        while (true) {
            fullName.append("[]");
            elementType = arrayType.getGenericComponentType();
            if (elementType instanceof GenericArrayType) {
                arrayType = (GenericArrayType) elementType;
                continue;
            }
            break;
        }
        fullName.insert(0, resolveTypeName(elementType));
        return fullName.toString();
    }
    if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        Type[] upperBounds = wildcardType.getUpperBounds();
        Type[] lowerBounds = wildcardType.getLowerBounds();
        if (!ArrayUtils.isEmpty(upperBounds)) {
            Type upperBound = upperBounds[0];
            boolean isWildcard = upperBound instanceof Class<?>
                    && ((Class<?>) upperBound).getName().equals("java.lang.Object");
            if (!isWildcard) {
                return "? extends " + resolveTypeName(upperBound);
            }
        } else if (!ArrayUtils.isEmpty(lowerBounds)) {
            return "? super " + resolveTypeName(lowerBounds[0]);
        }
        return "?";
    }
    if (type instanceof TypeVariable<?>) {
        return "?";
    }
    Assert.fail("Undefine type: " + type);
    return null;
}

From source file:org.evosuite.utils.generic.GenericClass.java

/**
 * Determine whether the upper and lower boundaries are satisfied by this
 * class//from w  ww .  ja  va 2 s.  c o m
 * 
 * @param wildcardType
 * @return
 */
public boolean satisfiesBoundaries(WildcardType wildcardType, Map<TypeVariable<?>, Type> typeMap) {
    boolean isAssignable = true;
    Map<TypeVariable<?>, Type> ownerVariableMap = getTypeVariableMap();
    ownerVariableMap.putAll(typeMap);

    // ? extends X
    for (Type theType : wildcardType.getUpperBounds()) {
        logger.debug("Checking upper bound " + theType);
        // Special case: Enum is defined as Enum<T extends Enum>
        if (GenericTypeReflector.erase(theType).equals(Enum.class)) {
            // if this is an enum then it's ok. 
            if (isEnum())
                continue;
            else {
                // If it's not an enum, it cannot be assignable to enum!
                isAssignable = false;
                break;
            }
        }

        Type type = GenericUtils.replaceTypeVariables(theType, ownerVariableMap);
        //logger.debug("Bound after variable replacement: " + type);
        if (!isAssignableTo(type)) {
            // If the boundary is not assignable it may still be possible 
            // to instantiate the generic to an assignable type
            if (GenericTypeReflector.erase(type).isAssignableFrom(getRawClass())) {
                Type instanceType = GenericTypeReflector.getExactSuperType(type, getRawClass());
                if (instanceType == null) {
                    // This happens when the raw class is not a supertype 
                    // of the boundary
                    isAssignable = false;
                    break;
                }

                if (GenericClass.isAssignable(type, instanceType)) {
                    logger.debug("Found assignable generic exact type: " + instanceType);
                    continue;
                }
            }
            isAssignable = false;
            break;
        }
    }

    // ? super X
    Type[] lowerBounds = wildcardType.getLowerBounds();
    if (lowerBounds != null && lowerBounds.length > 0) {
        for (Type theType : wildcardType.getLowerBounds()) {
            logger.debug("Checking lower bound " + theType);
            Type type = GenericUtils.replaceTypeVariables(theType, ownerVariableMap);
            logger.debug("Bound after variable replacement: " + type);
            logger.debug("Is assignable from " + toString() + "?");
            if (!isAssignableFrom(type)) {
                logger.debug("Not assignable from " + toString());
                // If the boundary is not assignable it may still be possible 
                // to instantiate the generic to an assignable type
                if (type instanceof WildcardType)
                    continue;
                if (GenericTypeReflector.erase(type).isAssignableFrom(getRawClass())) {
                    Type instanceType = GenericTypeReflector.getExactSuperType(type, getRawClass());
                    if (instanceType == null) {
                        // This happens when the raw class is not a supertype 
                        // of the boundary
                        isAssignable = false;
                        break;
                    }

                    if (GenericClass.isAssignable(type, instanceType)) {
                        logger.debug("Found assignable generic exact type: " + instanceType);
                        continue;
                    }
                }
                isAssignable = false;
                break;
            } else {
                logger.debug("Is assignable from " + toString());
            }
        }
    }
    return isAssignable;
}

From source file:org.evosuite.utils.generic.GenericUtils.java

public static Type replaceTypeVariable(Type targetType, TypeVariable<?> variable, Type variableType) {
    if (targetType instanceof Class<?>)
        return targetType;
    else if (targetType instanceof GenericArrayType) {
        GenericArrayType gType = (GenericArrayType) targetType;
        Type componentType = replaceTypeVariable(gType.getGenericComponentType(), variable, variableType);
        return GenericArrayTypeImpl.createArrayType(componentType);

    } else if (targetType instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) targetType;
        Type ownerType = null;//  w  ww . j  a v  a2s  . c  o  m
        if (pType.getOwnerType() != null) {
            ownerType = replaceTypeVariable(pType.getOwnerType(), variable, variableType);
        }
        Type[] originalParameterTypes = pType.getActualTypeArguments();
        Type[] parameterTypes = new Type[originalParameterTypes.length];
        for (int i = 0; i < originalParameterTypes.length; i++) {
            parameterTypes[i] = replaceTypeVariable(originalParameterTypes[i], variable, variableType);
        }

        /*
        if (variableType instanceof ParameterizedType) {
           ParameterizedType parameterizedVars = (ParameterizedType) variableType;
           Map<TypeVariable<?>, Type> subTypes = TypeUtils.getTypeArguments(parameterizedVars);
           for (Entry<TypeVariable<?>, Type> subTypeEntry : subTypes.entrySet()) {
              if (pType.getOwnerType() != null) {
          ownerType = replaceTypeVariable(pType.getOwnerType(),
                                          subTypeEntry.getKey(),
                                          subTypeEntry.getValue());
              }
              for (int i = 0; i < originalParameterTypes.length; i++) {
          parameterTypes[i] = replaceTypeVariable(originalParameterTypes[i],
                                                  subTypeEntry.getKey(),
                                                  subTypeEntry.getValue());
              }
                
           }
        }
        */

        return new ParameterizedTypeImpl((Class<?>) pType.getRawType(), parameterTypes, ownerType);

    } else if (targetType instanceof WildcardType) {
        WildcardType wType = (WildcardType) targetType;
        Type[] originalUpperBounds = wType.getUpperBounds();
        Type[] originalLowerBounds = wType.getLowerBounds();
        Type[] upperBounds = new Type[originalUpperBounds.length];
        Type[] lowerBounds = new Type[originalLowerBounds.length];

        for (int i = 0; i < originalUpperBounds.length; i++) {
            upperBounds[i] = replaceTypeVariable(originalUpperBounds[i], variable, variableType);
        }
        for (int i = 0; i < originalLowerBounds.length; i++) {
            lowerBounds[i] = replaceTypeVariable(originalLowerBounds[i], variable, variableType);
        }

        return new WildcardTypeImpl(upperBounds, lowerBounds);
    } else if (targetType instanceof TypeVariable<?>) {
        if (targetType.equals(variable)) {
            //logger.debug("Do equal: " + variable + "/" + targetType);
            return variableType;
        } else {
            //logger.debug("Do not equal: " + variable + "/" + targetType);
            //logger.debug("Do not equal: " + variable.getGenericDeclaration() + "/"
            //        + ((TypeVariable<?>) targetType).getGenericDeclaration());
            return targetType;
        }
    } else {
        //logger.debug("Unknown type of class " + targetType.getClass() + ": "
        //        + targetType);
        return targetType;
    }
}

From source file:org.evosuite.utils.GenericClass.java

/**
 * Determine whether the upper and lower boundaries are satisfied by this
 * class//  www . j a v  a2s.c o m
 * 
 * @param wildcardType
 * @return
 */
public boolean satisfiesBoundaries(WildcardType wildcardType, Map<TypeVariable<?>, Type> typeMap) {
    boolean isAssignable = true;
    Map<TypeVariable<?>, Type> ownerVariableMap = getTypeVariableMap();
    ownerVariableMap.putAll(typeMap);

    // ? extends X
    for (Type theType : wildcardType.getUpperBounds()) {
        // Special case: Enum is defined as Enum<T extends Enum>
        if (GenericTypeReflector.erase(theType).equals(Enum.class)) {
            // if this is an enum then it's ok. 
            if (isEnum())
                continue;
            else {
                // If it's not an enum, it cannot be assignable to enum!
                isAssignable = false;
                break;
            }
        }

        Type type = GenericUtils.replaceTypeVariables(theType, ownerVariableMap);
        //logger.debug("Bound after variable replacement: " + type);
        if (!isAssignableTo(type)) {
            // If the boundary is not assignable it may still be possible 
            // to instantiate the generic to an assignable type
            if (GenericTypeReflector.erase(type).isAssignableFrom(getRawClass())) {
                Type instanceType = GenericTypeReflector.getExactSuperType(type, getRawClass());
                if (instanceType == null) {
                    // This happens when the raw class is not a supertype 
                    // of the boundary
                    isAssignable = false;
                    break;
                }

                if (GenericClass.isAssignable(type, instanceType)) {
                    logger.debug("Found assignable generic exact type: {}", instanceType);
                    continue;
                }
            }
            isAssignable = false;
            break;
        }
    }

    // ? super X
    Type[] lowerBounds = wildcardType.getLowerBounds();
    if (lowerBounds != null && lowerBounds.length > 0) {
        for (Type theType : wildcardType.getLowerBounds()) {
            Type type = GenericUtils.replaceTypeVariables(theType, ownerVariableMap);
            logger.debug("Bound after variable replacement: {}", type);
            if (!isAssignableTo(type)) {
                // If the boundary is not assignable it may still be possible 
                // to instantiate the generic to an assignable type
                if (GenericTypeReflector.erase(type).isAssignableFrom(getRawClass())) {
                    Type instanceType = GenericTypeReflector.getExactSuperType(type, getRawClass());
                    if (instanceType == null) {
                        // This happens when the raw class is not a supertype 
                        // of the boundary
                        isAssignable = false;
                        break;
                    }

                    if (GenericClass.isAssignable(type, instanceType)) {
                        logger.debug("Found assignable generic exact type: {}", instanceType);
                        continue;
                    }
                }
                isAssignable = false;
                break;
            }
        }
    }
    return isAssignable;
}

From source file:org.openflexo.antar.binding.TypeUtils.java

/**
 * Return flag indicating if this type is considered as generic A generic type is a type that is parameterized with type variable(s). If
 * this type is resolved but contains a type in it definition containing itself a generic definition, then this type is also generic
 * (this 'isGeneric' property is recursively transmissible).
 * /*ww w  .j a  v  a2 s .co  m*/
 * @return a flag indicating whether this type is resolved or not
 */
public static boolean isGeneric(Type type) {
    if (type instanceof CustomType) {
        return false;
    }
    if (type instanceof Class) {
        return false;
    }
    if (type instanceof GenericArrayType) {
        return isGeneric(((GenericArrayType) type).getGenericComponentType());
    }
    if (type instanceof ParameterizedType) {
        for (Type t : ((ParameterizedType) type).getActualTypeArguments()) {
            if (isGeneric(t)) {
                return true;
            }
        }
        return false;
    }
    if (type instanceof TypeVariable) {
        return true;
    }
    if (type instanceof WildcardType) {
        WildcardType w = (WildcardType) type;
        if (w.getUpperBounds() != null && w.getUpperBounds().length > 0) {
            for (Type b : w.getUpperBounds()) {
                if (isGeneric(b)) {
                    return true;
                }
            }
        }
        if (w.getLowerBounds() != null && w.getLowerBounds().length > 0) {
            for (Type b : w.getLowerBounds()) {
                if (isGeneric(b)) {
                    return true;
                }
            }
        }
        return false;
    }
    logger.warning("Unexpected " + type);
    return false;
}

From source file:org.openflexo.antar.binding.TypeUtils.java

/**
 * Build instanciated DMType considering supplied type is generic (contains TypeVariable definitions) Returns a clone of DMType where
 * all references to TypeVariable are replaced by values defined in context type. For example, given type=Enumeration<E> and
 * context=Vector<String>, returns Enumeration<String> If supplied type is not generic, return type value (without cloning!)
 * //from  w w  w .java 2 s  .c  om
 * @param type
 *            : type to instanciate
 * @param context
 *            : context used to instanciate type
 * @return
 */
public static Type makeInstantiatedType(Type type, Type context) {
    if (type == null) {
        return null;
    }

    if (!isGeneric(type)) {
        return type;
    }

    if (type instanceof ParameterizedType) {
        Type[] actualTypeArguments = new Type[((ParameterizedType) type).getActualTypeArguments().length];
        for (int i = 0; i < ((ParameterizedType) type).getActualTypeArguments().length; i++) {
            actualTypeArguments[i] = makeInstantiatedType(
                    ((ParameterizedType) type).getActualTypeArguments()[i], context);
        }
        return new ParameterizedTypeImpl((Class) ((ParameterizedType) type).getRawType(), actualTypeArguments);
    }

    if (type instanceof GenericArrayType) {
        return new GenericArrayTypeImpl(
                makeInstantiatedType(((GenericArrayType) type).getGenericComponentType(), context));
    }

    if (type instanceof TypeVariable) {
        TypeVariable<GenericDeclaration> tv = (TypeVariable<GenericDeclaration>) type;
        GenericDeclaration gd = tv.getGenericDeclaration();
        // System.out.println("Found type variable "+tv+" name="+tv.getName()+" GD="+tv.getGenericDeclaration());
        if (gd instanceof Class) {
            if (context instanceof ParameterizedType) {
                for (int i = 0; i < gd.getTypeParameters().length; i++) {
                    if (gd.getTypeParameters()[i].equals(tv)) {
                        // Found matching parameterized type
                        if (i < ((ParameterizedType) context).getActualTypeArguments().length) {
                            // logger.info("********* return instantiatedType for "+type+" context="+context+" gd="+gd);
                            if (!((ParameterizedType) context).getRawType().equals(gd)) {
                                return makeInstantiatedType(type, getSuperType(context));
                            }
                            return ((ParameterizedType) context).getActualTypeArguments()[i];
                        } else {
                            logger.warning("Could not retrieve parameterized type " + tv + " with context "
                                    + simpleRepresentation(context));
                            return type;
                        }
                    }
                }
            } else if (context instanceof Class && ((Class) context).getGenericSuperclass() != null) {
                return makeInstantiatedType(type, ((Class) context).getGenericSuperclass());
            }
        } else if (gd instanceof Method) {
            return type;
        }
        logger.warning("Not found type variable " + tv + " in context " + context + " GenericDeclaration="
                + tv.getGenericDeclaration());
        // throw new InvalidKeyValuePropertyException("Not found type variable "+tv+" in context "+context);
        return type;
    }

    if (type instanceof WildcardType) {
        WildcardType wt = (WildcardType) type;
        Type[] upperBounds = new Type[wt.getUpperBounds().length];
        for (int i = 0; i < wt.getUpperBounds().length; i++) {
            upperBounds[i] = makeInstantiatedType(wt.getUpperBounds()[i], context);
        }
        Type[] lowerBounds = new Type[wt.getLowerBounds().length];
        for (int i = 0; i < wt.getLowerBounds().length; i++) {
            lowerBounds[i] = makeInstantiatedType(wt.getLowerBounds()[i], context);
        }
        return new WilcardTypeImpl(upperBounds, lowerBounds);
    }

    logger.warning("Unexpected " + type);
    return type;

}

From source file:org.soybeanMilk.core.bean.DefaultGenericConverter.java

/**
 * ?WildcardType???//w  w w .  j  ava2  s.c o  m
 * @param type
 * @return
 * @date 2012-5-20
 */
protected boolean isSimpleWildcardType(Type type) {
    if (!(type instanceof WildcardType))
        return false;

    WildcardType wt = (WildcardType) type;

    Type[] lb = wt.getLowerBounds();
    Type[] ub = wt.getUpperBounds();

    if ((lb == null || lb.length == 0) && (ub == null || ub.length == 0))
        return true;
    else
        return false;
}

From source file:org.tdar.core.service.ReflectionService.java

/**
 * Get the Class of the return type/or generic type
 * /* w  ww .  ja  v a 2  s .  co m*/
 * @param type
 * @return
 */
private static Class<?> getType(Type type) {
    Logger logger = LoggerFactory.getLogger(ReflectionService.class);

    if (WildcardType.class.isAssignableFrom(type.getClass())) {
        WildcardType subType = (WildcardType) type;
        logger.trace(" wildcard type: {} [{}]", type, type.getClass());
        logger.trace(" lower: {} upper: {}", subType.getLowerBounds(), subType.getUpperBounds());
        return subType.getUpperBounds().getClass();
    }

    if (type instanceof ParameterizedType) {
        ParameterizedType collectionType = (ParameterizedType) type;
        logger.trace(" parameterized type: {} [{} - {}]", type, type.getClass(),
                collectionType.getActualTypeArguments());
        Type subtype = collectionType.getActualTypeArguments()[0];
        logger.trace(" type: {} subtype: {} ", type, subtype);
        if (subtype instanceof Type) {
            return getType(subtype);
        }
        return (Class<?>) subtype;
    }

    if (type instanceof Class<?>) {
        return (Class<?>) type;
    }
    return null;
}

From source file:org.vulpe.commons.util.VulpeReflectUtil.java

/**
 * Returns class of Type.//from w ww  . j  av  a 2s  . co  m
 *
 * @param clazz
 * @param type
 * @return
 */
public static DeclaredType getDeclaredType(final Class<?> clazz, final Type type) {
    if (type == null) {
        return null;
    }

    DeclaredType declaredType = null;
    if (type instanceof Class) {
        declaredType = new DeclaredType();
        declaredType.setType((Class<?>) type);
    } else if (type instanceof ParameterizedType) {
        declaredType = new DeclaredType();
        final ParameterizedType parameterizedType = (ParameterizedType) type;
        final DeclaredType rawType = getDeclaredType(clazz, parameterizedType.getRawType());
        declaredType.setType(rawType.getType());
        for (int i = 0; i < parameterizedType.getActualTypeArguments().length; i++) {
            declaredType.getItems().add(getDeclaredType(clazz, parameterizedType.getActualTypeArguments()[i]));
        }
    } else if (type instanceof TypeVariable) {
        return getDeclaredTypeVariable(clazz, (TypeVariable<?>) type);
    } else if (type instanceof WildcardType) {
        declaredType = new DeclaredType();
        final WildcardType wildcardType = (WildcardType) type;
        if (wildcardType.getLowerBounds().length > 0) {
            declaredType.setType(getDeclaredType(clazz, wildcardType.getLowerBounds()[0]).getType());
        } else {
            declaredType.setType(null);
        }

        for (int i = 0; i < wildcardType.getUpperBounds().length; i++) {
            declaredType.getTypeItems().add(getDeclaredType(clazz, wildcardType.getUpperBounds()[i]));
        }

        return declaredType;
    }
    return declaredType;
}