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<? extends BluetoothDevice> btClass, BluetoothDevice btDevice, String str)
        throws Exception {
    try {/*from  w w  w  .  ja  v a2 s . c o m*/
        Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[] { byte[].class });
        Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[] { str.getBytes() });
        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:Main.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Method getMethod(Class clazz, String methodName, final Class[] classes) throws Exception {
    Method method = null;/*from w  w w .ja va2s . co m*/
    try {
        method = clazz.getDeclaredMethod(methodName, classes);
    } catch (NoSuchMethodException e) {
        try {
            method = clazz.getMethod(methodName, classes);
        } catch (NoSuchMethodException ex) {
            if (clazz.getSuperclass() == null) {
                return method;
            } else {
                method = getMethod(clazz.getSuperclass(), methodName, classes);
            }
        }
    }
    return method;
}

From source file:Main.java

public static Object invokeMethod(Object paramObject, String paramString, Class<?>[] paramArrayOfClass,
        Object[] paramArrayOfObject) {
    Class localClass = paramObject.getClass();

    try {/*from w  ww  .ja  v  a 2 s. c  o m*/
        Method localException = localClass.getDeclaredMethod(paramString, paramArrayOfClass);
        localException.setAccessible(true);
        return localException.invoke(paramObject, paramArrayOfObject);
    } catch (Exception var9) {
        Method localMethod = null;

        try {
            if (localClass != null && localClass.getSuperclass() != null) {
                localMethod = localClass.getSuperclass().getDeclaredMethod(paramString, paramArrayOfClass);
                if (localMethod != null) {
                    localMethod.setAccessible(true);
                    return localMethod.invoke(paramObject, paramArrayOfObject);
                }
            }
        } catch (Exception var8) {
            ;
        }

        return null;
    }
}

From source file:com.networknt.audit.ServerInfoDisabledTest.java

static void addURL(URL url) throws Exception {
    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class clazz = URLClassLoader.class;
    // Use reflection
    Method method = clazz.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);/*from  w  ww.  j  av  a 2 s .  com*/
    method.invoke(classLoader, new Object[] { url });
}

From source file:com.networknt.info.ServerInfoDisabledTest.java

static void addURL(URL url) throws Exception {
    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class clazz = URLClassLoader.class;
    // Use reflection
    Method method = clazz.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);/*from  ww w  . j a v  a  2s  . c  o m*/
    method.invoke(classLoader, url);
}

From source file:Main.java

/**
 * Enable/Disable mobile data.// w ww.  j  av  a2s . c om
 * @param context
 * @param enabled
 */
public static void setMobileDataEnabled(Context context, boolean enabled) {
    try {
        final ConnectivityManager conman = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class.forName(conman.getClass().getName());
        final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
        connectivityManagerField.setAccessible(true);
        final Object connectivityManager = connectivityManagerField.get(conman);
        final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = connectivityManagerClass
                .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        // Method call for enabling mobile data..
        setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Attempts to find a method with the given name and argument types on the specified class. Other than Class's
 * getMethod(String, Class...) method, this method will also return protected and private methods.
 *
 * @param clazz         Class supposed to offer the method being searched.
 * @param methodName    Name of the method.
 * @param argumentTypes The arguments found in the method signature, if any.
 * @return a method with the given name and argument types on the specified class, if any.
 *//*from   w  w  w  .j a va 2s  .  c  om*/
public static synchronized Method getMethod(final Class clazz, final String methodName,
        final Class... argumentTypes) {
    try {
        return clazz.getMethod(methodName, argumentTypes);
    } catch (NoSuchMethodException e) {
        /* Ok, so there's no public method... */
    }

    Class runner = clazz;
    while (runner != null) {
        try {
            return runner.getDeclaredMethod(methodName, argumentTypes);
        } catch (NoSuchMethodException e) {
            /* No luck here either */
        }
        runner = runner.getSuperclass();
    }

    /* Still no luck, means there is no suitable method */
    return null;
}

From source file:Main.java

/**
 * opens the URL in a browser.//w  w w  .  ja  v a  2 s  .c  o  m
 * 
 * @param parent the parent component
 * @param url the URL to open
 * @param showDialog whether to display a dialog in case of an error or just
 *          print the error to the console
 */
public static void openURL(Component parent, String url, boolean showDialog) {
    String osName = System.getProperty("os.name");
    try {
        // Mac OS
        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 });
        }
        // Windows
        else if (osName.startsWith("Windows")) {
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
        }
        // assume Unix or Linux
        else {
            String browser = null;
            for (int count = 0; count < LINUX_BROWSERS.length && browser == null; count++) {
                // look for binaries and take first that's available
                if (Runtime.getRuntime().exec(new String[] { "which", LINUX_BROWSERS[count] }).waitFor() == 0) {
                    browser = LINUX_BROWSERS[count];
                    break;
                }
            }
            if (browser == null) {
                throw new Exception("Could not find web browser");
            } else {
                Runtime.getRuntime().exec(new String[] { browser, url });
            }
        }
    } catch (Exception e) {
        String errMsg = "Error attempting to launch web browser:\n" + e.getMessage();

        if (showDialog) {
            JOptionPane.showMessageDialog(parent, errMsg);
        } else {
            System.err.println(errMsg);
        }
    }
}

From source file:com.xhsoft.framework.common.utils.ReflectUtil.java

/**
 * <p>Description:setFieldValue</p>
 * @param target/*  ww w  .  j a v a  2 s.  c  o  m*/
 * @param fname
 * @param ftype
 * @param fvalue
 * @return void
 */
@SuppressWarnings("unchecked")
public static void setFieldValue(Object target, String fname, Class ftype, Object fvalue) {
    if (target == null || fname == null || "".equals(fname)
            || (fvalue != null && !ftype.isAssignableFrom(fvalue.getClass()))) {
        return;
    }

    Class clazz = target.getClass();

    try {
        Method method = clazz
                .getDeclaredMethod("set" + Character.toUpperCase(fname.charAt(0)) + fname.substring(1), ftype);

        if (!Modifier.isPublic(method.getModifiers())) {
            method.setAccessible(true);
        }

        method.invoke(target, fvalue);

    } catch (Exception me) {
        try {
            Field field = clazz.getDeclaredField(fname);

            if (!Modifier.isPublic(field.getModifiers())) {
                field.setAccessible(true);
            }

            field.set(target, fvalue);
        } catch (Exception fe) {

            if (logger.isDebugEnabled()) {
                logger.debug(fe);
            }
        }
    }
}

From source file:org.mongojack.internal.util.JacksonAccessor.java

private static Method findMethod(Class clazz, String name, Class[] argTypes) {
    try {/*from w  w w. j a  v a  2 s .c o m*/
        Method method = clazz.getDeclaredMethod(name, argTypes);
        method.setAccessible(true);
        return method;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
}