Example usage for java.lang.reflect TypeVariable getName

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

Introduction

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

Prototype

String getName();

Source Link

Document

Returns the name of this type variable, as it occurs in the source code.

Usage

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.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);
    }/*  ww w.j a v  a  2s.  c  o m*/
    if (0 < typeParams.size()) {
        sb.append(" <").append(StringUtils.join(typeParams, ", ")).append(">");
    }
}

From source file:net.firejack.platform.core.utils.Factory.java

private FieldInfo getField(Class<?> clazz, Field field) {
    FieldInfo info = null;/*from  w  w w . java2  s.  c o  m*/
    if (field != null) {
        info = new FieldInfo(field);
        if (field.getGenericType() instanceof TypeVariable) {
            TypeVariable genericType = (TypeVariable) field.getGenericType();
            TypeVariable<?>[] parameters = genericType.getGenericDeclaration().getTypeParameters();
            for (int i = 0; i < parameters.length; i++) {
                if (parameters[i].getName().equals(genericType.getName())) {
                    Class parameterClass = getGenericParameterClass(clazz,
                            (Class) genericType.getGenericDeclaration(), i);
                    info.setType(parameterClass);
                    break;
                }
            }
        }

        if (field.getGenericType() instanceof ParameterizedType) {
            ParameterizedType genericType = (ParameterizedType) field.getGenericType();

            Class<?> arrayType = null;
            if (genericType.getActualTypeArguments()[0] instanceof TypeVariable) {
                TypeVariable genericType1 = (TypeVariable) genericType.getActualTypeArguments()[0];
                TypeVariable<?>[] parameters = genericType1.getGenericDeclaration().getTypeParameters();
                for (int i = 0; i < parameters.length; i++) {
                    if (parameters[i].getName().equals(genericType1.getName())) {
                        arrayType = getGenericParameterClass(clazz,
                                (Class) genericType1.getGenericDeclaration(), i);
                        break;
                    }
                }
            } else {
                arrayType = (Class<?>) genericType.getActualTypeArguments()[0];
            }
            info.setGenericType(arrayType);
        }
    }
    return info;
}

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

/**
 * @param typeVarName/*w  w  w.  j  av a  2s .c o  m*/
 * @return the {@link TypeVariable} for the given name that is defined on
 *         this {@link Generic} type or null if no {@link TypeVariable} is
 *         defined with that name. This method only looks at the current
 *         type represented by this {@link Generic} object and not on super
 *         types.
 * @since 1.2.0.0
 */
public TypeVariable<?> getTypeVariable(String typeVarName) {
    Assert.notNull("typeVarName", typeVarName);
    TypeVariable<?> typeVariable = getTypeVariableCache().get(typeVarName);
    if (typeVariable == null) {
        typeVarName = typeVarName.trim();
        TypeVariable<?>[] typeParams = clazz.getTypeParameters();
        for (TypeVariable<?> typeParameter : typeParams) {
            if (typeVarName.equals(typeParameter.getName())) {
                typeVariable = typeParameter;
                getTypeVariableCache().put(typeVarName, typeVariable);
                break;
            }
        }
    }
    return typeVariable;
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

/**
 * @param runtime/*from  w  w w. j  a  v  a2 s  . co  m*/
 *          is <code>true</code> if we need name for class loading, <code>false</code> if we need
 *          name for source generation.
 * 
 * @return the fully qualified name of given {@link Type}.
 */
public static String getFullyQualifiedName(Type type, boolean runtime) {
    Assert.isNotNull(type);
    // Class
    if (type instanceof Class<?>) {
        Class<?> clazz = (Class<?>) type;
        // array
        if (clazz.isArray()) {
            return getFullyQualifiedName(clazz.getComponentType(), runtime) + "[]";
        }
        // object
        String name = clazz.getName();
        if (!runtime) {
            name = name.replace('$', '.');
        }
        return name;
    }
    // GenericArrayType
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        return getFullyQualifiedName(genericArrayType.getGenericComponentType(), runtime) + "[]";
    }
    // ParameterizedType
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type rawType = parameterizedType.getRawType();
        // raw type
        StringBuilder sb = new StringBuilder();
        sb.append(getFullyQualifiedName(rawType, runtime));
        // type arguments
        sb.append("<");
        boolean firstTypeArgument = true;
        for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
            if (!firstTypeArgument) {
                sb.append(",");
            }
            firstTypeArgument = false;
            sb.append(getFullyQualifiedName(typeArgument, runtime));
        }
        sb.append(">");
        // done
        return sb.toString();
    }
    // WildcardType
    if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        return "? extends " + getFullyQualifiedName(wildcardType.getUpperBounds()[0], runtime);
    }
    // TypeVariable
    TypeVariable<?> typeVariable = (TypeVariable<?>) type;
    return typeVariable.getName();
}

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

/**
 * Returns the exact return type of the given method in the given type. This
 * may be different from <tt>m.getGenericReturnType()</tt> when the method
 * was declared in a superclass, or <tt>type</tt> has a type parameter that
 * is used in the return type, or <tt>type</tt> is a raw type.
 *//*from   www . j a va  2  s.c o m*/
protected static Type getTypeFromExactReturnType(ParameterizedType returnType, ParameterizedType type) {
    Map<TypeVariable<?>, Type> typeMap = TypeUtils.getTypeArguments(returnType);
    Type[] actualParameters = new Type[type.getActualTypeArguments().length];
    int num = 0;
    for (TypeVariable<?> parameterType : ((Class<?>) type.getRawType()).getTypeParameters()) {
        //for(Type parameterType : type.getActualTypeArguments()) {
        //   if(parameterType instanceof TypeVariable<?>) {
        boolean replaced = false;
        for (TypeVariable<?> var : typeMap.keySet()) {
            // D'oh! Why the heck do we need this?? 
            if (var.getName().equals(parameterType.getName())) {
                //if(typeMap.containsKey(parameterType)) {
                actualParameters[num] = typeMap.get(var);
                replaced = true;
                break;
                //} else {
            }
        }
        if (!replaced) {
            actualParameters[num] = parameterType;
        }
        //}
        //       } else {
        //          LoggingUtils.getEvoLogger().info("Not a type variable "+parameterType);
        //          actualParameters[num] = parameterType;
        //          }
        num++;
    }

    return new ParameterizedTypeImpl((Class<?>) type.getRawType(), actualParameters, null);
}

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

/**
 * <p>// w  ww. j a v a 2s.c o  m
 * changeClassLoader
 * </p>
 * 
 * @param loader
 *            a {@link java.lang.ClassLoader} object.
 */
public void changeClassLoader(ClassLoader loader) {
    try {
        if (rawClass != null)
            rawClass = getClass(rawClass.getName(), loader);
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            // GenericClass rawType = new GenericClass(pt.getRawType());
            // rawType.changeClassLoader(loader);
            GenericClass ownerType = null;
            if (pt.getOwnerType() != null) {
                ownerType = new GenericClass(pt.getOwnerType());
                // ownerType.type = pt.getOwnerType();
                ownerType.changeClassLoader(loader);
            }
            List<GenericClass> parameterClasses = new ArrayList<GenericClass>();
            for (Type parameterType : pt.getActualTypeArguments()) {
                GenericClass parameter = new GenericClass(parameterType);
                // parameter.type = parameterType;
                parameter.changeClassLoader(loader);
                parameterClasses.add(parameter);
            }
            Type[] parameterTypes = new Type[parameterClasses.size()];
            for (int i = 0; i < parameterClasses.size(); i++)
                parameterTypes[i] = parameterClasses.get(i).getType();
            this.type = new ParameterizedTypeImpl(rawClass, parameterTypes,
                    ownerType != null ? ownerType.getType() : null);
        } else if (type instanceof GenericArrayType) {
            GenericClass componentClass = getComponentClass();
            componentClass.changeClassLoader(loader);
            this.type = GenericArrayTypeImpl.createArrayType(componentClass.getType());
        } else if (type instanceof WildcardType) {
            Type[] oldUpperBounds = ((WildcardType) type).getUpperBounds();
            Type[] oldLowerBounds = ((WildcardType) type).getLowerBounds();
            Type[] upperBounds = new Type[oldUpperBounds.length];
            Type[] lowerBounds = new Type[oldLowerBounds.length];

            for (int i = 0; i < oldUpperBounds.length; i++) {
                GenericClass bound = new GenericClass(oldUpperBounds[i]);
                // bound.type = oldUpperBounds[i];
                bound.changeClassLoader(loader);
                upperBounds[i] = bound.getType();
            }
            for (int i = 0; i < oldLowerBounds.length; i++) {
                GenericClass bound = new GenericClass(oldLowerBounds[i]);
                // bound.type = oldLowerBounds[i];
                bound.changeClassLoader(loader);
                lowerBounds[i] = bound.getType();
            }
            this.type = new WildcardTypeImpl(upperBounds, lowerBounds);
        } else if (type instanceof TypeVariable<?>) {
            for (TypeVariable<?> newVar : rawClass.getTypeParameters()) {
                if (newVar.getName().equals(((TypeVariable<?>) type).getName())) {
                    this.type = newVar;
                    break;
                }
            }
        } else {
            this.type = addTypeParameters(rawClass); //GenericTypeReflector.addWildcardParameters(raw_class);
        }
    } catch (ClassNotFoundException e) {
        logger.warn("Class not found: " + rawClass + " - keeping old class loader ", e);
    } catch (SecurityException e) {
        logger.warn("Class not found: " + rawClass + " - keeping old class loader ", e);
    }
}

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

/**
 * <p>//from   www.j a  v a  2 s  .co  m
 * changeClassLoader
 * </p>
 * 
 * @param loader
 *            a {@link java.lang.ClassLoader} object.
 */
public void changeClassLoader(ClassLoader loader) {
    try {
        if (rawClass != null)
            rawClass = getClass(rawClass.getName(), loader);
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            // GenericClass rawType = new GenericClass(pt.getRawType());
            // rawType.changeClassLoader(loader);
            GenericClass ownerType = null;
            if (pt.getOwnerType() != null) {
                ownerType = new GenericClass(pt.getOwnerType());
                // ownerType.type = pt.getOwnerType();
                ownerType.changeClassLoader(loader);
            }
            List<GenericClass> parameterClasses = new ArrayList<GenericClass>();
            for (Type parameterType : pt.getActualTypeArguments()) {
                GenericClass parameter = new GenericClass(parameterType);
                // parameter.type = parameterType;
                parameter.changeClassLoader(loader);
                parameterClasses.add(parameter);
            }
            Type[] parameterTypes = new Type[parameterClasses.size()];
            for (int i = 0; i < parameterClasses.size(); i++)
                parameterTypes[i] = parameterClasses.get(i).getType();
            this.type = new ParameterizedTypeImpl(rawClass, parameterTypes,
                    ownerType != null ? ownerType.getType() : null);
        } else if (type instanceof GenericArrayType) {
            GenericClass componentClass = getComponentClass();
            componentClass.changeClassLoader(loader);
            this.type = GenericArrayTypeImpl.createArrayType(componentClass.getType());
        } else if (type instanceof WildcardType) {
            Type[] oldUpperBounds = ((WildcardType) type).getUpperBounds();
            Type[] oldLowerBounds = ((WildcardType) type).getLowerBounds();
            Type[] upperBounds = new Type[oldUpperBounds.length];
            Type[] lowerBounds = new Type[oldLowerBounds.length];

            for (int i = 0; i < oldUpperBounds.length; i++) {
                GenericClass bound = new GenericClass(oldUpperBounds[i]);
                // bound.type = oldUpperBounds[i];
                bound.changeClassLoader(loader);
                upperBounds[i] = bound.getType();
            }
            for (int i = 0; i < oldLowerBounds.length; i++) {
                GenericClass bound = new GenericClass(oldLowerBounds[i]);
                // bound.type = oldLowerBounds[i];
                bound.changeClassLoader(loader);
                lowerBounds[i] = bound.getType();
            }
            this.type = new WildcardTypeImpl(upperBounds, lowerBounds);
        } else if (type instanceof TypeVariable<?>) {
            for (TypeVariable<?> newVar : rawClass.getTypeParameters()) {
                if (newVar.getName().equals(((TypeVariable<?>) type).getName())) {
                    this.type = newVar;
                    break;
                }
            }
        } else {
            this.type = addTypeParameters(rawClass); //GenericTypeReflector.addWildcardParameters(raw_class);
        }
    } catch (ClassNotFoundException e) {
        logger.warn("Class not found: {} - keeping old class loader ", rawClass, e);
    } catch (SecurityException e) {
        logger.warn("Class not found: {} - keeping old class loader ", rawClass, e);
    }
}

From source file:org.mousepilots.es.maven.model.generator.model.attribute.GenericMethodInfo.java

/**
 * Resolves the generic {@code type}/*from  w ww . jav  a 2 s  .c o  m*/
 * @param typeVariable2Value
 * @param type
 * @return 
 */
private String getGenericTypeString(Type type) {
    if (type instanceof Class) {
        //(raw) class
        return ((Class) type).getCanonicalName();
    } else if (type instanceof ParameterizedType) {
        // e.g. Map<K,V>
        final ParameterizedType parameterizedType = (ParameterizedType) type;
        return getGenericTypeString(parameterizedType.getRawType()) + "<"
                + StringUtils.join(Arrays.asList(parameterizedType.getActualTypeArguments()),
                        (Type actualTypeArgument) -> getGenericTypeString(actualTypeArgument), ",")
                + ">";

    } else if (type instanceof TypeVariable) {
        //e.g. T
        final TypeVariable<?> typeVariable = (TypeVariable) type;
        final Type typeVariableValue = typeVariable2Value.get(typeVariable);
        if (typeVariableValue == null) {
            return typeVariable.getName();
        } else {
            return getGenericTypeString(typeVariableValue);
        }
    } else if (type instanceof GenericArrayType) {
        //e.g. T[]
        final GenericArrayType genericArrayType = (GenericArrayType) type;
        return getGenericTypeString(genericArrayType.getGenericComponentType()) + "[]";
    } else {
        throw new IllegalArgumentException("unsupported type " + type);
    }
}

From source file:org.nextframework.controller.MultiActionController.java

protected Class<?> getCommandClass(Method method, int commandIndex) {
    //TODO TENTAR DESCOBRIR O COMMAND MESMO QUANDO UTILIZAR GENERICS
    Class<?> commandClass = null;
    Method metodoOriginal = method;
    do {//from  w  w w  . j av a  2 s. c  o m
        Type[] genericParameterTypes = method.getGenericParameterTypes();
        Type type = genericParameterTypes[commandIndex];
        if (type instanceof TypeVariable<?>) {
            TypeVariable<?> typeVariable = (TypeVariable<?>) type;
            String typeVariableName = typeVariable.getName();

            TypeVariable<?>[] typeParameters = this.getClass().getTypeParameters();
            if (typeParameters.length != 0) {
                throw new NextException("Implementar achar tipo de command por genericTypeParameters");
            }
            Type genericSuperclass = this.getClass().getGenericSuperclass();
            if (genericSuperclass instanceof ParameterizedType) {
                TypeVariable<?>[] typeParametersMethodClass = method.getDeclaringClass().getTypeParameters();
                int i = 0;
                for (TypeVariable<?> variable : typeParametersMethodClass) {
                    if (variable.getName().equals(typeVariableName)) {
                        commandClass = (Class<?>) ((ParameterizedType) genericSuperclass)
                                .getActualTypeArguments()[i];
                    }
                    i++;
                }
            }
            break;
        }
        if (type instanceof Object) {
            method = getSuperClassMethod(method);
        }
    } while (commandClass == null && method != null);
    if (commandClass == null) {
        commandClass = metodoOriginal.getParameterTypes()[metodoOriginal.getParameterTypes().length - 1];
    }
    //      if(commandClass.equals(Object.class)){
    //         logger.warn("Utilizando classe java.lang.Object como command");
    //      }
    return commandClass;
}