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.ms.commons.test.common.ReflectUtil.java

public static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) {
    try {/*from   w  w  w .  j  av  a 2  s  . c om*/
        Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
        return method;
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        if (clazz == Object.class) {
            return null;
        } else {
            return getDeclaredMethod(clazz.getSuperclass(), methodName, parameterTypes);
        }
    }
}

From source file:edu.stanford.junction.director.JAVADirector.java

/**
 * Method to Open the Browser with Given URL
 * @param url/*  w ww.  j a  v a 2 s  .  c  o m*/
 */
public static Process openUrl(String url) {
    String os = System.getProperty("os.name");
    Runtime runtime = Runtime.getRuntime();
    try {
        // Block for Windows Platform
        if (os.startsWith("Windows")) {
            String cmd = "rundll32 url.dll,FileProtocolHandler " + url;
            Process p = runtime.exec(cmd);
            return p;
        }
        //Block for Mac OS
        else if (os.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 });
            return null; // TODO find a better way
        }
        //Block for UNIX Platform 
        else {
            String[] browsers = { "firefox", "chrome", "opera", "konqueror", "epiphany", "mozilla",
                    "netscape" };
            String browser = null;
            for (int count = 0; count < browsers.length && browser == null; count++)
                if (runtime.exec(new String[] { "which", browsers[count] }).waitFor() == 0)
                    browser = browsers[count];
            if (browser == null)
                throw new Exception("Could not find web browser");
            else
                return runtime.exec(new String[] { browser, url });
        }
    } catch (Exception x) {
        System.err.println("Exception occurd while invoking Browser!");
        x.printStackTrace();
        return null;
    }
}

From source file:com.eucalyptus.upgrade.TestHarness.java

@SuppressWarnings("unchecked")
private static void runMethods(final CommandLine cmd, final Options opts) {
    String[] optVals = cmd.getOptionValues("test");
    for (int i = 0; i < optVals.length; i++) {
        String[] argParts = optVals[i].split(":");
        String className = argParts[0];
        String methodName = argParts[1];
        String[] methodArgsArray = new String[0];
        if (argParts.length > 2) {
            methodArgsArray = argParts[2].split(",");
            System.out.printf("Executing class:%s method:%s args:%s\n", className, methodName, argParts[2]);
        } else {/*from  www.ja v a2 s  . co m*/
            System.out.printf("Executing class:%s method:%s\n", className, methodName);
        }
        Class[] params = new Class[methodArgsArray.length];
        for (int j = 0; j < params.length; j++) {
            params[j] = String.class;
        }

        try {
            Class clazz = Class.forName(className);
            clazz.getDeclaredMethod(methodName, params).invoke(null, (Object[]) methodArgsArray);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("Executed method");
    }
    // List<Class> testList = Lists.transform( Lists.newArrayList(
    // cmd.getOptionValues( "test" ) ), new Function<String, Class>( ) {
    // public Class apply( String arg ) {
    // String[] argParts = arg.split(":");
    // String className = argParts[0];
    // System.out.println("CLASS NAME:" + className);
    // Class targetClass = null;
    // try {
    // targetClass = Class.forName( className );
    // } catch ( Exception e ) {
    // try {
    // targetClass = Class.forName( className + "Test" );
    // } catch ( Exception e1 ) {
    // }
    // }
    // if( targetClass == null ) {
    // printHelp( opts );
    // System.exit( 1 );
    // } else {
    // for( int i = 1; i < argParts.length; i++ ) {
    // String property = argParts[i].replaceAll("=.*","");
    // String value = argParts[i].replaceAll(".*=","");
    // try {
    // targetClass.getDeclaredMethod( property, String.class ).invoke( null,
    // value );
    // } catch ( Exception e ) {
    // System.out.println( e );
    // System.exit( 1 );
    // }
    // }
    // }
    // return targetClass;
    // }
    // } );
    // return testList;
}

From source file:org.apache.commons.httpclient.contrib.proxy.PluginProxyUtil.java

/**
 * Returns the proxy information for the specified sampleURL using JRE 1.4+
 * specific plugin classes.//from  w  ww .ja v  a2s  .  c  o  m
 *
 * Notes:
 *     Plugin 1.4+ Final added
 *     com.sun.java.browser.net.* classes ProxyInfo & ProxyService...
 *     Use those with JREs => 1.4+
 *
 * @param sampleURL the URL to check proxy settings for
 * @return ProxyHost the host and port of the proxy that should be used
 */
private static HttpHost detectProxySettingsJDK14_JDK15_JDK16(URL sampleURL) {
    HttpHost result = null;
    try {
        // Look around for the 1.4+ plugin proxy detection class...
        // Without it, cannot autodetect...
        Class<?> ProxyServiceClass = Class.forName("com.sun.java.browser.net.ProxyService");
        Method getProxyInfoMethod = ProxyServiceClass.getDeclaredMethod("getProxyInfo",
                new Class[] { URL.class });
        Object proxyInfoArrayObj = getProxyInfoMethod.invoke(null, new Object[] { sampleURL });

        if (proxyInfoArrayObj == null || Array.getLength(proxyInfoArrayObj) == 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("1.4+ reported NULL proxy (no proxy assumed)");
            }
            result = NO_PROXY_HOST;
        } else {
            Object proxyInfoObject = Array.get(proxyInfoArrayObj, 0);
            Class<?> proxyInfoClass = proxyInfoObject.getClass();
            Method getHostMethod = proxyInfoClass.getDeclaredMethod("getHost", (Class[]) null);
            String proxyIP = (String) getHostMethod.invoke(proxyInfoObject, (Object[]) null);
            Method getPortMethod = proxyInfoClass.getDeclaredMethod("getPort", (Class[]) null);
            Integer portInteger = (Integer) getPortMethod.invoke(proxyInfoObject, (Object[]) null);
            int proxyPort = portInteger.intValue();
            if (LOG.isDebugEnabled()) {
                LOG.debug("1.4+ Proxy info get Proxy:" + proxyIP + " get Port:" + proxyPort);
            }
            result = new HttpHost(proxyIP, proxyPort);
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        LOG.debug("Sun Plugin 1.4+ proxy detection class not found, " + "will try failover detection" /*, e*/);
    }
    return result;
}

From source file:org.activiti.idm.engine.IdmEngines.java

protected static void initIdmEngineFromSpringResource(URL resource) {
    try {//from  w w  w.j  a  v a  2 s  .  c o m
        Class<?> springConfigurationHelperClass = ReflectUtil
                .loadClass("org.activiti.idm.spring.SpringIdmConfigurationHelper");
        Method method = springConfigurationHelperClass.getDeclaredMethod("buildDmnEngine",
                new Class<?>[] { URL.class });
        IdmEngine idmEngine = (IdmEngine) method.invoke(null, new Object[] { resource });

        String idmEngineName = idmEngine.getName();
        EngineInfo idmEngineInfo = new EngineInfo(idmEngineName, resource.toString(), null);
        idmEngineInfosByName.put(idmEngineName, idmEngineInfo);
        idmEngineInfosByResourceUrl.put(resource.toString(), idmEngineInfo);

    } catch (Exception e) {
        throw new ActivitiException("couldn't initialize idm engine from spring configuration resource "
                + resource.toString() + ": " + e.getMessage(), e);
    }
}

From source file:org.activiti.form.engine.FormEngines.java

protected static void initFormEngineFromSpringResource(URL resource) {
    try {//from w  ww . j  av a  2 s .  c  o  m
        Class<?> springConfigurationHelperClass = ReflectUtil
                .loadClass("org.activiti.form.spring.SpringFormConfigurationHelper");
        Method method = springConfigurationHelperClass.getDeclaredMethod("buildContentEngine",
                new Class<?>[] { URL.class });
        FormEngine formEngine = (FormEngine) method.invoke(null, new Object[] { resource });

        String formEngineName = formEngine.getName();
        EngineInfo formEngineInfo = new EngineInfo(formEngineName, resource.toString(), null);
        formEngineInfosByName.put(formEngineName, formEngineInfo);
        formEngineInfosByResourceUrl.put(resource.toString(), formEngineInfo);

    } catch (Exception e) {
        throw new ActivitiException("couldn't initialize form engine from spring configuration resource "
                + resource.toString() + ": " + e.getMessage(), e);
    }
}

From source file:Main.java

public static Object setFieldValue(Map<String, String> map, Class<?> cls) throws Exception {
    Field[] fields = cls.getDeclaredFields();
    Object obj = cls.newInstance();
    for (Field field : fields) {
        Class<?> clsType = field.getType();
        String name = field.getName();
        String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
        Method methodSet = cls.getDeclaredMethod(strSet, clsType);
        if (map.containsKey(name)) {
            Object objValue = typeConversion(clsType, map.get(name));
            methodSet.invoke(obj, objValue);
        }/*w  w w  .j  av a 2s.  com*/
    }
    return obj;
}

From source file:com.chintans.venturebox.util.Utils.java

/**
 * Method borrowed from OpenDelta. Using reflection voodoo instead calling
 * the hidden class directly, to dev/test outside of AOSP tree.
 * /* ww w.  ja v  a2s  .  com*/
 * @author Jorrit "Chainfire" Jongma
 * @author The OmniROM Project
 */
public static boolean setPermissions(String path, int mode, int uid, int gid) {
    try {
        Class<?> FileUtils = Utils.class.getClassLoader().loadClass("android.os.FileUtils");
        Method setPermissions = FileUtils.getDeclaredMethod("setPermissions",
                new Class[] { String.class, int.class, int.class, int.class });
        return ((Integer) setPermissions.invoke(null,
                new Object[] { path, Integer.valueOf(mode), Integer.valueOf(uid), Integer.valueOf(gid) }) == 0);
    } catch (Exception e) {
        // A lot of voodoo could go wrong here, return failure instead of
        // crash
        e.printStackTrace();
    }
    return false;
}

From source file:org.activiti.dmn.engine.DmnEngines.java

protected static void initDmnEngineFromSpringResource(URL resource) {
    try {//  w  w  w.  j ava 2s. c o m
        Class<?> springConfigurationHelperClass = ReflectUtil
                .loadClass("org.activiti.dmn.spring.SpringDmnConfigurationHelper");
        Method method = springConfigurationHelperClass.getDeclaredMethod("buildDmnEngine",
                new Class<?>[] { URL.class });
        DmnEngine dmnEngine = (DmnEngine) method.invoke(null, new Object[] { resource });

        String dmnEngineName = dmnEngine.getName();
        EngineInfo dmnEngineInfo = new EngineInfo(dmnEngineName, resource.toString(), null);
        dmnEngineInfosByName.put(dmnEngineName, dmnEngineInfo);
        dmnEngineInfosByResourceUrl.put(resource.toString(), dmnEngineInfo);

    } catch (Exception e) {
        throw new ActivitiException("couldn't initialize dmn engine from spring configuration resource "
                + resource.toString() + ": " + e.getMessage(), e);
    }
}

From source file:org.activiti.engine.ProcessEngines.java

protected static void initProcessEngineFromSpringResource(URL resource) {
    try {//from  ww w  . j  av a 2  s.  c  om
        Class<?> springConfigurationHelperClass = ReflectUtil
                .loadClass("org.activiti.spring.SpringConfigurationHelper");
        Method method = springConfigurationHelperClass.getDeclaredMethod("buildProcessEngine",
                new Class<?>[] { URL.class });
        ProcessEngine processEngine = (ProcessEngine) method.invoke(null, new Object[] { resource });

        String processEngineName = processEngine.getName();
        ProcessEngineInfo processEngineInfo = new ProcessEngineInfoImpl(processEngineName, resource.toString(),
                null);
        processEngineInfosByName.put(processEngineName, processEngineInfo);
        processEngineInfosByResourceUrl.put(resource.toString(), processEngineInfo);

    } catch (Exception e) {
        throw new ActivitiException("couldn't initialize process engine from spring configuration resource "
                + resource.toString() + ": " + e.getMessage(), e);
    }
}