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

public static void main(String[] args) {
    try {//w  w w.  j a  va 2 s.c  om
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Object[] data = { "Hello" };
        method.invoke(x, data); // Output: Instance method: Hello
        method = clazz.getMethod("classMethod", (Class<?>[]) null);
        method.invoke(null, (Object[]) null); // Output: Class method
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:X.java

public static void main(String[] args) {
    try {//from  w  ww. java2s .  c  o m
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Type[] exp = method.getGenericExceptionTypes();
        for (Type anno : exp) {
            System.out.println(anno);
        }
        System.out.println();

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

From source file:X.java

public static void main(String[] args) {
    try {/*from w  ww. j  a  va2s . co  m*/
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Class<?>[] exp = method.getExceptionTypes();
        for (Class anno : exp) {
            System.out.println(anno);
        }
        System.out.println();

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

From source file:X.java

public static void main(String[] args) {
    try {/* w  w w . j  av  a  2  s  .c o m*/
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Annotation[] annos = method.getAnnotations();
        for (Annotation anno : annos) {
            System.out.println(anno);
        }
        System.out.println();

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

From source file:me.rgcjonas.portableMinecraftLauncher.Main.java

/**
 * entry point/*www . j a  va 2s .c o  m*/
 * @param args
 */
public static void main(String[] args) {
    File workDir = getWorkingDirectory();

    //ensure the working directory exists
    if (!workDir.exists()) {
        workDir.mkdir();
    }
    File launcherJar = new File(workDir, "launcher.jar");

    //download launcher if it doesn't exist
    if (!launcherJar.exists()) {
        try {
            URL launcherurl = new URL("https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft.jar");
            FileUtils.copyURLToFile(launcherurl, launcherJar);
        } catch (IOException e) {
            // shouldn't happen
            e.printStackTrace();
        }
    }

    URL[] urls;
    try {
        urls = new URL[] { launcherJar.toURI().toURL() };

        //this class loader mustn't be disposed while the launcher is running
        @SuppressWarnings("resource")
        LauncherClassLoader loader = new LauncherClassLoader(urls);

        //run it      
        Class<?> launcherFrame = loader.loadClass("net.minecraft.LauncherFrame");
        launcherFrame.getMethod("main", String[].class).invoke(null, (Object) args);
    } catch (Exception e) {
        // there's nothing we can do in that case but notify the dev
        e.printStackTrace();
    }
}

From source file:javarestart.JavaRestartLauncher.java

public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.out.println("Usage: <URL> {<MainClass>}");
        return;/* ww w  .j  a  v  a2 s .co m*/
    }

    if (args[0].equals("fork")) {
        String[] args2 = new String[args.length - 1];
        for (int i = 0; i < args.length - 1; i++) {
            args2[i] = args[i + 1];
        }
        fork(args2);
        return;
    }

    AppClassloader loader = new AppClassloader(args[0]);
    Thread.currentThread().setContextClassLoader(loader);
    String main;
    JSONObject obj = getJSON(args[0]);
    if (args.length < 2) {
        main = (String) obj.get("main");
    } else {
        main = args[1];
    }

    String splash = (String) obj.get("splash");
    if (splash != null) {
        SplashScreen scr = SplashScreen.getSplashScreen();
        if (scr != null) {
            URL url = loader.getResource(splash);
            scr.setImageURL(url);
        }
    }

    //auto close splash after 45 seconds
    Thread splashClose = new Thread() {
        @Override
        public void run() {
            try {
                sleep(45000);
            } catch (InterruptedException e) {
            }
            SplashScreen scr = SplashScreen.getSplashScreen();
            if ((scr != null) && (scr.isVisible())) {
                scr.close();
            }
        }
    };
    splashClose.setDaemon(true);
    splashClose.start();

    Class mainClass = loader.loadClass(main);
    Method mainMethod = mainClass.getMethod("main", String[].class);
    mainMethod.setAccessible(true);
    mainMethod.invoke(null, new Object[] { new String[0] });
}

From source file:com.domeastudio.util.JarClassLoader.java

public static void main(String[] args) {
    URL[] urls = new URL[] {};
    JarClassLoader jarClassLoader = new JarClassLoader(urls, Thread.currentThread().getContextClassLoader());
    try {// w  ww  . j  a v a 2s.com
        Geometry geometrySource = GeometryFormateHelper.getGeometry("POLYGON((1 1,3 4,6 7,10 45,1 1))");
        Geometry geometryTarget = GeometryFormateHelper.getGeometry("POINT(1 1)");
        //String path=jarClassLoader.getPath("lib/utility-1.0-SNAPSHOT.jar");
        jarClassLoader.addJar("utility-1.0-SNAPSHOT.jar");
        Class<?> clazz = jarClassLoader.loadClass("com.domeastudio.util.gis.SpatialOperationHelper");
        Class[] parameterTypes = new Class[] { Geometry.class, Geometry.class };
        Method method = clazz.getMethod("touches", parameterTypes);
        Boolean f = (Boolean) method.invoke(null, geometrySource, geometryTarget);
        System.out.println(f);
        jarClassLoader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // System.out.println("null:" + readWriteLicense.getLicenseName());
    // System.out.println("" + StringHelper.isEmptyAndBlank(""));
    //System.out.println("" + StringHelper.isEmptyAndBlank(" "));
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String name = "java.lang.String";
    String methodName = "toLowerCase";

    Class cl = Class.forName(name);
    java.lang.reflect.Constructor constructor = cl.getConstructor(new Class[] { String.class });
    Object invoker = constructor.newInstance(new Object[] { "AAA" });
    Class arguments[] = new Class[] {};
    java.lang.reflect.Method objMethod = cl.getMethod(methodName, arguments);
    Object result = objMethod.invoke(invoker, (Object[]) arguments);
    System.out.println(result);//w  w  w .j  av a2  s  .  c o  m
}

From source file:InvokeMain.java

public static void main(String[] argv) {
    //+/*from  w  w w.  ja  va2s  .  c  o m*/
    try {
        // First, find the class.
        Class c = Class.forName("InvokeMain"); // RECURSION
        System.out.println(c);

        // Create the array of Argument Types
        Class[] argTypes = { argv.getClass(), // array is Object!
        };

        // Now find the method
        Method m = c.getMethod("main", argTypes);
        System.out.println(m);

        // Create the actual argument array
        Object passedArgv[] = { argv };

        // Now invoke the method.
        m.invoke(null, passedArgv);

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

From source file:X.java

public static void main(String[] argv) {
    try {//from  w  ww . ja  v  a  2s . c o  m
        Class clX = X.class; // or Class.forName("X");
        // To find a method we need the array of matching Class types.
        Class[] argTypes = { String.class };

        // Now find a Method object for the given method.
        Method worker = clX.getMethod("work", argTypes);

        // To INVOKE the method, we need its actual arguments, as an array.
        Object[] theData = { "Chocolate Chips" };

        // The obvious last step: invoke the method.
        worker.invoke(new X(), theData);
    } catch (Exception e) {
        System.err.println("Invoke() failed: " + e);
    }
}