Example usage for java.lang Class getMethod

List of usage examples for java.lang Class getMethod

Introduction

In this page you can find the example usage for java.lang Class getMethod.

Prototype

@CallerSensitive
public Method getMethod(String name, Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException 

Source Link

Document

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static boolean hasNavigationBar(Context activity) {
    boolean hasNavigationBar = false;
    Resources rs = activity.getResources();
    int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) {
        hasNavigationBar = rs.getBoolean(id);
    }/* w ww.ja  v  a  2 s. c  om*/
    try {
        Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
        Method m = systemPropertiesClass.getMethod("get", String.class);
        String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
        if ("1".equals(navBarOverride)) {
            hasNavigationBar = false;
        } else if ("0".equals(navBarOverride)) {
            hasNavigationBar = true;
        }
    } catch (Exception e) {

    }

    return hasNavigationBar;
}

From source file:Main.java

private static Object invokeMethod(Object object, String methodName, Object[] params, Class... types)
        throws Exception {
    Object out = null;/*from  w  ww  . j av a 2  s.  com*/
    Class c = object instanceof Class ? (Class) object : object.getClass();
    if (types != null) {
        Method method = c.getMethod(methodName, types);
        out = method.invoke(object, params);
    } else {
        Method method = c.getMethod(methodName);
        out = method.invoke(object);
    }
    //System.out.println(object.getClass().getName() + "." + methodName + "() = "+ out);
    return out;
}

From source file:Main.java

/**
 * Encodes the given string./*  w w  w . j  a va  2s. c o m*/
 * Implementation can be used on Android platform also.
 * @param src - string to encode.
 * @return encoded string.
 */
public static String encodeString(String src) {
    try {
        try {
            Class<?> base64class = Class.forName("android.util.Base64");
            Method method = base64class.getMethod("encodeToString", new Class[] { byte[].class, int.class });
            return (String) method.invoke(null, new Object[] { src.getBytes(), 2 });
        } catch (ClassNotFoundException e0) {
            try {
                Class<?> base64class = Class.forName("org.apache.commons.codec.binary.Base64");
                Method method = base64class.getMethod("encodeBase64String", new Class[] { byte[].class });
                return (String) method.invoke(null, new Object[] { src.getBytes() });
            } catch (ClassNotFoundException e1) {
                throw new IllegalStateException("Unable to find class for Base64 encoding.");
            }
        }
    } catch (NoSuchMethodException ex) {
        throw new IllegalStateException("Unable to find method for Base64 encoding.", ex);
    } catch (Exception ex) {
        throw new IllegalStateException("Could not invoke method for Base64 encoding.", ex);
    }
}

From source file:Main.java

/**
 * Given a method, which may come from an interface, and a target class used in the current reflective invocation, find the corresponding target method if there is one. E.g. the method may be <code>IFoo.bar()</code> and the target class may be <code>DefaultFoo</code>. In this case, the method may be <code>DefaultFoo.bar()</code>. This enables attributes on that method to be found.
 * <p>/*from   ww w.j  av a2  s.c  o m*/
 * <b>NOTE:</b> In contrast to {@link org.springframework.aop.support.AopUtils#getMostSpecificMethod}, this method does <i>not</i> resolve Java 5 bridge methods automatically. Call {@link org.springframework.core.BridgeMethodResolver#findBridgedMethod} if bridge method resolution is desirable (e.g. for obtaining metadata from the original method definition).
 * 
 * @param method
 *          the method to be invoked, which may come from an interface
 * @param targetClass
 *          the target class for the current invocation. May be <code>null</code> or may not even implement the method.
 * @return the specific target method, or the original method if the <code>targetClass</code> doesn't implement it or is <code>null</code>
 * @see org.springframework.aop.support.AopUtils#getMostSpecificMethod
 */
public static Method getMostSpecificMethod(Method method, Class<?> targetClass) {
    if (method != null && targetClass != null) {
        try {
            method = targetClass.getMethod(method.getName(), method.getParameterTypes());
        } catch (NoSuchMethodException ex) {
            // Perhaps the target class doesn't implement this method:
            // that's fine, just use the original method.
        }
    }
    return method;
}

From source file:Main.java

/**
 * Gets a method and forces it to be accessible, even if it is not.
 *
 * @param clazz The class from which the method will be got.
 * @param methodName The name of the method.
 * @param parameterTypes The parameter types that the method must match.
 * @return The method, if it is found./*from ww w  .  ja  v  a  2s  . c  o m*/
 * @since 2.0.7
 */
public static Method getForcedAccessibleMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
    Method method;
    try {
        method = clazz.getMethod(methodName, parameterTypes);
    } catch (SecurityException e) {
        throw new RuntimeException("Cannot access method '" + methodName + "' in class '" + clazz.getName()
                + "' for security reasons", e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(
                "The method '" + methodName + "' in class '" + clazz.getName() + "' does not exist", e);
    }
    if (!method.isAccessible()) {
        method.setAccessible(true);
    }
    return method;
}

From source file:Main.java

public static Method getAppMethod(Class cls, Field field) throws Exception {
    char[] ca = field.getName().toCharArray();
    ca[0] = Character.toUpperCase(ca[0]);
    return cls.getMethod("set" + new String(ca), field.getType());
}

From source file:springfox.documentation.swagger.readers.parameter.ParameterAnnotationReader.java

private static Optional<Method> interfaceMethod(Class<?> iface, Method method) {
    try {/*from w ww  . j a  va  2 s  .  c  om*/
        return Optional.of(iface.getMethod(method.getName(), method.getParameterTypes()));
    } catch (NoSuchMethodException ex) {
        return Optional.absent();
    }
}

From source file:Main.java

public static Method findMethodForField(String methodName, Class<?> objectClass, Class<?> paramClass)
        throws NoSuchMethodException {
    try {// w  w  w  .  j av  a  2  s .c o m
        return objectClass.getMethod(methodName, new Class[] { paramClass });
    } catch (NoSuchMethodException ex) {
        Method[] methods = objectClass.getMethods();
        for (Method method : methods) {
            if (method.getName().equalsIgnoreCase(methodName)) {
                return method;
            }
        }
        throw ex;
    }
}

From source file:net.projectmonkey.spring.acl.util.reflect.MethodUtil.java

public static Method getMethod(final Class<?> clazz, final String internalMethod, final Class<?>... argTypes) {
    try {//from   w w w.ja  v  a2 s .  co m
        return clazz.getMethod(internalMethod, argTypes);
    } catch (NoSuchMethodException nsme) {
        throw new AuthorizationServiceException(
                "Object of class '" + clazz + "' does not provide the requested method: " + internalMethod);
    }
}

From source file:ee.ria.xroad.common.conf.globalconf.IdentifierDecoderHelper.java

/**
 * Returns a method from the class and method name string. Assumes, the
 * class name is in form [class].[method]
 *//*from   w  w  w  .j  a v a  2 s . c o  m*/
private static Method getMethodFromClassName(String classAndMethodName, Class<?>... parameterTypes)
        throws Exception {
    int lastIdx = classAndMethodName.lastIndexOf('.');
    if (lastIdx == -1) {
        throw new IllegalArgumentException("classAndMethodName must be in form of <class>.<method>");
    }

    String className = classAndMethodName.substring(0, lastIdx);
    String methodName = classAndMethodName.substring(lastIdx + 1);

    Class<?> clazz = Class.forName(className);
    return clazz.getMethod(methodName, parameterTypes);
}