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.feilong.commons.core.lang.reflect.FieldUtil.java

/**
 *  (?),key fieldNamevalue ./*from   www  .  j a v  a2s  . c o  m*/
 *
 * @param obj
 *            the obj
 * @param excludeFieldNames
 *            ?field names,?nullOrEmpty ?
 * @return the field value map
 * @throws ReflectException
 *             the reflect exception
 */
public static Map<String, Object> getFieldValueMap(Object obj, String[] excludeFieldNames)
        throws ReflectException {

    // (?,)
    Field[] fields = getDeclaredFields(obj);

    Map<String, Object> map = new TreeMap<String, Object>();
    if (Validator.isNotNullOrEmpty(fields)) {
        for (Field field : fields) {
            String fieldName = field.getName();

            if (Validator.isNotNullOrEmpty(excludeFieldNames)
                    && ArrayUtil.isContain(excludeFieldNames, fieldName)) {
                continue;
            }

            int modifiers = field.getModifiers();
            // ??? log
            boolean isPrivateAndStatic = Modifier.isPrivate(modifiers) && Modifier.isStatic(modifiers);
            log.debug("field name:[{}],modifiers:[{}],isPrivateAndStatic:[{}]", fieldName, modifiers,
                    isPrivateAndStatic);

            if (!isPrivateAndStatic) {
                //TODO see org.apache.commons.lang3.reflect.MemberUtils.setAccessibleWorkaround(AccessibleObject)
                field.setAccessible(true);
                try {
                    map.put(fieldName, field.get(obj));
                } catch (Exception e) {
                    log.error(e.getClass().getName(), e);
                    throw new ReflectException(e);
                }
            }
        }
    }
    return map;
}

From source file:net.minecraftforge.common.util.EnumHelper.java

public static void setFailsafeFieldValue(Field field, @Nullable Object target, @Nullable Object value)
        throws Exception {
    field.setAccessible(true);/*w  w  w  .  java 2  s .  co m*/
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    Object fieldAccessor = newFieldAccessor.invoke(reflectionFactory, field, false);
    fieldAccessorSet.invoke(fieldAccessor, target, value);
}

From source file:Dumper.java

/**
 * Predicate to determine if a given field is worth being printed. This
 * method could be overridden to reflect customized policy.
 *
 * @param field the field at stake/*from   w w  w.j av  a 2 s  . c o m*/
 *
 * @return true if found relevant
 */
public static boolean isFieldRelevant(Field field) {
    // We don't print static field since the Dumper is meant for instances
    if (Modifier.isStatic(field.getModifiers())) {
        return false;
    }

    // We don't print non-user visible entities
    if (field.getName().indexOf('$') != -1) {
        return false;
    }

    return true;
}

From source file:com.jdo.CloudContactUtils.java

public static Object cloneEntity(Object obj) {
    Object obj1 = null;//from w  w w.j a v a2s  .com
    try {
        obj1 = obj.getClass().newInstance();

        Field f[] = obj.getClass().getDeclaredFields();

        for (Field f1 : f) {

            if (!java.lang.reflect.Modifier.isStatic(f1.getModifiers())) {

                f1.setAccessible(true);

                f1.set(obj1, f1.get(obj));
            }
        }
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return obj1;

}

From source file:net.servicestack.client.Utils.java

public static Field[] getSerializableFields(Class type) {
    List<Field> fields = new ArrayList<Field>();
    for (Class<?> c = type; c != null; c = c.getSuperclass()) {
        if (c == Object.class)
            break;

        for (Field f : c.getDeclaredFields()) {
            if (Modifier.isStatic(f.getModifiers()))
                continue;
            if (!Modifier.isPublic(f.getModifiers()))
                continue;

            fields.add(f);//from ww  w.  ja va2 s .c o  m
        }
    }
    return fields.toArray(new Field[fields.size()]);
}

From source file:org.apache.hadoop.hbase.fs.HFileSystem.java

/**
 * Add an interceptor on the calls to the namenode#getBlockLocations from the DFSClient
 * linked to this FileSystem. See HBASE-6435 for the background.
 * <p/>// w w w.  j a  va 2  s.c om
 * There should be no reason, except testing, to create a specific ReorderBlocks.
 *
 * @return true if the interceptor was added, false otherwise.
 */
static boolean addLocationsOrderInterceptor(Configuration conf, final ReorderBlocks lrb) {
    if (!conf.getBoolean("hbase.filesystem.reorder.blocks", true)) { // activated by default
        LOG.debug("addLocationsOrderInterceptor configured to false");
        return false;
    }

    FileSystem fs;
    try {
        fs = FileSystem.get(conf);
    } catch (IOException e) {
        LOG.warn("Can't get the file system from the conf.", e);
        return false;
    }

    if (!(fs instanceof DistributedFileSystem)) {
        LOG.debug("The file system is not a DistributedFileSystem. " + "Skipping on block location reordering");
        return false;
    }

    DistributedFileSystem dfs = (DistributedFileSystem) fs;
    DFSClient dfsc = dfs.getClient();
    if (dfsc == null) {
        LOG.warn("The DistributedFileSystem does not contain a DFSClient. Can't add the location "
                + "block reordering interceptor. Continuing, but this is unexpected.");
        return false;
    }

    try {
        Field nf = DFSClient.class.getDeclaredField("namenode");
        nf.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(nf, nf.getModifiers() & ~Modifier.FINAL);

        ClientProtocol namenode = (ClientProtocol) nf.get(dfsc);
        if (namenode == null) {
            LOG.warn("The DFSClient is not linked to a namenode. Can't add the location block"
                    + " reordering interceptor. Continuing, but this is unexpected.");
            return false;
        }

        ClientProtocol cp1 = createReorderingProxy(namenode, lrb, conf);
        nf.set(dfsc, cp1);
        LOG.info("Added intercepting call to namenode#getBlockLocations so can do block reordering"
                + " using class " + lrb.getClass());
    } catch (NoSuchFieldException e) {
        LOG.warn("Can't modify the DFSClient#namenode field to add the location reorder.", e);
        return false;
    } catch (IllegalAccessException e) {
        LOG.warn("Can't modify the DFSClient#namenode field to add the location reorder.", e);
        return false;
    }

    return true;
}

From source file:com.ms.commons.summer.web.util.json.JsonBeanUtils.java

private static boolean isTransientField(String name, Class beanClass) {
    try {//from   ww  w  .  j av  a 2s . com
        Field field = beanClass.getDeclaredField(name);
        return (field.getModifiers() & Modifier.TRANSIENT) == Modifier.TRANSIENT;
    } catch (Exception e) {
        // swallow exception
    }
    return false;
}

From source file:com.sunchenbin.store.feilong.core.lang.reflect.FieldUtil.java

/**
 * ?field?list./*from w w w  . j  av  a 2  s .  c  om*/
 * 
 * <h3>??:</h3>
 * 
 * <blockquote>
 * <ol>
 * <li>{@code if isNullOrEmpty(fields)  return emptyList}</li>
 * <li>(?,) {@link #getAllFields(Object)}</li>
 * <li> <code>excludeFieldNames</code></li>
 * <li> {@link Modifier#isPrivate(int)} and {@link Modifier#isStatic(int)}</li>
 * </ol>
 * </blockquote>
 *
 * @param obj
 *            the obj
 * @param excludeFieldNames
 *            ?field names,?nullOrEmpty ?
 * @return the field value map
 * @see #getAllFields(Object)
 * @since 1.4.0
 */
public static List<Field> getAllFieldList(Object obj, String[] excludeFieldNames) {
    // (?,)
    Field[] fields = getAllFields(obj);

    if (Validator.isNullOrEmpty(fields)) {
        return Collections.emptyList();
    }

    List<Field> fieldList = new ArrayList<Field>();

    for (Field field : fields) {
        String fieldName = field.getName();

        if (Validator.isNotNullOrEmpty(excludeFieldNames)
                && ArrayUtils.contains(excludeFieldNames, fieldName)) {
            continue;
        }

        int modifiers = field.getModifiers();
        // ??? log
        boolean isPrivateAndStatic = Modifier.isPrivate(modifiers) && Modifier.isStatic(modifiers);
        LOGGER.debug("field name:[{}],modifiers:[{}],isPrivateAndStatic:[{}]", fieldName, modifiers,
                isPrivateAndStatic);

        if (!isPrivateAndStatic) {
            fieldList.add(field);
        }
    }
    return fieldList;
}

From source file:com.liferay.cli.support.util.ReflectionUtils.java

/**
 * Determine whether the given field is a "public static final" constant.
 * //from  w  w  w  . ja  va  2s .c o  m
 * @param field the field to check
 */
public static boolean isPublicStaticFinal(final Field field) {
    final int modifiers = field.getModifiers();
    return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers);
}

From source file:com.github.abel533.mapperhelper.EntityHelper.java

/**
 * ?Field/*  w  w w .  j a v  a2 s.  co m*/
 *
 * @param entityClass
 * @param fieldList
 * @return
 */
private static List<Field> getAllField(Class<?> entityClass, List<Field> fieldList) {
    if (fieldList == null) {
        fieldList = new ArrayList<Field>();
    }
    if (entityClass.equals(Object.class)) {
        return fieldList;
    }
    Field[] fields = entityClass.getDeclaredFields();
    for (Field field : fields) {
        //??bug#2
        if (!Modifier.isStatic(field.getModifiers())) {
            fieldList.add(field);
        }
    }
    Class<?> superClass = entityClass.getSuperclass();
    if (superClass != null && !superClass.equals(Object.class) && (superClass.isAnnotationPresent(Entity.class)
            || (!Map.class.isAssignableFrom(superClass) && !Collection.class.isAssignableFrom(superClass)))) {
        return getAllField(entityClass.getSuperclass(), fieldList);
    }
    return fieldList;
}