Example usage for java.lang.reflect Method getName

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

Introduction

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

Prototype

@Override
public String getName() 

Source Link

Document

Returns the name of the method represented by this Method object, as a String .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class<?> class1 = Class.forName("Rate");
    Object obj = class1.newInstance();
    for (Method m : class1.getMethods()) {
        if (m.getName().equals("myMethod")) {
            Class<?>[] parameterTypes = m.getParameterTypes();
            System.out.println(Arrays.toString(m.getParameterTypes()));
            Object methodArgs[] = new Object[parameterTypes.length];
            for (Class<?> parameterType : parameterTypes) {
                if (parameterType == Double.TYPE) {
                    double value = 0.5;
                    methodArgs[0] = value;
                }/*ww  w . j av  a  2  s  .  c o m*/
            }
            Rate rate = (Rate) m.invoke(obj, methodArgs);
            System.out.println(rate.getValue());
        }
    }
}

From source file:MethodSpy.java

public static void main(String... args) {
    try {/*from w  ww. ja v a2  s . co  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());

        }

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

From source file:GetMethods.java

public static void main(String[] args) throws Exception {
    GetMethods object = new GetMethods();
    Class clazz = object.getClass();

    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        System.out.println("Method name        = " + method.getName());
        System.out.println("Method return type = " + method.getReturnType().getName());

        Class[] paramTypes = method.getParameterTypes();
        for (Class c : paramTypes) {
            System.out.println("Param type         = " + c.getName());
        }/* w  w w  .  j  a v a 2s  .c o  m*/
    }
    Method method = clazz.getMethod("add", new Class[] { int.class, int.class });
    System.out.println("Method name: " + method.getName());
}

From source file:MyClass.java

public static void main(String[] argv) {
    Class<MyClass> cls = MyClass.class;
    for (Method m : cls.getMethods()) {
        System.out.println(m.getName());
        System.out.println(getModifiers(m));
        System.out.println(getParameters(m));
        System.out.println(getExceptionList(m));
    }/*  w w  w  . ja v  a 2  s  . co  m*/
}

From source file:MethodSpy.java

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

public static void main(String[] args) throws Exception {
    Class computerClass = MyClass.class;
    Method[] methods = computerClass.getDeclaredMethods();
    MyClass computer = new MyClass();
    for (Method method : methods) {
        Object result = method.invoke(computer, new Object[0]);
        System.out.println(method.getName() + ": " + result);
    }/*  w w  w. ja v  a2 s .  c  o  m*/
}

From source file:Main.java

public static void main(String[] args) {
    Class aClass = String.class;

    // Get the methods
    Method[] methods = aClass.getDeclaredMethods();

    // Loop through the methods and print out their names
    for (Method method : methods) {
        System.out.println(method.getName());
    }/* ww  w . j ava 2s . co  m*/
}

From source file:Main.java

public static void main(String[] args) {
    Method[] methods = java.lang.Integer.class.getDeclaredMethods();

    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        Class retType = m.getReturnType();
        Class[] paramTypes = m.getParameterTypes();
        String name = m.getName();
        System.out.print(Modifier.toString(m.getModifiers()));
        System.out.print(" " + retType.getName() + " " + name + "(");
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getName());
        }//  w  w  w  .  ja va 2  s .c o  m
        System.out.println(");");
    }
}

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  a2  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:com.nabla.project.application.tool.runner.ServiceRunner.java

/**
 * DOCUMENT ME!//from   www .j ava 2  s .  c o m
 * 
 * @param args DOCUMENT ME!
 * @throws Exception DOCUMENT ME!
 * @throws RuntimeException DOCUMENT ME!
 */
public static void main(String args[]) throws Exception {

    ObjectInputStream ois = new ObjectInputStream(System.in);
    Object methodArgs[] = (Object[]) ois.readObject();

    if (args.length < 3) {

        throw new RuntimeException(
                "Error : usage : java com.nabla.project.application.tool.runner.ServiceRunner configFileName beanName methodName");

    }

    String configFileName = args[0];
    String beanName = args[1];
    String methodName = args[2];
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { configFileName });
    Object service = context.getBean(beanName);
    Method serviceMethod = null;
    Method methods[] = service.getClass().getMethods();

    for (Method method : methods) {

        if (method.getName().equals(methodName)) {

            serviceMethod = method;

        }

    }

    if (serviceMethod == null) {

        throw new RuntimeException("Method " + methodName + " not found in class " + service.getClass());

    }

    serviceMethod.invoke(service, methodArgs);

}