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

static public boolean setPin(Class btClass, BluetoothDevice btDevice, String str) throws Exception {
    try {/*from  w  ww  .  j a  v a2 s  .  co m*/
        Method method = btClass.getDeclaredMethod("setPin", new Class[] { byte[].class });
        Boolean value = (Boolean) method.invoke(btDevice, new Object[] { str.getBytes() });
        Log.e(TAG, "" + value);
    } catch (Exception e) {
        // TODO: handle exception
        Log.e(TAG, "setPin error++");
        return false;
    }
    return true;
}

From source file:Main.java

static public void setPairingConfirmation(Class<?> btClass, BluetoothDevice device, boolean isConfirm)
        throws Exception {
    Method setPairingConfirmation = btClass.getDeclaredMethod("setPairingConfirmation", boolean.class);
    setPairingConfirmation.invoke(device, isConfirm);
}

From source file:Main.java

/**
 * @param clz class name/* w  ww  .  ja va  2  s.co m*/
 * @param funcName function name
 * @return true if the function is in the class, else false
 */
public static boolean existsFunc(Class clz, String funcName, Class parameterTypes) {
    if (null != clz) {
        try {
            return clz.getDeclaredMethod(funcName, parameterTypes) != null;
        } catch (Exception e) {
        }
        if (clz != Object.class) {
            return existsFunc(clz.getSuperclass(), funcName, parameterTypes);
        }
    }
    return false;
}

From source file:BareBonesBrowserLaunch.java

/**
 * Opens the specified web page in the user's default browser
 * //from  www.j  a  va  2s .co m
 * @param url
 *            A web address (URL) of a web page (ex:
 *            "http://www.google.com/")
 */
public static void openURL(String url) {
    try {
        // attempt to use Desktop library from JDK 1.6+
        Class<?> d = Class.forName("java.awt.Desktop");
        d.getDeclaredMethod("browse", new Class[] { java.net.URI.class }).invoke(
                d.getDeclaredMethod("getDesktop").invoke(null), new Object[] { java.net.URI.create(url) });
        // above code mimicks: java.awt.Desktop.getDesktop().browse()
    } catch (Exception ignore) {
        // library not available or failed
        String osName = System.getProperty("os.name");
        try {
            if (osName.startsWith("Mac OS")) {
                Class.forName("com.apple.eio.FileManager")
                        .getDeclaredMethod("openURL", new Class[] { String.class })
                        .invoke(null, new Object[] { url });
            } else if (osName.startsWith("Windows")) {
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            } else { // assume Unix or Linux
                String browser = null;
                for (String b : browsers)
                    if (browser == null && Runtime.getRuntime().exec(new String[] { "which", b })
                            .getInputStream().read() != -1)
                        Runtime.getRuntime().exec(new String[] { browser = b, url });
                if (browser == null)
                    throw new Exception(Arrays.toString(browsers));
            }
        } catch (Exception e) {
            throw new RuntimeException(errMsg + "\n" + e.toString());
        }
    }
}

From source file:Main.java

public static Method getMethod(Class<?> classObject, String methodName, Class... parametersType) {
    Class sCls = classObject.getSuperclass();

    while (sCls != Object.class) {
        try {/*from  ww w.  ja  v a2s .c om*/
            return sCls.getDeclaredMethod(methodName, parametersType);
        } catch (NoSuchMethodException var5) {
            sCls = sCls.getSuperclass();
        }
    }

    throw new RuntimeException("Method not found " + methodName);
}

From source file:BareBonesBrowserLaunch.java

public static void openURL(String url) {
    String osName = System.getProperty("os.name");
    try {/*  w w  w  . j a va  2s.  c  o  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, new Object[] { url });
        } else if (osName.startsWith("Windows"))
            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) {
        JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
    }
}

From source file:Main.java

public static Object getVariableFromMethod(Activity activity, String method) {
    Object value = false;/*  w  w w  .j a  v  a 2 s  .  c o  m*/
    Class c = activity.getClass();
    try {
        Method m = (Method) c.getDeclaredMethod(method, new Class[] {});
        m.setAccessible(true);
        value = m.invoke(activity, new Object[] {});
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

public static Method safelyGetSuperclassMethod(Class<?> cls, String methodName, Class<?>... parametersType) {
    Class<?> sCls = cls.getSuperclass();
    while (sCls != Object.class) {
        try {/*w  w w .j a  v  a 2s  . co m*/
            return sCls.getDeclaredMethod(methodName, parametersType);
        } catch (NoSuchMethodException e) {
            // Just super it again
        }
        sCls = sCls.getSuperclass();
    }
    throw new RuntimeException("Method not found " + methodName);
}

From source file:Main.java

static public boolean setPin(Class btClass, BluetoothDevice btDevice, String str) throws Exception {
    try {//from  www  . j  a  v  a 2 s.com
        Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[] { byte[].class });
        Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[] { str.getBytes() });
        Log.v("tag", "++++++++++++" + returnValue);
        Log.e("returnValue", "" + returnValue);
    } catch (SecurityException e) {
        // throw new RuntimeException(e.getMessage());
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // throw new RuntimeException(e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;

}

From source file:net.fabricmc.loom.task.ProcessModsTask.java

public static void addFile(File file, Object object) {
    try {/* w  ww. j a v  a  2  s  .  co m*/
        URLClassLoader classLoader = (URLClassLoader) object.getClass().getClassLoader();
        Class urlClassLoaderClass = URLClassLoader.class;
        Method method = urlClassLoaderClass.getDeclaredMethod("addURL", URL.class);
        method.setAccessible(true);
        method.invoke(classLoader, file.toURI().toURL());
    } catch (NoSuchMethodException | IllegalAccessException | MalformedURLException
            | InvocationTargetException e) {
        e.printStackTrace();
    }
}