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:net.ostis.sc.memory.SCKeynodesBase.java

protected static void init(SCSession session, Class<? extends SCKeynodesBase> klass) {
    if (log.isDebugEnabled())
        log.debug("Start retrieving keynodes for " + klass);

    try {/*from w  w  w . j a  v  a2  s  .com*/
        //
        // Search default segment for keynodes
        //
        SCSegment defaultSegment = null;
        {
            DefaultSegment annotation = klass.getAnnotation(DefaultSegment.class);
            if (annotation != null) {
                defaultSegment = session.openSegment(annotation.value());
                Validate.notNull(defaultSegment, "Default segment \"{0}\" not found", annotation.value());
                klass.getField(annotation.fieldName()).set(null, defaultSegment);
            }
        }

        Field[] fields = klass.getFields();
        for (Field field : fields) {
            int modifiers = field.getModifiers();

            if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
                Class<?> type = field.getType();
                if (type.equals(SCSegment.class)) {
                    //
                    // We have segment field. Load segment by uri.
                    //
                    SegmentURI annotation = field.getAnnotation(SegmentURI.class);

                    if (annotation != null) {
                        String uri = annotation.value();
                        SCSegment segment = session.openSegment(uri);
                        field.set(null, segment);
                    } else {
                        // May be it already has value?
                        if (log.isWarnEnabled()) {
                            if (field.get(null) == null)
                                log.warn(field + " doesn't have value");
                        }
                    }
                } else {
                    if (!(checkKeynode(session, defaultSegment, field) || checkKeynodeURI(session, field)
                            || checkKeynodesNumberPatternURI(session, klass, field))) {

                        if (log.isWarnEnabled()) {
                            if (field.get(null) == null)
                                log.warn(field + " doesn't have annotations and value");
                        }

                    }
                }
            }
        }
    } catch (Exception e) {
        // TODO: handle
        e.printStackTrace();
    }
}

From source file:com.cloudbees.jenkins.support.impl.LoadStats.java

/**
 * The fields that a {@link LoadStatistics} has change as you move from pre-1.607 to post 1.607, so better to
 * just look and see what there is rather than hard-code.
 *
 * @return the fields that correspond to {@link MultiStageTimeSeries}
 *///from w w  w . ja  v a2 s  .  c o m
private static List<Field> findFields() {
    List<Field> result = new ArrayList<Field>();
    for (Field f : LoadStatistics.class.getFields()) {
        if (Modifier.isPublic(f.getModifiers()) && MultiStageTimeSeries.class.isAssignableFrom(f.getType())
                && f.getAnnotation(Deprecated.class) == null) {
            result.add(f);
        }
    }
    return result;
}

From source file:com.nridge.core.base.field.data.DataBeanBag.java

/**
 * Accepts a POJO containing one or more public fields and
 * creates a DataBag from them.  The DataBeanObject1 test
 * class provides a reference example./*from  ww  w. java2s .  c  o m*/
 *
 * @param anObject POJO instance.
 *
 * @return Data bag instance populated with field information.
 *
 * @throws NSException Thrown if object is null.
 * @throws NoSuchFieldException Thrown if field does not exist.
 * @throws IllegalAccessException Thrown if access is illegal.
 */
public static DataBag fromFieldsToBag(Object anObject)
        throws NSException, NoSuchFieldException, IllegalAccessException {
    DataField dataField;
    boolean isPublicAccess;

    if (anObject == null)
        throw new NSException("Object is null");

    DataBag dataBag = new DataBag(anObject.toString());

    Class<?> objClass = anObject.getClass();
    Field[] fieldArray = objClass.getDeclaredFields();
    for (Field objField : fieldArray) {
        isPublicAccess = Modifier.isPublic(objField.getModifiers());
        if (!isPublicAccess)
            objField.setAccessible(true);
        dataField = reflectField(anObject, objField);
        dataBag.add(dataField);

    }

    return dataBag;
}

From source file:com.cnksi.core.tools.utils.Reflections.java

/**
 * ?private/protected???public?????JDKSecurityManager
 *//*  w w w.ja  v a 2 s  . com*/
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:com.sonatype.security.ldap.api.DeepEqualsBuilder.java

private static void reflectionAppend(Object lhs, Object rhs, Class clazz, EqualsBuilder builder,
        boolean useTransients, String[] excludeFields) {
    while (clazz.getSuperclass() != null) {

        Field[] fields = clazz.getDeclaredFields();
        List excludedFieldList = excludeFields != null ? Arrays.asList(excludeFields) : Collections.EMPTY_LIST;
        AccessibleObject.setAccessible(fields, true);
        for (int i = 0; i < fields.length && builder.isEquals(); i++) {
            Field f = fields[i];
            if (!excludedFieldList.contains(f.getName()) && (f.getName().indexOf('$') == -1)
                    && (useTransients || !Modifier.isTransient(f.getModifiers()))
                    && (!Modifier.isStatic(f.getModifiers()))) {
                try {
                    Object lhsChild = f.get(lhs);
                    Object rhsChild = f.get(rhs);
                    Class testClass = getTestClass(lhsChild, rhsChild);
                    boolean hasEqualsMethod = classHasEqualsMethod(testClass);

                    if (testClass != null && !hasEqualsMethod) {
                        reflectionAppend(lhsChild, rhsChild, testClass, builder, useTransients, excludeFields);
                    } else {
                        builder.append(lhsChild, rhsChild);
                    }//www.  j a v a2  s .  co m
                } catch (IllegalAccessException e) {
                    // this can't happen. Would get a Security exception instead
                    // throw a runtime exception in case the impossible happens.
                    throw new InternalError("Unexpected IllegalAccessException");
                }
            }
        }

        // now for the parent
        clazz = clazz.getSuperclass();
        reflectionAppend(lhs, rhs, clazz, builder, useTransients, excludeFields);
    }
}

From source file:org.apache.bookkeeper.common.conf.ConfigDef.java

/**
 * Build the config definitation of a config class.
 *
 * @param configClass config class/*from w w  w. ja v a  2  s .  c o  m*/
 * @return config definition.
 */
@SuppressWarnings("unchecked")
public static ConfigDef of(Class configClass) {
    ConfigDef.Builder builder = ConfigDef.builder();

    Field[] fields = configClass.getDeclaredFields();
    for (Field field : fields) {
        if (Modifier.isStatic(field.getModifiers()) && field.getType().equals(ConfigKey.class)) {
            field.setAccessible(true);
            try {
                builder.withConfigKey((ConfigKey) field.get(null));
            } catch (IllegalAccessException e) {
                log.error("Illegal to access {}#{}", configClass.getSimpleName(), field.getName(), e);
            }
        }
    }

    return builder.build();
}

From source file:com.klwork.common.utils.ReflectionUtils.java

/**
 * Determine whether the given field is a "public static final" constant.
 * @param field the field to check//  w ww.  jav a  2s . c  om
 */
public static boolean isPublicStatic(Field field) {
    int modifiers = field.getModifiers();
    return (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers));
}

From source file:com.serli.chell.framework.form.FormStructure.java

private static boolean isFormField(Field field, Class<?> fieldType) {
    return ((field.getModifiers() & (Modifier.STATIC | Modifier.FINAL)) == 0) && (fieldType.equals(String.class)
            || fieldType.equals(String[].class) && !field.isAnnotationPresent(HtmlTransient.class));
}

From source file:gumga.framework.presentation.api.CSVGeneratorAPI.java

public static List<Field> getAllAtributes(Class clazz) {
    List<Field> fields = new ArrayList<>();
    Class superClass = clazz.getSuperclass();
    if (superClass != null && !superClass.equals(Object.class)) {
        fields.addAll(getAllAtributes(clazz.getSuperclass()));
    }/*  w w w .  ja  v a  2s  . c  om*/
    for (Field f : clazz.getDeclaredFields()) {
        if (!Modifier.isStatic(f.getModifiers())) {
            fields.add(f);
        }
    }
    return fields;
}

From source file:com.ms.commons.test.common.ReflectUtil.java

public static Field[] getStaticFields(Field[] fields) {
    if (fields == null) {
        return null;
    }//from   w ww . j a va 2s  . c  o m
    List<Field> staticFields = new ArrayList<Field>();
    for (Field field : fields) {
        if (Modifier.isStatic(field.getModifiers())) {
            staticFields.add(field);
        }
    }
    return staticFields.toArray(new Field[0]);
}