Example usage for java.lang.reflect Modifier FINAL

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

Introduction

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

Prototype

int FINAL

To view the source code for java.lang.reflect Modifier FINAL.

Click Source Link

Document

The int value representing the final modifier.

Usage

From source file:com.viadee.acceptancetests.roo.addon.AcceptanceTestGroupMetadata.java

public AcceptanceTestGroupMetadata(String identifier, JavaType aspectName,
        PhysicalTypeMetadata governorPhysicalTypeMetadata, AcceptanceTestsUtils acceptanceTestsUtils) {
    super(identifier, aspectName, governorPhysicalTypeMetadata);
    Assert.isTrue(isValid(identifier),//from w  ww . j a  va2s .c o m
            "Metadata identification string '" + identifier + "' does not appear to be a valid");
    Assert.notNull(acceptanceTestsUtils, "Acceptance test utils required");
    _acceptanceTestsUtils = acceptanceTestsUtils;

    if (!isValid()) {
        return;
    }

    builder.addMethod(new DefaultMethodMetadata(identifier, Modifier.FINAL,
            new JavaSymbolName("applicationDriver"), ABSTRACT_APPLICATION_DRIVER, parameters(),
            parameterNames(), null, null,
            "return new de.viadee.lib.acceptancetests.impl.selenium.SeleniumApplicationDriver(applicationBaseUrl());"));

    builder.addMethod(new DefaultMethodMetadata(identifier, Modifier.FINAL,
            new JavaSymbolName("setUpApplicationDriver"), VOID_PRIMITIVE, parameters(), parameterNames(),
            annotations(new JavaType("org.junit.Before")), throwsException(), "applicationDriver().setUp();"));

    builder.addMethod(new DefaultMethodMetadata(identifier, Modifier.FINAL,
            new JavaSymbolName("tearDownApplicationDriver"), VOID_PRIMITIVE, parameters(), parameterNames(),
            annotations(new JavaType("org.junit.After")), throwsException(),
            "applicationDriver().tearDown();"));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, STRING, "applicationBaseUrl", parameters(),
            parameterNames(),
            String.format("return \"http://localhost:8080/%s/\";", applicationContextName())));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, VOID_PRIMITIVE, "assertThat",
            parameters(ABSTRACT_CONDITION_WITHOUT_ANNOTATIONS), parameterNames("abstractCondition"),
            "org.junit.Assert.assertTrue(abstractCondition.failureMessage(), abstractCondition.value());"));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, ABSTRACT_BUTTON, "buttonWithCaption",
            parameters(STRING_WITHOUT_ANNOTATIONS), parameterNames("caption"),
            "return applicationDriver().buttonWithCaption(caption);"));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, ABSTRACT_BUTTON, "buttonWithAltText",
            parameters(STRING_WITHOUT_ANNOTATIONS), parameterNames("altText"),
            "return applicationDriver().buttonWithAltText(altText);"));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, VOID_PRIMITIVE, "typeTextIntoTextfield",
            parameters(STRING_WITHOUT_ANNOTATIONS, ABSTRACT_TEXTFIELD_WITHOUT_ANNOTATIONS),
            parameterNames("text", "textfield"),
            "applicationDriver().typeTextIntoTextfield(text, textfield);"));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, ABSTRACT_TEXTFIELD, "textfieldWithLabel",
            parameters(STRING_WITHOUT_ANNOTATIONS), parameterNames("label"),
            "return applicationDriver().textfieldWithLabel(label);"));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, VOID_PRIMITIVE,
            "clickClickableAndWaitForNextPage", parameters(CLICKABLE_WITHOUT_ANNOTATIONS),
            parameterNames("clickable"), "applicationDriver().clickClickableAndWaitForNextPage(clickable);"));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, ABSTRACT_LINK, "linkWithCaption",
            parameters(STRING_WITHOUT_ANNOTATIONS), parameterNames("caption"),
            "return applicationDriver().linkWithCaption(caption);"));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, VOID_PRIMITIVE, "open",
            parameters(STRING_WITHOUT_ANNOTATIONS), parameterNames("url"), "applicationDriver().open(url);"));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, VOID_PRIMITIVE, "assertTextIsPresent",
            parameters(STRING_WITHOUT_ANNOTATIONS), parameterNames("text"),
            "org.junit.Assert.assertTrue(String.format(\"The text \\\"%s\\\" could not be found!\", text), applicationDriver().isTextPresent(text));"));

    StringBuilder waitForHalfASecondBody = new StringBuilder();
    waitForHalfASecondBody.append("try {\r");
    waitForHalfASecondBody.append("  Thread.sleep(500);\r");
    waitForHalfASecondBody.append("} catch (InterruptedException e) {\r");
    waitForHalfASecondBody.append("  // Do nothing\r");
    waitForHalfASecondBody.append("}\r");

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, VOID_PRIMITIVE, "waitForHalfASecond",
            parameters(), parameterNames(), waitForHalfASecondBody.toString()));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, ABSTRACT_CRITERION, "entryInColumnEqualsValue",
            parameters(STRING_WITHOUT_ANNOTATIONS, STRING_WITHOUT_ANNOTATIONS),
            parameterNames("columnName", "columnEntry"),
            "return applicationDriver().entryInColumnEqualsValue(columnName, columnEntry);"));

    builder.addMethod(newAcceptanceTestGroupMethod(identifier, ABSTRACT_TABLE, "tableWithColumns",
            parameters(VARARG_OF_STRING_WITHOUT_ANNOTATIONS), parameterNames("columnHeaders"),
            "return applicationDriver().tableWithColumns(columnHeaders);"));

    // Create a representation of the desired output ITD
    itdTypeDetails = builder.build();
}

From source file:org.jodconverter.filter.DefaultFilterChainITest.java

static void setFinalStatic(final Field field, final Object newValue) throws Exception {

    field.setAccessible(true);/*from w  w w  .j av a  2  s  .c o  m*/
    final Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(null, newValue);
}

From source file:hu.bme.mit.sette.common.model.snippet.SnippetInputFactoryContainer.java

/**
 * Validates the class and its annotations.
 *
 * @param validator/* w  w  w .j a  va2 s  .c  om*/
 *            a validator
 */
private void validateClass(final AbstractValidator<?> validator) {
    // check: "public final class", no superclass, interface, declared
    // class, exactly one constructor
    ClassValidator v = new ClassValidator(javaClass);
    v.type(ClassType.REGULAR_CLASS);
    v.withModifiers(Modifier.PUBLIC | Modifier.FINAL);
    v.withoutModifiers(Modifier.ABSTRACT);
    v.synthetic(false);
    v.superclass(Object.class).interfaceCount(0).memberClassCount(0);
    v.declaredConstructorCount(1);

    // check: no SETTE annotations
    if (!SetteAnnotationUtils.getSetteAnnotations(javaClass).isEmpty()) {
        v.addException("The class must not have any SETTE annotations");
    }

    validator.addChildIfInvalid(v);
}

From source file:org.lunarray.model.descriptor.builder.annotation.resolver.property.accessor.AccessorPropertyResolver.java

/**
 * Process the property.//from   w  w w.  j  a  va2  s  . c o  m
 * 
 * @param entityType
 *            The entity type.
 * @param methods
 *            The methods.
 * @param fields
 *            The fields.
 * @param method
 *            The accessor method.
 * @param <P>
 *            The property type.
 * @return The property.
 */
private <P> DescribedProperty<P> processProperty(final DescribedEntity<?> entityType,
        final List<Method> methods, final List<Field> fields, final Method method) {
    final PropertyBuilder<P> builder = AbstractProperty.createBuilder();
    for (final Annotation a : method.getAnnotations()) {
        builder.addAnnotation(a);
    }
    final PropertyMatcher<Method> resolver = this.getAccessorMatcherResolver();
    @SuppressWarnings("unchecked")
    // About as sure as we can get.
    final Class<P> type = (Class<P>) resolver.extractType(method);
    builder.accessor(method).genericType(resolver.extractGenericType(method));
    final String name = resolver.extractName(method);
    builder.name(name).type(type).entityType(entityType);
    // Resolve field.
    for (final Field field : fields) {
        if (this.getFieldMatcherResolver().matches(field, name, type)) {
            for (final Annotation a : field.getAnnotations()) {
                builder.addAnnotation(a);
            }
            builder.raw(field);
        }
    }
    // Resolve mutator.
    Method mutator = null;
    for (final Method mutatorCandidate : methods) {
        if (this.getMutatorMatcherResolver().matches(mutatorCandidate, name, type)) {
            for (final Annotation a : mutatorCandidate.getAnnotations()) {
                builder.addAnnotation(a);
            }
            mutator = mutatorCandidate;
        }
    }
    if (CheckUtil.isNull(mutator)) {
        builder.addModifier(Modifier.FINAL);
    } else {
        builder.mutator(mutator);
    }
    return builder.buildDescribed();
}

From source file:org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDataChangeListenerTest.java

void setFinalStatic(Class cls, String fieldName, Object newValue) throws Exception {
    Field fields[] = FieldUtils.getAllFields(cls);
    for (Field field : fields) {
        if (fieldName.equals(field.getName())) {
            field.setAccessible(true);/*  www. j a  v a  2  s. c o m*/
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
            field.set(null, newValue);
            break;
        }
    }
}

From source file:org.springframework.data.keyvalue.riak.RiakMappedClass.java

private void initFields() {
    ReflectionUtils.doWithFields(this.clazz, new FieldCallback() {

        @Override//from  w w w . j  a v  a 2 s.c  o m
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if (!field.isAccessible())
                ReflectionUtils.makeAccessible(field);

            if (field.isAnnotationPresent(Transient.class) || field.isSynthetic()
                    || field.getModifiers() == Modifier.FINAL || field.getModifiers() == Modifier.TRANSIENT) {
                return;
            }

            // Field can only have one of these, if more than one throw an
            // error
            List<Annotation> annots = org.springframework.data.keyvalue.riak.util.AnnotationUtils
                    .getFoundAnnotation(Id.class, Column.class, Embedded.class, Version.class, ManyToOne.class,
                            OneToMany.class, Basic.class);

            // Probably allow auto generation at some point, but for now
            // have to add one of the annotations
            if (annots.size() > 1)
                throw new IllegalArgumentException(String.format(
                        "The field %s must have only one of the following annotations: "
                                + "@Id, @Basic, @Column, @Embedded, @Version, @ManyToOne, @OneToMany",
                        field.getName()));

            Annotation annot = annots.get(0);

            if (annot.annotationType().equals(Id.class))
                RiakMappedClass.this.id = field;

            // Create a new mapped field here and then add to a list of
            // property MappedFields
            propertyFields.add(new RiakMappedField(field, annot));
        }
    });
    Map<Class<? extends Annotation>, Annotation> fieldAnnotMap = new HashMap<Class<? extends Annotation>, Annotation>();
    for (Class<? extends Annotation> a : entityAnnotations) {
        Target target = a.getAnnotation(Target.class);
        if (target != null && (ArrayUtils.contains(target.value(), ElementType.FIELD)
                || ArrayUtils.contains(target.value(), ElementType.METHOD))) {
            Annotation fieldAnnot;
            if ((fieldAnnot = this.clazz.getAnnotation(a)) != null) {
                fieldAnnotMap.put(a, fieldAnnot);
            }
        }
    }
}

From source file:org.lambdamatic.mongodb.apt.testutil.FieldAssertion.java

/**
 * Checks that the actual field has no 'final' modifier.
 * //  www  . j a  va  2 s  .co m
 * @return this {@link FieldAssertion} for fluent linking
 */
public FieldAssertion isNotFinal() {
    isNotNull();
    if ((actual.getModifiers() & Modifier.FINAL) > 0) {
        failWithMessage("Expected field <%s> *NOT* to be final", actual.getName());
    }
    return this;
}

From source file:org.apache.nifi.controller.MonitorMemoryTest.java

private CapturingLogger wrapAndReturnCapturingLogger() throws Exception {
    Field loggerField = MonitorMemory.class.getDeclaredField("logger");
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);//from w w  w. j av a2 s. c  o  m
    modifiersField.setInt(loggerField, loggerField.getModifiers() & ~Modifier.FINAL);

    loggerField.setAccessible(true);
    CapturingLogger capturingLogger = new CapturingLogger((Logger) loggerField.get(null));
    loggerField.set(null, capturingLogger);
    return capturingLogger;
}

From source file:code.elix_x.excore.utils.nbt.mbt.encoders.NBTClassEncoder.java

public void populate(MBT mbt, NBTTagCompound nbt, Object o) {
    Class clazz = o.getClass();/*from   w w  w.j ava  2 s . c o m*/
    try {
        while (clazz != null && clazz != Object.class) {
            if (!clazz.isAnnotationPresent(MBTIgnore.class)) {
                for (Field field : clazz.getDeclaredFields()) {
                    if (!field.isAnnotationPresent(MBTIgnore.class)) {
                        field.setAccessible(true);
                        if (nbt.hasKey(field.getName())) {
                            if (encodeStatic || !Modifier.isStatic(field.getModifiers())) {
                                if (Modifier.isFinal(field.getModifiers())) {
                                    if (encodeFinal) {
                                        Field modifiers = Field.class.getDeclaredField("modifiers");
                                        modifiers.setAccessible(true);
                                        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);

                                        if (field.getGenericType() instanceof ParameterizedType) {
                                            Type[] types = ((ParameterizedType) field.getGenericType())
                                                    .getActualTypeArguments();
                                            Class[] clas = new Class[] {};
                                            for (Type type : types) {
                                                if (type instanceof Class) {
                                                    clas = ArrayUtils.add(clas, (Class) type);
                                                }
                                            }
                                            field.set(o, mbt.fromNBT(nbt.getTag(field.getName()),
                                                    field.getType(), clas));
                                        } else {
                                            field.set(o,
                                                    mbt.fromNBT(nbt.getTag(field.getName()), field.getType()));
                                        }
                                    }
                                } else {
                                    if (field.getGenericType() instanceof ParameterizedType) {
                                        Type[] types = ((ParameterizedType) field.getGenericType())
                                                .getActualTypeArguments();
                                        Class[] clas = new Class[] {};
                                        for (Type type : types) {
                                            if (type instanceof Class) {
                                                clas = ArrayUtils.add(clas, (Class) type);
                                            }
                                        }
                                        field.set(o, mbt.fromNBT(nbt.getTag(field.getName()), field.getType(),
                                                clas));
                                    } else {
                                        field.set(o, mbt.fromNBT(nbt.getTag(field.getName()), field.getType()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            clazz = encodeSuper ? clazz.getSuperclass() : Object.class;
        }
    } catch (IllegalArgumentException e) {
        Throwables.propagate(e);
    } catch (IllegalAccessException e) {
        Throwables.propagate(e);
    } catch (NoSuchFieldException e) {
        Throwables.propagate(e);
    } catch (SecurityException e) {
        Throwables.propagate(e);
    }
}

From source file:hu.bme.mit.sette.common.model.snippet.SnippetContainer.java

/**
 * Validates the class and its annotations.
 *
 * @param validator//from   ww  w  . j  a  v a2  s.  com
 *            a validator
 * @return the {@link SetteSnippetContainer} annotation
 */
private SetteSnippetContainer validateClass(final AbstractValidator<?> validator) {
    // check: "public final class", no superclass, interface, declared
    // class, exactly one constructor
    ClassValidator v = new ClassValidator(javaClass);
    v.type(ClassType.REGULAR_CLASS);
    v.withModifiers(Modifier.PUBLIC | Modifier.FINAL);
    v.withoutModifiers(Modifier.ABSTRACT);
    v.synthetic(false);
    v.superclass(Object.class).interfaceCount(0).memberClassCount(0);
    v.declaredConstructorCount(1);

    // check: only @SetteSnippetContainer
    SetteSnippetContainer containerAnn = null;

    AnnotationMap classAnns = SetteAnnotationUtils.getSetteAnnotations(javaClass);

    containerAnn = (SetteSnippetContainer) classAnns.get(SetteSnippetContainer.class);

    if (containerAnn == null) {
        v.addException(
                "The Java class must have the annotation @" + SetteSnippetContainer.class.getSimpleName());
    } else {
        if (classAnns.size() != 1) {
            v.addException("The Java class must not have any " + "SETTE annotations other than @"
                    + SetteSnippetContainer.class.getSimpleName());
        }

        if (StringUtils.isBlank(containerAnn.category())) {
            v.addException(
                    "The category in @" + SetteSnippetContainer.class.getSimpleName() + " must not be blank");
        }

        if (StringUtils.isBlank(containerAnn.goal())) {
            v.addException(
                    "The goal in @" + SetteSnippetContainer.class.getSimpleName() + " must not be blank");
        }

        if (containerAnn.requiredJavaVersion() == null) {
            v.addException("The reqired Java version in @" + SetteSnippetContainer.class.getSimpleName()
                    + " must not be null");
        }
    }

    validator.addChildIfInvalid(v);

    return containerAnn;
}