Example usage for java.lang.reflect Method getModifiers

List of usage examples for java.lang.reflect Method getModifiers

Introduction

In this page you can find the example usage for java.lang.reflect Method getModifiers.

Prototype

@Override
public int getModifiers() 

Source Link

Usage

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Invoke the given setter on the given object.
 *///ww  w  . j ava2 s.  c o m
public static void set(Object target, Method setter, Object value) {
    if (target == null || setter == null)
        return;
    makeAccessible(setter, setter.getModifiers());
    try {
        setter.invoke(target, new Object[] { value });
    } catch (Throwable t) {
        throw wrapReflectionException(t, _loc.get("set-method",
                new Object[] { target, setter, value, value == null ? "" : value.getClass() }));
    }
}

From source file:com.tmind.framework.pub.utils.MethodUtils.java

public static Method findMethod(Class start, String methodName, int argCount) {
    // For overridden methods we need to find the most derived version.
    // So we start with the given class and walk up the superclass chain.
    for (Class cl = start; cl != null; cl = cl.getSuperclass()) {
        Method methods[] = getPublicDeclaredMethods(cl);
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method == null) {
                continue;
            }/*from   w ww . j  av a 2  s  . com*/
            // skip static methods.
            int mods = method.getModifiers();
            if (Modifier.isStatic(mods)) {
                continue;
            }
            if (method.getName().equals(methodName) && method.getParameterTypes().length == argCount) {
                return method;
            }
        }
    }

    // Now check any inherited interfaces.  This is necessary both when
    // the argument class is itself an interface, and when the argument
    // class is an abstract class.
    Class ifcs[] = start.getInterfaces();
    for (int i = 0; i < ifcs.length; i++) {
        Method m = findMethod(ifcs[i], methodName, argCount);
        if (m != null) {
            return m;
        }
    }

    return null;
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Return the return value of the given getter in the given object.
 *//*from  w w w.  jav a  2  s.  c  om*/
public static Object get(Object target, Method getter) {
    if (target == null || getter == null)
        return null;
    makeAccessible(getter, getter.getModifiers());
    try {
        return getter.invoke(target, (Object[]) null);
    } catch (Throwable t) {
        throw wrapReflectionException(t);
    }
}

From source file:com.laidians.utils.ClassUtils.java

/**
 * Determine whether the given method is overridable in the given target class.
 * @param method the method to check/*from  w w w.  ja  v a 2 s  .co m*/
 * @param targetClass the target class to check against
 */
private static boolean isOverridable(Method method, Class targetClass) {
    if (Modifier.isPrivate(method.getModifiers())) {
        return false;
    }
    if (Modifier.isPublic(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
        return true;
    }
    return getPackageName(method.getDeclaringClass()).equals(getPackageName(targetClass));
}

From source file:com.tmind.framework.pub.utils.MethodUtils.java

public static Method findMethod(Class start, String methodName, int argCount, Class args[]) {
    // For overriden methods we need to find the most derived version.
    // So we start with the given class and walk up the superclass chain.
    for (Class cl = start; cl != null; cl = cl.getSuperclass()) {
        Method methods[] = getPublicDeclaredMethods(cl);
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method == null) {
                continue;
            }/*ww  w  .j a  va 2  s  .  c  om*/
            // skip static methods.
            int mods = method.getModifiers();
            if (Modifier.isStatic(mods)) {
                continue;
            }
            // make sure method signature matches.
            Class params[] = method.getParameterTypes();
            if (method.getName().equals(methodName) && params.length == argCount) {
                boolean different = false;
                if (argCount > 0) {
                    for (int j = 0; j < argCount; j++) {
                        if (params[j] != args[j]) {
                            different = true;
                            continue;
                        }
                    }
                    if (different) {
                        continue;
                    }
                }
                return method;
            }
        }
    }

    // Now check any inherited interfaces.  This is necessary both when
    // the argument class is itself an interface, and when the argument
    // class is an abstract class.
    Class ifcs[] = start.getInterfaces();
    for (int i = 0; i < ifcs.length; i++) {
        Method m = findMethod(ifcs[i], methodName, argCount);
        if (m != null) {
            return m;
        }
    }

    return null;
}

From source file:h2o.common.spring.util.ClassUtils.java

/**
 * Determine whether the given method is overridable in the given target class.
 * @param method the method to check/*from   ww w  .  j a  v a  2 s.  c o m*/
 * @param targetClass the target class to check against
 */
@SuppressWarnings("rawtypes")
private static boolean isOverridable(Method method, Class targetClass) {
    if (Modifier.isPrivate(method.getModifiers())) {
        return false;
    }
    if (Modifier.isPublic(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
        return true;
    }
    return getPackageName(method.getDeclaringClass()).equals(getPackageName(targetClass));
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Invoke the given setter on the given object.
 *///  w  w  w .  ja  v a2  s .  c  om
public static void set(Object target, Method setter, Object value) {
    if (target == null || setter == null)
        return;
    makeAccessible(setter, setter.getModifiers());
    try {
        setter.invoke(target, new Object[] { value });
    } catch (Throwable t) {
        throw wrapReflectionException(t);
    }
}

From source file:JarClassLoader.java

public void invokeClass(String name, String[] args)
        throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
    Class c = loadClass(name);/*  w  ww. j a v  a  2s  .  co  m*/
    Method m = c.getMethod("main", new Class[] { args.getClass() });
    m.setAccessible(true);
    int mods = m.getModifiers();
    if (m.getReturnType() != void.class || !Modifier.isStatic(mods) || !Modifier.isPublic(mods)) {
        throw new NoSuchMethodException("main");
    }
    try {
        m.invoke(null, new Object[] { args });
    } catch (IllegalAccessException e) {

    }
}

From source file:com.tmind.framework.pub.utils.MethodUtils.java

/**
 * <p>Return an accessible method (that is, one that can be invoked via
 * reflection) that implements the specified Method.  If no such method
 * can be found, return <code>null</code>.</p>
 *
 * @param method The method that we wish to call
 *//* ww  w.  ja v a 2 s  .c o m*/
public static Method getAccessibleMethod(Method method) {

    // Make sure we have a method to check
    if (method == null) {
        return (null);
    }

    // If the requested method is not public we cannot call it
    if (!Modifier.isPublic(method.getModifiers())) {
        return (null);
    }

    // If the declaring class is public, we are done
    Class clazz = method.getDeclaringClass();
    if (Modifier.isPublic(clazz.getModifiers())) {
        return (method);
    }

    // Check the implemented interfaces and subinterfaces
    method = getAccessibleMethodFromInterfaceNest(clazz, method.getName(), method.getParameterTypes());
    return (method);

}

From source file:net.paslavsky.springrest.SpringRestClientAOPAdvisor.java

@Override
public boolean matches(Method method, Class<?> targetClass) {
    return !ReflectionUtils.isObjectMethod(method) && Modifier.isAbstract(method.getModifiers());
}