commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcucchi...@apache.org
Subject svn commit: r1546799 - in /commons/proper/lang/trunk/src: changes/ main/java/org/apache/commons/lang3/reflect/ test/java/org/apache/commons/lang3/reflect/ test/java/org/apache/commons/lang3/reflect/testbed/
Date Sun, 01 Dec 2013 10:34:03 GMT
Author: mcucchiara
Date: Sun Dec  1 10:34:02 2013
New Revision: 1546799

URL: http://svn.apache.org/r1546799
Log:
LANG-934 - Add removeFinalModifier to FieldUtils

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/StaticContainer.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1546799&r1=1546798&r2=1546799&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Sun Dec  1 10:34:02 2013
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.2" date="TBA" description="Next release">
+    <action issue="LANG-934" type="add" dev="mcucchiara">Add removeFinalModifier to
FieldUtils</action>
     <action issue="LANG-863" type="add" due-to="Daneel S. Yaitskov" dev="sebb">Method
returns number of inheritance hops between parent and subclass</action>
     <action issue="LANG-932" type="fix" due-to="Ville Skyttä" dev="sebb">Spelling
fixes</action>
     <action issue="LANG-931" type="update" dev="britter" due-to="Christoph Schneegans">Misleading
Javadoc comment in StrBuilderReader class</action>

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java?rev=1546799&r1=1546798&r2=1546799&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java
Sun Dec  1 10:34:02 2013
@@ -16,15 +16,15 @@
  */
 package org.apache.commons.lang3.reflect;
 
+import org.apache.commons.lang3.ClassUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.Validate;
+
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.commons.lang3.ClassUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.Validate;
-
 /**
  * Utilities for working with {@link Field}s by reflection. Adapted and refactored from the
dormant [reflect] Commons sandbox
  * component.
@@ -666,6 +666,28 @@ public class FieldUtils {
     }
 
     /**
+     * Remove the final modifier from a {@link Field}
+     * @param field to remove the final modifier
+     * @throws IllegalArgumentException
+     *             if the field is {@code null}
+     */
+    public static void removeFinalModifier(Field field)  {
+        Validate.isTrue(field != null, "The field must not be null");
+
+        try {
+            if(Modifier.isFinal(field.getModifiers())){
+                Field modifiersField = Field.class.getDeclaredField("modifiers");
+                modifiersField.setAccessible(true);
+                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
+            }
+        } catch (NoSuchFieldException ignored) {
+            // The field class contains always a modifiers field
+        } catch (IllegalAccessException ignored) {
+             // The modifiers field is made accessible
+        }
+    }
+
+    /**
      * Writes a {@code public} {@link Field}. Superclasses will be considered.
      * 
      * @param target

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java?rev=1546799&r1=1546798&r2=1546799&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
Sun Dec  1 10:34:02 2013
@@ -1253,4 +1253,11 @@ public class FieldUtilsTest {
         FieldUtils.getField(Ambig.class, "VALUE");
     }
 
+    @Test
+    public void testRemoveFinalModifier() throws Exception {
+        Field field = StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE_2");
+        assertTrue(Modifier.isFinal(field.getModifiers()));
+        FieldUtils.removeFinalModifier(field);
+        assertFalse(Modifier.isFinal(field.getModifiers()));
+    }
 }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/StaticContainer.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/StaticContainer.java?rev=1546799&r1=1546798&r2=1546799&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/StaticContainer.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/StaticContainer.java
Sun Dec  1 10:34:02 2013
@@ -22,10 +22,18 @@ package org.apache.commons.lang3.reflect
 public class StaticContainer {
     public static final Object IMMUTABLE_PUBLIC = "public";
     protected static final Object IMMUTABLE_PROTECTED = "protected";
+    @SuppressWarnings("unused")
     static final Object IMMUTABLE_PACKAGE = "";
     @SuppressWarnings("unused")
     private static final Object IMMUTABLE_PRIVATE = "private";
 
+    /**
+     * This final modifier of this field is meant to be removed by a test.
+     * Using this field may produce unpredictable results.
+     */
+    @SuppressWarnings("unused")
+    private static final Object IMMUTABLE_PRIVATE_2 = "private";
+
     public static Object mutablePublic;
     protected static Object mutableProtected;
     static Object mutablePackage;



Mime
View raw message