Example usage for java.lang.reflect Method toGenericString

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

Introduction

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

Prototype

@Override
public String toGenericString() 

Source Link

Document

Returns a string describing this Method , including type parameters.

Usage

From source file:MethodTrouble.java

public static void main(String... args) {
    try {/* www .  j  a  va 2s.co m*/
        String mName = args[0];
        Class cArg = Class.forName(args[1]);
        Class<?> c = (new MethodTrouble<Integer>()).getClass();
        Method m = c.getMethod(mName, cArg);
        System.out.format("Found:%n  %s%n", m.toGenericString());

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

From source file:EnumSpy.java

public static void main(String... args) {
    try {/*from www  . ja va2 s . c  om*/
        Class<?> c = Class.forName(args[0]);
        if (!c.isEnum()) {
            out.format("%s is not an enum type%n", c);
            return;
        }
        out.format("Class:  %s%n", c);

        Field[] flds = c.getDeclaredFields();
        List<Field> cst = new ArrayList<Field>(); // enum constants
        List<Field> mbr = new ArrayList<Field>(); // member fields
        for (Field f : flds) {
            if (f.isEnumConstant())
                cst.add(f);
            else
                mbr.add(f);
        }
        if (!cst.isEmpty())
            print(cst, "Constant");
        if (!mbr.isEmpty())
            print(mbr, "Field");

        Constructor[] ctors = c.getDeclaredConstructors();
        for (Constructor ctor : ctors) {
            out.format(fmt, "Constructor", ctor.toGenericString(), synthetic(ctor));
        }

        Method[] mths = c.getDeclaredMethods();
        for (Method m : mths) {
            out.format(fmt, "Method", m.toGenericString(), synthetic(m));
        }

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

From source file:MethodSpy.java

public static void main(String... args) {
    try {//from ww w  .  j  a v  a 2s .c om
        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());

        }

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

From source file:MethodSpy.java

public static void main(String... args) {
    try {//w  ww .j  a  v  a2 s.  c  om
        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:Main.java

public static void printMethods(Class cl) {
    Method[] methods = cl.getDeclaredMethods();

    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        System.out.println(m.toGenericString());

    }/*from  w  ww.  ja  va2  s .c  o  m*/
}

From source file:InheritedMethods.java

private static void printMethods(Class c) {
    out.format("Methods from %s%n", c);
    Method[] meths = c.getDeclaredMethods();
    if (meths.length != 0) {
        for (Method m : meths)
            out.format("  Method:  %s%n", m.toGenericString());
    } else {/*ww  w . ja  v  a 2  s. c  o m*/
        out.format("  -- no methods --%n");
    }
    out.format("%n");
}

From source file:com.m3.methodcache.interceptor.AbstractCacheResultInterceptor.java

/**
 * Extracts method part only from method#toGenericString
 *
 * @param invokedMethod/*from  w  ww  .  j a  v  a2 s.  c  om*/
 * @return method part only
 */
protected static String getMethodPartOnly(Method invokedMethod) {
    return invokedMethod.toGenericString().replaceFirst("[^\\(]+?\\.([^\\(\\.]+?\\(.+)", "$1");
}

From source file:org.springframework.web.servlet.mvc.support.MvcUrlUtils.java

/**
 * Extract the mapping from the given controller method, including both type and
 * method-level mappings. If multiple mappings are found, the first one is used.
 */// ww w  .  ja  v a2s.c  om
public static String getMethodMapping(Method method) {
    RequestMapping methodAnnot = AnnotationUtils.findAnnotation(method, RequestMapping.class);
    Assert.notNull(methodAnnot, "No mappings on " + method.toGenericString());
    PatternsRequestCondition condition = new PatternsRequestCondition(methodAnnot.value());

    RequestMapping typeAnnot = AnnotationUtils.findAnnotation(method.getDeclaringClass(), RequestMapping.class);
    if (typeAnnot != null) {
        condition = new PatternsRequestCondition(typeAnnot.value()).combine(condition);
    }

    Set<String> patterns = condition.getPatterns();
    if (patterns.size() > 1) {
        logger.warn("Multiple mappings on " + method.toGenericString() + ", using the first one");
    }

    return (patterns.size() == 0) ? "/" : patterns.iterator().next();
}

From source file:plugin.PluginHarness.java

public static void loadPlugins() throws PluginLoadingException {

    unloadAllPlugins();// w w  w.j ava 2s.  co m

    CommandList.dumpCommands();

    File allPlugins = new File("plugins");

    if (!allPlugins.exists())
        allPlugins.mkdirs();

    for (File cPlugin : allPlugins.listFiles()) {

        String ezName = FilenameUtils.removeExtension(cPlugin.getName());

        String pluginLogTag = "[" + ezName + "] ";

        log.info(pluginLogTag + "Attempting to load plugin");

        GroovyClassLoader gcl = new GroovyClassLoader();
        try {

            Class c = gcl.parseClass(cPlugin);

            Object listenerInst = c.newInstance();

            if (!listenerInst.getClass().isAnnotationPresent(PluginInfo.class)) {
                log.error(pluginLogTag + "Could not load plugin, PluginInfo annotation not present");
                continue;
            }

            PluginInfo loadPlug = listenerInst.getClass().getAnnotation(PluginInfo.class);

            boolean plugValid = true;

            for (Method method : c.getMethods()) {

                if (method.isAnnotationPresent(CommandTag.class)) {

                    if (method.getParameters().length != 2) {
                        log.error(pluginLogTag + "Method " + method.toGenericString()
                                + " could not be loaded, expected 2 parameters.");
                        plugValid = false;
                        break;
                    }
                    Class<?> eventClazz = method.getParameters()[0].getType();

                    if (!eventClazz.equals(IMessage.class)) {
                        log.error(pluginLogTag + "Method " + method.toGenericString()
                                + " could not be loaded, parameter 1 should be of type IMessage");
                        plugValid = false;
                        break;
                    }
                    eventClazz = method.getParameters()[1].getType();

                    if (!eventClazz.equals(List.class)) {
                        log.error(pluginLogTag + "Method " + method.toGenericString()
                                + " could not be loaded, parameter 2 should be of type List<String>");
                        plugValid = false;
                        break;
                    }

                    CommandTag cmd = method.getAnnotation(CommandTag.class);
                    // pretty name, scope, command string, description, method
                    CommandList.putC(cmd.prettyName(), cmd.channelScope(), cmd.commandPattern(),
                            cmd.description(), (chatSource, vargs) -> {

                                try {
                                    method.invoke(listenerInst, chatSource, vargs);
                                } catch (InvocationTargetException | IllegalAccessException e) {
                                    throw e.getCause();
                                }

                            });

                    log.info(pluginLogTag + "Registered chat command \"" + cmd.prettyName());

                } else if (method.isAnnotationPresent(EventSubscriber.class)) { // validate method signature as being instance of Event

                    if (method.getParameters().length != 1) {
                        log.error(pluginLogTag + "Method " + method.toGenericString()
                                + " could not be loaded, expected only 1 parameter.");
                        plugValid = false;
                        break;
                    }

                    Class<?> eventClazz = method.getParameters()[0].getType();

                    if (eventClazz.equals(Event.class) || !Event.class.isAssignableFrom(eventClazz)) {
                        log.error(pluginLogTag + "Method " + method.toGenericString()
                                + " could not be loaded, parameter should extend Event");
                        plugValid = false;
                        break;
                    }
                }
            }

            if (!plugValid) {
                log.error(pluginLogTag + "One or more errors occurred, loading attempted");
                continue;
            }

            cli.getDispatcher().registerListener(listenerInst);
            registeredListeners.add(listenerInst);

            log.info(pluginLogTag + "Registered listener: " + listenerInst.toString());

            log.info(pluginLogTag + "Successfully loaded" + " plugin: \"" + loadPlug.name() + "\" version: \""
                    + loadPlug.version() + "\" by: \"" + loadPlug.author() + "\"");

        } catch (Exception e) {
            log.error(pluginLogTag + "Unknown error occurred", e);
        }
    }

}

From source file:org.midonet.config.ConfigProvider.java

private static String findDeclaredGroupName(Method method) {
    ConfigGroup configGroup = method.getAnnotation(ConfigGroup.class);

    if (configGroup == null) {
        configGroup = method.getDeclaringClass().getAnnotation(ConfigGroup.class);
    }/* w w  w. j av  a2 s  . c  o m*/

    if (configGroup != null) {
        return configGroup.value();
    }

    throw new IllegalArgumentException("Method " + method.toGenericString() + " is being used as an "
            + "config entry accessor but is not annotated (or the class "
            + "declaring it) with @ConfigGroup annotation");
}