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

public static void main(String... args) {
    try {/*from w w  w.java  2s.co  m*/
        Class<?> c = Class.forName("Main");
        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            Class<?>[] pType = m.getParameterTypes();
            Type[] gpType = m.getGenericParameterTypes();
            for (int i = 0; i < pType.length; i++) {
                out.format(fmt, "ParameterType", pType[i]);
                out.format(fmt, "GenericParameterType", gpType[i]);
            }
        }
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Deet.java

public static void main(String... args) {
    if (args.length != 4) {
        err.format("Usage: java Deet <classname> <langauge> <country> <variant>%n");
        return;/*  w w  w . j a  v a  2  s .  com*/
    }

    try {
        Class<?> c = Class.forName(args[0]);
        Object t = c.newInstance();

        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            String mname = m.getName();
            if (!mname.startsWith("test") || (m.getGenericReturnType() != boolean.class)) {
                continue;
            }
            Type[] pType = m.getGenericParameterTypes();
            if ((pType.length != 1) || Locale.class.isAssignableFrom(pType[0].getClass())) {
                continue;
            }

            out.format("invoking %s()%n", mname);
            try {
                m.setAccessible(true);
                Object o = m.invoke(t, new Locale(args[1], args[2], args[3]));
                out.format("%s() returned %b%n", mname, (Boolean) o);

                // Handle any exceptions thrown by method to be invoked.
            } catch (InvocationTargetException x) {
                Throwable cause = x.getCause();
                err.format("invocation of %s failed: %s%n", mname, cause.getMessage());
            }
        }

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    }
}

From source file:org.apache.openejb.util.ListSetters.java

public static void main(final String[] args) throws Exception {
    final Options options = new Options();
    options.addOption(OptionBuilder.isRequired(true).hasArg(true).withLongOpt("class")
            .withDescription("the class to introspect").create("c"));

    final CommandLine line;
    try {/*from   www .  j av  a  2  s .com*/
        line = new PosixParser().parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }

    if (line.hasOption("help")) {
        help(options);
        return;
    }

    final String clazz = line.getOptionValue("class");
    final Collection<Class<?>> ancestors = Classes
            .ancestors(Thread.currentThread().getContextClassLoader().loadClass(clazz));
    final List<String> list = new LinkedList<>();
    for (final Class<?> c : ancestors) {
        for (final Method m : c.getDeclaredMethods()) {
            if (!Modifier.isPublic(m.getModifiers())) {
                continue;
            }

            if (!m.getName().startsWith("set")) {
                continue;
            }

            if (m.getGenericParameterTypes().length != 1) {
                continue;
            }

            list.add(m.getName().substring(3));
        }
    }
    Collections.sort(list);
    for (final String s : list) {
        System.out.println("- " + s);
    }
}

From source file:MethodSpy.java

public static void main(String... args) {
    try {/*from ww w  . j a  va  2s .  c  o m*/
        Class<?> c = Class.forName(args[0]);
        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            if (!m.getName().equals(args[1])) {
                continue;
            }
            out.format("%s%n", m.toGenericString());

            out.format(fmt, "ReturnType", m.getReturnType());
            out.format(fmt, "GenericReturnType", m.getGenericReturnType());

            Class<?>[] pType = m.getParameterTypes();
            Type[] gpType = m.getGenericParameterTypes();
            for (int i = 0; i < pType.length; i++) {
                out.format(fmt, "ParameterType", pType[i]);
                out.format(fmt, "GenericParameterType", gpType[i]);
            }

        }

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

From source file:io.servicecomb.swagger.generator.core.utils.ParamUtils.java

public static Type getGenericParameterType(Method method, int paramIdx) {
    return method.getGenericParameterTypes()[paramIdx];
}

From source file:Main.java

private static Method findMethod(final Class<?> beanType, final Type[] paramTypeHolder,
        final String methodName) {
    for (final Method m : beanType.getMethods()) {
        if (methodName.equals(m.getName()) && !Modifier.isStatic(m.getModifiers())) {
            final Type[] paramTypes = m.getGenericParameterTypes();
            if (paramTypes.length == 1) {
                paramTypeHolder[0] = paramTypes[0];
                return m;
            }//  ww w .  j  a va  2  s. c o m
        }
    }
    return null;
}

From source file:org.eclipse.wb.internal.core.xml.model.description.rules.CreatePropertiesPropertyDescriptorRule.java

private static Class<?> resolvePropertyType(ComponentDescription componentDescription, Method setMethod) {
    Class<?> propertyType = setMethod.getParameterTypes()[0];
    final Type genericPropertyType = setMethod.getGenericParameterTypes()[0];
    if (genericPropertyType instanceof TypeVariable<?>) {
        final Class<?> declaringClass = setMethod.getDeclaringClass();
        final Class<?> actualClass = componentDescription.getComponentClass();
        return ExecutionUtils.runObjectIgnore(new RunnableObjectEx<Class<?>>() {
            public Class<?> runObject() throws Exception {
                String typeName = GenericsUtils.getTypeName(
                        GenericTypeResolver.superClass(GenericTypeResolver.EMPTY, actualClass, declaringClass),
                        genericPropertyType);
                return actualClass.getClassLoader().loadClass(typeName);
            }//from ww w  . ja v a  2  s. c  om
        }, propertyType);
    }
    return propertyType;
}

From source file:org.mashti.jetson.json.JsonRequestEncoder.java

private static void writeRequestParameters(final Method method, final Object[] arguments,
        final JsonGenerator generator) throws IOException {

    final Type[] param_types = method.getGenericParameterTypes();
    JsonGeneratorUtil.writeValuesAs(generator, PARAMETERS_KEY, param_types, arguments);
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

public static Class<?> getGenericCollectionTypeOfMethodParameter(Method theMethod, int theParamIndex) {
    Class<?> type;//from ww w  .ja v a 2s  .com
    Type genericParameterType = theMethod.getGenericParameterTypes()[theParamIndex];
    if (Class.class.equals(genericParameterType)) {
        return null;
    }
    ParameterizedType collectionType = (ParameterizedType) genericParameterType;
    Type firstArg = collectionType.getActualTypeArguments()[0];
    if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) {
        ParameterizedType pt = ((ParameterizedType) firstArg);
        type = (Class<?>) pt.getRawType();
    } else {
        type = (Class<?>) firstArg;
    }
    return type;
}

From source file:com.curl.orb.servlet.InstanceManagementUtil.java

/**
 * Get Generic types of method's parameters.
 *//*www.  ja  v a2s .  c om*/
public static Class<?>[][] getGenericMethodParameterTypes(Method method) {
    Type[] genericType = method.getGenericParameterTypes();
    int len = genericType.length;
    Class<?>[][] genericTypes = new Class<?>[len][];
    for (int i = 0; i < len; i++)
        genericTypes[i] = getGenericTypes(genericType[i]);
    return genericTypes;
}