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

public static <I, P extends I> Method findMethod(final Object obj, final String name, final P param,
        final Class<I> type) throws NoSuchMethodException {

    Class<?> c = obj.getClass();

    while (c != null && c != Object.class) {
        try {//from w w w  .j  a  va2  s  . c o m
            return c.getDeclaredMethod(name, type);
        } catch (final Exception e) {
        }
        c = c.getSuperclass();
    }

    throw new NoSuchMethodException();

}

From source file:Main.java

public static Object invokeMethod(Object target, Class clazz, String methodName, Class[] paramTypes,
        Object[] paramValues) {/*  w  ww  .j  a  va2s  .  co  m*/
    try {
        Method method = clazz.getDeclaredMethod(methodName, paramTypes);
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        return method.invoke(target, paramValues);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:BrowserLauncher.java

public static void openURL(String url) {
    int os = PlatformDetector.detect();

    try {//from   ww w  .  jav  a  2 s . c om
        if (os == PlatformDetector.MACOS) {
            Class macUtils = Class.forName("com.apple.mrj.MRJFileUtils");
            Method openURL = macUtils.getDeclaredMethod("openURL", new Class[] { String.class });
            openURL.invoke(null, new Object[] { url });
        } else if (os == PlatformDetector.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 void callInjectViews(Object activity) {
    try {/* w  w w  .java 2  s .  c  o  m*/
        Class<?> viewMembersInjectorClass = Class.forName("roboguice.inject.ViewListener$ViewMembersInjector");
        Method injectViewsMethod = viewMembersInjectorClass.getDeclaredMethod("injectViews", Object.class);
        injectViewsMethod.setAccessible(true);
        injectViewsMethod.invoke(null, activity);
    } catch (ClassNotFoundException | InvocationTargetException | IllegalArgumentException
            | IllegalAccessException | SecurityException | NoSuchMethodException e) {
        throw new RuntimeException("Could not invoke RoboGuice method!", e);
    }
}

From source file:Main.java

public static boolean modifyPermissionAutoStart(Context context, boolean enable) {
    try {//from   w  w  w. j  a  va  2 s.  c  om
        Class cls = Class.forName("android.miui.AppOpsUtils");
        if (cls != null) {
            Method declaredMethod = cls.getDeclaredMethod("setApplicationAutoStart",
                    new Class[] { Context.class, String.class, Boolean.TYPE });
            if (declaredMethod != null) {
                declaredMethod.invoke(cls,
                        new Object[] { context, context.getPackageName(), Boolean.valueOf(enable) });
                return true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.clustercontrol.plugin.ClassUtils.java

/**
 * ??URLCLASSPATH??<br/>/*w w w  .j  a v a 2 s.  c om*/
 * @param ?URL
 * @throws IOException
 */
public static void addURL(URL u) throws IOException {
    URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

    for (URL url : sysLoader.getURLs()) {
        if (url.toString().equals(u.toString())) {
            log.info("URL " + u + " is already in CLASSPATH");
            return;
        }
    }

    Class<URLClassLoader> sysClass = URLClassLoader.class;
    try {
        Method method = sysClass.getDeclaredMethod("addURL", new Class[] { URL.class });
        method.setAccessible(true);
        method.invoke(sysLoader, new Object[] { u });
    } catch (Throwable t) {
        log.warn(t.getMessage(), t);
        throw new IOException("could not add URL " + u + " to CLASSPATH");
    }
}

From source file:Main.java

/**
 * @param context//from   w  ww.j a va 2 s .c o  m
 * @param type
 * @param field
 * @param errResId
 * @param methodPrefix
 * @param methodParameters
 */
public static void checkIfMethodExists(Context context, Class<?> type, Field field, int errResId,
        String methodPrefix, Class<?>... methodParameters) {
    try {
        Method m = type.getDeclaredMethod(methodPrefix + getFirstLetterUppercased(field.getName()),
                methodParameters);

        if (!Modifier.isPublic(m.getModifiers()) || Modifier.isStatic(m.getModifiers()))
            throw new RuntimeException(context.getString(errResId, field.getName()));
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(context.getString(errResId, field.getName()));
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static String getDeviceSerial() {
    String serial = "null";
    try {// w  w w. j  a  va 2s.c om
        Class clazz = Class.forName("android.os.Build");
        Class paraTypes = Class.forName("java.lang.String");
        Method method = clazz.getDeclaredMethod("getString", paraTypes);
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        serial = (String) method.invoke(new Build(), "ro.serialno");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return serial;
}

From source file:URLUtilities.java

/**
 * Calls <code>java.net.URLEncoder.encode(String, String)</code> via
 * reflection, if we are running on JRE 1.4 or later, otherwise reverts to
 * the deprecated <code>URLEncoder.encode(String)</code> method.
 *
 * @param s  the string to encode.//  w ww  .  ja  v  a  2 s . com
 * @param encoding  the encoding.
 *
 * @return The encoded string.
 *
 * @since 1.0.6
 */
public static String encode(String s, String encoding) {
    Class c = URLEncoder.class;
    String result = null;
    try {
        Method m = c.getDeclaredMethod("encode", STRING_ARGS_2);
        try {
            result = (String) m.invoke(null, new Object[] { s, encoding });
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    } catch (NoSuchMethodException e) {
        // we're running on JRE 1.3.1 so this is the best we have...
        result = URLEncoder.encode(s);
    }
    return result;
}

From source file:Main.java

public static void callInjectViews(Object activity) {
    try {/* ww  w  .j  a  va2  s  . c o  m*/
        Class<?> viewMembersInjectorClass = Class.forName("roboguice.inject.ViewListener$ViewMembersInjector");
        Method injectViewsMethod = viewMembersInjectorClass.getDeclaredMethod("injectViews", Object.class);
        injectViewsMethod.setAccessible(true);
        injectViewsMethod.invoke(null, activity);
    } catch (ClassNotFoundException e) {
        propagateRuntimeException(e);
    } catch (NoSuchMethodException e) {
        propagateRuntimeException(e);
    } catch (SecurityException e) {
        propagateRuntimeException(e);
    } catch (IllegalAccessException e) {
        propagateRuntimeException(e);
    } catch (IllegalArgumentException e) {
        propagateRuntimeException(e);
    } catch (InvocationTargetException e) {
        propagateRuntimeException(e);
    }
}