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.anteam.demo.codec.common.CoderUtil.java

public static Object encode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key)
        throws EncoderException {
    if (source == null || encryptAlgorithm == null) {
        return null;
    }/*from w w  w  . j  a  va 2  s .  c o  m*/
    Object result = source;
    if (source instanceof byte[]) {
        return CoderUtil.encode((byte[]) source, encryptAlgorithm, key);
    } else if (source instanceof String) {
        return CoderUtil.encode((String) source, encryptAlgorithm, key);
    }
    Field[] fields = source.getClass().getDeclaredFields();
    for (Field field : fields) {
        Encrypted encrypted = field.getAnnotation(Encrypted.class);
        if (encrypted != null) {
            ReflectionUtils.makeAccessible(field);
            if (!Modifier.isStatic(field.getModifiers())) {
                try {
                    field.set(source, CoderUtil.encode(field.get(source)));
                } catch (IllegalAccessException e) {
                    LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e);
                }
            }
        }
    }
    return result;
}

From source file:com.anteam.demo.codec.common.CoderUtil.java

public static Object decode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key)
        throws DecoderException {
    if (source == null || encryptAlgorithm == null) {
        return null;
    }//from w w w .  j  a  va 2  s . c  o m
    Object result = source;
    if (source instanceof byte[]) {
        return CoderUtil.decode((byte[]) source, encryptAlgorithm, key);
    } else if (source instanceof String) {
        return CoderUtil.decode((String) source, encryptAlgorithm, key);
    }
    Field[] fields = source.getClass().getDeclaredFields();
    for (Field field : fields) {
        Encrypted encrypted = field.getAnnotation(Encrypted.class);
        if (encrypted != null) {
            ReflectionUtils.makeAccessible(field);
            if (!Modifier.isStatic(field.getModifiers())) {
                try {
                    field.set(source, CoderUtil.decode(field.get(source)));
                } catch (IllegalAccessException e) {
                    LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e);
                }
            }
        }
    }
    return result;
}

From source file:microsoft.exchange.webservices.data.core.service.schema.ServiceObjectSchema.java

/**
 * Adds the schema property names to dictionary.
 *
 * @param type                   The type.
 * @param propertyNameDictionary The property name dictionary.
 *//*w  w  w  . j av a 2s . c o  m*/
protected static void addSchemaPropertyNamesToDictionary(Class<?> type,
        Map<PropertyDefinition, String> propertyNameDictionary) {

    Field[] fields = type.getDeclaredFields();
    for (Field field : fields) {
        int modifier = field.getModifiers();
        if (Modifier.isPublic(modifier) && Modifier.isStatic(modifier)) {
            Object o;
            try {
                o = field.get(null);
                if (o instanceof PropertyDefinition) {
                    PropertyDefinition propertyDefinition = (PropertyDefinition) o;
                    propertyNameDictionary.put(propertyDefinition, field.getName());
                }
            } catch (IllegalArgumentException e) {
                LOG.error(e);

                // Skip the field
            } catch (IllegalAccessException e) {
                LOG.error(e);

                // Skip the field
            }
        }
    }
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static boolean isFinal(Field field) {
    return Modifier.isFinal(field.getModifiers());
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static boolean isStatic(Field field) {
    return Modifier.isStatic(field.getModifiers());
}

From source file:common.utils.ReflectionAssert.java

/**
 * All fields are checked for null values (except the static ones).
 * Private fields are also checked./*from www.ja  v a2  s. c om*/
 * This is NOT recursive, only the values of the first object will be checked.
 * An assertion error will be thrown when a property is null.
 * 
 * @param message    a message for when the assertion fails
 * @param object     the object that will be checked for null values.
 */
public static void assertPropertiesNotNull(String message, Object object) {
    Set<Field> fields = ReflectionUtils.getAllFields(object.getClass());
    for (Field field : fields) {
        if (!Modifier.isStatic(field.getModifiers())) {
            String formattedMessage = formatMessage(message,
                    "Property '" + field.getName() + "' in object '" + object.toString() + "' is null ");
            assertNotNull(formattedMessage, ReflectionUtils.getFieldValue(object, field));
        }
    }

}

From source file:microsoft.exchange.webservices.data.core.service.schema.ServiceObjectSchema.java

/**
 * Initialize schema property names.//from ww w  . j  a  v a  2  s .  c om
 */
public static void initializeSchemaPropertyNames() {
    synchronized (lockObject) {
        for (Class<?> type : ServiceObjectSchema.allSchemaTypes.getMember()) {
            Field[] fields = type.getDeclaredFields();
            for (Field field : fields) {
                int modifier = field.getModifiers();
                if (Modifier.isPublic(modifier) && Modifier.isStatic(modifier)) {
                    Object o;
                    try {
                        o = field.get(null);
                        if (o instanceof PropertyDefinition) {
                            PropertyDefinition propertyDefinition = (PropertyDefinition) o;
                            propertyDefinition.setName(field.getName());
                        }
                    } catch (IllegalArgumentException e) {
                        LOG.error(e);

                        // Skip the field
                    } catch (IllegalAccessException e) {
                        LOG.error(e);

                        // Skip the field
                    }
                }
            }
        }
    }
}

From source file:com.ginema.api.enricher.SensitiveDataExtractor.java

/**
 * Recursive method to enrich the object
 * /*from w ww  .j  a  v  a  2s .  c  o m*/
 * @param o
 * @param holder
 * @throws IllegalAccessException
 */
private static void enrichObjectTree(Object o, SensitiveDataHolder holder) throws Exception {
    for (Field f : o.getClass().getDeclaredFields()) {
        if (!ReflectionUtils.isPrimitive(f)) {
            Method getter = PropertyDescriptorHolder.getGetterMethod(o.getClass(), f.getName());
            if (getter == null && !java.lang.reflect.Modifier.isStatic(f.getModifiers())) {
                throw new IllegalArgumentException("No getter found for property " + f.getName());
            }
            if (getter == null)
                continue;
            Object value = getter.invoke(o, null);

            if (ClassUtils.isAssignable(f.getType(), SensitiveDataField.class)) {
                populateHolderMapByType(holder, (SensitiveDataField<?>) value);
            }
            checkAndEnrichObject(holder, value);
            checkAndEnrichCollection(holder, value);
        }
    }

}

From source file:org.venice.piazza.servicecontroller.messaging.ServiceMessageThreadManagerTest.java

static void setFinalStatic(Field field, Object newValue) throws Exception {
    field.setAccessible(true);//from  w  w  w. ja  v a  2s.  co  m
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

    field.set(null, newValue);

}

From source file:org.apache.atlas.repository.graphdb.titan0.Titan0Database.java

/**
 * Titan loads index backend name to implementation using
 * StandardIndexProvider.ALL_MANAGER_CLASSES But
 * StandardIndexProvider.ALL_MANAGER_CLASSES is a private static final
 * ImmutableMap Only way to inject Solr5Index is to modify this field. So,
 * using hacky reflection to add Sol5Index
 *//*from   ww w . j a  v  a 2s.  co m*/
private static void addSolr5Index() {
    try {
        Field field = StandardIndexProvider.class.getDeclaredField("ALL_MANAGER_CLASSES");
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        Map<String, String> customMap = new HashMap<>(StandardIndexProvider.getAllProviderClasses());
        customMap.put("solr", Solr5Index.class.getName()); // for
                                                           // consistency
                                                           // with Titan
                                                           // 1.0.0
        customMap.put("solr5", Solr5Index.class.getName()); // for backward
                                                            // compatibility
        ImmutableMap<String, String> immap = ImmutableMap.copyOf(customMap);
        field.set(null, immap);

        LOG.debug("Injected solr5 index - {}", Solr5Index.class.getName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}