Example usage for java.lang Class getMethod

List of usage examples for java.lang Class getMethod

Introduction

In this page you can find the example usage for java.lang Class getMethod.

Prototype

@CallerSensitive
public Method getMethod(String name, Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException 

Source Link

Document

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class cls = java.lang.String.class;
    Method method = cls.getMethod("substring", new Class[] { int.class });
    System.out.println(method);/* ww  w.  j a v  a2 s.  co  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Main cls = new Main();
    Class c = cls.getClass();

    // parameter type is null
    Method m = c.getMethod("show", null);
    System.out.println("method = " + m.toString());

    // method Long
    Class[] cArg = new Class[1];
    cArg[0] = Long.class;
    Method lMethod = c.getMethod("showLong", cArg);
    System.out.println("method = " + lMethod.toString());
}

From source file:CompileString.java

public static void main(String[] args) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    String program = "class Test{" + "   public static void main (String [] args){"
            + "      System.out.println (\"Hello, World\");" + "      System.out.println (args.length);"
            + "   }" + "}";

    Iterable<? extends JavaFileObject> fileObjects;
    fileObjects = getJavaSourceFromString(program);

    compiler.getTask(null, null, null, null, null, fileObjects).call();

    Class<?> clazz = Class.forName("Test");
    Method m = clazz.getMethod("main", new Class[] { String[].class });
    Object[] _args = new Object[] { new String[0] };
    m.invoke(null, _args);//from w w w.  ja  v a 2 s .c  o  m
}

From source file:MethodTrouble.java

public static void main(String... args) {
    try {// w  w w. ja v  a  2  s. c  o 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:MyClassLoader.java

static public void main(String args[]) throws Exception {
    MyClassLoader x = new MyClassLoader();
    Class c = x.loadClass(args[0]);
    Class getArg1[] = { (new String[1]).getClass() };
    Method m = c.getMethod("main", getArg1);
    String[] my1 = { "arg1 passed", "arg2 passed" };
    Object myarg1[] = { my1 };/*from w ww . j a va2 s  .c  o m*/
    m.invoke(null, myarg1);

}

From source file:Main.java

static public void main(String args[]) throws Exception {
    URL myurl[] = { new URL("file:///C:/CH3/ClassLoader/web/"), new URL("http://www.java2s.edu/~xyx/test/") };
    URLClassLoader x = new URLClassLoader(myurl);
    Class c = x.loadClass("TestURL");

    Class getArg1[] = { (new String[1]).getClass() };
    Method m = c.getMethod("main", getArg1);
    String[] my1 = { "arg1 passed", "arg2 passed" };
    Object myarg1[] = { my1 };/*from w  w w  .ja  v  a2  s. c  o m*/
    m.invoke(null, myarg1);

    Object ob = c.newInstance();
    Class arg2[] = {};
    Method m2 = c.getMethod("tt", arg2);
    m2.invoke(ob, null);

    Class arg3[] = { (new String()).getClass(), int.class };
    Method m3 = c.getMethod("tt", arg3);
    Object myarg2[] = { "Arg1", new Integer(100) };
    m3.invoke(ob, myarg2);
}

From source file:de.andrena.tools.macker.plugin.CommandLineFile.java

public static void main(String[] args) throws Exception {
    if (args.length == 0 || args.length > 2) {
        System.err.println("Usage: CommandLineFile <main class> [command line arguments file]");
        System.exit(1);// w  w w .  j a  va  2s  .  c o m
    }

    String className = args[0];
    Class<?> clazz = Class.forName(className);
    Method main = clazz.getMethod("main", new Class[] { String[].class });

    List<String> lines = new ArrayList<String>();
    if (args.length == 2) {
        Reader in = new InputStreamReader(new FileInputStream(args[1]), "UTF-8");
        try {
            lines = IOUtils.readLines(in);
        } finally {
            in.close();
        }
    }

    try {
        main.invoke(null, new Object[] { lines.toArray(new String[lines.size()]) });
    } catch (InvocationTargetException ex) {
        Throwable cause = ex.getTargetException();
        if (cause instanceof Error) {
            throw (Error) cause;
        }
        throw (Exception) cause;
    }
}

From source file:MyClass.java

public static void main(String[] args) {
    Class<MyClass> myClass = MyClass.class;
    try {// w  w w .  j av  a  2s. c om
        MyClass p = myClass.newInstance();
        Method setName = myClass.getMethod("setName", String.class);
        setName.invoke(p, "abc");
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException
            | IllegalArgumentException | InvocationTargetException e) {
        System.out.println(e.getMessage());
    }
}

From source file:X.java

public static void main(String[] args) {
    try {/*from ww w . j  a  v a  2  s  . c om*/
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);

        System.out.println(method.getDeclaringClass());

    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:X.java

public static void main(String[] args) {
    try {//w ww.  j a v  a  2s. c  om
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);

        System.out.println(method.getDefaultValue());

    } catch (Exception e) {
        System.err.println(e);
    }
}