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:org.activiti.content.engine.ContentEngines.java

protected static void initContentEngineFromSpringResource(URL resource) {
    try {//from w w w. ja v a 2s .  com
        Class<?> springConfigurationHelperClass = ReflectUtil
                .loadClass("org.activiti.content.spring.SpringContentConfigurationHelper");
        Method method = springConfigurationHelperClass.getDeclaredMethod("buildContentEngine",
                new Class<?>[] { URL.class });
        ContentEngine contentEngine = (ContentEngine) method.invoke(null, new Object[] { resource });

        String contentEngineName = contentEngine.getName();
        EngineInfo contentEngineInfo = new EngineInfo(contentEngineName, resource.toString(), null);
        contentEngineInfosByName.put(contentEngineName, contentEngineInfo);
        contentEngineInfosByResourceUrl.put(resource.toString(), contentEngineInfo);

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

From source file:Main.java

private static Method getBooleanColumnSetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    String methodName = null;//from w  ww  . j a va  2s  . co  m
    if (isStartWithIs(field.getName())) {
        methodName = "set" + fieldName.substring(2, 3).toUpperCase() + fieldName.substring(3);
    } else {
        methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    }
    try {
        return entityType.getDeclaredMethod(methodName, field.getType());
    } catch (NoSuchMethodException e) {
        //Logger.d(methodName + " not exist");
    }
    return null;
}

From source file:Main.java

private static Method getBooleanColumnSetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    String methodName = null;/*from  www  .j av  a 2s  .  co  m*/
    if (isStartWithIs(field.getName())) {
        methodName = "set" + fieldName.substring(2, 3).toUpperCase() + fieldName.substring(3);
    } else {
        methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    }
    try {
        return entityType.getDeclaredMethod(methodName, field.getType());
    } catch (NoSuchMethodException e) {
        Log.d("getBooleanColumnSetMethod", methodName + " not exist");
    }
    return null;
}

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

/**
 * Use Sun-specific internal plugin proxy classes for 1.3.X
 * Look around for the 1.3.X plugin proxy detection class. Without it,
 * cannot autodetect...//from w  ww .  j a va  2  s .  co  m
 *
 * @param sampleURL the URL to check proxy settings for
 * @return ProxyHost the host and port of the proxy that should be used
 * @throws ProxyDetectionException if detection failed
 */
private static HttpHost detectProxySettingsJDK13(URL sampleURL) throws ProxyDetectionException {
    HttpHost result = null;
    try {
        // Attempt to discover proxy info by asking internal plugin
        // code to locate proxy path to server sampleURL...
        Class<?> pluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
        Method getDefaultProxyHandlerMethod = pluginProxyHandler.getDeclaredMethod("getDefaultProxyHandler",
                (Class[]) null);
        Object proxyHandlerObj = getDefaultProxyHandlerMethod.invoke(null, (Object[]) null);
        if (proxyHandlerObj != null) {
            Class<?> proxyHandlerClass = proxyHandlerObj.getClass();
            Method getProxyInfoMethod = proxyHandlerClass.getDeclaredMethod("getProxyInfo",
                    new Class[] { URL.class });
            Object proxyInfoObject = getProxyInfoMethod.invoke(proxyHandlerObj, new Object[] { sampleURL });
            if (proxyInfoObject != null) {
                Class<?> proxyInfoClass = proxyInfoObject.getClass();
                Method getProxyMethod = proxyInfoClass.getDeclaredMethod("getProxy", (Class[]) null);
                boolean useProxy = (getProxyMethod.invoke(proxyInfoObject, (Object[]) null) != null);
                if (useProxy) {
                    String proxyIP = (String) getProxyMethod.invoke(proxyInfoObject, (Object[]) null);
                    Method getProxyPortMethod = proxyInfoClass.getDeclaredMethod("getPort", (Class[]) null);
                    Integer portInteger = (Integer) getProxyPortMethod.invoke(proxyInfoObject, (Object[]) null);
                    int proxyPort = portInteger.intValue();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("1.3.X: proxy=" + proxyIP + " port=" + proxyPort);
                    }
                    result = new HttpHost(proxyIP, proxyPort);
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("1.3.X reported NULL for " + "proxyInfo.getProxy (no proxy assumed)");
                    }
                    result = NO_PROXY_HOST;
                }
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("NULL proxyInfo in 1.3.X auto proxy " + "detection, (no proxy assumed)");
                }
                result = NO_PROXY_HOST;
            }
        } else {
            throw new ProxyDetectionException("Sun Plugin 1.3.X failed to provide a default proxy handler");
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        LOG.debug("Sun Plugin 1.3.X proxy detection class not " + "found, will try failover detection" /*, e*/);
    }
    return result;
}

From source file:com.us.reflect.ClassFinder.java

/**
 *  before/*from  w ww .j  a va2s . co  m*/
 * @param invocation
 */
public static void beforeAction(ActionInvocation invc, String actionPath) throws Exception {
    ActionInvocation invocation = (ActionInvocation) invc;
    final ActionProxy proxy = invocation.getProxy();
    Class<?> classFromPath = findClass(actionPath);// ?Class
    Class<?> targetClass = classFromPath == null ? proxy.getAction().getClass() : classFromPath;
    String methodName = findMethodName(actionPath);// ??? checkExist(sessionKey)
    String[] paramers = findParameters(actionPath);
    Class<?>[] paramersType = findParameterTypes(targetClass, paramers);
    Method method = targetClass.getDeclaredMethod(methodName, paramersType);
    Object runner = targetClass.newInstance();
    Field[] allFiled = targetClass.getDeclaredFields();
    for (Field field : allFiled) {
        String beanName = field.getName();
        Object bean = DaoHelper.getBeanById(beanName);
        if (bean != null) {
            BeanUtils.setProperty(runner, beanName, bean);
        }
    }
    method.invoke(runner, getRuntimeArgs(invocation, paramers));
}

From source file:org.apache.niolex.commons.reflect.MethodUtil.java

/**
 * Retrieve the method with the specified name and parameter types from this class including static method.
 * If this method is not found, we will try to look at it from the super class too.
 *
 * @param clazz the class to be used for reflection
 * @param methodName the method name//from  www  .j a  v  a 2  s  .c  om
 * @param parameterTypes the method parameter types
 * @return the method if found
 * @throws SecurityException if a security manager is present and the reflection is rejected
 * @throws ItemNotFoundException if method not found in this class and all of it's super classes
 */
public static final Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
    try {
        return clazz.getDeclaredMethod(methodName, parameterTypes);
    } catch (NoSuchMethodException e) {
        clazz = clazz.getSuperclass();
        if (clazz != null) {
            return getMethod(clazz, methodName, parameterTypes);
        } else {
            throw new ItemNotFoundException("Method not found.", e);
        }
    }
}

From source file:Main.java

public static void setDataEnabled(Context context, boolean enabled) {
    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Class<?> conMgrClass = null;
    Field iConMgrField = null;//  w  w w.  j a  va 2  s .  com
    Object iConMgr = null;
    Class<?> iConMgrClass = null;
    Method setMobileDataEnabledMethod = null;
    try {
        conMgrClass = Class.forName(conMgr.getClass().getName());
        iConMgrField = conMgrClass.getDeclaredField("mService");
        iConMgrField.setAccessible(true);
        iConMgr = iConMgrField.get(conMgr);
        iConMgrClass = Class.forName(iConMgr.getClass().getName());
        setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        setMobileDataEnabledMethod.invoke(iConMgr, enabled);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Method getColumnSetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    Method setMethod = null;/*  ww w. j  a  v a  2 s  . co  m*/
    if (field.getType() == boolean.class) {
        setMethod = getBooleanColumnSetMethod(entityType, field);
    }
    if (setMethod == null) {
        String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
            setMethod = entityType.getDeclaredMethod(methodName, field.getType());
        } catch (NoSuchMethodException e) {
            //Logger.d(methodName + " not exist");
        }
    }

    if (setMethod == null && !Object.class.equals(entityType.getSuperclass())) {
        return getColumnSetMethod(entityType.getSuperclass(), field);
    }
    return setMethod;
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Finds a method on a class./*from   w ww  .java  2s  .c om*/
 *
 * @param type           class containing the method
 * @param methodName     name of the method to search for
 * @param parameterTypes parameter types declared in the method signature
 * @return found method
 */
public static Method getMethod(Class type, String methodName, Class<?>... parameterTypes) {
    Method method = null;
    Class currentType = type;
    while (method == null && !currentType.equals(Object.class)) {
        try {
            method = currentType.getDeclaredMethod(methodName, parameterTypes);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            currentType = currentType.getSuperclass();
        }
    }

    return method;
}

From source file:Main.java

public static Method getColumnSetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    Method setMethod = null;//w w w .j  av a 2s  .co  m
    if (field.getType() == boolean.class) {
        setMethod = getBooleanColumnSetMethod(entityType, field);
    }
    if (setMethod == null) {
        String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
            setMethod = entityType.getDeclaredMethod(methodName, field.getType());
        } catch (NoSuchMethodException e) {
            Log.d("getColumnSetMethod", methodName + " not exist");
        }
    }

    if (setMethod == null && !Object.class.equals(entityType.getSuperclass())) {
        return getColumnSetMethod(entityType.getSuperclass(), field);
    }
    return setMethod;
}