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

/**
 * get the method start with 'get' or 'is'.
 *//*from www. j av a2s .c  om*/
public static Method getGetter(Object bean, String property) {
    Map<String, Method> cache = GETTER_CACHE.get(bean.getClass());
    if (cache == null) {
        cache = new ConcurrentHashMap<>();
        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);
                }
            }
        }
        Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache);
        if (old != null) {
            cache = old;
        }
    }
    return cache.get(property);
}

From source file:GenericReflectionTest.java

public static void printMethod(Method m) {
    String name = m.getName();//from ww  w.j  a v  a  2  s . co m
    System.out.print(Modifier.toString(m.getModifiers()));
    System.out.print(" ");
    printTypes(m.getTypeParameters(), "<", ", ", "> ", true);

    printType(m.getGenericReturnType(), false);
    System.out.print(" ");
    System.out.print(name);
    System.out.print("(");
    printTypes(m.getGenericParameterTypes(), "", ", ", "", false);
    System.out.println(")");
}

From source file:com.netflix.astyanax.util.StringUtils.java

public static <T> String joinClassGettersValues(final T object, String name, Class<T> clazz) {
    Method[] methods = clazz.getDeclaredMethods();

    StringBuilder sb = new StringBuilder();
    sb.append(name).append("[");
    sb.append(org.apache.commons.lang.StringUtils.join(Collections2.transform(
            // Filter any field that does not start with lower case
            // (we expect constants to start with upper case)
            Collections2.filter(Arrays.asList(methods), new Predicate<Method>() {
                @Override/*from   w w w  .  jav a  2 s  . c o m*/
                public boolean apply(Method method) {
                    if ((method.getModifiers() & Modifier.STATIC) == Modifier.STATIC)
                        return false;
                    return org.apache.commons.lang.StringUtils.startsWith(method.getName(), "get");
                }
            }),
            // Convert field to "name=value". value=*** on error
            new Function<Method, String>() {
                @Override
                public String apply(Method method) {
                    Object value;
                    try {
                        value = method.invoke(object);
                    } catch (Exception e) {
                        value = "***";
                    }
                    return org.apache.commons.lang.StringUtils.uncapitalize(
                            org.apache.commons.lang.StringUtils.substring(method.getName(), 3)) + "=" + value;
                }
            }), ","));
    sb.append("]");

    return sb.toString();
}

From source file:com.weibo.motan.demo.client.DemoRpcClient.java

public static DubboBenchmark.BenchmarkMessage prepareArgs()
        throws InvocationTargetException, IllegalAccessException {

    boolean b = true;
    int i = 100000;
    String s = "??";

    DubboBenchmark.BenchmarkMessage.Builder builder = DubboBenchmark.BenchmarkMessage.newBuilder();

    Method[] methods = builder.getClass().getDeclaredMethods();
    for (Method m : methods) {
        if (m.getName().startsWith("setField") && ((m.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC)) {
            Parameter[] params = m.getParameters();
            if (params.length == 1) {
                String n = params[0].getParameterizedType().getTypeName();
                m.setAccessible(true);/*from   w  w  w.ja  v a2  s  .c  om*/
                if (n.endsWith("java.lang.String")) {
                    m.invoke(builder, new Object[] { s });
                } else if (n.endsWith("int")) {
                    m.invoke(builder, new Object[] { i });
                } else if (n.equals("boolean")) {
                    m.invoke(builder, new Object[] { b });
                }

            }
        }
    }

    return builder.build();

}

From source file:com.haulmont.cuba.core.config.type.TypeFactory.java

private static boolean isAcceptableMethod(Class<?> returnType, Method factoryMethod) {
    int modifiers = factoryMethod.getModifiers();
    return Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)
            && returnType.isAssignableFrom(factoryMethod.getReturnType());
}

From source file:org.jiemamy.utils.reflect.ModifierUtil.java

/**
 * {@code abstract}??????//from   ww w  .  j a v a 2  s  . co  m
 * 
 * @param method 
 * @return {@code abstract}???{@code true}?????????{@code false}
 * @throws IllegalArgumentException ?{@code null}???
 */
public static boolean isAbstract(Method method) {
    Validate.notNull(method);
    int mod = method.getModifiers();
    return Modifier.isAbstract(mod);
}

From source file:org.gradle.internal.reflect.JavaMethod.java

private static Method findMethod(Class origTarget, Class target, String name, boolean allowStatic,
        Class<?>[] paramTypes) {
    for (Method method : target.getDeclaredMethods()) {
        if (!allowStatic && Modifier.isStatic(method.getModifiers())) {
            continue;
        }//from www. jav a 2 s . c  o m
        if (method.getName().equals(name) && Arrays.equals(method.getParameterTypes(), paramTypes)) {
            return method;
        }
    }

    Class<?> parent = target.getSuperclass();
    if (parent == null) {
        throw new NoSuchMethodException(String.format("Could not find method %s(%s) on %s.", name,
                StringUtils.join(paramTypes, ", "), origTarget.getSimpleName()));
    } else {
        return findMethod(origTarget, parent, name, allowStatic, paramTypes);
    }
}

From source file:MainClass.java

public static void printMethods(Class cl) {
    Method[] methods = cl.getDeclaredMethods();

    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        Class retType = m.getReturnType();
        Class[] paramTypes = m.getParameterTypes();
        String name = m.getName();
        System.out.print(Modifier.toString(m.getModifiers()));
        System.out.print(" " + retType.getName() + " " + name + "(");
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getName());
        }/*from  w  ww. ja va 2  s.c  o m*/
        System.out.println(");");
    }
}

From source file:Main.java

/**
 * @param context/*from   w w  w.  j av  a  2  s .  c  om*/
 * @param type
 * @param field
 * @param errResId
 * @param methodPrefix
 * @param methodParameters
 */
public static void checkIfMethodExists(Context context, Class<?> type, Field field, int errResId,
        String methodPrefix, Class<?>... methodParameters) {
    try {
        Method m = type.getDeclaredMethod(methodPrefix + getFirstLetterUppercased(field.getName()),
                methodParameters);

        if (!Modifier.isPublic(m.getModifiers()) || Modifier.isStatic(m.getModifiers()))
            throw new RuntimeException(context.getString(errResId, field.getName()));
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(context.getString(errResId, field.getName()));
    }
}

From source file:com.acuityph.commons.util.ClassUtils.java

/**
 * Checks if the given method matches the JavaBean property setter signature.
 *
 * @param method/* w w  w .  j  a  va 2  s .c  o  m*/
 *        the method to check
 * @return {@code true}, if the method matchers the JavaBean property setter signature
 */
public static boolean isPropertySetter(final Method method) {
    Assert.notNull(method, "method is null!");
    final int mod = method.getModifiers();
    final boolean expectsOneParameter = method.getParameterTypes().length == 1;
    final boolean returnsVoid = void.class.equals(method.getReturnType());
    return !isPrivate(mod) && !isStatic(mod) && expectsOneParameter && returnsVoid
            && isPropertySetterName(method.getName());
}