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:de.xirp.ate.ATEManager.java

/**
 * Executes the a the <code>algorithm( )</code> method from a
 * given class, which is indicated by is name.
 * //from  w ww.j a  v a2s.co m
 * @param className
 *            The class to execute the method from.
 */
public static void execute(String className) {
    File clazz = new File(Constants.MAZE_CODE_DIR);
    try {
        URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { clazz.toURI().toURL() });
        Class<?> forName = Class.forName(className, true, classLoader);
        Method declaredMethod = forName.getDeclaredMethod("algorithm", new Class[0]); //$NON-NLS-1$
        declaredMethod.invoke(forName.newInstance(), new Object[0]);
    } catch (Exception e) {
        logClass.error("Error: " + e.getMessage() + Constants.LINE_SEPARATOR, e); //$NON-NLS-1$
    }
}

From source file:com.springframework.beans.BeanUtils.java

/**
 * Find a method with the given method name and the given parameter types,
 * declared on the given class or one of its superclasses. Will return a public,
 * protected, package access, or private method.
 * <p>Checks {@code Class.getDeclaredMethod}, cascading upwards to all superclasses.
 * @param clazz the class to check/*from w  ww .  j av a2  s. c  o m*/
 * @param methodName the name of the method to find
 * @param paramTypes the parameter types of the method to find
 * @return the Method object, or {@code null} if not found
 * @see Class#getDeclaredMethod
 */
public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
    try {
        return clazz.getDeclaredMethod(methodName, paramTypes);
    } catch (NoSuchMethodException ex) {
        if (clazz.getSuperclass() != null) {
            return findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes);
        }
        return null;
    }
}

From source file:ch.ralscha.extdirectspring.util.MethodInfo.java

/**
 * Find a method that is annotated with a specific annotation. Starts with the method
 * and goes up to the superclasses of the class.
 *
 * @param method the starting method/*  w  ww .  j  av  a 2s.  c  om*/
 * @param annotation the annotation to look for
 * @return the method if there is a annotated method, else null
 */
public static Method findMethodWithAnnotation(Method method, Class<? extends Annotation> annotation) {
    if (method.isAnnotationPresent(annotation)) {
        return method;
    }

    Class<?> cl = method.getDeclaringClass();
    while (cl != null && cl != Object.class) {
        try {
            Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
            if (equivalentMethod.isAnnotationPresent(annotation)) {
                return equivalentMethod;
            }
        } catch (NoSuchMethodException e) {
            // do nothing here
        }
        cl = cl.getSuperclass();
    }

    return null;
}

From source file:com.ecyrd.management.SimpleMBean.java

private static Method findGetterSetter(Class<?> clazz, String name, Class<?> parm) {
    try {/*w w w .j  ava  2s  .  c  om*/
        Class<?>[] params = { parm };
        Class<?>[] emptyparms = {};

        Method m = clazz.getDeclaredMethod(name, parm != null ? params : emptyparms);

        return m;
    } catch (Exception e) {
        // There's nothing to do, really - we just return a null.
    }

    return null;
}

From source file:com.rockagen.commons.util.ClassUtil.java

/**
 * obtain method list of specified class If recursively is true, obtain
 * method from all class hierarchy//from w w  w.  j  av a2 s . co m
 * 
 * @param clazz class
 * @param recursively recursively
 * @param methodName method name
 * @param parameterTypes parameter types
 * @return method
 */
public static Method getDeclaredMethod(Class<?> clazz, boolean recursively, String methodName,
        Class<?>... parameterTypes) {

    try {
        return clazz.getDeclaredMethod(methodName, parameterTypes);
    } catch (NoSuchMethodException e) {
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null && recursively) {
            return getDeclaredMethod(superClass, true, methodName, parameterTypes);
        }
    } catch (SecurityException e) {
        log.error("{}", e.getMessage(), e);
    }
    return null;
}

From source file:com.dubsar_dictionary.Dubsar.FAQActivity.java

/**
 * Set Proxy for Android 4.1 and above./*www. ja  v a  2  s .c  om*/
 */
@SuppressWarnings("all")
private static boolean setProxyJBPlus(WebView webview, String host, int port) {
    Log.d(LOG_TAG, "Setting proxy with >= 4.1 API.");

    try {
        Class wvcClass = Class.forName("android.webkit.WebViewClassic");
        Class wvParams[] = new Class[1];
        wvParams[0] = Class.forName("android.webkit.WebView");
        Method fromWebView = wvcClass.getDeclaredMethod("fromWebView", wvParams);
        Object webViewClassic = fromWebView.invoke(null, webview);

        Class wv = Class.forName("android.webkit.WebViewClassic");
        Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
        Object mWebViewCoreFieldInstance = getFieldValueSafely(mWebViewCoreField, webViewClassic);

        Class wvc = Class.forName("android.webkit.WebViewCore");
        Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");
        Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField, mWebViewCoreFieldInstance);

        Class bf = Class.forName("android.webkit.BrowserFrame");
        Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");
        Object sJavaBridge = getFieldValueSafely(sJavaBridgeField, mBrowserFrame);

        Class ppclass = Class.forName("android.net.ProxyProperties");
        Class pparams[] = new Class[3];
        pparams[0] = String.class;
        pparams[1] = int.class;
        pparams[2] = String.class;
        Constructor ppcont = ppclass.getConstructor(pparams);

        Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge");
        Class params[] = new Class[1];
        params[0] = Class.forName("android.net.ProxyProperties");
        Method updateProxyInstance = jwcjb.getDeclaredMethod("updateProxy", params);

        updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance(host, port, null));
    } catch (Exception ex) {
        Log.e(LOG_TAG, "Setting proxy with >= 4.1 API failed with error: " + ex.getMessage());
        return false;
    }

    Log.d(LOG_TAG, "Setting proxy with >= 4.1 API successful!");
    return true;
}

From source file:com.dubsar_dictionary.Dubsar.FAQActivity.java

@SuppressWarnings("all")
private static boolean setProxyICS(WebView webview, String host, int port) {
    try {//from  w  ww .  j  a  v a  2s  . c  o m
        Log.d(LOG_TAG, "Setting proxy with 4.0 API.");

        Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge");
        Class params[] = new Class[1];
        params[0] = Class.forName("android.net.ProxyProperties");
        Method updateProxyInstance = jwcjb.getDeclaredMethod("updateProxy", params);

        Class wv = Class.forName("android.webkit.WebView");
        Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
        Object mWebViewCoreFieldInstance = getFieldValueSafely(mWebViewCoreField, webview);

        Class wvc = Class.forName("android.webkit.WebViewCore");
        Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");
        Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField, mWebViewCoreFieldInstance);

        Class bf = Class.forName("android.webkit.BrowserFrame");
        Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");
        Object sJavaBridge = getFieldValueSafely(sJavaBridgeField, mBrowserFrame);

        Class ppclass = Class.forName("android.net.ProxyProperties");
        Class pparams[] = new Class[3];
        pparams[0] = String.class;
        pparams[1] = int.class;
        pparams[2] = String.class;
        Constructor ppcont = ppclass.getConstructor(pparams);

        updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance(host, port, null));

        Log.d(LOG_TAG, "Setting proxy with 4.0 API successful!");
        return true;
    } catch (Exception ex) {
        Log.e(LOG_TAG, "failed to set HTTP proxy: " + ex);
        return false;
    }
}

From source file:com.mastfrog.acteur.ClasspathResourcePage.java

private static Method findMethod(Class<?> on, String name, Class<?>... params) throws SecurityException {
    Class<?> curr = on;
    //NOTE:  Does not check interfaces
    while (curr != Object.class) {
        try {//from w  w  w. ja va 2  s.  c om
            return curr.getDeclaredMethod(name, params);
        } catch (NoSuchMethodException ex) {
            //                Logger.getLogger(ClasspathResourcePage.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            curr = curr.getSuperclass();
        }
    }
    return null;
}

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

/**
 * Retrieve the <em>default value</em> of a named Annotation attribute, given the {@link Class annotation type}.
 * /* w  ww .ja  va2 s .  com*/
 * @param annotationType
 *            the <em>annotation type</em> for which the default value should be retrieved
 * @param attributeName
 *            the name of the attribute value to retrieve.
 * @return the default value of the named attribute, or <code>null</code> if not found
 * @see #getDefaultValue(Annotation, String)
 */
public static Object getDefaultValue(final Class<? extends Annotation> annotationType,
        final String attributeName) {
    try {
        final Method method = annotationType.getDeclaredMethod(attributeName, new Class[0]);
        return method.getDefaultValue();
    } catch (final Exception ex) {
        return null;
    }
}

From source file:Main.java

public static String getDefaultRealm() throws ClassNotFoundException, NoSuchMethodException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Object kerbConf;//  w  w w .  j  a v a 2s. com
    Class<?> classRef;
    Method getInstanceMethod;
    Method getDefaultRealmMethod;
    if (System.getProperty("java.vendor").contains("IBM")) {
        classRef = Class.forName("com.ibm.security.krb5.internal.Config");
    } else {
        classRef = Class.forName("sun.security.krb5.Config");
    }
    getInstanceMethod = classRef.getMethod("getInstance", new Class[0]);
    kerbConf = getInstanceMethod.invoke(classRef, new Object[0]);
    getDefaultRealmMethod = classRef.getDeclaredMethod("getDefaultRealm", new Class[0]);
    return (String) getDefaultRealmMethod.invoke(kerbConf, new Object[0]);
}