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: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 ww  . j  a  v  a  2 s  . c  o  m
    }
    Method method = clazz.getMethod("add", new Class[] { int.class, int.class });
    System.out.println("Method name: " + method.getName());
}

From source file:com.heliosdecompiler.bootstrapper.Bootstrapper.java

public static void main(String[] args) {
    Options options = new Options();
    options.addOption(Option.builder("Xfu").longOpt("Xforceupdate").desc("Force the patching process").build());
    options.addOption(Option.builder("Xh").longOpt("Xhelp").desc("Help!").build());
    CommandLineParser parser = new DefaultParser();
    try {//from  w w w.  ja v a2s .  c  om
        CommandLine commandLine = parser.parse(options, args, true);
        if (commandLine.hasOption("Xhelp")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java -jar bootstrapper.jar", options);
        } else if (commandLine.hasOption("Xforceupdate")) {
            forceUpdate();
        } else {
            //                boolean shouldStart = true;
            //                ServerSocket socket = null;
            //                try {
            //                    socket = new ServerSocket(21354);
            //                } catch (IOException e) {
            //                    shouldStart = false;
            //                } finally {
            //                    if (socket != null) {
            //                        socket.close();
            //                    }
            //                }

            //                if (shouldStart) {
            String[] forward = commandLine.getArgs();
            HeliosData heliosData = loadHelios();
            System.out.println("Running Helios version " + heliosData.buildNumber);

            System.getProperties().put("com.heliosdecompiler.buildNumber",
                    String.valueOf(heliosData.buildNumber));
            System.getProperties().put("com.heliosdecompiler.version", String.valueOf(heliosData.version));
            System.getProperties().put("com.heliosdecompiler.args", args);

            new Thread(new UpdaterTask(heliosData.buildNumber)).start();

            ClassLoader classLoader = new URLClassLoader(new URL[] { IMPL_FILE.toURI().toURL() }, null);
            Class<?> bootloader = Class.forName(heliosData.mainClass, false, classLoader);
            bootloader.getMethod("main", String[].class).invoke(null, new Object[] { forward });
            //                } else {
            //                    String message = Arrays.asList(forward).stream().collect(Collectors.joining(" "));
            //                    Socket clientSocket = new Socket("127.0.0.1", 21354);
            //                    clientSocket.getOutputStream().write(message.getBytes(StandardCharsets.UTF_8));
            //                    clientSocket.getOutputStream().close();
            //                    clientSocket.close();
            //                }
        }
    } catch (Throwable t) {
        displayError(t);
        System.exit(1);
    }
}

From source file:ClassLoaderDemo0.java

public static void main(String[] argv) {
    System.out.println("ClassLoaderDemo starting");
    ClassLoaderDemo0 loader = new ClassLoaderDemo0();
    Class c = null;
    Object demo;/* www  . j  a v  a 2  s  . co m*/
    try {
        /* Load the "Demo" class from memory */

        System.out.println("About to load class  Demo");
        c = loader.loadClass("Demo", true);
        System.out.println("About to instantiate class Demo");
        demo = c.newInstance();
        System.out.println("Got Demo class loaded: " + demo);

        /* Now try to call a method */

        Method mi = c.getMethod("test", null);
        mi.invoke(demo, null);

    } catch (InvocationTargetException e) {
        // The invoked method threw an exception. We get it
        // wrapped up inside another exception, hence the
        // extra call here:
        e.getTargetException().printStackTrace();
        System.out.println("Could not run test method");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Could not run test method");
    }
}

From source file:com.asprise.imaging.core.Imaging.java

public static void main(String[] args) {
    if (args == null) {
        args = new String[0];
    }//from   w w w. ja va  2s .  c o  m

    // Try UI mode if possible
    try {
        if (!(args.length > 0 && "console".equalsIgnoreCase(args[args.length - 1]))
                && !java.awt.GraphicsEnvironment.isHeadless()) {
            Class<?> cls = Class.forName("com.asprise.imaging.scan.ui.workbench.demo.FrameScanDemo");
            Method meth = cls.getMethod("main", String[].class);
            String[] params = null; // init params accordingly
            meth.invoke(null, (Object) args); // static method doesn't have an instance
            return;
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }

    String copyright = "Copyright Asprise, " + Calendar.getInstance().get(Calendar.YEAR)
            + ". All Rights Reserved. Visit www.asprise.com";
    String version = "Library version: " + getLibraryVersion();

    try {
        JOptionPane.showMessageDialog(null, version, copyright,
                JOptionPane.OK_OPTION | JOptionPane.INFORMATION_MESSAGE);
    } catch (Throwable t) {
        // ignore exception
    }

    System.out.println(copyright);
    System.out.println(version);
    System.out.println(Utils.getEnvInfo(false));
}

From source file:org.eclipse.swt.snippets.SnippetLauncher.java

public static void main(String[] args) {
    File sourceDir = SnippetsConfig.SNIPPETS_SOURCE_DIR;
    boolean hasSource = sourceDir.exists();
    int count = 500;
    if (hasSource) {
        File[] files = sourceDir.listFiles();
        if (files.length > 0)
            count = files.length;/* ww w  .ja  v a2  s. c o  m*/
    }
    for (int i = 1; i < count; i++) {
        if (SnippetsConfig.isPrintingSnippet(i))
            continue; // avoid printing to printer
        String className = "Snippet" + i;
        Class<?> clazz = null;
        try {
            clazz = Class.forName(SnippetsConfig.SNIPPETS_PACKAGE + "." + className);
        } catch (ClassNotFoundException e) {
        }
        if (clazz != null) {
            System.out.println("\n" + clazz.getName());
            if (hasSource) {
                File sourceFile = new File(sourceDir, className + ".java");
                try (FileReader reader = new FileReader(sourceFile);) {
                    char[] buffer = new char[(int) sourceFile.length()];
                    reader.read(buffer);
                    String source = String.valueOf(buffer);
                    int start = source.indexOf("package");
                    start = source.indexOf("/*", start);
                    int end = source.indexOf("* For a list of all");
                    System.out.println(source.substring(start, end - 3));
                    boolean skip = false;
                    String platform = SWT.getPlatform();
                    if (source.contains("PocketPC")) {
                        platform = "PocketPC";
                        skip = true;
                    } else if (source.contains("OpenGL")) {
                        platform = "OpenGL";
                        skip = true;
                    } else if (source.contains("JavaXPCOM")) {
                        platform = "JavaXPCOM";
                        skip = true;
                    } else {
                        String[] platforms = { "win32", "gtk" };
                        for (int p = 0; p < platforms.length; p++) {
                            if (!platforms[p].equals(platform) && source.contains("." + platforms[p])) {
                                platform = platforms[p];
                                skip = true;
                                break;
                            }
                        }
                    }
                    if (skip) {
                        System.out.println("...skipping " + platform + " example...");
                        continue;
                    }
                } catch (Exception e) {
                }
            }
            Method method = null;
            String[] param = SnippetsConfig.getSnippetArguments(i);
            try {
                method = clazz.getMethod("main", param.getClass());
            } catch (NoSuchMethodException e) {
                System.out.println("   Did not find main(String [])");
            }
            if (method != null) {
                try {
                    method.invoke(clazz, new Object[] { param });
                } catch (IllegalAccessException e) {
                    System.out.println("   Failed to launch (illegal access)");
                } catch (IllegalArgumentException e) {
                    System.out.println("   Failed to launch (illegal argument to main)");
                } catch (InvocationTargetException e) {
                    System.out.println("   Exception in Snippet: " + e.getTargetException());
                }
            }
        }
    }
}

From source file:Main.java

public static void setObjValue(String methodName, Object object, String value) {
    try {/*from  w w w.  j a  v a2 s.c o  m*/
        Class<? extends Object> cls = object.getClass();
        cls.getMethod(methodName, String.class).invoke(object, value);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static java.lang.reflect.Method getComplexTypeMethod(Class clazz) {
    try {// ww w  .  j a va  2  s. c o  m
        return clazz.getMethod("_srcName", new Class[0]);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

private static Method getIsVarArgsMethod(Class memberClass) {
    try {// w  ww  .ja v  a  2  s. c  om
        return memberClass.getMethod("isVarArgs", (Class[]) null);
    } catch (NoSuchMethodException e) {
        return null; // pre 1.5 JRE
    }
}

From source file:Main.java

/**
 * @param cls/*from  w ww .j  a v  a 2s  .  co m*/
 * @param argClass
 * @return Method of valueOf with argClass
 */
private static Method valueOfMethod(Class cls) {
    try {
        return cls.getMethod("valueOf", new Class[] { String.class });
    } catch (Throwable t) {
        return null;
    }
}

From source file:GetColor.java

public static void setObjectColor(Object obj, Color color) {
    Class cls = obj.getClass(); //#1

    try {/*from  ww  w . j a  va2 s  .  c  o m*/
        Method method = cls.getMethod("setColor", //#2
                new Class[] { Color.class });

        method.invoke(obj, new Object[] { color }); //#3
    } catch (NoSuchMethodException ex) { //#4
        throw new IllegalArgumentException(cls.getName() + " does not support" + "method setColor(:Color)");
    } catch (IllegalAccessException ex) { //#5
        throw new IllegalArgumentException(
                "Insufficient access permissions to call" + "setColor(:Color) in class " + cls.getName());
    } catch (InvocationTargetException ex) { //#6
        throw new RuntimeException(ex);
    }
}