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.uphyca.testing.junit3.support.v4.FragmentTestCase.java

/**
 * This function is called by various TestCase implementations, at
 * tearDown() time, in order to scrub out any class variables. This protects
 * against memory leaks in the case where a test case creates a non-static
 * inner class (thus referencing the test case) and gives it to someone else
 * to hold onto.//w w w .j  a  v  a  2  s  .c  om
 * 
 * @param testCaseClass
 *            The class of the derived TestCase implementation.
 * 
 * @throws IllegalAccessException
 */
protected void scrubClass(final Class<?> testCaseClass) throws IllegalAccessException {
    final Field[] fields = getClass().getDeclaredFields();
    for (Field field : fields) {
        final Class<?> fieldClass = field.getDeclaringClass();
        if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive()
                && (field.getModifiers() & Modifier.FINAL) == 0) {
            try {
                field.setAccessible(true);
                field.set(this, null);
            } catch (Exception e) {
                android.util.Log.d("TestCase", "Error: Could not nullify field!");
            }

            if (field.get(this) != null) {
                android.util.Log.d("TestCase", "Error: Could not nullify field!");
            }
        }
    }
}

From source file:org.eclipse.skalli.testutil.PropertyTestUtil.java

public static final void checkPropertyDefinitions(Class<?> classToCheck,
        Map<Class<?>, String[]> requiredProperties, Map<String, Object> values)
        throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException,
        InstantiationException, InvocationTargetException {
    Class<?> clazz = classToCheck;
    while (clazz != null) {

        // assert that the model class under test has a suitable constructor (either
        // a default constructor if requiredProperties is empty, or a constructor with the
        // correct number and type of parameters), and can be instantiated
        Object instance = assertExistsConstructor(clazz, requiredProperties, values);

        for (Field field : clazz.getDeclaredFields()) {
            if (hasAnnotation(field, PropertyName.class)) {

                // assert that the field is public static final
                Assert.assertTrue(clazz.getName() + ": constant " + field.getName() + " is not declared STATIC",
                        (field.getModifiers() & Modifier.STATIC) == Modifier.STATIC); //$NON-NLS-1$
                Assert.assertTrue(clazz.getName() + ": constant " + field.getName() + " is not declared PUBLIC",
                        (field.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC); //$NON-NLS-1$
                Assert.assertTrue(clazz.getName() + ": constant " + field.getName() + " is not declared FINAL",
                        (field.getModifiers() & Modifier.FINAL) == Modifier.FINAL); //$NON-NLS-1$

                // assert that the constant if of type String and has a value assigned.
                Object object = field.get(null);
                Assert.assertNotNull(clazz.getName() + ": constant " + field.getName() + " is NULL", object); //$NON-NLS-1$
                Assert.assertTrue(clazz.getName() + ": constants " + field.getName() + " is not of type STRING",
                        object instanceof String); //$NON-NLS-1$

                // assert that there is a private non-static field with a name matching the
                // value of the constant unless the property is marked as derived
                String fieldName = (String) object;
                if (!hasAnnotation(field, Derived.class)) {
                    Assert.assertTrue(clazz.getName() + ": must have a private field named " + fieldName,
                            hasPrivateField(clazz, fieldName));
                }/*from ww w.j a  va2s.  c o m*/

                // assert that the values argument contains a test value for this field
                Assert.assertTrue(clazz.getName() + ": no test value for field " + fieldName,
                        values.containsKey(fieldName));

                Methods methods = new Methods();

                // assert that the class has a getter for this property
                methods.getMethod = assertExistsGetMethod(clazz, fieldName);

                // assert that the class has a setter for this property if it is an optional property;
                // required properties must be set in the constructor;
                // skip properties that are annotated as @Derived
                if (isOptionalProperty(clazz, fieldName, requiredProperties)
                        && !hasAnnotation(field, Derived.class)) {
                    Class<?> returnType = methods.getMethod.getReturnType();
                    if (!Collection.class.isAssignableFrom(returnType)) {
                        methods.setMethod = assertExistsSetMethod(clazz, fieldName, returnType);
                    } else {
                        Class<?> entryType = ((Collection<?>) values.get(fieldName)).iterator().next()
                                .getClass();
                        methods.addMethod = assertExistsCollectionMethod(clazz, fieldName, entryType, "add");
                        methods.removeMethod = assertExistsCollectionMethod(clazz, fieldName, entryType,
                                "remove");
                        methods.hasMethod = assertExistsCollectionMethod(clazz, fieldName, entryType, "has");
                    }
                    // call the setter/adder and getter methods with the given test value
                    if (instance != null) {
                        assertChangeReadCycle(clazz, fieldName, methods, instance, values.get(fieldName));
                    }
                } else {
                    if (instance != null) {
                        assertReadCycle(clazz, methods, instance, values.get(fieldName));
                    }
                }
            }
        }

        // check the properties of the parent class (EntityBase!)
        clazz = clazz.getSuperclass();
    }
}

From source file:org.apache.zeppelin.service.ShiroAuthenticationServiceTest.java

private void setFinalStatic(Field field, Object newValue) throws NoSuchFieldException, IllegalAccessException {
    field.setAccessible(true);//from ww  w.  j a v a2 s.c o  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.gvnix.addon.gva.security.providers.safe.SafeSecurityProviderUserMetadata.java

public SafeSecurityProviderUserMetadata(String identifier, JavaType aspectName,
        PhysicalTypeMetadata governorPhysicalTypeMetadata) {
    super(identifier, aspectName, governorPhysicalTypeMetadata);
    /*Validate.isTrue(isValid(identifier), "Metadata identification string '"
        + identifier + "' does not appear to be a valid");*/

    // Helper itd generation
    this.helper = new ItdBuilderHelper(this, governorPhysicalTypeMetadata,
            builder.getImportRegistrationResolver());

    // Adding Fields
    builder.addField(getField("serialVersionUID", "5767016615242591655L", JavaType.LONG_PRIMITIVE,
            Modifier.PRIVATE + Modifier.STATIC + Modifier.FINAL));
    // User Details
    builder.addField(getField("username", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("password", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("accountNonExpired", null, JAVA_TYPE_BOOLEAN, Modifier.PRIVATE));
    builder.addField(getField("accountNonLocked", null, JAVA_TYPE_BOOLEAN, Modifier.PRIVATE));
    builder.addField(getField("credentialsNonExpired", null, JAVA_TYPE_BOOLEAN, Modifier.PRIVATE));
    builder.addField(getField("enabled", null, JAVA_TYPE_BOOLEAN, Modifier.PRIVATE));
    builder.addField(getField("authorities", null, GRANTED_AUTHORITY, Modifier.PRIVATE));
    // SAFE USER DATA
    builder.addField(getField("nombre", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("email", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("apellido1", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("apellido2", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("cif", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("habilitado", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("idHDFI", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("iusserDN", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("nif", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("oid", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("razonSocial", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("representante", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("serialNumber", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("subjectDN", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("tipoAut", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("tipoCertificado", null, JAVA_TYPE_STRING, Modifier.PRIVATE));

    // Creating methods
    builder.addMethod(getGetterMethod("username", JavaType.STRING));
    builder.addMethod(getSetterMethod("username", JavaType.STRING));
    builder.addMethod(getGetterMethod("password", JavaType.STRING));
    builder.addMethod(getSetterMethod("password", JavaType.STRING));
    builder.addMethod(getGetterMethod("accountNonExpired", JAVA_TYPE_BOOLEAN));
    builder.addMethod(getSetterMethod("accountNonExpired", JAVA_TYPE_BOOLEAN));
    builder.addMethod(getGetterMethod("accountNonLocked", JAVA_TYPE_BOOLEAN));
    builder.addMethod(getSetterMethod("accountNonLocked", JAVA_TYPE_BOOLEAN));
    builder.addMethod(getGetterMethod("credentialsNonExpired", JAVA_TYPE_BOOLEAN));
    builder.addMethod(getSetterMethod("credentialsNonExpired", JAVA_TYPE_BOOLEAN));
    builder.addMethod(getGetterMethod("enabled", JAVA_TYPE_BOOLEAN));
    builder.addMethod(getSetterMethod("enabled", JAVA_TYPE_BOOLEAN));
    builder.addMethod(getGetterMethod("authorities", GRANTED_AUTHORITY));
    builder.addMethod(getSetterMethod("authorities", GRANTED_AUTHORITY));
    builder.addMethod(getGetterMethod("nombre", JavaType.STRING));
    builder.addMethod(getSetterMethod("nombre", JavaType.STRING));
    builder.addMethod(getGetterMethod("email", JavaType.STRING));
    builder.addMethod(getSetterMethod("email", JavaType.STRING));
    builder.addMethod(getGetterMethod("apellido1", JavaType.STRING));
    builder.addMethod(getSetterMethod("apellido1", JavaType.STRING));
    builder.addMethod(getGetterMethod("apellido2", JavaType.STRING));
    builder.addMethod(getSetterMethod("apellido2", JavaType.STRING));
    builder.addMethod(getGetterMethod("cif", JavaType.STRING));
    builder.addMethod(getSetterMethod("cif", JavaType.STRING));
    builder.addMethod(getGetterMethod("habilitado", JavaType.STRING));
    builder.addMethod(getSetterMethod("habilitado", JavaType.STRING));
    builder.addMethod(getGetterMethod("idHDFI", JavaType.STRING));
    builder.addMethod(getSetterMethod("idHDFI", JavaType.STRING));
    builder.addMethod(getGetterMethod("iusserDN", JavaType.STRING));
    builder.addMethod(getSetterMethod("iusserDN", JavaType.STRING));
    builder.addMethod(getGetterMethod("nif", JavaType.STRING));
    builder.addMethod(getSetterMethod("nif", JavaType.STRING));
    builder.addMethod(getGetterMethod("oid", JavaType.STRING));
    builder.addMethod(getSetterMethod("oid", JavaType.STRING));
    builder.addMethod(getGetterMethod("razonSocial", JavaType.STRING));
    builder.addMethod(getSetterMethod("razonSocial", JavaType.STRING));
    builder.addMethod(getGetterMethod("representante", JavaType.STRING));
    builder.addMethod(getSetterMethod("representante", JavaType.STRING));
    builder.addMethod(getGetterMethod("serialNumber", JavaType.STRING));
    builder.addMethod(getSetterMethod("serialNumber", JavaType.STRING));
    builder.addMethod(getGetterMethod("subjectDN", JavaType.STRING));
    builder.addMethod(getSetterMethod("subjectDN", JavaType.STRING));
    builder.addMethod(getGetterMethod("tipoAut", JavaType.STRING));
    builder.addMethod(getSetterMethod("tipoAut", JavaType.STRING));
    builder.addMethod(getGetterMethod("tipoCertificado", JavaType.STRING));
    builder.addMethod(getSetterMethod("tipoCertificado", JavaType.STRING));

    // Create a representation of the desired output ITD
    itdTypeDetails = builder.build();/*  w  w  w. java 2  s  .  com*/
}

From source file:com.mollie.api.resource.BaseResource.java

/**
 * Convenience method to copy all public properties from a src object into
 * a dst object of the same type./*  w  w  w.jav a 2s  .co m*/
 *
 * @param src Source object to copy properties from
 * @param dst Target object
 */
protected void copyInto(T src, T dst) {
    Field[] fromFields = returnedClass().getDeclaredFields();
    Object value = null;

    try {
        for (Field field : fromFields) {
            int modifiers = field.getModifiers();

            if ((modifiers & Modifier.PUBLIC) == Modifier.PUBLIC
                    && (modifiers & Modifier.FINAL) != Modifier.FINAL
                    && (modifiers & Modifier.STATIC) != Modifier.STATIC) {
                value = field.get(src);
                field.set(dst, value);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.github.geequery.codegen.ast.JavaUnit.java

/**
 * Equals//from   w  ww  . j  av a 2  s  . c  om
 * @param idfields ?fields
 * @param overwirte ?
 * @param doSuperMethod ?
 * @return
 */
public boolean createEqualsMethod(List<JavaField> idfields, boolean overwirte, String doSuperMethod) {
    JavaMethod equals = new JavaMethod("equals");
    equals.setReturnType(boolean.class);
    equals.addparam(IClassUtil.of(Object.class), "rhs0", Modifier.FINAL);
    if (methods.containsKey(equals.getKey())) {//?
        if (!overwirte) {
            return false;
        }
    }
    equals.addContent("if (rhs0 == null)return false;");
    String simpleName = getSimpleName();
    equals.addContent(simpleName + " rhs=(" + simpleName + ")rhs0;");
    //
    for (int i = 0; i < idfields.size(); i++) {
        JavaField field = idfields.get(i);
        String name = field.getName();
        if (Modifier.isAbstract(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) {
            continue;
        }
        equals.addContent("if(!ObjectUtils.equals(" + name + ", rhs." + name + ")) return false;");
    }
    if (StringUtils.isEmpty(doSuperMethod)) {
        equals.addContent("return true;");
    } else {
        equals.addContent("return super." + doSuperMethod + "(rhs);");
    }
    addMethod(equals);
    addImport(ObjectUtils.class);
    return true;
}

From source file:org.gvnix.addon.gva.security.providers.safe.SafeSecurityProviderUserAuthorityMetadata.java

public SafeSecurityProviderUserAuthorityMetadata(String identifier, JavaType aspectName,
        PhysicalTypeMetadata governorPhysicalTypeMetadata) {
    super(identifier, aspectName, governorPhysicalTypeMetadata);
    /*Validate.isTrue(isValid(identifier), "Metadata identification string '"
        + identifier + "' does not appear to be a valid");*/

    // Helper itd generation
    this.helper = new ItdBuilderHelper(this, governorPhysicalTypeMetadata,
            builder.getImportRegistrationResolver());

    // Adding Fields
    builder.addField(getField("serialVersionUID", "-2443806778851127910L", JavaType.LONG_PRIMITIVE,
            Modifier.PRIVATE + Modifier.STATIC + Modifier.FINAL));

    builder.addField(getField("authority", null, JAVA_TYPE_STRING, Modifier.PRIVATE));

    // User Details
    builder.addField(getField("nif", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("usrtipo", null, JAVA_TYPE_STRING, Modifier.PRIVATE));
    builder.addField(getField("idgrupo", null, JAVA_TYPE_STRING, Modifier.PUBLIC));
    builder.addField(getField("idrol", null, JAVA_TYPE_STRING, Modifier.PUBLIC));
    builder.addField(getField("idaplicacion", null, JAVA_TYPE_STRING, Modifier.PUBLIC));

    // Creating getters and setters
    builder.addMethod(getGetterMethod("authority", JavaType.STRING));
    builder.addMethod(getSetterMethod("authority", JavaType.STRING));
    builder.addMethod(getGetterMethod("nif", JavaType.STRING));
    builder.addMethod(getSetterMethod("nif", JavaType.STRING));
    builder.addMethod(getGetterMethod("usrtipo", JavaType.STRING));
    builder.addMethod(getSetterMethod("usrtipo", JavaType.STRING));
    builder.addMethod(getGetterMethod("idgrupo", JavaType.STRING));
    builder.addMethod(getSetterMethod("idgrupo", JavaType.STRING));
    builder.addMethod(getGetterMethod("idrol", JavaType.STRING));
    builder.addMethod(getSetterMethod("idrol", JavaType.STRING));
    builder.addMethod(getGetterMethod("idaplicacion", JavaType.STRING));
    builder.addMethod(getSetterMethod("idaplicacion", JavaType.STRING));

    // Creating methods
    builder.addMethod(getHashCodeMethod());
    builder.addMethod(getEqualsMethod());

    // Create a representation of the desired output ITD
    itdTypeDetails = builder.build();/*from ww w . j  av  a2  s .c  om*/
}

From source file:org.gvnix.addon.gva.security.providers.safe.SafeSecurityProviderExtLoadWSS4JMetadata.java

public SafeSecurityProviderExtLoadWSS4JMetadata(String identifier, JavaType aspectName,
        PhysicalTypeMetadata governorPhysicalTypeMetadata) {
    super(identifier, aspectName, governorPhysicalTypeMetadata);

    // Helper itd generation
    this.helper = new ItdBuilderHelper(this, governorPhysicalTypeMetadata,
            builder.getImportRegistrationResolver());

    // Adding Fields
    builder.addField(getField("LOGGER", "Logger.getLogger(ExtLoadWSS4JOutInterceptor.class)",
            new JavaType("org.apache.log4j.Logger"), Modifier.PRIVATE + Modifier.STATIC + Modifier.FINAL));
    builder.addField(getField(TRACEABILITY, null, JAVA_TYPE_STRING, Modifier.PRIVATE));

    // Creating constructors
    builder.addConstructor(getExtLoadWSS4JConst());
    builder.addConstructor(getExtLoadWSS4JConstWithParam());

    // Creating methods
    builder.addMethod(getSetterMethod(TRACEABILITY, JAVA_TYPE_STRING));
    builder.addMethod(getHandleMessageMethod());

    // Create a representation of the desired output ITD
    itdTypeDetails = builder.build();//w w  w .j a  v  a 2  s  .  com
}

From source file:org.eclipse.gemini.blueprint.service.importer.support.StaticServiceProxyCreator.java

/**
 * Apply 'greedy' proxying by discovering the exposed classes.
 * /*from ww w  . j  a v a2s.  com*/
 * @param ref
 * @return
 */
Class<?>[] discoverProxyClasses(ServiceReference ref) {
    boolean trace = log.isTraceEnabled();

    if (trace)
        log.trace("Generating greedy proxy for service " + OsgiStringUtils.nullSafeToString(ref));

    String[] classNames = OsgiServiceReferenceUtils.getServiceObjectClasses(ref);

    if (trace)
        log.trace("Discovered raw classes " + ObjectUtils.nullSafeToString(classNames));

    // try to get as many classes as possible
    Class<?>[] classes = ClassUtils.loadClassesIfPossible(classNames, classLoader);

    if (trace)
        log.trace("Visible classes are " + ObjectUtils.nullSafeToString(classes));

    // exclude final classes
    classes = ClassUtils.excludeClassesWithModifier(classes, Modifier.FINAL);

    if (trace)
        log.trace("Filtering out final classes; left out with " + ObjectUtils.nullSafeToString(classes));

    // remove classes if needed
    if (interfacesOnlyProxying) {
        Set<Class<?>> clazzes = new LinkedHashSet<Class<?>>(classes.length);
        for (int classIndex = 0; classIndex < classes.length; classIndex++) {
            Class<?> clazz = classes[classIndex];
            if (clazz.isInterface())
                clazzes.add(clazz);
        }
        if (trace)
            log.trace("Filtering out concrete classes; left out with " + clazzes);

        classes = (Class[]) clazzes.toArray(new Class[clazzes.size()]);
    }

    // remove class duplicates/parents
    classes = ClassUtils.removeParents(classes);

    if (trace)
        log.trace("Filtering out parent classes; left out with " + classes);

    return classes;
}

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

/**
 * Checks that the actual field has a 'final' modifier.
 * //ww w. ja va2 s. c o  m
 * @return this {@link FieldAssertion} for fluent linking
 */
public FieldAssertion isFinal() {
    isNotNull();
    if ((actual.getModifiers() & Modifier.FINAL) == 0) {
        failWithMessage("Expected field <%s> to be final", actual.getName());
    }
    return this;
}