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:ReflectUtils.java

/**
 * Adds all static methods (from Class.getMethodCalls) to the list
 * @param aClass//from www .ja  va 2  s  . c o  m
 * @param list
 * @return number of methods added
 */
public static int addStaticMethods(Class aClass, List<Member> list) {
    Method[] methods = aClass.getMethods();
    for (Method m : methods) {
        if (Modifier.isStatic(m.getModifiers())) {
            list.add(m);
        }
    }
    return methods.length;
}

From source file:Main.java

/**
 * set method is accessible//from  w w  w . jav a 2s . co m
 * 
 * @param method {@link java.lang.reflect.Method}
 */
public static void makeAccessible(final Method method) {
    if (!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
        method.setAccessible(true);
    }
}

From source file:Util.java

private static void fillGetterMethods(Class<?> pojoClass, Map<String, Method> baseMap) {
    if (pojoClass.getSuperclass() != Object.class)
        fillGetterMethods(pojoClass.getSuperclass(), baseMap);

    Method[] methods = pojoClass.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        if (!Modifier.isStatic(m.getModifiers()) && m.getParameterTypes().length == 0
                && m.getReturnType() != null && Modifier.isPublic(m.getModifiers())) {
            String name = m.getName();
            if (name.startsWith(IS))
                baseMap.put(toProperty(IS.length(), name), m);
            else if (name.startsWith(GET))
                baseMap.put(toProperty(GET.length(), name), m);
        }//w w w  .j a v a2  s.c  om
    }
}

From source file:Main.java

public static boolean hasGetterSignature(Method m) {
    // First: static methods can't be getters
    if (Modifier.isStatic(m.getModifiers())) {
        return false;
    }//from  w ww .  j  av  a 2s  . com
    // Must take no args
    Class<?>[] pts = m.getParameterTypes();
    if (pts != null && pts.length != 0) {
        return false;
    }
    // Can't be a void method
    if (Void.TYPE == m.getReturnType()) {
        return false;
    }
    // Otherwise looks ok:
    return true;
}

From source file:Main.java

public static List<Method> findAnnotatedMethods(final Class<?> type,
        final Class<? extends Annotation> annotation) {
    final List<Method> methods = new ArrayList<>();
    Method[] ms = type.getDeclaredMethods();
    for (Method method : ms) {
        // Must not static
        if (Modifier.isStatic(method.getModifiers())) {
            continue;
        }//from w  w w . j a v a  2s .c  o  m
        // Must be public
        if (Modifier.isPublic(method.getModifiers())) {
            continue;
        }
        // Must has only one parameter
        if (method.getParameterTypes().length != 1) {
            continue;
        }
        // Must has annotation
        if (!method.isAnnotationPresent(annotation)) {
            continue;
        }
        methods.add(method);
    }
    return methods;
}

From source file:Main.java

private static Method findMethod(final Class<?> beanType, final Type[] paramTypeHolder,
        final String methodName) {
    for (final Method m : beanType.getMethods()) {
        if (methodName.equals(m.getName()) && !Modifier.isStatic(m.getModifiers())) {
            final Type[] paramTypes = m.getGenericParameterTypes();
            if (paramTypes.length == 1) {
                paramTypeHolder[0] = paramTypes[0];
                return m;
            }/*from www. j  ava2  s.  c  o  m*/
        }
    }
    return null;
}

From source file:net.mindengine.blogix.utils.BlogixUtils.java

private static Method findMethodInClass(Class<?> controllerClass, String methodName) {
    Method[] methods = controllerClass.getMethods();
    for (Method method : methods) {
        if (Modifier.isStatic(method.getModifiers()) && method.getName().equals(methodName)) {
            return method;
        }//from  www .  ja v a  2s .com
    }
    return null;
}

From source file:Main.java

public static Method getGetter(Object bean, String property) {
    Map<String, Method> cache = GETTER_CACHE.get(bean.getClass());
    if (cache == null) {
        cache = new ConcurrentHashMap<String, Method>();
        for (Method method : bean.getClass().getMethods()) {
            if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())
                    && !void.class.equals(method.getReturnType()) && method.getParameterTypes().length == 0) {
                String name = method.getName();
                if (name.length() > 3 && name.startsWith("get")) {
                    cache.put(name.substring(3, 4).toLowerCase() + name.substring(4), method);
                } else if (name.length() > 2 && name.startsWith("is")) {
                    cache.put(name.substring(2, 3).toLowerCase() + name.substring(3), method);
                }//from   w w w  .j av a 2  s .co m
            }
        }
        Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache);
        if (old != null) {
            cache = old;
        }
    }
    return cache.get(property);
}

From source file:com.agileapes.couteau.context.spring.event.impl.GenericTranslationScheme.java

static boolean isGetter(Method method) {
    return Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())
            && method.getParameterTypes().length == 0 && !method.getReturnType().equals(void.class)
            && (method.getName().matches("get[A-Z].*")
                    || (method.getName().matches("is[A-Z].*") && method.getReturnType().equals(boolean.class)));
}

From source file:Main.java

public static String getSetMethod(Class<?> c, String prefix, String postfix) {
    StringWriter sw = new StringWriter();
    if (prefix == null)
        prefix = "";
    if (postfix == null)
        postfix = "";
    Method[] ms = c.getDeclaredMethods();
    if (ms != null && ms.length > 0) {
        for (Method m : ms) {
            String name = m.getName();
            if (name.startsWith("set") && isPublicNoStatic(m.getModifiers())) {
                sw.append(prefix).append(name).append("(").append(postfix).append(");\r\n");
            }//from  w ww  .  j a  va2  s .c  o m
        }
    }
    return sw.toString();
}