Example usage for java.lang Class getDeclaredMethod

List of usage examples for java.lang Class getDeclaredMethod

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:com.lucidtechnics.blackboard.TargetConstructor.java

private static final Class loadClass(byte[] _byteArray) throws Exception {
    //override classDefine (as it is protected) and define the class.
    Class myClass = null;/* w ww . j av  a 2  s .co m*/

    ClassLoader classLoader = getClassLoader();
    Class classLoaderClass = Class.forName("java.lang.ClassLoader");
    Method method = classLoaderClass.getDeclaredMethod("defineClass",
            new Class[] { String.class, byte[].class, int.class, int.class });

    // protected method invocaton
    method.setAccessible(true);

    try {
        Object[] argumentArray = new Object[] { null, _byteArray, new Integer(0),
                new Integer(_byteArray.length) };
        myClass = (Class) method.invoke(classLoader, argumentArray);
    } finally {
        method.setAccessible(false);
    }

    return myClass;
}

From source file:com.eviware.soapui.support.Tools.java

public static void openURL(String url) {
    String osName = System.getProperty("os.name");

    try {/*from  w ww .  java 2s. co m*/
        if (osName.startsWith("Mac OS")) {
            Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
            Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
            openURL.invoke(null, url);
        } else if (osName.startsWith("Windows")) {
            if (url.startsWith("file:")) {
                url = URLDecoder.decode(url.substring(5), "utf-8");
                while (url.startsWith("/")) {
                    url = url.substring(1);
                }

                url = url.replace('/', '\\');

                if (!new File(url).exists()) {
                    UISupport.showErrorMessage("File [" + url + "] not found");
                    return;
                }
            }

            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
        } else { // assume Unix or Linux
            String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
            String browser = null;
            for (int count = 0; count < browsers.length && browser == null; count++) {
                if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) {
                    browser = browsers[count];
                }
            }
            if (browser == null) {
                throw new Exception("Could not find web browser");
            } else {
                Runtime.getRuntime().exec(new String[] { browser, url });
            }
        }
    } catch (Exception e) {
        UISupport.showErrorMessage(e);
    }
}

From source file:android.reflect.ClazzLoader.java

/**
 * ????/*from   ww  w .  j  ava  2s.c  om*/
 */
public static <O, V> V invoke(Class<?> clazz, O o, String methodName, Class<?>[] parameterTypes,
        Object[] args) {
    V v = null;

    if (Assert.notEmpty(methodName)) {
        Class<?> desClazz = clazz != null ? clazz : (o != null ? o.getClass() : null);
        if (desClazz != null) {
            try {
                Method method = desClazz.getDeclaredMethod(methodName, parameterTypes);
                if (method != null) {
                    method.setAccessible(true);

                    v = (V) method.invoke(o, args);
                }
            } catch (Throwable t) {
                Log.e(TAG, "methodName: " + methodName);
                Log.e(TAG, t);
            }
        }
    }

    return v;
}

From source file:com.ocs.dynamo.utils.ClassUtils.java

/**
 * Retrieves the getter method for a certain property
 * /*  w w w. j  a  va 2s  .  c  om*/
 * @param clazz
 *            the class
 * @param fieldName
 *            the name of the property
 * @return
 */
public static Method getGetterMethod(Class<?> clazz, String fieldName) {
    Method method = null;
    if (clazz != null) {
        try {
            // first, try to find a "get" method
            method = clazz.getDeclaredMethod(GET + StringUtils.capitalize(fieldName), new Class[] {});
        } catch (NoSuchMethodException | SecurityException ex) {
            try {
                // next, try to find an "is" method
                method = clazz.getDeclaredMethod(IS + StringUtils.capitalize(fieldName), new Class[] {});
            } catch (NoSuchMethodException | SecurityException ex2) {
                // if that fails, try the superclass
                if (clazz.getSuperclass() != null) {
                    return getGetterMethod(clazz.getSuperclass(), fieldName);
                }
            }
        }
    }
    return method;
}

From source file:azkaban.utils.Utils.java

public static Object invokeStaticMethod(ClassLoader loader, String className, String methodName, Object... args)
        throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
        IllegalAccessException, InvocationTargetException {
    Class<?> clazz = loader.loadClass(className);

    Class<?>[] argTypes = new Class[args.length];
    for (int i = 0; i < args.length; ++i) {
        // argTypes[i] = args[i].getClass();
        argTypes[i] = args[i].getClass();
    }/*from  w w w .  java  2 s  .  co  m*/

    Method method = clazz.getDeclaredMethod(methodName, argTypes);
    return method.invoke(null, args);
}

From source file:com.microsoft.tfs.client.common.ui.browser.BrowserFacade.java

private static void launchWithWorkbenchBrowserSupport(final URI uri, final String title, final String tooltip,
        final String browserId, final LaunchMode launchMode) throws IllegalArgumentException, SecurityException,
        IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    URL url;// www  .j  a v  a 2  s . co m
    try {
        url = uri.toURL();
    } catch (final MalformedURLException e) {
        throw new RuntimeException(e);
    }

    /*
     * compute style int for IWorkbenchBrowserSupport.createBrowser
     */
    int style = 0;
    if (launchMode == LaunchMode.EXTERNAL) {
        style |= (1 << 7); // IWorkbenchBrowserSupport.AS_EXTERNAL
    } else if (launchMode == LaunchMode.INTERNAL) {
        style |= (1 << 5); // IWorkbenchBrowserSupport.AS_EDITOR
    }

    /*
     * Method: IWorkbench#getBrowserSupport
     */
    final Method getBrowserSupportMethod = IWorkbench.class.getDeclaredMethod("getBrowserSupport", //$NON-NLS-1$
            new Class[] {});

    /*
     * Interface: IWorkbenchBrowserSupport
     */
    final Class iWorkbenchBrowserSupportInterface = getBrowserSupportMethod.getReturnType();

    /*
     * Object: workbench browser support object
     */
    final Object workbenchBrowserSupport = getBrowserSupportMethod.invoke(PlatformUI.getWorkbench(),
            (Object[]) null);

    /*
     * Method: IWorkbenchBrowserSupport#createBrowser(int, String, String,
     * String)
     */
    final Method createBrowserMethod = iWorkbenchBrowserSupportInterface.getDeclaredMethod("createBrowser", //$NON-NLS-1$
            new Class[] { Integer.TYPE, String.class, String.class, String.class });

    /*
     * Interface: IWebBrowser
     */
    final Class iWebBrowserInterface = createBrowserMethod.getReturnType();

    /*
     * Object: web browser object
     */
    final Object webBrowser = createBrowserMethod.invoke(workbenchBrowserSupport,
            new Object[] { new Integer(style), browserId, title, tooltip });

    /*
     * Launch the browser: IWebBrowser#openURL
     */
    iWebBrowserInterface.getDeclaredMethod("openURL", new Class[] //$NON-NLS-1$
    { URL.class }).invoke(webBrowser, new Object[] { url });
}

From source file:net.minecraftforge.fml.relauncher.ReflectionHelper.java

/**
 * Finds a method with the specified name and parameters in the given class and makes it accessible.
 * Note: for performance, store the returned value and avoid calling this repeatedly.
 * <p>//w  ww .j  av  a 2  s  .c o  m
 * Throws an exception if the method is not found.
 *
 * @param clazz          The class to find the method on.
 * @param methodName     The name of the method to find (used in developer environments, i.e. "getWorldTime").
 * @param methodObfName  The obfuscated name of the method to find (used in obfuscated environments, i.e. "getWorldTime").
 *                       If the name you are looking for is on a class that is never obfuscated, this should be null.
 * @param parameterTypes The parameter types of the method to find.
 * @return The method with the specified name and parameters in the given class.
 */
@Nonnull
public static Method findMethod(@Nonnull Class<?> clazz, @Nonnull String methodName,
        @Nullable String methodObfName, Class<?>... parameterTypes) {
    Preconditions.checkNotNull(clazz);
    Preconditions.checkArgument(StringUtils.isNotEmpty(methodName), "Method name cannot be empty");

    String nameToFind;
    if (methodObfName == null || (Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment")) {
        nameToFind = methodName;
    } else {
        nameToFind = methodObfName;
    }

    try {
        Method m = clazz.getDeclaredMethod(nameToFind, parameterTypes);
        m.setAccessible(true);
        return m;
    } catch (Exception e) {
        throw new UnableToFindMethodException(e);
    }
}

From source file:cascading.util.Util.java

public static Object invokeStaticMethod(Class type, String methodName, Object[] parameters,
        Class[] parameterTypes) {
    try {/*  w w w  .j a  va  2 s.co m*/
        Method method = type.getDeclaredMethod(methodName, parameterTypes);

        method.setAccessible(true);

        return method.invoke(null, parameters);
    } catch (Exception exception) {
        throw new FlowException("unable to invoke static method: " + type.getName() + "." + methodName,
                exception);
    }
}

From source file:com.aliyun.openservices.odps.console.utils.CommandParserUtils.java

private static String getCommandUsageString(Class<?> commandClass) {
    try {/*from   www.j a v a 2 s.  c  om*/
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(os);
        Method printMethod = commandClass.getDeclaredMethod("printUsage", new Class<?>[] { PrintStream.class });
        printMethod.invoke(null, ps);
        return os.toString("UTF8");

    } catch (Exception e) {
        return null;
    }
}

From source file:net.sf.jabref.gui.openoffice.OpenOfficePanel.java

private static void addURL(List<URL> jarList) throws IOException {
    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class<URLClassLoader> sysclass = URLClassLoader.class;
    try {/*from w  w  w  . ja v  a2s .  c  om*/
        Method method = sysclass.getDeclaredMethod("addURL", CLASS_PARAMETERS);
        method.setAccessible(true);
        for (URL anU : jarList) {
            method.invoke(sysloader, anU);
        }
    } catch (SecurityException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        LOGGER.error("Could not add URL to system classloader", e);
        throw new IOException("Error, could not add URL to system classloader", e);

    }
}