Example usage for java.lang.reflect ParameterizedType getActualTypeArguments

List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments

Introduction

In this page you can find the example usage for java.lang.reflect ParameterizedType getActualTypeArguments.

Prototype

Type[] getActualTypeArguments();

Source Link

Document

Returns an array of Type objects representing the actual type arguments to this type.

Usage

From source file:org.uimafit.factory.ConfigurationParameterFactory.java

private static String getConfigurationParameterType(Field field) {
    Class<?> parameterClass = field.getType();
    String parameterClassName;//from  w  w  w .j a v  a 2s. c o  m
    if (parameterClass.isArray()) {
        parameterClassName = parameterClass.getComponentType().getName();
    } else if (Collection.class.isAssignableFrom(parameterClass)) {
        ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
        parameterClassName = ((Class<?>) (collectionType.getActualTypeArguments()[0])).getName();
    } else {
        parameterClassName = parameterClass.getName();
    }
    String parameterType = javaUimaTypeMap.get(parameterClassName);
    if (parameterType == null) {
        return ConfigurationParameter.TYPE_STRING;
    }
    return parameterType;
}

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()) + "[]";
    }//from   www . j a  va  2s  . 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:de.adesso.referencer.search.helper.MyHelpMethods.java

public static String getClassNameFromListOfClasses(Field f) {
    String result = "";
    try {/*from   w ww .j  a v a2  s.  com*/
        ParameterizedType listType = (ParameterizedType) f.getGenericType();
        Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0];
        result = listClass.getName();
    } catch (Exception e) {
        result = "Error!";
        e.printStackTrace();
    }
    return result;
}

From source file:de.adesso.referencer.search.helper.MyHelpMethods.java

public static Class getClassFromListOfClasses(Field f) {
    try {//w  ww .j av a 2  s .  c o  m
        //            System.out.println("f="+f.toString());
        //            System.out.println("f.getGenericType()="+f.getGenericType().toString());
        ParameterizedType listType = (ParameterizedType) f.getGenericType();
        Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0];

        return listClass;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    // return null;
}

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 w  w  w. j  ava  2s  . co 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.solmix.datax.support.InvokerObject.java

private static GenericParameterNode buildGenericParameterTree(ParameterizedType type) {
    GenericParameterNode gpn = new GenericParameterNode();
    Type paramTypes[] = type.getActualTypeArguments();
    if (paramTypes.length == 1 && (paramTypes[0] instanceof Class<?>)) {
        gpn.addClass((Class<?>) paramTypes[0]);
        return gpn;
    }//from w w  w . ja  v  a  2s  . co m
    gpn.addClass((Class<?>) type.getRawType());
    for (int j = 0; j < paramTypes.length; j++)
        if (paramTypes[j] instanceof Class) {
            if (gpn.getChildNode() == null)
                gpn.setChildNode(new GenericParameterNode((Class<?>) paramTypes[j]));
            else
                gpn.getChildNode().addClass((Class<?>) paramTypes[j]);
        } else {
            gpn.setChildNode(buildGenericParameterTree((ParameterizedType) paramTypes[j]));
        }

    return gpn;
}

From source file:ca.uhn.fhir.context.ModelScanner.java

private static Class<?> getGenericCollectionTypeOfCodedField(Field next) {
    Class<?> type;/*from  w w w .  jav a2s  . c  o m*/
    ParameterizedType collectionType = (ParameterizedType) next.getGenericType();
    Type firstArg = collectionType.getActualTypeArguments()[0];
    if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) {
        ParameterizedType pt = ((ParameterizedType) firstArg);
        firstArg = pt.getActualTypeArguments()[0];
        type = (Class<?>) firstArg;
    } else {
        type = (Class<?>) firstArg;
    }
    return type;
}

From source file:com.qingstor.sdk.utils.QSJSONUtil.java

private static void setParameterToMap(Method m, Object source, Field f, Object data) {
    if (data != null) {
        try {/* www  .  j ava2  s  .c  o  m*/
            if (data instanceof JSONArray || data instanceof JSONObject) {
                Class fClass = f.getType();
                if (fClass.equals(List.class)) {
                    List invokeData = new ArrayList();
                    ParameterizedType stringListType = (ParameterizedType) f.getGenericType();
                    Class<?> cls = (Class<?>) stringListType.getActualTypeArguments()[0];

                    if (cls.equals(String.class) || cls.equals(Integer.class) || cls.equals(Double.class)
                            || cls.equals(Long.class) || cls.equals(Float.class)) {
                        if (data instanceof JSONArray) {
                            JSONArray jsonData = (JSONArray) data;
                            for (int i = 0; i < jsonData.length(); i++) {

                                Object o = toObject(jsonData, i);
                                invokeData.add(o);
                            }
                        }
                    } else {
                        if (data instanceof JSONArray) {
                            JSONArray jsonData = (JSONArray) data;
                            for (int i = 0; i < jsonData.length(); i++) {
                                Object fObject = cls.newInstance();
                                JSONObject o = toJSONObject(jsonData, i);
                                Class tmpClass = fObject.getClass();
                                while (tmpClass != Object.class) {
                                    Field[] fields = tmpClass.getDeclaredFields();
                                    initParameter(o, fields, tmpClass, fObject);
                                    tmpClass = tmpClass.getSuperclass();
                                }
                                invokeData.add(fObject);
                            }
                        }
                    }

                    m.invoke(source, invokeData);

                } else if (fClass.equals(Map.class)) {
                    Map invokeData = new HashMap();
                    if (data instanceof JSONObject) {
                        JSONObject jsonData = (JSONObject) data;
                        for (int i = 0; i < jsonData.length(); i++) {
                            String key = toJSONObject(jsonData, i) + "";
                            Object value = toJSONObject(jsonData, key);
                            invokeData.put(key, value);
                        }
                    }
                    m.invoke(source, invokeData);
                } else {

                    Object invokeData = f.getType().newInstance();
                    Class tmpClass = invokeData.getClass();
                    while (tmpClass != Object.class) {
                        Field[] fields = tmpClass.getDeclaredFields();
                        initParameter((JSONObject) data, fields, tmpClass, invokeData);
                        tmpClass = tmpClass.getSuperclass();
                    }
                    m.invoke(source, invokeData);
                }

            } else {
                if (f.getType().equals(data.getClass())) {
                    m.invoke(source, data);
                } else {
                    m.invoke(source, getParseValue(f.getType(), data));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.batoo.common.reflect.ReflectHelper.java

private static Class<?> getTypeImpl(Class<?> clazz, Class<?> originalType, String methodName,
        List<Type> arguments) {
    final Map<TypeVariable<?>, Type> typeMap = Maps.newHashMap();

    if (arguments.size() > 0) {
        final TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
        for (int i = 0; i < typeParameters.length; i++) {
            typeMap.put(typeParameters[i], arguments.get(i));
        }//from  ww w . j  a v  a 2  s .  co m
    }

    try {
        final Method m = clazz.getDeclaredMethod(methodName);

        final Type type = typeMap.get(m.getGenericReturnType());
        if (type != null) {
            return ReflectHelper.checkAndReturn(originalType, m, (Class<?>) type);
        }

        return ReflectHelper.checkAndReturn(originalType, m, m.getReturnType());
    } catch (final NoSuchMethodException e) {
        final List<Type> typeArguments = Lists.newArrayList();

        if (clazz.getGenericSuperclass() instanceof ParameterizedType) {
            final ParameterizedType parameterizedType = (ParameterizedType) clazz.getGenericSuperclass();

            for (final Type typeArgument : parameterizedType.getActualTypeArguments()) {
                if (typeArgument instanceof TypeVariable) {
                    typeArguments.add(typeMap.get(typeArgument));
                } else {
                    typeArguments.add(typeArgument);
                }
            }
        }

        final Class<?> superclass = clazz.getSuperclass();

        if (superclass == Object.class) {
            return originalType;
        }

        return ReflectHelper.getTypeImpl(superclass, originalType, methodName, typeArguments);
    }
}

From source file:com.googlecode.android_scripting.rpc.MethodDescriptor.java

/**
 * Appends the name of the given type to the {@link StringBuilder}.
 * //w w w  .j  ava2 s .  co m
 * @param builder
 *          string builder to append to
 * @param type
 *          type whose name to append
 */
private static void appendTypeName(final StringBuilder builder, final Type type) {
    if (type instanceof Class<?>) {
        builder.append(((Class<?>) type).getSimpleName());
    } else {
        ParameterizedType parametrizedType = (ParameterizedType) type;
        builder.append(((Class<?>) parametrizedType.getRawType()).getSimpleName());
        builder.append("<");

        Type[] arguments = parametrizedType.getActualTypeArguments();
        for (int i = 0; i < arguments.length; i++) {
            if (i > 0) {
                builder.append(", ");
            }
            appendTypeName(builder, arguments[i]);
        }
        builder.append(">");
    }
}