Example usage for java.lang.reflect Field getModifiers

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

Introduction

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

Prototype

public int getModifiers() 

Source Link

Document

Returns the Java language modifiers for the field represented by this Field object, as an integer.

Usage

From source file:com.l2jfree.util.Introspection.java

private static boolean writeFields(Class<?> c, Object accessor, StringBuilder dest, String eol, boolean init) {
    if (c == null)
        throw new IllegalArgumentException("No class specified.");
    else if (!c.isInstance(accessor))
        throw new IllegalArgumentException(accessor + " is not a " + c.getCanonicalName());
    for (Field f : c.getDeclaredFields()) {
        int mod = f.getModifiers();
        if (Modifier.isStatic(mod))
            continue;
        if (init)
            init = false;//  ww  w. j  a v a  2s. c o m
        else if (eol == null)
            dest.append(", ");
        String fieldName = null;
        final Column column = f.getAnnotation(Column.class);
        if (column != null)
            fieldName = column.name();
        if (StringUtils.isEmpty(fieldName))
            fieldName = f.getName();
        dest.append(fieldName);
        dest.append(" = ");
        try {
            f.setAccessible(true);
            Object val = f.get(accessor);
            if (accessor == val)
                dest.append("this");
            else
                deepToString(val, dest, null);
        } catch (Exception e) {
            dest.append("???");
        } finally {
            try {
                f.setAccessible(false);
            } catch (Exception e) {
                // ignore
            }
        }
        if (eol != null)
            dest.append(eol);
    }
    return !init;
}

From source file:org.apache.hadoop.hive.metastore.VerifyingObjectStore.java

private static void dumpObject(StringBuilder errorStr, String name, Object p, Class<?> c, int level)
        throws IllegalAccessException {
    String offsetStr = repeat("  ", level);
    if (p == null || c == String.class || c.isPrimitive() || ClassUtils.wrapperToPrimitive(c) != null) {
        errorStr.append(offsetStr).append(name + ": [" + p + "]\n");
    } else if (ClassUtils.isAssignable(c, Iterable.class)) {
        errorStr.append(offsetStr).append(name + " is an iterable\n");
        Iterator<?> i1 = ((Iterable<?>) p).iterator();
        int i = 0;
        while (i1.hasNext()) {
            Object o1 = i1.next();
            Class<?> t = o1 == null ? Object.class : o1.getClass(); // ...
            dumpObject(errorStr, name + "[" + (i++) + "]", o1, t, level + 1);
        }//from  www.  j ava2 s  .  c o m
    } else if (c.isArray()) {
        int len = Array.getLength(p);
        Class<?> t = c.getComponentType();
        errorStr.append(offsetStr).append(name + " is an array\n");
        for (int i = 0; i < len; ++i) {
            dumpObject(errorStr, name + "[" + i + "]", Array.get(p, i), t, level + 1);
        }
    } else if (ClassUtils.isAssignable(c, Map.class)) {
        Map<?, ?> c1 = (Map<?, ?>) p;
        errorStr.append(offsetStr).append(name + " is a map\n");
        dumpObject(errorStr, name + ".keys", c1.keySet(), Set.class, level + 1);
        dumpObject(errorStr, name + ".vals", c1.values(), Collection.class, level + 1);
    } else {
        errorStr.append(offsetStr).append(name + " is of type " + c.getCanonicalName() + "\n");
        // TODO: this doesn't include superclass.
        Field[] fields = c.getDeclaredFields();
        AccessibleObject.setAccessible(fields, true);
        for (int i = 0; i < fields.length; i++) {
            Field f = fields[i];
            if (f.getName().indexOf('$') != -1 || Modifier.isStatic(f.getModifiers()))
                continue;
            dumpObject(errorStr, name + "." + f.getName(), f.get(p), f.getType(), level + 1);
        }
    }
}

From source file:ReflectionUtils.java

/**
 * ,DeclaredField./*from  www.  ja v a 2s. co m*/
 */
protected static void makeAccessible(final Field field) {
    if (!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
        field.setAccessible(true);
    }
}

From source file:Debug.java

public static Debug getDebugLevel(Class cls) throws NoSuchFieldException {
    try {//  www .  j  a  v a2  s .co m
        Field fld = cls.getField("debug");
        if (fld.getType() != Debug.class || !Modifier.isStatic(fld.getModifiers()))
            throw new NoSuchFieldException();
        return (Debug) fld.get(null);
    } catch (IllegalArgumentException e) {
        throw new NoSuchFieldException();
    } catch (IllegalAccessException e) {
        throw new NoSuchFieldException();
    } catch (SecurityException e) {
        throw new NoSuchFieldException();
    }
}

From source file:Debug.java

public static void setDebugLevel(Class cls, Debug level) throws NoSuchFieldException {
    try {//from w ww. j a v  a2  s.  co m
        Field fld = cls.getField("debug");
        if (fld.getType() != Debug.class || !Modifier.isStatic(fld.getModifiers()))
            throw new NoSuchFieldException();
        fld.set(null, level);
    } catch (IllegalArgumentException e) {
        throw new NoSuchFieldException();
    } catch (IllegalAccessException e) {
        throw new NoSuchFieldException();
    } catch (SecurityException e) {
        throw new NoSuchFieldException();
    }
}

From source file:com.kangyonggan.cms.util.Reflections.java

/**
 * ?private/protected???public?????JDKSecurityManager
 *///from  w  w w . ja va2 s . c  o m
public static void makeAccessible(Field field) {
    boolean temp = (!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())
            || Modifier.isFinal(field.getModifiers())) && !field.isAccessible();
    if (temp) {
        field.setAccessible(true);
    }
}

From source file:com.Da_Technomancer.crossroads.API.packets.Message.java

private static boolean acceptField(Field f, Class<?> type) {
    int mods = f.getModifiers();
    if (Modifier.isFinal(mods) || Modifier.isStatic(mods) || Modifier.isTransient(mods))
        return false;

    return handlers.containsKey(type);
}

From source file:org.openflamingo.uploader.el.ELService.java

public static Object findConstant(String className, String constantName) throws SystemException {
    try {//from w w w  . j  a  v a 2 s .co  m
        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        Field field = clazz.getField(constantName);
        if ((field.getModifiers() & (Modifier.PUBLIC | Modifier.STATIC)) != (Modifier.PUBLIC
                | Modifier.STATIC)) {
            // throw new SystemException(ErrorCode.E0114, className, constantName);
        }
        return field.get(null);
    } catch (IllegalAccessException ex) {
        throw new SystemException(ex);
    } catch (NoSuchFieldException ex) {
        throw new SystemException(ex);
    } catch (ClassNotFoundException ex) {
        throw new SystemException(ex);
    }
}

From source file:$.Reflections.java

/**
     * ?private/protected???public?????JDKSecurityManager
     *///ww w . j a  va  2 s  .  c o  m
    public static void makeAccessible(Field field) {
        if ((!Modifier.isPublic(field.getModifiers())
                || !Modifier.isPublic(field.getDeclaringClass().getModifiers())
                || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
            field.setAccessible(true);
        }
    }

From source file:Main.java

public static List<Field> getAccessibleFields(Class<?> clazz, Class<?> limit) {
    Package topPackage = clazz.getPackage();
    List<Field> fieldList = new ArrayList<Field>();
    int topPackageHash = topPackage == null ? 0 : topPackage.hashCode();
    boolean top = true;
    do {//from   w w  w.ja v  a 2 s.  com
        if (clazz == null) {
            break;
        }
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            if (top == true) { // add all top declared fields
                fieldList.add(field);
                continue;
            }
            int modifier = field.getModifiers();
            if (Modifier.isPrivate(modifier) == true) {
                continue; // ignore super private fields
            }
            if (Modifier.isPublic(modifier) == true) {
                addFieldIfNotExist(fieldList, field); // add super public methods
                continue;
            }
            if (Modifier.isProtected(modifier) == true) {
                addFieldIfNotExist(fieldList, field); // add super protected methods
                continue;
            }
            // add super default methods from the same package
            Package pckg = field.getDeclaringClass().getPackage();
            int pckgHash = pckg == null ? 0 : pckg.hashCode();
            if (pckgHash == topPackageHash) {
                addFieldIfNotExist(fieldList, field);
            }
        }
        top = false;
    } while ((clazz = clazz.getSuperclass()) != limit);

    return fieldList;
}