Example usage for java.lang.reflect Modifier isFinal

List of usage examples for java.lang.reflect Modifier isFinal

Introduction

In this page you can find the example usage for java.lang.reflect Modifier isFinal.

Prototype

public static boolean isFinal(int mod) 

Source Link

Document

Return true if the integer argument includes the final modifier, false otherwise.

Usage

From source file:de.bstreit.java.oscr.initialdata.AbstractDataContainer.java

/**
 * //from   w  ww. ja  v a  2  s.co  m
 * @param field
 * @return true, if the field is a constant (public static final) and of the
 *         expected type provided by {@link #getType()}
 */
private boolean isConstantAndOfType(Field field) {
    final int modifiers = field.getModifiers();

    final boolean isConstant = Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)
            && Modifier.isPublic(modifiers);

    final boolean correctType = field.getType().equals(getType());

    return isConstant && correctType;
}

From source file:adalid.core.Key.java

private void finaliseFields() {
    String name;//from   w  w  w.j  av  a  2s  .c om
    Class<?> type;
    int modifiers;
    boolean restricted;
    Object o;
    int depth = depth();
    int round = round();
    for (Field field : XS1.getFields(getClass(), Key.class)) { // getClass().getDeclaredFields()
        field.setAccessible(true);
        logger.trace(field);
        name = field.getName();
        type = field.getType();
        if (!KeyField.class.isAssignableFrom(type)) {
            continue;
        }
        modifiers = field.getModifiers();
        restricted = Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers);
        if (restricted) {
            continue;
        }
        String errmsg = "failed to initialize field \"" + field + "\" at " + this;
        try {
            o = field.get(this);
            if (o == null) {
                logger.debug(message(type, name, o, depth, round));
            } else if (o instanceof KeyField) {
                finaliseKeyField(field, (KeyField) o);
            }
        } catch (IllegalArgumentException | IllegalAccessException ex) {
            logger.error(errmsg, ThrowableUtils.getCause(ex));
            TLC.getProject().getParser().increaseErrorCount();
        }
    }
}

From source file:org.walkmod.settergetter.visitors.SetterGetterGenerator.java

@Override
public void visit(ClassOrInterfaceDeclaration n, VisitorContext arg) {
    if (!n.isInterface()) {

        ClassOrInterfaceDeclaration coid = new ClassOrInterfaceDeclaration();
        coid.setName(n.getName());// ww  w  .  j a  v  a  2 s.  co m
        coid.setModifiers(n.getModifiers());
        coid.setInterface(false);

        List<BodyDeclaration> members = new LinkedList<BodyDeclaration>();
        coid.setMembers(members);
        List<TypeDeclaration> types = new LinkedList<TypeDeclaration>();
        types.add(coid);
        cu.setTypes(types);

        Collection<FieldDeclaration> fields = getFields(n);
        if (fields != null) {

            arg.addResultNode(cu);
            for (FieldDeclaration fd : fields) {

                List<VariableDeclarator> variables = fd.getVariables();
                for (VariableDeclarator vd : variables) {
                    String fieldName = vd.getId().getName();
                    Parameter parameter = createParameter(fd.getType(), fieldName);
                    try {
                        if (!Modifier.isFinal(fd.getModifiers())) {

                            addMethodDeclaration(coid, ModifierSet.PUBLIC, ASTManager.VOID_TYPE,
                                    "set" + WordUtils.capitalize(fieldName), parameter,
                                    "{ this." + fieldName + " = " + fieldName + "; }");

                        }
                        if (!isBoolean(fd.getType())) {
                            Parameter p = null;
                            addMethodDeclaration(coid, ModifierSet.PUBLIC, fd.getType(),
                                    "get" + WordUtils.capitalize(fieldName), p, "{return " + fieldName + ";}");
                        } else {
                            Parameter p = null;
                            addMethodDeclaration(coid, ModifierSet.PUBLIC, fd.getType(),
                                    "is" + WordUtils.capitalize(fieldName), p, "{return " + fieldName + ";}");
                        }

                    } catch (ParseException e1) {
                        throw new WalkModException(e1);
                    }
                }
            }
        }
    }
}

From source file:com.jk.util.JKObjectUtil.java

/**
 * Checks if is final./*from  www  .ja v a  2s.c o  m*/
 *
 * @param field
 *            the field
 * @return true, if is final
 */
public static boolean isFinal(final Field field) {
    return Modifier.isFinal(field.getModifiers());
}

From source file:org.evosuite.regression.ObjectFields.java

private static Collection<Field> getAllFields(Class<?> commonAncestor) {
    Collection<Field> result = new ArrayList<Field>();
    Class<?> ancestor = commonAncestor;

    while (!ancestor.equals(Object.class)) {
        for (Field f : ancestor.getDeclaredFields()) {
            if (Modifier.isFinal(f.getModifiers()))
                continue;
            if (Modifier.isStatic(f.getModifiers()))
                continue;
            result.add(f);//from   w w w .  j av  a2  s  . co  m
        }

        // result.addAll(Arrays.asList(ancestor.getDeclaredFields()));
        ancestor = ancestor.getSuperclass();
    }
    return result;
}

From source file:adalid.core.Tab.java

private void finaliseFields() {
    String name;/*from   ww w. j  av  a2s .  c  om*/
    Class<?> type;
    int modifiers;
    boolean restricted;
    Object o;
    int depth = depth();
    int round = round();
    for (Field field : XS1.getFields(getClass(), Tab.class)) { // getClass().getDeclaredFields()
        field.setAccessible(true);
        logger.trace(field);
        name = field.getName();
        type = field.getType();
        if (!TabField.class.isAssignableFrom(type)) {
            continue;
        }
        modifiers = field.getModifiers();
        restricted = Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers);
        if (restricted) {
            continue;
        }
        String errmsg = "failed to initialize field \"" + field + "\" at " + this;
        try {
            o = field.get(this);
            if (o == null) {
                logger.debug(message(type, name, o, depth, round));
            } else if (o instanceof TabField) {
                finaliseTabField(field, (TabField) o);
            }
        } catch (IllegalArgumentException | IllegalAccessException ex) {
            logger.error(errmsg, ThrowableUtils.getCause(ex));
            TLC.getProject().getParser().increaseErrorCount();
        }
    }
}

From source file:org.projectforge.framework.xstream.XmlObjectWriter.java

/**
 * Reader and Writer will ignore static and final fields.
 * @return true if the field has to be ignored by the writer.
 *//* w  w  w.  j a v  a2s  . co m*/
public static boolean ignoreField(final Field field) {
    final int modifiers = field.getModifiers();
    return Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) == true
            || Modifier.isTransient(modifiers);
}

From source file:org.pentaho.reporting.libraries.css.model.StyleKeyRegistry.java

private void registerClass(final Class c) {
    // Log.debug ("Registering stylekeys from " + c);
    try {//from   w  w w .  ja  v a2  s .c om
        final Field[] fields = c.getFields();
        for (int i = 0; i < fields.length; i++) {
            final Field field = fields[i];
            final int modifiers = field.getModifiers();
            if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
                if (Modifier.isFinal(modifiers) == false) {
                    logger.warn("Invalid implementation: StyleKeys should be 'public static final': " + c);
                }
                if (field.getType().isAssignableFrom(StyleKey.class)) {
                    final StyleKey value = (StyleKey) field.get(null);
                    if (value == null) {
                        logger.warn("Invalid implementation: StyleKeys fields must not be null: " + c);
                    }
                    // ignore the returned value, all we want is to trigger the key
                    // creation
                    // Log.debug ("Loaded key " + value);
                }
            }
        }
    } catch (IllegalAccessException e) {
        // wont happen, we've checked it..
    }
}

From source file:io.smartspaces.activity.annotation.StandardConfigurationPropertyAnnotationProcessor.java

/**
 * Injects config parameters into a given field of a given object, if the
 * field is marked with/*  w  ww.  j  a v a2 s .c  o  m*/
 * {@link io.smartspaces.activity.annotation.ConfigurationProperty}.
 *
 * @param obj
 *          object that contains the field into which config parameters will
 *          be injected
 * @param field
 *          field into which a config parameter will be injected
 * @param errors
 *          list container to accumulate errors
 * @param successes
 *          list container to accumulate successes
 */
private void processField(Object obj, Field field, List<String> errors, List<String> successes) {
    ConfigurationProperty annotation = field.getAnnotation(ConfigurationProperty.class);
    if (annotation == null) {
        return;
    }
    int initialErrorSize = errors.size();
    String fieldName = field.getName();
    if (Modifier.isFinal(field.getModifiers())) {
        errors.add(String.format("Field '%s' is marked final and may have unpredictable effects", fieldName));
    }
    String property = annotation.name();
    if (property == null || property.isEmpty()) {
        property = annotation.value();
    }
    property = property.trim();
    if (property.isEmpty()) {
        errors.add(String.format("Field '%s' has property name that is all white space or empty", fieldName));
    }

    boolean required = annotation.required();
    String delimiter = annotation.delimiter();
    boolean accessible = field.isAccessible();
    if (!accessible) {
        field.setAccessible(true);
    }
    try {
        Object value = null;
        Class<?> type = field.getType();
        if (required) {
            Object defaultValue = field.get(obj);
            boolean valueIsNotDefault = !Objects.equal(defaultValue, Defaults.defaultValue(type));
            if (valueIsNotDefault) {
                errors.add(String.format(
                        "Field '%s' into which a required property '%s' "
                                + "is to be injected already has a value: '%s', set 'required = false', "
                                + "or set the value of the property in the configuration "
                                + "(do not initialize the field directly).",
                        fieldName, property, defaultValue));
            }

            if (!configuration.containsProperty(property) && !property.isEmpty()) {
                errors.add(String.format("Field '%s' does not contain required property '%s'", fieldName,
                        property));
            }
        }

        // If value is required but not present, an error has already been
        // registered.
        if (type == int.class || type == Integer.class) {
            value = configuration.getPropertyInteger(property, null);
        } else if (type == long.class || type == Long.class) {
            value = configuration.getPropertyLong(property, null);
        } else if (type == double.class || type == Double.class) {
            value = configuration.getPropertyDouble(property, null);
        } else if (type == boolean.class || type == Boolean.class) {
            value = configuration.getPropertyBoolean(property, null);
        } else if (type == String.class) {
            value = configuration.getPropertyString(property);
        } else if (type.isAssignableFrom(List.class)) {
            value = configuration.getPropertyStringList(property, delimiter);
        } else if (type.isAssignableFrom(Set.class)) {
            value = configuration.getPropertyStringSet(property, delimiter);
        } else {
            errors.add(String.format(String.format("Field '%s' has unsupported type '%s'", fieldName,
                    tryGetValueForErrorMessage(property))));
        }
        if (errors.size() != initialErrorSize) {
            return;
        }
        String header = "@" + ConfigurationProperty.class.getSimpleName();
        if (value != null) {
            successes.add(String.format("%s field '%s' injected property '%s' with value '%s'", header,
                    fieldName, property, value));
            field.set(obj, value);
        } else {
            successes.add(String.format("%s field '%s' has no value from property '%s', skipping", header,
                    fieldName, property));
        }
    } catch (IllegalAccessException e) {
        errors.add(String.format("Field '%s' can not be accessed: %s", fieldName, e.toString()));
    } catch (Exception e) {
        errors.add(String.format("Field '%s' with property '%s' encountered error: %s", fieldName,
                tryGetValueForErrorMessage(property), e.toString()));
    } finally {
        if (!accessible) {
            field.setAccessible(false);
        }
    }
}

From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteriaTest.java

@Test
public void notAllowdModifiers() throws IllegalAccessException {
    Field[] declaredFields = Modifier.class.getDeclaredFields();
    int maxModifierValue = 0;
    for (Field field : declaredFields) {
        int modifiers = field.getModifiers();
        if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)
                && field.getType().equals(Integer.TYPE)) {
            try {
                Integer value = (Integer) field.get(Modifier.class);
                maxModifierValue = Math.max(maxModifierValue, value.intValue());
            } catch (IllegalArgumentException e) {
                throw new AssertionError(e);
            }//from   w ww .j a  va2  s . c o  m
        }
    }
    maxModifierValue = maxModifierValue << 2;
    try {
        memberCriteria.withModifiers(maxModifierValue);
        throw new AssertionError("modifiers are not allowed");
    } catch (IllegalArgumentException e) {
        String message = e.getMessage();
        assertTrue(message.startsWith("modifiers must be"));
    }
}