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:Main.java

public static void main(String... args) {
    try {/*  w  ww. java2 s. c om*/
        Class<?> c = Class.forName(args[0]);
        out.format("Class:%n  %s%n%n", c.getCanonicalName());
        out.format("Modifiers:%n  %s%n%n", Modifier.toString(c.getModifiers()));

        out.format("Type Parameters:%n");
        TypeVariable[] tv = c.getTypeParameters();
        if (tv.length != 0) {
            out.format("  ");
            for (TypeVariable t : tv)
                out.format("%s ", t.getName());
            out.format("%n%n");
        } else {
            out.format("  -- No Type Parameters --%n%n");
        }

        out.format("Implemented Interfaces:%n");
        Type[] intfs = c.getGenericInterfaces();
        if (intfs.length != 0) {
            for (Type intf : intfs)
                out.format("  %s%n", intf.toString());
            out.format("%n");
        } else {
            out.format("  -- No Implemented Interfaces --%n%n");
        }

        out.format("Inheritance Path:%n");
        List<Class> l = new ArrayList<Class>();
        printAncestor(c, l);
        if (l.size() != 0) {
            for (Class<?> cl : l)
                out.format("  %s%n", cl.getCanonicalName());
            out.format("%n");
        } else {
            out.format("  -- No Super Classes --%n%n");
        }

        out.format("Annotations:%n");
        Annotation[] ann = c.getAnnotations();
        if (ann.length != 0) {
            for (Annotation a : ann)
                out.format("  %s%n", a.toString());
            out.format("%n");
        } else {
            out.format("  -- No Annotations --%n%n");
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

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 va2s  .c om*/
    }
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static String prettyPrint(Type type) {
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        return prettyPrint(genericArrayType.getGenericComponentType()) + "[]";
    }/* w  w w .j  a  va 2  s  .  c o m*/
    if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        return wildcardType.toString();
    }
    if (type instanceof TypeVariable) {
        TypeVariable<?> typeVariable = (TypeVariable) type;
        return typeVariable.getName();
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        StringBuilder sb = new StringBuilder();
        sb.append(prettyPrint(parameterizedType.getRawType()));
        Type[] typeArguments = parameterizedType.getActualTypeArguments();
        if (typeArguments != null && typeArguments.length > 0) {
            sb.append("<");
            for (Type typeArgument : typeArguments) {
                sb.append(prettyPrint(typeArgument)).append(", ");
            }
            sb.delete(sb.length() - 2, sb.length()); // last ", "
            sb.append(">");
        }
        return sb.toString();
    }
    Class<?> clazz = (Class<?>) type;
    if (clazz.isArray()) {
        return prettyPrint(clazz.getComponentType()) + "[]";
    }
    return clazz.getSimpleName();
}

From source file:com.github.rvesse.airline.Accessor.java

private static Type[] getTypeParameters(Class<?> desiredType, Type type) {
    if (type instanceof Class) {
        Class<?> rawClass = (Class<?>) type;

        // if this is the collection class we're done
        if (desiredType.equals(type)) {
            return null;
        }// ww  w . ja  v a2  s  .  co m

        for (Type iface : rawClass.getGenericInterfaces()) {
            Type[] collectionType = getTypeParameters(desiredType, iface);
            if (collectionType != null) {
                return collectionType;
            }
        }

        return getTypeParameters(desiredType, rawClass.getGenericSuperclass());
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;

        Type rawType = parameterizedType.getRawType();
        if (desiredType.equals(rawType)) {
            return parameterizedType.getActualTypeArguments();
        }

        Type[] collectionTypes = getTypeParameters(desiredType, rawType);
        if (collectionTypes != null) {
            for (int i = 0; i < collectionTypes.length; i++) {
                if (collectionTypes[i] instanceof TypeVariable) {
                    TypeVariable<?> typeVariable = (TypeVariable<?>) collectionTypes[i];
                    TypeVariable<?>[] rawTypeParams = ((Class<?>) rawType).getTypeParameters();
                    for (int j = 0; j < rawTypeParams.length; j++) {
                        if (typeVariable.getName().equals(rawTypeParams[j].getName())) {
                            collectionTypes[i] = parameterizedType.getActualTypeArguments()[j];
                        }
                    }
                }
            }
        }
        return collectionTypes;
    }
    return null;
}

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  av a 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:eu.stratosphere.api.java.typeutils.TypeExtractor.java

private static TypeInformation<?> findCorrespondingInfo(TypeVariable<?> typeVar, Type type,
        TypeInformation<?> corrInfo) {
    if (type instanceof TypeVariable) {
        TypeVariable<?> variable = (TypeVariable<?>) type;
        if (variable.getName().equals(typeVar.getName())
                && variable.getGenericDeclaration().equals(typeVar.getGenericDeclaration())) {
            return corrInfo;
        }//from  w  w w. j ava 2s . c  o m
    } else if (type instanceof ParameterizedType
            && Tuple.class.isAssignableFrom((Class<?>) ((ParameterizedType) type).getRawType())) {
        ParameterizedType tuple = (ParameterizedType) type;
        Type[] args = tuple.getActualTypeArguments();

        for (int i = 0; i < args.length; i++) {
            TypeInformation<?> info = findCorrespondingInfo(typeVar, args[i],
                    ((TupleTypeInfo<?>) corrInfo).getTypeAt(i));
            if (info != null) {
                return info;
            }
        }
    }
    return null;
}

From source file:jp.go.nict.langrid.servicecontainer.handler.jsonrpc.servlet.JsonRpcServlet.java

private static void appendGenericsInfo(Class<?> clazz, StringBuilder b) {
    for (TypeVariable<? extends GenericDeclaration> v : clazz.getTypeParameters()) {
        if (b.length() == 0) {
            b.append("&lt;");
        } else {/*from ww w  .j a  v a  2  s  .  c  o m*/
            b.append(",");
        }
        b.append(v.getName());
    }
    if (b.length() != 0)
        b.append("&gt;");
}

From source file:eu.stratosphere.api.java.typeutils.TypeExtractor.java

private static Type materializeTypeVariable(ArrayList<Type> typeHierarchy, TypeVariable<?> typeVar) {

    TypeVariable<?> inTypeTypeVar = typeVar;
    // iterate thru hierarchy from top to bottom until type variable gets a class assigned
    for (int i = typeHierarchy.size() - 1; i >= 0; i--) {
        Type curT = typeHierarchy.get(i);

        // parameterized type
        if (curT instanceof ParameterizedType) {
            Class<?> rawType = ((Class<?>) ((ParameterizedType) curT).getRawType());

            for (int paramIndex = 0; paramIndex < rawType.getTypeParameters().length; paramIndex++) {

                TypeVariable<?> curVarOfCurT = rawType.getTypeParameters()[paramIndex];

                // check if variable names match
                if (curVarOfCurT.getName().equals(inTypeTypeVar.getName())
                        && curVarOfCurT.getGenericDeclaration().equals(inTypeTypeVar.getGenericDeclaration())) {
                    Type curVarType = ((ParameterizedType) curT).getActualTypeArguments()[paramIndex];

                    // another type variable level
                    if (curVarType instanceof TypeVariable<?>) {
                        inTypeTypeVar = (TypeVariable<?>) curVarType;
                    }/* ww w .  j  a  v  a 2 s. c  o m*/
                    // class
                    else {
                        return curVarType;
                    }
                }
            }
        }
    }
    // can not be materialized, most likely due to type erasure
    return null;
}

From source file:jp.terasoluna.fw.util.GenericsUtil.java

/**
 * ??<code>Type</code>??// ww  w .  j a v  a 2 s  .c  o  m
 * @param type ????<code>Type</code>
 * @param ancestorTypeList <code>type</code>??? ??????<code>ParameterizedType</code>?
 * @return ?
 * @throws IllegalStateException <code>type</code>? <code>Class</code>???? <code>TypeVariable</code>????? 
 *             <code>type</code>?? ????????? <code>type</code>???<code>Class</code>????
 *             (??)?
 */
protected static Class<? extends Object> resolveTypeVariable(Type type,
        List<ParameterizedType> ancestorTypeList) throws IllegalStateException {

    if (isNotTypeVariable(type)) {
        return getRawClass(type);
    }

    // TypeVariable:?
    TypeVariable<?> targetType = (TypeVariable<?>) type;
    Type actualType = null;
    for (int i = ancestorTypeList.size() - 1; i >= 0; i--) {
        ParameterizedType ancestorType = ancestorTypeList.get(i);

        // ?????
        GenericDeclaration declaration = targetType.getGenericDeclaration();
        if (!(declaration instanceof Class)) {
            throw new IllegalStateException("TypeVariable(" + targetType.getName()
                    + " is not declared at Class " + "(ie. is declared at Method or Constructor)");
        }

        // ?Generics????????
        Class<?> declaredClass = (Class<?>) declaration;
        if (declaredClass != ancestorType.getRawType()) {
            continue;
        }

        // ????????
        // ConcreteAbstractBLogic<R,P> extends AbstractBLogic<P,R>
        // ????????type????
        Type[] parameterTypes = declaredClass.getTypeParameters();
        int index = ArrayUtils.indexOf(parameterTypes, targetType);
        if (index == -1) {
            // ???????????????
            // ??????????
            // ???Generics?API?????????????
            // ?????????
            throw new IllegalStateException("Class(" + declaredClass.getName()
                    + ") does not declare TypeValidable(" + targetType.getName() + ")");
        }
        actualType = ancestorType.getActualTypeArguments()[index];
        if (log.isDebugEnabled()) {
            log.debug("resolved " + targetType.getName() + " -> " + actualType);
        }

        if (isNotTypeVariable(actualType)) {
            return getRawClass(actualType);
        }
        targetType = (TypeVariable<?>) actualType;
    }

    throw new IllegalStateException(
            "Concrete type of Type(" + type + ") was not found in ancestorList(" + ancestorTypeList + ")");
}

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

private String typeVariableToString(TypeVariable<?> typeVariable) {
    String name = typeVariable.getName();
    return name;
}