Example usage for java.lang.reflect Modifier isPublic

List of usage examples for java.lang.reflect Modifier isPublic

Introduction

In this page you can find the example usage for java.lang.reflect Modifier isPublic.

Prototype

public static boolean isPublic(int mod) 

Source Link

Document

Return true if the integer argument includes the public modifier, false otherwise.

Usage

From source file:Main.java

private static void getClassModifier(Class clazz) {
    int modifier = clazz.getModifiers();

    if (Modifier.isPublic(modifier)) {
        System.out.println(clazz.getName() + " class modifier is public");
    }/*from  w  ww  .j  a  va2s.  c  o  m*/
}

From source file:Main.java

public static Method[] getSelfMethod(Object o) {
    Class c = o.getClass();/*  ww  w  .j  av a2  s . co  m*/
    Method[] ms = o.getClass().getMethods();
    List list = new ArrayList();
    for (Method m : ms) {
        int mod = m.getModifiers();
        if (m.getDeclaringClass().equals(c) && Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
            list.add(m);
        }

    }
    return (Method[]) list.toArray(new Method[0]);
}

From source file:DeclarationInfoDemo.java

public static void printModifiers(final Class dataType) {
    final int modifiers = dataType.getModifiers();
    if (Modifier.isPrivate(modifiers)) {
        System.out.print("private ");
    }//from   ww  w  .j a  v a 2  s  .co  m
    if (Modifier.isPrivate(modifiers)) {
        System.out.print("private ");
    }
    if (Modifier.isPublic(modifiers)) {
        System.out.print("private ");
    }
    if (Modifier.isAbstract(modifiers)) {
        System.out.print("abstract ");
    }
    if (Modifier.isFinal(modifiers)) {
        System.out.print("final ");
    }
    if (Modifier.isNative(modifiers)) {
        System.out.print("native ");
    }
    if (Modifier.isInterface(modifiers)) {
        System.out.print("interface ");
    }
    if (Modifier.isStatic(modifiers)) {
        System.out.print("static ");
    }
    if (Modifier.isStrict(modifiers)) {
        System.out.print("strict ");
    }
    if (Modifier.isSynchronized(modifiers)) {
        System.out.print("synchronized ");
    }
    if (Modifier.isTransient(modifiers)) {
        System.out.print("transient ");
    }
    if (Modifier.isVolatile(modifiers)) {
        System.out.print("volatile ");
    }
    System.out.println(dataType.getName());
}

From source file:Main.java

private static void init() {
    try {//from   ww w .  ja  v a2  s.  c om
        // read all com.android.internal.R
        Class clazz = Class.forName("com.android.internal.R$layout");
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // public static final
            if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())
                    && Modifier.isFinal(field.getModifiers())) {
                try {
                    int id = field.getInt(null);
                    sSystemLayoutResIds.put(id, field.getName());
                } catch (IllegalAccessException e) {
                }
            }
        }
    } catch (Exception e) {
    }
}

From source file:Main.java

public static <T> Constructor<T> findConstructor(Class<T> paramClass, boolean paramBoolean) {
    Constructor localConstructor;
    try {/*from ww w . ja v a  2 s  .  com*/
        localConstructor = paramClass.getDeclaredConstructor(new Class[0]);
        if (paramBoolean) {
            checkAndFixAccess(localConstructor);
            return localConstructor;
        }
        if (!Modifier.isPublic(localConstructor.getModifiers()))
            throw new IllegalArgumentException("Default constructor for " + paramClass.getName()
                    + " is not accessible (non-public?): not allowed to try modify access via Reflection: can not instantiate type");
    } catch (NoSuchMethodException localNoSuchMethodException) {
        return null;
    } catch (Exception localException) {
        while (true)
            unwrapAndThrowAsIAE(localException, "Failed to find default constructor of class "
                    + paramClass.getName() + ", problem: " + localException.getMessage());
    }
    return localConstructor;
}

From source file:Main.java

/**
 * find all getter and is method and return the value by map.
 *//*from w  w w  . ja v a 2s .  c  o m*/
public static Map<String, Object> getProperties(Object bean) {
    Map<String, Object> map = new HashMap<>();
    for (Method method : bean.getClass().getMethods()) {
        String name = method.getName();
        if (((name.length() > 3 && name.startsWith("get")) || (name.length() > 2 && name.startsWith("is")))
                && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0
                && method.getDeclaringClass() != Object.class) {
            int i = name.startsWith("get") ? 3 : 2;
            String key = name.substring(i, i + 1).toLowerCase() + name.substring(i + 1);
            try {
                map.put(key, method.invoke(bean, new Object[0]));
            } catch (Exception e) {
            }
        }
    }
    return map;
}

From source file:Main.java

public static Object getField(Field field, Object target) {
    if (!Modifier.isPublic(field.getModifiers())) {
        field.setAccessible(true);/*from  ww  w . j a v a  2s  .  c om*/
    }
    try {
        return field.get(target);
    } catch (IllegalAccessException iae) {
        throw new IllegalArgumentException("Could not get field " + field, iae);
    }
}

From source file:Main.java

public static <T> Constructor<T> findConstructor(Class<T> paramClass, boolean paramBoolean)
        throws IllegalArgumentException {
    Constructor localConstructor;
    try {//from   w w  w  .  j  ava2  s  .co m
        localConstructor = paramClass.getDeclaredConstructor(new Class[0]);
        if (paramBoolean) {
            checkAndFixAccess(localConstructor);
            return localConstructor;
        }
        if (!Modifier.isPublic(localConstructor.getModifiers()))
            throw new IllegalArgumentException("Default constructor for " + paramClass.getName()
                    + " is not accessible (non-public?): not allowed to try modify access via Reflection: can not instantiate type");
    } catch (NoSuchMethodException localNoSuchMethodException) {
        return null;
    } catch (Exception localException) {
        while (true)
            unwrapAndThrowAsIAE(localException, "Failed to find default constructor of class "
                    + paramClass.getName() + ", problem: " + localException.getMessage());
    }
    return localConstructor;
}

From source file:Main.java

/**
 * @param context//  ww  w .  j  a  v 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: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 ww. j  a va 2  s .  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;
}