Author: hlship
Date: Tue Jan 26 16:03:01 2010
New Revision: 903294

URL: http://svn.apache.org/viewvc?rev=903294&view=rev
Log:
Move the logic for removing a field into TransformField

Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/InternalClassTransformationImpl.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ClassTransformation.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TransformField.java

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/InternalClassTransformationImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/InternalClassTransformationImpl.java?rev=903294&r1=903293&r2=903294&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/InternalClassTransformationImpl.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/InternalClassTransformationImpl.java
 Tue Jan 26 16:03:01 2010
@@ -147,6 +147,8 @@
 
         private Object claimTag;
 
+        boolean removed;
+
         TransformFieldImpl(CtField field, boolean added)
         {
             this.field = field;
@@ -216,6 +218,30 @@
             return claimTag != null;
         }
 
+        public void remove()
+        {
+            if (removed)
+                throw new RuntimeException(String
+                        .format("Field %s.%s has already been marked for 
removal.", ctClass
+                                .getName(), name));
+
+            removed = true;
+
+            formatter.format("remove field %s;\n\n", name);
+        }
+
+        void doRemove()
+        {
+            try
+            {
+                ctClass.removeField(field);
+            }
+            catch (NotFoundException ex)
+            {
+                throw new RuntimeException(ex);
+            }
+        }
+
         public void replaceAccess(ComponentValueProvider<FieldValueConduit> 
conduitProvider)
         {
             replaceAccess(addIndirectInjectedField(FieldValueConduit.class, 
name + "$conduit",
@@ -254,7 +280,7 @@
 
             replaceWriteAccess(name, writeMethodName);
 
-            removeField(name);
+            remove();
         }
 
         public <T> void assignIndirect(TransformMethod method, 
ComponentValueProvider<T> provider)
@@ -324,8 +350,6 @@
     // Key is field name, value is expression used to replace read access
     private Map<String, String> fieldWriteTransforms;
 
-    private Set<String> removedFieldNames;
-
     /**
      * Contains the assembled Javassist code for the class' default 
constructor.
      */
@@ -465,7 +489,6 @@
         methodSignatures = null;
         fieldReadTransforms = null;
         fieldWriteTransforms = null;
-        removedFieldNames = null;
         constructor = null;
         formatter = null;
         methodToInvocationBuilder = null;
@@ -513,7 +536,7 @@
 
             idAllocator.allocateId(name);
 
-            fields.put(name, new TransformFieldImpl(field, false));
+            TransformFieldImpl tfi = fields.put(name, new 
TransformFieldImpl(field, false));
 
             int modifiers = field.getModifiers();
 
@@ -527,8 +550,7 @@
 
             if (name.equals("metaClass") && 
getFieldType(name).equals("groovy.lang.MetaClass"))
             {
-                claimField(name, "Ignored");
-
+                tfi.claim("Ignored");
                 continue;
             }
 
@@ -634,18 +656,6 @@
 
     }
 
-    private CtField findDeclaredCtField(String fieldName)
-    {
-        try
-        {
-            return ctClass.getDeclaredField(fieldName);
-        }
-        catch (NotFoundException ex)
-        {
-            throw new RuntimeException(ex);
-        }
-    }
-
     public String newMemberName(String suggested)
     {
         failIfFrozen();
@@ -1155,6 +1165,8 @@
 
     public List<String> findFieldsWithAnnotation(final Class<? extends 
Annotation> annotationClass)
     {
+        Defense.notNull(annotationClass, "annotationClass");
+
         FieldFilter filter = new FieldFilter()
         {
             public boolean accept(String fieldName, String fieldType)
@@ -1168,21 +1180,18 @@
 
     public List<String> findFields(final FieldFilter filter)
     {
-        failIfFrozen();
-
-        List<String> result = CollectionFactory.newList();
+        Defense.notNull(filter, "filter");
 
-        for (TransformFieldImpl field : fields.values())
+        List<TransformField> fields2 = matchFields(new 
Predicate<TransformField>()
         {
-            String fieldName = field.getName();
-
-            if (filter.accept(fieldName, field.getType()))
-                result.add(fieldName);
-        }
-
-        Collections.sort(result);
+            @Override
+            public boolean accept(TransformField object)
+            {
+                return filter.accept(object.getName(), object.getType());
+            }
+        });
 
-        return result;
+        return toFieldNames(fields2);
     }
 
     public List<TransformField> matchFields(Predicate<TransformField> 
predicate)
@@ -1292,15 +1301,10 @@
 
         for (TransformFieldImpl f : fields.values())
         {
-            if (f.added || f.isClaimed())
+            if (f.added || f.removed || f.isClaimed())
                 continue;
 
-            String name = f.getName();
-
-            if (removedFieldNames != null && removedFieldNames.contains(name))
-                continue;
-
-            names.add(name);
+            names.add(f.getName());
         }
 
         Collections.sort(names);
@@ -1719,7 +1723,7 @@
 
         BodyBuilder constructor = new BodyBuilder();
 
-        // This is realy -1 + 2: The first value in constructorArgs is the
+        // This is really -1 + 2: The first value in constructorArgs is the
         // InternalComponentResources, which doesn't
         // count toward's the Instantiator's constructor ... then we add in 
the Model and String
         // description.
@@ -1924,14 +1928,7 @@
 
     public void removeField(String fieldName)
     {
-        formatter.format("remove field %s;\n\n", fieldName);
-
-        // TODO: We could check that there's an existing field read and field 
write transform ...
-
-        if (removedFieldNames == null)
-            removedFieldNames = CollectionFactory.newSet();
-
-        removedFieldNames.add(fieldName);
+        getField(fieldName).remove();
     }
 
     public void replaceReadAccess(String fieldName, String methodName)
@@ -1976,20 +1973,10 @@
         if (fieldReadTransforms != null || fieldWriteTransforms != null)
             replaceFieldAccess();
 
-        if (removedFieldNames != null)
+        for (TransformFieldImpl tfi : fields.values())
         {
-            for (String fieldName : removedFieldNames)
-            {
-                try
-                {
-                    CtField field = ctClass.getDeclaredField(fieldName);
-                    ctClass.removeField(field);
-                }
-                catch (NotFoundException ex)
-                {
-                    throw new RuntimeException(ex);
-                }
-            }
+            if (tfi.removed)
+                tfi.doRemove();
         }
     }
 

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ClassTransformation.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ClassTransformation.java?rev=903294&r1=903293&r2=903294&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ClassTransformation.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ClassTransformation.java
 Tue Jan 26 16:03:01 2010
@@ -233,7 +233,7 @@
 
     /**
      * Finds any declared <em>instance</em> fields that have not been claimed 
(via {...@link #claimField(String, Object)})
-     * and returns the names of those fields. May return an empty array.
+     * and have not been either added or deleted, and returns the names of 
those fields. May return an empty array.
      */
     List<String> findUnclaimedFields();
 
@@ -474,6 +474,7 @@
      *            the name of the field to remove
      * @see #replaceReadAccess(String, String)
      * @see #replaceWriteAccess(String, String)
+     * @deprecated Use {...@link TransformField#remove()} instead
      */
     void removeField(String fieldName);
 

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TransformField.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TransformField.java?rev=903294&r1=903293&r2=903294&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TransformField.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TransformField.java
 Tue Jan 26 16:03:01 2010
@@ -93,4 +93,13 @@
      * method signature.
      */
     <T> void assignIndirect(TransformMethodSignature signature, 
ComponentValueProvider<T> provider);
+
+    /**
+     * Marks the field for removal (at the end of the class transformation). 
Often, a field is deleted
+     * after access to the field is {...@linkplain 
#replaceAccess(ComponentValueProvider) replaced}.
+     * 
+     * @throws IllegalStateException
+     *             if the field has already been marked for deletion
+     */
+    void remove();
 }


Reply via email to