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.xwtec.xwserver.util.json.JSONObject.java

private static boolean isTransientField(Field field) {
    return (field.getModifiers() & Modifier.TRANSIENT) == Modifier.TRANSIENT;
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static String getCppFieldGetterDeclaration(Field f, Class<?> relClass) {
    return getCppModifiers(f.getModifiers()) + getCppType(f.getType(), relClass) + " " + "get"
            + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1) + "();";
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static String getCppFieldSetterDeclaration(Field f, Class<?> relClass) {
    return getCppModifiers(f.getModifiers()) + "void set" + f.getName().substring(0, 1).toUpperCase()
            + f.getName().substring(1) + "("
            + addConstRefIndicator(f.getType(), getCppType(f.getType(), relClass)) + " "
            + classToParamNameDecl(f.getType(), 0) + ");";
}

From source file:com.github.helenusdriver.driver.impl.ClassInfoImpl.java

/**
 * Finds all final fields in this class and all super classes up to and
 * excluding the first class that is not annotated with the corresponding
 * entity annotation.//from w w  w.j  a va2s .c o  m
 *
 * @author paouelle
 *
 * @return a non-<code>null</code> map of all final fields and their default
 *         values
 */
private Map<Field, Object> findFinalFields() {
    final Map<Field, Object> ffields = new HashMap<>(8);
    MutableObject<Object> obj = null; // lazy evaluated

    // go up the hierarchy until we hit the class for which we have a default
    // serialization constructor as that class will have its final fields
    // properly initialized by the ctor whereas all others will have their final
    // fields initialized with 0, false, null, ...
    for (Class<? super T> clazz = this.clazz; clazz != constructor.getDeclaringClass(); clazz = clazz
            .getSuperclass()) {
        for (final Field field : clazz.getDeclaredFields()) {
            final int mods = field.getModifiers();

            if (Modifier.isFinal(mods) && !Modifier.isStatic(mods)) {
                field.setAccessible(true); // so we can access its value directly
                if (obj == null) {
                    // instantiates a dummy version and access its value
                    try {
                        // find default ctor even if private
                        final Constructor<T> ctor = this.clazz.getDeclaredConstructor();

                        ctor.setAccessible(true); // in case it was private
                        final T t = ctor.newInstance();

                        obj = new MutableObject<>(t);
                    } catch (NoSuchMethodException | IllegalAccessException | InstantiationException e) {
                        throw new IllegalArgumentException(
                                "unable to instantiate object: " + this.clazz.getName(), e);
                    } catch (InvocationTargetException e) {
                        final Throwable t = e.getTargetException();

                        if (t instanceof Error) {
                            throw (Error) t;
                        } else if (t instanceof RuntimeException) {
                            throw (RuntimeException) t;
                        } else { // we don't expect any of those
                            throw new IllegalArgumentException(
                                    "unable to instantiate object: " + this.clazz.getName(), t);
                        }
                    }
                }
                try {
                    ffields.put(field, field.get(obj.getValue()));
                } catch (IllegalAccessException e) {
                    throw new IllegalArgumentException("unable to access final value for field: "
                            + field.getDeclaringClass().getName() + "." + field.getName(), e);
                }
            }
        }
    }
    return ffields;
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static boolean addGetterMethod(Field f, Class clss, List<Class> classes) {
    if (!Modifier.isPublic(f.getModifiers())) {
        return false;
    }//from ww  w  .  j  a  va  2s.  c om
    if (!f.getType().isPrimitive() && !String.class.equals(f.getType()) && !classes.contains(f.getType())) {
        return false;
    }
    Method ma[] = clss.getMethods();
    for (int i = 0; i < ma.length; i++) {
        Method method = ma[i];
        if (method.getName().equalsIgnoreCase(f.getName())) {
            return false;
        }
        if (method.getName().equalsIgnoreCase("get" + f.getName())) {
            return false;
        }
        if (method.getName().equalsIgnoreCase("set" + f.getName())) {
            return false;
        }
    }
    return true;
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static boolean addSetterMethod(Field f, Class clss, List<Class> classes) {
    if (!Modifier.isPublic(f.getModifiers())) {
        return false;
    }//  www .  j  ava  2s .co  m
    if (!f.getType().isPrimitive() && !String.class.equals(f.getType()) && !classes.contains(f.getType())) {
        return false;
    }
    if (Modifier.isFinal(f.getModifiers())) {
        return false;
    }
    Method ma[] = clss.getMethods();
    for (int i = 0; i < ma.length; i++) {
        Method method = ma[i];
        if (method.getName().equalsIgnoreCase(f.getName())) {
            return false;
        }
        if (method.getName().equalsIgnoreCase("get" + f.getName())) {
            return false;
        }
        if (method.getName().equalsIgnoreCase("set" + f.getName())) {
            return false;
        }
    }
    return true;
}

From source file:net.sf.json.JSONObject.java

private static boolean isTransientField(String name, Class beanClass, JsonConfig jsonConfig) {
    try {//  w w w  .ja v a2s  .com
        Field field = beanClass.getDeclaredField(name);
        if ((field.getModifiers() & Modifier.TRANSIENT) == Modifier.TRANSIENT)
            return true;
        return isTransient(field, jsonConfig);
    } catch (Exception e) {
        log.info("Error while inspecting field " + beanClass + "." + name + " for transient status.", e);
    }
    return false;
}

From source file:com.ms.commons.test.BaseTestCase.java

/**
 * ()/*w w  w .ja v a2 s .com*/
 */
protected void setObject(Object object, String fieldName, Object value) {
    Class<?> clazz = (object.getClass() == Class.class) ? (Class<?>) object : object.getClass();
    try {
        Field field = ReflectUtil.getDeclaredField(clazz, fieldName);
        field.setAccessible(true);
        if (Modifier.isStatic(field.getModifiers())) {
            field.set(null, value);
        } else {
            field.set(object, value);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ms.commons.test.BaseTestCase.java

/**
 * ()/*from   www.j  a  v a2 s .  co m*/
 */
@SuppressWarnings("unchecked")
protected <T> T getObject(Object object, String fieldName) {
    Class<?> clazz = (object.getClass() == Class.class) ? (Class<?>) object : object.getClass();
    try {
        Field field = ReflectUtil.getDeclaredField(clazz, fieldName);
        field.setAccessible(true);
        if (Modifier.isStatic(field.getModifiers())) {
            return (T) field.get(null);
        } else {
            return (T) field.get(object);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.helenusdriver.driver.impl.FieldInfoImpl.java

/**
 * Instantiates a new <code>FieldInfo</code> object not part of a defined
 * table./*from   w w  w .  ja  v  a 2  s  .  com*/
 *
 * @author vasu
 *
 * @param  cinfo the non-<code>null</code> class info for the POJO
 * @param  field the non-<code>null</code> field to create an info object for
 * @throws IllegalArgumentException if unable to find a getter or setter
 *         method for the field of if improperly annotated
 */
FieldInfoImpl(ClassInfoImpl<T> cinfo, Field field) {
    this.clazz = cinfo.getObjectClass();
    this.cinfo = cinfo;
    this.tinfo = null;
    this.declaringClass = field.getDeclaringClass();
    this.field = field;
    field.setAccessible(true); // make it accessible in case we need to
    this.isFinal = Modifier.isFinal(field.getModifiers());
    this.name = field.getName();
    this.type = DataTypeImpl.unwrapOptionalIfPresent(field.getType(), field.getGenericType());
    this.column = null;
    this.persisted = null;
    this.persister = null;
    this.suffix = field.getAnnotation(SuffixKey.class);
    this.mandatory = true; // keyspace suffixes are mandatory fields
    this.index = null; // we don't care about this for keyspace suffixes
    this.partitionKey = null; // we don't care about this for keyspace suffixes
    this.clusteringKey = null; // we don't care about this for keyspace suffixes
    this.typeKey = null; // we don't care about this for keyspace suffixes
    this.multiKeyType = null; // we don't care about this for keyspace suffixes
    this.definition = null; // we don't care about this for keyspace suffixes
    this.decoder = null; // we don't care about this for keyspace suffixes
    this.getter = findGetterMethod(declaringClass);
    this.setter = findSetterMethod(declaringClass);
    this.finalValue = findFinalValue();
}