Example usage for java.lang.reflect Method getGenericParameterTypes

List of usage examples for java.lang.reflect Method getGenericParameterTypes

Introduction

In this page you can find the example usage for java.lang.reflect Method getGenericParameterTypes.

Prototype

@Override
public Type[] getGenericParameterTypes() 

Source Link

Usage

From source file:com.xpfriend.fixture.cast.temp.PojoUtil.java

public static Class<?> getPropertyComponentType(Object object, Column column, Table table, Row row) {
    String propertyName = column.getName();
    try {/*from   w w w.j  av a  2  s  . c o  m*/
        String name = find(propertyName, object);
        PropertyDescriptor descriptor = TypeConverter.getBeanUtils().getPropertyUtils()
                .getPropertyDescriptor(object, name);
        Method method = descriptor.getWriteMethod();
        Type genericType = method.getGenericParameterTypes()[0];
        if (genericType != null) {
            if (genericType instanceof Class) {
                genericType = ((Class<?>) genericType).getGenericSuperclass();
            }
            if (genericType instanceof ParameterizedType) {
                Type[] es = ((ParameterizedType) genericType).getActualTypeArguments();
                if (es != null && es.length > 0 && es[0] instanceof Class) {
                    return (Class<?>) es[0];
                }
            }
        }
        return String.class;
    } catch (NoSuchMethodException e) {
        throw createNoSuchPropertyException(object, propertyName, table, row);
    } catch (InvocationTargetException e) {
        throw new ConfigException(e.getCause());
    } catch (IllegalAccessException e) {
        throw new ConfigException(e);
    }
}

From source file:io.swagger.jaxrs.ParameterExtractor.java

public static List<Parameter> getParameters(Swagger swagger, Class cls, Method method) {
    List<Parameter> parameters = new ArrayList<>();
    Type[] genericParameterTypes = method.getGenericParameterTypes();
    Annotation[][] paramAnnotations = method.getParameterAnnotations();
    for (int i = 0; i < genericParameterTypes.length; i++) {
        Type type = null;/*  w  w  w  .  jav a  2s  . c  o m*/
        if (SynapseEndpointServiceMarker.class.isAssignableFrom(cls)) {
            //an RPC style endpoint (implements SynapseEndpointServiceMarker)
            type = TypeFactory.defaultInstance().constructType(ParameterWrapper.class, cls);
        } else if (cls.getAnnotation(Service.class) != null) {
            //an endpoint with @Service annotation
            if (genericParameterTypes[i] instanceof ParameterizedType && Request.class
                    .isAssignableFrom((Class<?>) ((ParameterizedType) genericParameterTypes[i]).getRawType())) {
                //if Request or Event Object
                type = TypeFactory.defaultInstance().constructType(
                        ((ParameterizedType) genericParameterTypes[i]).getActualTypeArguments()[0], cls);
            } else {
                type = TypeFactory.defaultInstance().constructType(genericParameterTypes[i], cls);
            }

        }

        extractParameters(swagger, parameters, type, Arrays.asList(paramAnnotations[i]));
    }
    return parameters;
}

From source file:com.wavemaker.runtime.server.json.JSONUtils.java

public static List<FieldDefinition> getParameterTypes(Method m, JSONArray params, TypeState typeState) {

    List<FieldDefinition> fieldDefinitions = new ArrayList<FieldDefinition>();
    for (Type type : m.getGenericParameterTypes()) {
        fieldDefinitions.add(ReflectTypeUtils.getFieldDefinition(type, typeState, false, null));
    }// w w w .  ja va 2s.  c o  m

    Annotation[][] paramAnnotations = m.getParameterAnnotations();

    for (int i = 0; i < paramAnnotations.length; i++) {
        for (Annotation ann : paramAnnotations[i]) {
            if (ann instanceof JSONParameterTypeField) {

                JSONParameterTypeField paramTypeField = (JSONParameterTypeField) ann;
                int pos = paramTypeField.typeParameter();
                String typeString = (String) params.get(pos);

                try {
                    Class<?> newType = org.springframework.util.ClassUtils.forName(typeString);

                    if (Collection.class.isAssignableFrom(newType)) {
                        throw new WMRuntimeException(MessageResource.JSONUTILS_PARAMTYPEGENERIC, i,
                                m.getName());
                    }

                    fieldDefinitions.set(i,
                            ReflectTypeUtils.getFieldDefinition(newType, typeState, false, null));
                } catch (ClassNotFoundException e) {
                    throw new WMRuntimeException(MessageResource.JSONPARAMETER_COULD_NOTLLOAD_TYPE, e,
                            typeString, m.getName(), i);
                } catch (LinkageError e) {
                    throw new WMRuntimeException(e);
                }
            }
        }
    }

    return fieldDefinitions;
}

From source file:com.github.juanmf.java2plant.Parser.java

protected static void addMethodUses(Set<Relation> relations, Class<?> fromType, Method m) {
    Type[] genericParameterTypes = m.getGenericParameterTypes();
    for (int i = 0; i < genericParameterTypes.length; i++) {
        addMethodUse(relations, fromType, genericParameterTypes[i], m);
    }/*from w w  w  .  ja va 2 s  .  co  m*/
}

From source file:com.allinfinance.system.util.BeanUtils.java

/**
 * POJONULL?//  w  w w.  java2 s  .co  m
 * @param obj
 * @param val1 
 * @param val2 ?
 * @return
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
public static Object setNullValueWithValue(Object obj, String val1, int val2)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Method[] methods = obj.getClass().getMethods();
    String propertyName = null;
    String methodName = null;
    for (Method method : methods) {
        methodName = method.getName();
        if (methodName.startsWith("set")) {
            String type = method.getGenericParameterTypes()[0].toString();
            propertyName = methodName.substring(methodName.indexOf("set") + 3);
            propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
            if (getProperty(obj, propertyName) == null)
                if ("class java.lang.String".equals(type)) {
                    copyProperty(obj, propertyName, val1);
                } else {
                    copyProperty(obj, propertyName, val2);
                }

        }
    }
    return obj;
}

From source file:com.sf.ddao.crud.param.CRUDParameterService.java

public static Class<?> getCRUDDaoBean(Context ctx, int idx) {
    final MethodCallCtx callCtx = CtxHelper.get(ctx, MethodCallCtx.class);
    final Method method = callCtx.getMethod();
    if (idx != USE_GENERICS) {
        Type beanClass;// w w  w. jav  a2  s  .  co m
        if (idx == DefaultParameter.RETURN_ARG_IDX) {
            beanClass = method.getGenericReturnType();
        } else {
            beanClass = method.getGenericParameterTypes()[idx];
        }
        if (beanClass instanceof Class) {
            return (Class) beanClass;
        }
    }
    Class<?> iFace = callCtx.getSubjClass();
    for (Type type : iFace.getGenericInterfaces()) {
        final ParameterizedType parameterizedType = (ParameterizedType) type;
        if (parameterizedType.getRawType().equals(method.getDeclaringClass())) {
            final Type[] typeArguments = parameterizedType.getActualTypeArguments();
            return (Class<?>) typeArguments[GENERICS_ARG_NUM];
        }
    }
    throw new RuntimeException(iFace + " expected to extend " + CRUDDao.class);
}

From source file:com.arpnetworking.jackson.BuilderDeserializer.java

private static void addTo(final Set<Type> visited, final Map<Class<?>, JsonDeserializer<?>> deserializerMap,
        final Type targetType) {
    if (visited.contains(targetType)) {
        return;// w ww .  java  2s. co  m
    }
    visited.add(targetType);

    if (targetType instanceof Class<?>) {
        @SuppressWarnings("unchecked")
        final Class<Object> targetClass = (Class<Object>) targetType;
        try {
            // Look-up and register the builder for this class
            final Class<? extends Builder<? extends Object>> builderClass = getBuilderForClass(targetClass);
            deserializerMap.put(targetClass, BuilderDeserializer.of(builderClass));

            LOGGER.info("Registered builder for class; builderClass=" + builderClass + " targetClass="
                    + targetClass);

            // Process all setters on the builder
            for (final Method method : builderClass.getMethods()) {
                if (isSetterMethod(builderClass, method)) {
                    final Type setterArgumentType = method.getGenericParameterTypes()[0];
                    // Recursively register builders for each setter's type
                    addTo(visited, deserializerMap, setterArgumentType);
                }
            }
        } catch (final ClassNotFoundException e) {
            // Log that the class is not build-able
            LOGGER.debug("Ignoring class without builder; targetClass=" + targetClass);
        }

        // Support for JsonSubTypes annotation
        if (targetClass.isAnnotationPresent(JsonSubTypes.class)) {
            final JsonSubTypes.Type[] subTypes = targetClass.getAnnotation(JsonSubTypes.class).value();
            for (final JsonSubTypes.Type subType : subTypes) {
                addTo(visited, deserializerMap, subType.value());
            }
        }

        // Support for JsonTypeInfo annotation
        // TODO(vkoskela): Support JsonTypeInfo classpath scan [MAI-116]
    }

    if (targetType instanceof ParameterizedType) {
        // Recursively register builders for each parameterized type
        final ParameterizedType targetParameterizedType = (ParameterizedType) targetType;
        final Type rawType = targetParameterizedType.getRawType();
        addTo(visited, deserializerMap, rawType);
        for (final Type argumentActualType : targetParameterizedType.getActualTypeArguments()) {
            addTo(visited, deserializerMap, argumentActualType);
        }
    }
}

From source file:api.wiki.WikiGenerator.java

private static String methodDisplayName(Method apiMethod) {
    String methodName = apiMethod.getName();
    String parameterTypes = stream(apiMethod.getGenericParameterTypes()).map(WikiGenerator::typeDisplayName)
            .collect(joining(", "));
    return typeDisplayName(apiMethod.getGenericReturnType()) + " " + methodName + "(" + parameterTypes + ")";
}

From source file:GenericClass.java

public static GenericClass forParameter(Method method, int index) {
    return forGenericType(method.getGenericParameterTypes()[index]);
}

From source file:org.alfresco.module.org_alfresco_module_rm.api.PublicAPITestUtil.java

/**
 * Get all types references by the supplied method signature (i.e. the parameters, return type and exceptions).
 *
 * @param method The method to analyse.//from w  w  w . j a va2 s  . c  om
 * @return The set of types.
 */
private static Set<Type> getTypesFromMethod(Method method) {
    Set<Type> methodTypes = new HashSet<>();
    methodTypes.addAll(Sets.newHashSet(method.getGenericParameterTypes()));
    methodTypes.add(method.getGenericReturnType());
    methodTypes.addAll(Sets.newHashSet(method.getGenericExceptionTypes()));
    return methodTypes;
}