Example usage for java.lang.reflect TypeVariable getBounds

List of usage examples for java.lang.reflect TypeVariable getBounds

Introduction

In this page you can find the example usage for java.lang.reflect TypeVariable getBounds.

Prototype

Type[] getBounds();

Source Link

Document

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

Usage

From source file:org.dimitrovchi.conf.service.ServiceParameterUtils.java

static AnnotationParameters annotationParameters() {
    final Class<?>[] stack = ClassResolver.CLASS_RESOLVER.getClassContext();
    final Class<?> caller = stack[3];
    final List<Class<? extends Annotation>> interfaces = new ArrayList<>();
    Class<?> topCaller = null;
    for (int i = 3; i < stack.length && caller.isAssignableFrom(stack[i]); i++) {
        final Class<?> c = stack[i];
        topCaller = stack[i];/*  www. j  a va2  s. co m*/
        if (c.getTypeParameters().length != 0) {
            final TypeVariable<? extends Class<?>> var = c.getTypeParameters()[0];
            final List<Class<? extends Annotation>> bounds = new ArrayList<>(var.getBounds().length);
            for (final Type type : var.getBounds()) {
                if (type instanceof Class<?> && ((Class<?>) type).isAnnotation()) {
                    bounds.add((Class) type);
                }
            }
            if (bounds.size() > interfaces.size()) {
                interfaces.clear();
                interfaces.addAll(bounds);
            }
        }
    }
    final Map<Class<? extends Annotation>, List<Annotation>> annotationMap = new IdentityHashMap<>();
    for (int i = 3; i < stack.length && caller.isAssignableFrom(stack[i]); i++) {
        final Class<?> c = stack[i];
        for (final Class<? extends Annotation> itf : interfaces) {
            final Annotation annotation = c.getAnnotation(itf);
            if (annotation != null) {
                List<Annotation> annotationList = annotationMap.get(itf);
                if (annotationList == null) {
                    annotationMap.put(itf, annotationList = new ArrayList<>());
                }
                annotationList.add(0, annotation);
            }
        }
    }
    return new AnnotationParameters(topCaller, interfaces, annotationMap);
}

From source file:antre.TypeResolver.java

/**
 * Resolves the first bound for the {@code typeVariable}, returning {@code Unknown.class} if none
 * can be resolved.// w  w w  .j  a v  a 2s.c  o  m
 */
public static Type resolveBound(TypeVariable<?> typeVariable) {
    Type[] bounds = typeVariable.getBounds();
    if (bounds.length == 0)
        return Unknown.class;

    Type bound = bounds[0];
    if (bound instanceof TypeVariable)
        bound = resolveBound((TypeVariable<?>) bound);

    return bound == Object.class ? Unknown.class : bound;
}

From source file:io.werval.runtime.util.TypeResolver.java

/**
 * Resolves the first bound for the {@code typeVariable}, returning {@code Unknown.class} if none can be resolved.
 *
 * @param typeVariable TypeVariable/*from   w w  w.j a v a 2  s  . c  om*/
 *
 * @return Type.
 */
public static Type resolveBound(TypeVariable<?> typeVariable) {
    Type[] bounds = typeVariable.getBounds();
    if (bounds.length == 0) {
        return Unknown.class;
    }

    Type bound = bounds[0];
    if (bound instanceof TypeVariable) {
        bound = resolveBound((TypeVariable<?>) bound);
    }

    return bound == Object.class ? Unknown.class : bound;
}

From source file:com.autentia.common.util.ClassWithList.java

private static void print(TypeVariable v) {
    System.out.println("Type variable");
    System.out.println("Name: " + v.getName());
    System.out.println("Declaration: " + v.getGenericDeclaration());
    System.out.println("Bounds:");
    for (Type t : v.getBounds()) {
        print(t);//from   w w  w  .j a v  a2  s  .c  om
    }
}

From source file:com.holonplatform.core.internal.utils.TypeUtils.java

/**
 * Get the underlying class of a {@link Type}, if available.
 * @param type the type for which to obtain the class
 * @return the type class//from   w  ww .j  av  a2  s  .  c  o  m
 */
public static Optional<Class<?>> getRawType(Type type) {
    if (type instanceof Class) {
        return Optional.of((Class<?>) type);
    } else if (type instanceof ParameterizedType) {
        return Optional.of((Class<?>) ((ParameterizedType) type).getRawType());
    } else if (type instanceof GenericArrayType) {
        return getRawType(((GenericArrayType) type).getGenericComponentType())
                .map(c -> Array.newInstance(c, 0).getClass());
    } else if (type instanceof TypeVariable) {
        final TypeVariable<?> typeVar = (TypeVariable<?>) type;
        if (typeVar.getBounds() != null && typeVar.getBounds().length > 0) {
            return getRawType(typeVar.getBounds()[0]);
        }
    }
    return Optional.empty();
}

From source file:Main.java

final static Type expand(Type type, Class<?> inClass) {
    Type expandedType = null;/*www.  j a va  2s .c om*/
    if (type instanceof TypeVariable) {
        @SuppressWarnings("unchecked")
        // for the moment we assume it is a class, we can later handle ctr and methods
        TypeVariable<GenericDeclaration> tvType = (TypeVariable<GenericDeclaration>) type;
        if (inClass == null)
            inClass = genericDeclarationToClass(tvType.getGenericDeclaration());
        expandedType = resolveTypeVariable(tvType, inClass);
        if (type.equals(expandedType))
            expandedType = tvType.getBounds()[0];
    } else if (type instanceof WildcardType) {
        WildcardType wType = (WildcardType) type;
        expandedType = wType.getUpperBounds().length > 0 ? expand(wType.getUpperBounds()[0], inClass)
                : Object.class;
    } else
        return type;

    return expandedType == null || type.equals(expandedType) ? Object.class : expandedType;
}

From source file:com.jaspersoft.jasperserver.war.helper.GenericParametersHelper.java

private static Map<String, Class<?>> getCurrentParameterValues(
        TypeVariable<? extends Class<?>>[] typeParameters, Type[] previousTypeArguments,
        Map<String, Class<?>> inputParameterValues) {
    Map<String, Class<?>> result = inputParameterValues != null ? inputParameterValues
            : new HashMap<String, Class<?>>();
    if (typeParameters != null && typeParameters.length > 0) {
        Map<String, Class<?>> currentParameterValues = new HashMap<String, Class<?>>();
        for (int i = 0; i < typeParameters.length; i++) {
            TypeVariable<? extends Class<?>> currentVariable = typeParameters[i];
            if (previousTypeArguments != null && previousTypeArguments.length > i) {
                // fill current type parameters with arguments from subclass declaration
                Type argumentType = previousTypeArguments[i];
                if (argumentType instanceof Class<?>) {
                    currentParameterValues.put(currentVariable.getName(), (Class<?>) argumentType);
                    continue;
                } else if (argumentType instanceof TypeVariable<?>
                        && result.containsKey(((TypeVariable<?>) argumentType).getName())) {
                    currentParameterValues.put(currentVariable.getName(),
                            result.get(((TypeVariable<?>) argumentType).getName()));
                    continue;
                }/*from   w  ww.j a va 2 s. c om*/
            }
            Class<?> variableClass = null;
            final Type[] bounds = currentVariable.getBounds();
            if (bounds != null && bounds.length > 0) {
                if (bounds[0] instanceof Class<?>) {
                    variableClass = (Class<?>) bounds[0];
                } else if (bounds[0] instanceof ParameterizedType) {
                    variableClass = (Class) ((ParameterizedType) bounds[0]).getRawType();
                }
            }
            currentParameterValues.put(currentVariable.getName(), variableClass);
        }
        result.clear();
        result.putAll(currentParameterValues);
    }
    return result;
}

From source file:com.link_intersystems.lang.reflect.TypeVariableToStringTransformer.java

public String transform(TypeVariable<?> typeVariable) {
    String genericTypeName = typeVariable.getName();
    StringBuilder toStringBuilder = new StringBuilder(genericTypeName);
    Type[] boundsArr = typeVariable.getBounds();

    if (boundsArr.length != 1 || !Object.class.equals(boundsArr[0])) {
        toStringBuilder.append(" extends ");
        Iterator<Type> boundsIterator = IteratorUtils.arrayIterator(boundsArr);

        while (boundsIterator.hasNext()) {
            Type boundsType = boundsIterator.next();

            if (boundsType instanceof Class<?>) {
                Class<?> classObj = Class.class.cast(boundsType);
                String classToString = classToString(classObj);
                toStringBuilder.append(classToString);
            } else if (boundsType instanceof TypeVariable<?>) {
                TypeVariable<?> boundsTypeVariable = (TypeVariable<?>) boundsType;
                String typeVariableAsString = typeVariableToString(boundsTypeVariable);
                toStringBuilder.append(typeVariableAsString);
            } else if (boundsType instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) boundsType;
                String string = parameterizedTypeToString(parameterizedType);
                toStringBuilder.append(string);
            } else {
                toStringBuilder.append("???");
            }//  w  w w .  jav a  2  s  . c o m

            if (boundsIterator.hasNext()) {
                toStringBuilder.append(" & ");
            }
        }
    }

    return toStringBuilder.toString();
}

From source file:com.mobileman.projecth.persistence.impl.DaoImpl.java

/**
 * /* w ww.  ja  v a  2  s  . c  om*/
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public DaoImpl() {
    java.lang.reflect.Type type = ((ParameterizedType) getClass().getGenericSuperclass())
            .getActualTypeArguments()[0];
    if (java.lang.reflect.TypeVariable.class.isInstance(type)) {
        TypeVariable typeVariable = TypeVariable.class.cast(type);
        this.entityClass = (Class<T>) typeVariable.getBounds()[0];
    } else {
        this.entityClass = (Class<T>) type;
    }

    if (this.entityClass == null) {
        throw new NullPointerException("null entity class !");
    }
}

From source file:com.github.juanmf.java2plant.render.PlantRenderer.java

private void addClassTypeParams(StringBuilder sb, Class<?> aClass) {
    List<String> typeParams = new ArrayList<>();
    // TODO: we are leaving lower bounds out, e.g. <? super Integer>
    for (TypeVariable t : aClass.getTypeParameters()) {
        Type[] bounds = t.getBounds();
        String jointBounds = TypesHelper.getSimpleName(StringUtils.join(bounds, "&"));
        typeParams.add(t.getName() + " extends " + jointBounds);
    }//from ww w.j a  v a 2  s  . c  o m
    if (0 < typeParams.size()) {
        sb.append(" <").append(StringUtils.join(typeParams, ", ")).append(">");
    }
}