You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2011/07/25 01:03:37 UTC

svn commit: r1150516 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/internal/transform/ main/java/org/apache/tapestry5/services/ main/java/org/apache/tapestry5/services/transform/ test/java/org/apache/tapestry5/interna...

Author: hlship
Date: Sun Jul 24 23:03:36 2011
New Revision: 1150516

URL: http://svn.apache.org/viewvc?rev=1150516&view=rev
Log:
TAP5-1508: Convert the last implementation of CCTW, InjectWorker, to CCTW2

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectionProviderToInjectionProvider2.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/transform/InjectionProvider2.java
      - copied, changed from r1150515, tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/InjectionProvider.java
Removed:
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/transform/InjectWorkerTest.java
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectNamedWorker.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectWorker.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/InjectionProvider.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectNamedWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectNamedWorker.java?rev=1150516&r1=1150515&r2=1150516&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectNamedWorker.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectNamedWorker.java Sun Jul 24 23:03:36 2011
@@ -30,6 +30,8 @@ import javax.inject.Named;
 
 /**
  * Processes the combination of {@link javax.inject.Inject} and {@link javax.inject.Named} annotations.
+ * <p/>
+ * TODO: This likely can be converted into an {@link org.apache.tapestry5.services.transform.InjectionProvider2}.
  *
  * @since 5.3
  */
@@ -55,7 +57,7 @@ public class InjectNamedWorker implement
 
     public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model)
     {
-        Flow<PlasticField> fields = F.flow(plasticClass.getAllFields()).filter(MATCHER);
+        Flow<PlasticField> fields = F.flow(plasticClass.getUnclaimedFields()).filter(MATCHER);
 
         for (PlasticField field : fields)
         {

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectWorker.java?rev=1150516&r1=1150515&r2=1150516&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectWorker.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectWorker.java Sun Jul 24 23:03:36 2011
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007 The Apache Software Foundation
+// Copyright 2006, 2007, 2011 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -14,94 +14,79 @@
 
 package org.apache.tapestry5.internal.transform;
 
-import java.lang.annotation.Annotation;
-import java.util.List;
-
-import javax.inject.Named;
-
+import org.apache.tapestry5.func.F;
 import org.apache.tapestry5.func.Predicate;
 import org.apache.tapestry5.ioc.ObjectLocator;
 import org.apache.tapestry5.ioc.OperationTracker;
 import org.apache.tapestry5.ioc.annotations.Inject;
 import org.apache.tapestry5.model.MutableComponentModel;
-import org.apache.tapestry5.services.ClassTransformation;
-import org.apache.tapestry5.services.ComponentClassTransformWorker;
-import org.apache.tapestry5.services.InjectionProvider;
-import org.apache.tapestry5.services.TransformField;
+import org.apache.tapestry5.plastic.PlasticClass;
+import org.apache.tapestry5.plastic.PlasticField;
+import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
+import org.apache.tapestry5.services.transform.InjectionProvider2;
+import org.apache.tapestry5.services.transform.TransformationSupport;
 
 /**
  * Performs injection triggered by any field annotated with the {@link org.apache.tapestry5.ioc.annotations.Inject}
  * annotation or the {@link javax.inject.Inject} annotation.
  * <p/>
- * The implementation of this worker mostly delegates to a chain of command of
- * {@link org.apache.tapestry5.services.InjectionProvider}s.
+ * The implementation of this worker mostly delegates to a chain of command of {@link InjectionProvider2}.
  */
-public class InjectWorker implements ComponentClassTransformWorker
+public class InjectWorker implements ComponentClassTransformWorker2
 {
     private final ObjectLocator locator;
 
     // Really, a chain of command
 
-    private final InjectionProvider injectionProvider;
+    private final InjectionProvider2 injectionProvider;
 
     private final OperationTracker tracker;
 
-    public InjectWorker(ObjectLocator locator, InjectionProvider injectionProvider, OperationTracker tracker)
+    private final Predicate<PlasticField> MATCHER = new Predicate<PlasticField>()
+    {
+        public boolean accept(PlasticField field)
+        {
+            // For the moment, InjectNamedWorker handles javax.inject.Inject w/ Named, and this code
+            // handles javax.inject.Inject otherwise. InjectNamedWorker runs *first* so if we can see
+            // an unclaimed field, with javax.inject.Inject, it is safe to continue.
+
+            return field.hasAnnotation(Inject.class) ||
+                    field.hasAnnotation(javax.inject.Inject.class);
+        }
+    };
+
+    public InjectWorker(ObjectLocator locator, InjectionProvider2 injectionProvider, OperationTracker tracker)
     {
         this.locator = locator;
         this.injectionProvider = injectionProvider;
         this.tracker = tracker;
     }
 
-    public final void transform(final ClassTransformation transformation, final MutableComponentModel model)
+    public void transform(final PlasticClass plasticClass, TransformationSupport support, final MutableComponentModel model)
     {
-    	List<TransformField> fields = matchFields(transformation);
-    	
-        for (final TransformField field : fields)
+        for (final PlasticField field : F.flow(plasticClass.getUnclaimedFields()).filter(MATCHER))
         {
-        	final String fieldName = field.getName();
-        	
-            tracker.run("Injecting field " + fieldName, new Runnable()
+            final String fieldName = field.getName();
+
+            tracker.run(String.format("Injecting field  %s.%s", plasticClass.getClassName(), fieldName), new Runnable()
             {
                 public void run()
                 {
-
-                    Inject inject = field.getAnnotation(Inject.class);
-                    
-                    Annotation annotation = inject == null? field.getAnnotation(javax.inject.Inject.class): inject;
-
                     try
                     {
-                        String fieldType = field.getType();
-
-                        Class type = transformation.toClass(fieldType);
-
-                        boolean success = injectionProvider.provideInjection(fieldName, type, locator, transformation,
-                                model);
+                        boolean success = injectionProvider.provideInjection(field, locator, model);
 
                         if (success)
-                            field.claim(annotation);
-                    }
-                    catch (RuntimeException ex)
+                        {
+                            field.claim("@Inject");
+                        }
+                    } catch (RuntimeException ex)
                     {
-                        throw new RuntimeException(TransformMessages.fieldInjectionError(transformation.getClassName(),
+                        throw new RuntimeException(TransformMessages.fieldInjectionError(plasticClass.getClassName(),
                                 fieldName, ex), ex);
                     }
                 }
             });
         }
     }
-    
-    private List<TransformField> matchFields(final ClassTransformation transformation)
-    {	
-    	Predicate<TransformField> predicate = new Predicate<TransformField>()
-    	{
-			public boolean accept(TransformField field) 
-			{
-				return field.getAnnotation(Inject.class) != null 
-						|| (field.getAnnotation(javax.inject.Inject.class) != null && field.getAnnotation(Named.class) == null);
-			}
-		};
-    	return transformation.matchFields(predicate);
-    }
 }

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectionProviderToInjectionProvider2.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectionProviderToInjectionProvider2.java?rev=1150516&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectionProviderToInjectionProvider2.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectionProviderToInjectionProvider2.java Sun Jul 24 23:03:36 2011
@@ -0,0 +1,56 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.internal.transform;
+
+import org.apache.tapestry5.internal.services.ComponentClassCache;
+import org.apache.tapestry5.ioc.ObjectLocator;
+import org.apache.tapestry5.ioc.services.Coercion;
+import org.apache.tapestry5.model.MutableComponentModel;
+import org.apache.tapestry5.plastic.PlasticField;
+import org.apache.tapestry5.services.InjectionProvider;
+import org.apache.tapestry5.services.transform.InjectionProvider2;
+
+
+/**
+ * Converts old style {@link InjectionProvider} to the new {@link InjectionProvider2}.
+ *
+ * @since 5.3
+ */
+public class InjectionProviderToInjectionProvider2 implements Coercion<InjectionProvider, InjectionProvider2>
+{
+    private final ComponentClassCache classCache;
+
+    public InjectionProviderToInjectionProvider2(ComponentClassCache classCache)
+    {
+        this.classCache = classCache;
+    }
+
+    public InjectionProvider2 coerce(final InjectionProvider input)
+    {
+        return new InjectionProvider2()
+        {
+            public boolean provideInjection(PlasticField field, ObjectLocator locator, MutableComponentModel componentModel)
+            {
+                Class fieldType = classCache.forName(field.getTypeName());
+
+                // We don't currently provide a TransformationSupport into the BridgeClassTransformation,
+                // as it should not be needed.
+
+                return input.provideInjection(field.getName(), fieldType, locator,
+                        new BridgeClassTransformation(field.getPlasticClass(), null, componentModel), componentModel);
+            }
+        };
+    }
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/InjectionProvider.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/InjectionProvider.java?rev=1150516&r1=1150515&r2=1150516&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/InjectionProvider.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/InjectionProvider.java Sun Jul 24 23:03:36 2011
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007 The Apache Software Foundation
+// Copyright 2006, 2007, 2011 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -23,13 +23,15 @@ import org.apache.tapestry5.model.Mutabl
  * field type.
  * <p/>
  * This interface will be used as part of a {@link org.apache.tapestry5.ioc.services.ChainBuilder chain of command}.
+ *
+ * @deprecated Deprecated in 5.3, use {@link org.apache.tapestry5.services.transform.InjectionProvider2} instead. Instances
+ *             of InjectProvider will be {@linkplain org.apache.tapestry5.ioc.services.TypeCoercer coerced} to InjectionProvider2.
  */
 public interface InjectionProvider
 {
     /**
-     * Peform the injection, if possible. Most often, this will result in a call to {@link
-     * ClassTransformation#injectField(String, Object)}. The caller is responsible for invoking {@link
-     * ClassTransformation#claimField(String, Object)}.
+     * Perform the injection, if possible. Most often, this will result in a call to {@link
+     * TransformField#inject(Object)}. The caller is responsible for claiming the field.
      *
      * @param fieldName      the name of the field requesting injection
      * @param fieldType      the type of the field

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java?rev=1150516&r1=1150515&r2=1150516&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java Sun Jul 24 23:03:36 2011
@@ -96,6 +96,7 @@ import org.apache.tapestry5.services.met
 import org.apache.tapestry5.services.pageload.PageLoadModule;
 import org.apache.tapestry5.services.templates.ComponentTemplateLocator;
 import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
+import org.apache.tapestry5.services.transform.InjectionProvider2;
 import org.apache.tapestry5.util.StringToEnumCoercion;
 import org.apache.tapestry5.validator.*;
 import org.slf4j.Logger;
@@ -546,6 +547,8 @@ public final class TapestryModule
      * <dd>Checks for the {@link org.apache.tapestry5.annotations.Log} annotation</dd>
      * <dt>HeartbeatDeferred
      * <dd>Support for the {@link HeartbeatDeferred} annotation, which defers method invocation to the end of the {@link Heartbeat}
+     * <dt>Inject</dt>
+     * <dd>Used with the {@link org.apache.tapestry5.ioc.annotations.Inject} annotation, when a value is supplied</dd>
      * </dl>
      */
     @Contribute(ComponentClassTransformWorker2.class)
@@ -609,7 +612,9 @@ public final class TapestryModule
 
         configuration.addInstance("PageReset", PageResetAnnotationWorker.class);
         configuration.addInstance("InjectService", InjectServiceWorker.class);
+
         configuration.addInstance("InjectNamed", InjectNamedWorker.class);
+        configuration.addInstance("Inject", InjectWorker.class);
 
         configuration.addInstance("Persist", PersistWorker.class);
 
@@ -627,21 +632,6 @@ public final class TapestryModule
     }
 
     /**
-     * Adds a number of standard component class transform workers:
-     * <dl>
-     * <dt>Inject</dt>
-     * <dd>Used with the {@link org.apache.tapestry5.ioc.annotations.Inject} annotation, when a value is supplied</dd>
-     * </dl>
-     */
-    @Contribute(ComponentClassTransformWorker2.class)
-    @Primary
-    public static void provideOldStyleClassTransformWorkers(
-            OrderedConfiguration<ComponentClassTransformWorker> configuration)
-    {
-        configuration.addInstance("Inject", InjectWorker.class);
-    }
-
-    /**
      * <dl>
      * <dt>Annotation</dt>
      * <dd>Checks for {@link org.apache.tapestry5.beaneditor.DataType} annotation</dd>
@@ -988,6 +978,7 @@ public final class TapestryModule
      * <li>String to {@link Pattern}</li>
      * <li>String to {@link DateFormat}</li>
      * <li>{@link ComponentClassTransformWorker} to {@link ComponentClassTransformWorker2}</li>
+     * <li>{@link InjectionProvider} to {@link InjectionProvider2}</li>
      * <li>{@link Resource} to {@link DynamicTemplate}</li>
      * <li>{@link Asset} to {@link Resource}</li>
      * <li>String to {@link JSONObject}</li>
@@ -1007,6 +998,9 @@ public final class TapestryModule
                                              final AssetSource assetSource,
 
                                              @Core
+                                             final ComponentClassCache classCache,
+
+                                             @Core
                                              final DynamicTemplateParser dynamicTemplateParser)
     {
         configuration.add(CoercionTuple.create(ComponentResources.class, PropertyOverrides.class,
@@ -1177,6 +1171,9 @@ public final class TapestryModule
                 return new GenericValueEncoderFactory(input);
             }
         }));
+
+        configuration.add(CoercionTuple.create(InjectionProvider.class, InjectionProvider2.class,
+                new InjectionProviderToInjectionProvider2(classCache)));
     }
 
     /**
@@ -1358,11 +1355,13 @@ public final class TapestryModule
      * configuration can be extended to allow for different automatic injections
      * (based on some combination of field
      * type and field name).
+     * <p/>
+     * Note that contributions to this service may be old-style {@link InjectionProvider}, which will
+     * be coerced to {@link InjectionProvider2}.
      */
-
-    public InjectionProvider buildInjectionProvider(List<InjectionProvider> configuration)
+    public InjectionProvider2 buildInjectionProvider(List<InjectionProvider2> configuration)
     {
-        return chainBuilder.build(InjectionProvider.class, configuration);
+        return chainBuilder.build(InjectionProvider2.class, configuration);
     }
 
     /**

Copied: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/transform/InjectionProvider2.java (from r1150515, tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/InjectionProvider.java)
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/transform/InjectionProvider2.java?p2=tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/transform/InjectionProvider2.java&p1=tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/InjectionProvider.java&r1=1150515&r2=1150516&rev=1150516&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/InjectionProvider.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/transform/InjectionProvider2.java Sun Jul 24 23:03:36 2011
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007 The Apache Software Foundation
+// Copyright 2011 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -12,10 +12,12 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package org.apache.tapestry5.services;
+package org.apache.tapestry5.services.transform;
 
 import org.apache.tapestry5.ioc.ObjectLocator;
+import org.apache.tapestry5.ioc.annotations.UsesOrderedConfiguration;
 import org.apache.tapestry5.model.MutableComponentModel;
+import org.apache.tapestry5.plastic.PlasticField;
 
 /**
  * Provides some form of injection when the value for an {@link org.apache.tapestry5.ioc.annotations.Inject} annotation is
@@ -24,20 +26,20 @@ import org.apache.tapestry5.model.Mutabl
  * <p/>
  * This interface will be used as part of a {@link org.apache.tapestry5.ioc.services.ChainBuilder chain of command}.
  */
-public interface InjectionProvider
+@UsesOrderedConfiguration(InjectionProvider2.class)
+public interface InjectionProvider2
 {
     /**
-     * Peform the injection, if possible. Most often, this will result in a call to {@link
-     * ClassTransformation#injectField(String, Object)}. The caller is responsible for invoking {@link
-     * ClassTransformation#claimField(String, Object)}.
+     * Perform the injection, if possible. Most often, this will result in a call to {@link
+     * org.apache.tapestry5.plastic.PlasticField#inject(Object)}. The caller is responsible for invoking {@link
+     * org.apache.tapestry5.plastic.PlasticField#claim(Object)}.
      *
-     * @param fieldName      the name of the field requesting injection
-     * @param fieldType      the type of the field
+     * @param field          that has the {@link org.apache.tapestry5.ioc.annotations.Inject} annotation
      * @param locator        allows services to be located
-     * @param transformation allows the code for the class to be transformed
      * @param componentModel defines the relevant aspects of the component
      * @return true if an injection has been made (terminates the command chain), false to continue down the chain
      */
-    boolean provideInjection(String fieldName, Class fieldType, ObjectLocator locator,
-                             ClassTransformation transformation, MutableComponentModel componentModel);
+    boolean provideInjection(PlasticField field, ObjectLocator locator,
+                             MutableComponentModel componentModel);
 }
+