List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:org.alfresco.module.org_alfresco_module_rm.api.PublicAPITestUtil.java
/** * Find all classes that are within the supplied type. For example a {@code Pair<Set<String>, Integer>} contains * references to four classes./*from w w w . j ava2 s . co m*/ * * @param type The type to examine. * @param processedTypes The set of types which have already been processed. If {@code type} is within this set then * the method returns an empty set, to prevent analysis of the same type multiple times, and to guard * against circular references. The underlying set is updated with the given type. * @return The set of classes used to form the given type. */ private static Set<Class<?>> getClassesFromType(Type type, Set<Type> processedTypes) { Set<Class<?>> returnClasses = new HashSet<>(); if (processedTypes.add(type)) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; returnClasses.add((Class<?>) parameterizedType.getRawType()); for (Type t : parameterizedType.getActualTypeArguments()) { returnClasses.addAll(getClassesFromType(t, processedTypes)); } } else if (type instanceof Class) { Class<?> clazz = (Class<?>) type; if (clazz.isArray()) { returnClasses.add(clazz.getComponentType()); } returnClasses.add(clazz); } else if (type instanceof WildcardType) { // No-op - Caller can choose what type to use. } else if (type instanceof TypeVariable<?>) { TypeVariable<?> typeVariable = (TypeVariable<?>) type; for (Type bound : typeVariable.getBounds()) { returnClasses.addAll(getClassesFromType(bound, processedTypes)); } } else if (type instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) type; returnClasses .addAll(getClassesFromType(genericArrayType.getGenericComponentType(), processedTypes)); } else { throw new IllegalStateException("This test was not written to work with type " + type); } } return returnClasses; }
From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java
private static Class detectSourceCollectionPayload(Method getter) { Class sourceArgClass = null;/*from www . j a v a 2 s. c om*/ Type sourceArg = getter.getGenericReturnType(); if (sourceArg instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) sourceArg; Type[] typeArguments = type.getActualTypeArguments(); sourceArgClass = (Class) typeArguments[0]; } return sourceArgClass; }
From source file:org.opencb.commons.utils.CommandLineUtils.java
private static String getType(Type genericType) { String type;/*from www . ja va2 s.c o m*/ if (genericType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericType; Type rawType = parameterizedType.getRawType(); if (rawType instanceof Class && Collection.class.isAssignableFrom((Class) rawType)) { return getType(parameterizedType.getActualTypeArguments()[0]) + "*"; } } type = genericType.getTypeName(); type = type.substring(1 + Math.max(type.lastIndexOf("."), type.lastIndexOf("$"))); type = Arrays.asList(org.apache.commons.lang3.StringUtils.splitByCharacterTypeCamelCase(type)).stream() .map(String::toUpperCase).collect(Collectors.joining("_")); if (type.equals("INTEGER")) { type = "INT"; } return type; }
From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java
private static Map<Integer, Class> detectSourceMapPayload(Method getter) { Map<Integer, Class> result = new HashMap(); Type sourceArg = getter.getGenericReturnType(); if (sourceArg instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) sourceArg; Type[] typeArguments = type.getActualTypeArguments(); if (typeArguments.length != 2) { System.err.println("Cannot determine payload type of source Map "); return null; }//from w w w . java2 s.c o m result.put(0, (Class) typeArguments[0]); result.put(1, (Class) typeArguments[1]); } return result; }
From source file:com.startechup.tools.ModelParser.java
/** * Invokes the setter method that was link to the json key. * * @param method The setter method to be invoked. * @param classInstance The instance of the container class. * @param key The json key serving as the reference of the value in the json object * @param jsonObject The API response object. *//* w w w . j a va2s .c om*/ private static void initMethodInvocation(Method method, Object classInstance, String key, JSONObject jsonObject) { Object value = getValueFromJsonObject(jsonObject, key, method.getName()); // Only invoke the method when the value is not null if (value != null) { method.setAccessible(true); Object castedObject = value; if (value instanceof Number) { castedObject = castNumberObject(method.getParameterTypes()[0], value); } else if (value instanceof JSONArray) { if (method.getParameterTypes()[0].isArray()) { //TODO find a way to genetically convert json array to array, for now throw our custom exception throwException("Cannot parse " + JSONArray.class + " to " + method.getParameterTypes()[0]); } else { Object parameterInstance = getNewInstance(method.getParameterTypes()[0]); if (parameterInstance instanceof Collection) { ParameterizedType parameterizedType = (ParameterizedType) method .getGenericParameterTypes()[0]; Class<?> classType = (Class<?>) parameterizedType.getActualTypeArguments()[0]; castedObject = parse(classType, (JSONArray) value); } else { //TODO find a way to genetically convert json array to the other parameter class, for now throw our custom exception throwException("Cannot parse " + JSONArray.class + " to " + method.getParameterTypes()[0]); } } } else if (value instanceof JSONObject) { castedObject = parse(method.getParameterTypes()[0], ((JSONObject) value)); } // Finally invoke the method after casting the values into the method parameter type invoke(method, classInstance, castedObject); } }
From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java
private static Class detectDestCollectionPayload(Method setter) { Class destArgClass = null;// w w w.j a v a 2 s. c o m Type[] genericParameterTypes = setter.getGenericParameterTypes(); Type genericParameterType = genericParameterTypes[0]; if (genericParameterType instanceof ParameterizedType) { ParameterizedType aType = (ParameterizedType) genericParameterType; Type[] destArgs = aType.getActualTypeArguments(); if (destArgs.length != 1) { System.out.println("3: Cannot determine payload type or multiple types found !!!"); return null; } destArgClass = (Class) destArgs[0]; } return destArgClass; }
From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java
private static Map<Integer, Class> detectDestMapPayload(Method setter) { Map<Integer, Class> result = new HashMap(); Type[] genericParameterTypes = setter.getGenericParameterTypes(); Type genericParameterType = genericParameterTypes[0]; if (genericParameterType instanceof ParameterizedType) { ParameterizedType aType = (ParameterizedType) genericParameterType; Type[] destArgs = aType.getActualTypeArguments(); if (destArgs.length != 2) { System.err.println("2: Cannot determine payload type of source Map, operation aborted !!!"); return null; }//ww w .ja v a2 s . c o m result.put(0, (Class) destArgs[0]); result.put(0, (Class) destArgs[1]); } return result; }
From source file:com.stratio.deep.es.utils.UtilES.java
private static <T> Object subDocumentListCase(Type type, ArrayWritable arrayWritable) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {//from w w w .j a v a 2s.c om ParameterizedType listType = (ParameterizedType) type; Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0]; List list = new ArrayList(); Writable[] writetable = arrayWritable.get(); for (int i = 0; i < writetable.length; i++) { list.add(getObjectFromJson(listClass, (LinkedMapWritable) writetable[i])); } return list; }
From source file:net.radai.beanz.util.ReflectionUtil.java
public static Type getKeyType(Type type) { if (isMap(type)) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type[] typeArguments = parameterizedType.getActualTypeArguments(); if (typeArguments.length != 2) { throw new UnsupportedOperationException(); }//w w w . j a v a 2s .c o m return typeArguments[0]; } if (type instanceof Class<?>) { //this is something like a plain Map(), no generic information, so type unknown return null; } } throw new UnsupportedOperationException(); }
From source file:org.opencb.commons.utils.CommandLineUtils.java
private static String getType(ParameterDescription parameterDescription) { String type = ""; if (parameterDescription.getParameter().arity() == 0) { return type; } else {/*from w w w . ja v a 2 s . c o m*/ if (parameterDescription.isDynamicParameter()) { Type genericType = parameterDescription.getParameterized().getGenericType(); if (genericType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericType; Type rawType = parameterizedType.getRawType(); if (rawType instanceof Class && Map.class.isAssignableFrom((Class) rawType)) { String key = getType(parameterizedType.getActualTypeArguments()[0]); String assignment = parameterDescription.getParameter().getAssignment(); String value = getType(parameterizedType.getActualTypeArguments()[1]); type = key + assignment + value; } } else { type = getType(genericType); } } else { Type genericType = parameterDescription.getParameterized().getGenericType(); type = getType(genericType); if (type.equals("BOOLEAN") && parameterDescription.getParameterized().getParameter().arity() == -1) { type = ""; } } } return type; }