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.shop.commons.swagger.Reader.java

private void read(ReaderContext context) {
    final SwaggerDefinition swaggerDefinition = context.getCls().getAnnotation(SwaggerDefinition.class);
    if (swaggerDefinition != null) {
        readSwaggerConfig(swaggerDefinition);
    }//from  ww w  .j av a 2s .c om
    for (Method method : context.getCls().getMethods()) {
        if (ReflectionUtils.isOverriddenMethod(method, context.getCls())) {
            continue;
        }
        final Operation operation = new Operation();
        String operationPath = null;
        String httpMethod = null;

        final Type[] genericParameterTypes = method.getGenericParameterTypes();
        final Annotation[][] paramAnnotations = method.getParameterAnnotations();

        for (ReaderExtension extension : ReaderExtensions.getExtensions()) {
            if (operationPath == null) {
                operationPath = extension.getPath(context, method);
            }
            if (httpMethod == null) {
                httpMethod = extension.getHttpMethod(context, method);
            }
            if (operationPath == null || httpMethod == null) {
                continue;
            }

            if (extension.isReadable(context)) {
                extension.setDeprecated(operation, method);
                extension.applyConsumes(context, operation, method);
                extension.applyProduces(context, operation, method);
                extension.applyOperationId(operation, method);
                extension.applySummary(operation, method);
                extension.applyDescription(operation, method);
                extension.applySchemes(context, operation, method);
                extension.applySecurityRequirements(context, operation, method);
                extension.applyTags(context, operation, method);
                extension.applyResponses(context, operation, method);
                extension.applyImplicitParameters(context, operation, method);
                extension.applyExtensions(context, operation, method);
                for (int i = 0; i < genericParameterTypes.length; i++) {
                    extension.applyParameters(context, operation, genericParameterTypes[i],
                            paramAnnotations[i]);
                }
            }
        }

        if (httpMethod != null) {
            if (operation.getResponses() == null) {
                operation.defaultResponse(new Response().description("successful operation"));
            }

            final Map<String, String> regexMap = new HashMap<String, String>();
            final String parsedPath = PathUtils.parsePath(operationPath, regexMap);

            Path path = swagger.getPath(parsedPath);
            if (path == null) {
                path = new Path();
                swagger.path(parsedPath, path);
            }
            path.set(httpMethod.toLowerCase(), operation);
        }
    }
}

From source file:org.powertac.logtool.common.DomainObjectReader.java

private boolean tryMethodCall(Object thing, Method method, String[] args) {
    Type[] argTypes = method.getGenericParameterTypes();
    if (argTypes.length != args.length)
        // bail if arglist lengths do not match
        return false;
    Object[] realArgs;/*from  w w w .j av  a  2  s. c  om*/
    if (0 == argTypes.length) {
        // no args
        realArgs = null;
    } else {
        try {
            realArgs = resolveArgs(argTypes, args);
            if (null == realArgs || realArgs.length != args.length) {
                log.debug("Could not resolve args: method " + method.getName() + ", class = "
                        + thing.getClass().getName() + ", args = " + args);
                return false;
            }
        } catch (MissingDomainObject mdo) {
            return false;
        }
    }
    try {
        method.invoke(thing, realArgs);
        return true;
    } catch (Exception e) {
        log.error("Exception calling method " + thing.getClass().getName() + "." + method.getName()
                + " on args " + args);
    }
    return false;
}

From source file:com.centurylink.mdw.hub.service.SwaggerReader.java

private void read(ReaderContext context) {
    final SwaggerDefinition swaggerDefinition = context.getCls().getAnnotation(SwaggerDefinition.class);
    if (swaggerDefinition != null) {
        readSwaggerConfig(swaggerDefinition);
    }/*w ww.j av a  2 s  .com*/
    for (Method method : context.getCls().getMethods()) {
        if (ReflectionUtils.isOverriddenMethod(method, context.getCls())) {
            continue;
        }
        final Operation operation = new Operation();
        String operationPath = null;
        String httpMethod = null;

        final Type[] genericParameterTypes = method.getGenericParameterTypes();
        final Annotation[][] paramAnnotations = method.getParameterAnnotations();

        // Avoid java.util.ServiceLoader mechanism which finds ServletReaderExtension
        // for (ReaderExtension extension : ReaderExtensions.getExtensions()) {
        for (ReaderExtension extension : getExtensions()) {
            if (operationPath == null) {
                operationPath = extension.getPath(context, method);
            }
            if (httpMethod == null) {
                httpMethod = extension.getHttpMethod(context, method);
            }
            if (operationPath == null || httpMethod == null) {
                continue;
            }

            if (extension.isReadable(context)) {
                extension.setDeprecated(operation, method);
                extension.applyConsumes(context, operation, method);
                extension.applyProduces(context, operation, method);
                extension.applyOperationId(operation, method);
                extension.applySummary(operation, method);
                extension.applyDescription(operation, method);
                extension.applySchemes(context, operation, method);
                extension.applySecurityRequirements(context, operation, method);
                extension.applyTags(context, operation, method);
                extension.applyResponses(context, operation, method);
                extension.applyImplicitParameters(context, operation, method);
                for (int i = 0; i < genericParameterTypes.length; i++) {
                    extension.applyParameters(context, operation, genericParameterTypes[i],
                            paramAnnotations[i]);
                }
            }
        }

        if (httpMethod != null && operationPath != null) {
            if (operation.getResponses() == null) {
                operation.defaultResponse(new Response().description("OK"));
            } else {
                for (String k : operation.getResponses().keySet()) {
                    if (k.equals("200")) {
                        Response response = operation.getResponses().get(k);
                        if ("successful operation".equals(response.getDescription()))
                            response.setDescription("OK");
                    }
                }
            }

            final Map<String, String> regexMap = new HashMap<String, String>();
            final String parsedPath = PathUtils.parsePath(operationPath, regexMap);

            Path path = swagger.getPath(parsedPath);
            if (path == null) {
                path = new Path();
                swagger.path(parsedPath, path);
            }
            path.set(httpMethod.toLowerCase(), operation);
        }
    }
}

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

/**
 * @return generic signature for given {@link Method}. This signature is not same signature as in
 *         JVM or JDT, just some string that unique identifies constructor in its {@link Class}.
 *         It uses {@link Method#getGenericParameterTypes()}, so for each {@link TypeVariable} its
 *         name will be used in signature.
 *//*w w  w  .ja  v a 2 s  . c  om*/
public static String getMethodGenericSignature(Method method) {
    Assert.isNotNull(method);
    return getMethodSignature(method.getName(), method.getGenericParameterTypes());
}

From source file:com.linecorp.bot.spring.boot.support.LineMessageHandlerSupport.java

private HandlerMethod getMethodHandlerMethodFunction(Object consumer, Method method) {
    final EventMapping mapping = AnnotatedElementUtils.getMergedAnnotation(method, EventMapping.class);
    if (mapping == null) {
        return null;
    }/*www . jav  a 2 s.  co  m*/

    Preconditions.checkState(method.getParameterCount() == 1, "Number of parameter should be 1. But {}",
            (Object[]) method.getParameterTypes());
    // TODO: Support more than 1 argument. Like MVC's argument resolver?

    final Type type = method.getGenericParameterTypes()[0];

    final Predicate<Event> predicate = new EventPredicate(type);
    return new HandlerMethod(predicate, consumer, method, getPriority(mapping, type));
}

From source file:com.snaplogic.snaps.uniteller.util.Utilities.java

/**
 * @param method/*from  w  ww. j  a  v a 2  s .  co  m*/
 * @return SnapType
 */
public SnapType getDataTypes(Method method) {
    String type = method.getGenericParameterTypes()[0].toString();
    if (StringUtils.containsIgnoreCase(type, String.class.getName())) {
        return SnapType.STRING;
    } else if (StringUtils.contains(type, INT)) {
        return SnapType.INTEGER;
    } else if (StringUtils.containsIgnoreCase(type, Float.class.getSimpleName())
            || StringUtils.containsIgnoreCase(type, Long.class.getSimpleName())
            || StringUtils.containsIgnoreCase(type, Double.class.getSimpleName())) {
        return SnapType.NUMBER;
    } else if (StringUtils.containsIgnoreCase(type, Calendar.class.getName())) {
        return SnapType.DATETIME;
    }
    return SnapType.ANY;
}

From source file:org.rhq.scripting.javascript.JavascriptCompletor.java

private String getSignature(Object object, Method m) {

    StringBuilder buf = new StringBuilder();
    Type[] params = m.getGenericParameterTypes();
    int i = 0;/*from w w  w . ja  v  a  2  s  .c o m*/
    m = metadataProvider.getUnproxiedMethod(m);

    buf.append(metadataProvider.getTypeName(m.getGenericReturnType(), false));
    buf.append(" ");

    buf.append(m.getName());
    buf.append("(");
    boolean first = true;
    for (Type type : params) {
        if (!first) {
            buf.append(", ");
        } else {
            first = false;
        }

        String name = metadataProvider.getTypeName(type, false);
        String paramName = metadataProvider.getParameterName(m, i);
        if (paramName != null) {
            name += " " + paramName;
        }

        buf.append(name);

        i++;
    }
    buf.append(")");
    return buf.toString();
}

From source file:org.carewebframework.cal.ui.reporting.drilldown.DrillDownDisplay.java

/**
 * When debug is true, dataObject is interrogated and the classes get/bean methods are invoked
 * and displayed on the display as well.
 *
 * @param dataObject Object to interrogate.
 * @param checkOnly If true, only checks to see if the object has additional debug info.
 * @return True if the object is a type for which additional debug info is available..
 *///from ww  w.j  av a  2s  .com
private boolean debugObject(Object dataObject, boolean checkOnly) {
    if (dataObject != null) {
        Row row;
        Class<?> clazz = dataObject.getClass();

        if (!checkOnly) {
            log.debug("Adding Verbose DrillDown Object Debug Information");
            addRow("-------DEBUG--------", clazz.getName());
            row = getLastRow();
            row.appendChild(new Label());
            row.setSclass(Constants.SCLASS_DRILLDOWN_GRID);
        }

        try {
            Object[] params = null;
            //Method[] methods = clazz.getDeclaredMethods();
            Method[] methods = clazz.getMethods();

            if (!(dataObject instanceof String)) {
                for (Method method : methods) {
                    if (Modifier.PUBLIC == method.getModifiers()) {
                        // Assumes getter methods
                        if (method.getName().startsWith("get")
                                && method.getGenericParameterTypes().length == 0) {
                            if (checkOnly) {
                                return true;
                            }

                            Object invokedObject = method.invoke(getDataObject(), params);
                            String methodName = method.getName();
                            addRowViaObject(methodName, invokedObject);
                            addLink(invokedObject, clazz.getName() + "." + methodName);
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    return false;
}

From source file:org.codehaus.enunciate.modules.rest.RESTOperation.java

/**
 * Gets the collection type for the specified parameter as an array type.
 *
 * @param method The method./* ww  w .ja  va 2  s . co  m*/
 * @param i      The parameter index.
 * @return The conversion.
 */
protected Class getCollectionTypeAsArrayType(Method method, int i) {
    Class parameterType;
    Type collectionType = method.getGenericParameterTypes()[i];
    if (collectionType instanceof ParameterizedType) {
        Type[] typeArgs = ((ParameterizedType) collectionType).getActualTypeArguments();
        if (typeArgs.length < 1) {
            throw new IllegalStateException("A type parameter must be supplied to the collection for parameter "
                    + i + " of method " + method.getDeclaringClass().getName() + "." + method.getName() + ".");
        }

        Class componentType = (Class) typeArgs[0];
        parameterType = Array.newInstance(componentType, 0).getClass();
    } else {
        throw new IllegalStateException("Non-parameterized collection found at parameter" + i + " on method "
                + method.getDeclaringClass().getName() + "." + method.getName()
                + ". Only parameterized collections are supported.");
    }
    return parameterType;
}

From source file:org.sybila.parasim.core.impl.ObservingMethod.java

public ObservingMethod(Object target, Method method) {
    Validate.notNull(method);/* w  w w .  j  av  a2s .  com*/
    Validate.notNull(target);
    if (method.getParameterTypes().length == 0) {
        throw new IllegalArgumentException("Method without parameters can't be observing point.");
    }
    Observes observes = ReflectionUtils.loadAnnotation(method.getParameterAnnotations()[0], Observes.class);
    Validate.notNull(observes, "Method where the first parameter isn't annotated by " + Observes.class.getName()
            + " annotation can't be observing point.");
    this.method = method;
    this.target = target;
    injectionPoints = new InjectionPoint[method.getGenericParameterTypes().length - 1];
    for (int i = 1; i < method.getGenericParameterTypes().length; i++) {
        injectionPoints[i - 1] = new GenericInjectionPoint(method.getParameterAnnotations()[i],
                method.getGenericParameterTypes()[i]);
    }
}