tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject svn commit: r911912 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/internal/transform/ test/java/org/apache/tapestry5/internal/transform/
Date Fri, 19 Feb 2010 18:53:23 GMT
Author: hlship
Date: Fri Feb 19 18:53:23 2010
New Revision: 911912

URL: http://svn.apache.org/viewvc?rev=911912&view=rev
Log:
Rewrite PageActivationContextWorker in terms of the updated ClassTransformation APIs

Removed:
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/transform/PageActivationContextWorkerTest.java
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageActivationContextWorker.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/TransformMessages.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageActivationContextWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageActivationContextWorker.java?rev=911912&r1=911911&r2=911912&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageActivationContextWorker.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageActivationContextWorker.java
Fri Feb 19 18:53:23 2010
@@ -1,10 +1,10 @@
-// Copyright 2008, 2009 The Apache Software Foundation
+// Copyright 2008, 2009, 2010 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
+// 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,
@@ -14,56 +14,114 @@
 
 package org.apache.tapestry5.internal.transform;
 
+import java.util.List;
+
+import org.apache.tapestry5.EventConstants;
 import org.apache.tapestry5.annotations.PageActivationContext;
 import org.apache.tapestry5.model.MutableComponentModel;
+import org.apache.tapestry5.runtime.ComponentEvent;
 import org.apache.tapestry5.services.ClassTransformation;
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
-import org.apache.tapestry5.services.TransformMethodSignature;
-
-import java.lang.reflect.Modifier;
-import java.util.List;
+import org.apache.tapestry5.services.ComponentMethodAdvice;
+import org.apache.tapestry5.services.ComponentMethodInvocation;
+import org.apache.tapestry5.services.FieldAccess;
+import org.apache.tapestry5.services.TransformConstants;
+import org.apache.tapestry5.services.TransformField;
 
 /**
- * Provides the page activation context handlers.  This worker must be scheduled before {@link
- * org.apache.tapestry5.internal.transform.OnEventWorker} in order for the added event handler
methods to be properly
- * picked up and processed.
- *
+ * Provides the page activation context handlers. This worker must be scheduled before
+ * {@link org.apache.tapestry5.internal.transform.OnEventWorker} in order for the added event
handler methods to be
+ * properly picked up and processed.
+ * 
  * @see org.apache.tapestry5.annotations.PageActivationContext
  */
 public class PageActivationContextWorker implements ComponentClassTransformWorker
 {
     public void transform(ClassTransformation transformation, MutableComponentModel model)
     {
-        List<String> fields = transformation.findFieldsWithAnnotation(PageActivationContext.class);
+
+        List<TransformField> fields = transformation.matchFieldsWithAnnotation(PageActivationContext.class);
+
+        // In the future we may add rules for ordering the fields (new attribute on annotation?)
 
         if (fields.size() > 1)
             throw new RuntimeException(TransformMessages.illegalNumberOfPageActivationContextHandlers(fields));
 
-        for (String fieldName : fields)
+        // So there's 0 or 1 of these
+
+        for (TransformField field : fields)
         {
-            PageActivationContext annotation = transformation.getFieldAnnotation(fieldName,
-                                                                                 PageActivationContext.class);
+            transformField(transformation, model, field);
+        }
+    }
 
-            String fieldType = transformation.getFieldType(fieldName);
+    private void transformField(ClassTransformation transformation, MutableComponentModel
model,
+            final TransformField field)
+    {
+        final PageActivationContext annotation = field.getAnnotation(PageActivationContext.class);
+
+        ComponentMethodAdvice advice = createAdvice(field, annotation);
+
+        transformation.getMethod(TransformConstants.DISPATCH_COMPONENT_EVENT).addAdvice(advice);
+
+        if (annotation.activate())
+            model.addEventHandler(EventConstants.ACTIVATE);
 
-            if (annotation.activate())
+        if (annotation.passivate())
+            model.addEventHandler(EventConstants.PASSIVATE);
+
+        // We don't claim the field, and other workers may even replace it with a FieldValueConduit.
+    }
+
+    private ComponentMethodAdvice createAdvice(TransformField field, final PageActivationContext
annotation)
+    {
+        final String fieldType = field.getType();
+        final FieldAccess access = field.getAccess();
+
+        return new ComponentMethodAdvice()
+        {
+            public void advise(ComponentMethodInvocation invocation)
             {
-                TransformMethodSignature activate
-                        = new TransformMethodSignature(Modifier.PROTECTED | Modifier.FINAL,
"void",
-                                                       "onActivate",
-                                                       new String[] { fieldType }, null);
-                transformation.addTransformedMethod(activate, fieldName + " = $1;");
+                invocation.proceed();
+
+                ComponentEvent event = (ComponentEvent) invocation.getParameter(0);
+
+                if (event.isAborted())
+                    return;
+
+                handleActivateEvent(event, invocation);
+
+                handlePassivateEvent(event, invocation);
             }
 
-            if (annotation.passivate())
+            private void handleActivateEvent(ComponentEvent event, ComponentMethodInvocation
invocation)
             {
-                TransformMethodSignature passivate
-                        = new TransformMethodSignature(Modifier.PROTECTED | Modifier.FINAL,
"java.lang.Object",
-                                                       "onPassivate",
-                                                       null, null);
-                transformation.addTransformedMethod(passivate, "return ($w) " + fieldName
+ ";");
+                if (annotation.activate() && event.matches(EventConstants.ACTIVATE,
"", 1))
+                {
+                    event.setMethodDescription(access.toString());
+
+                    Object value = event.coerceContext(0, fieldType);
+
+                    access.write(invocation.getInstance(), value);
+
+                    invocation.overrideResult(true);
+                }
             }
-        }
 
+            private void handlePassivateEvent(ComponentEvent event, ComponentMethodInvocation
invocation)
+            {
+
+                if (annotation.passivate() && event.matches(EventConstants.PASSIVATE,
"", 0))
+                {
+                    event.setMethodDescription(access.toString());
+
+                    Object value = access.read(invocation.getInstance());
+
+                    event.storeResult(value);
+
+                    invocation.overrideResult(true);
+                }
+            }
+        };
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/TransformMessages.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/TransformMessages.java?rev=911912&r1=911911&r2=911912&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/TransformMessages.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/TransformMessages.java
Fri Feb 19 18:53:23 2010
@@ -20,8 +20,10 @@
 import org.apache.tapestry5.annotations.MixinClasses;
 import org.apache.tapestry5.internal.structure.InternalComponentResourcesImpl;
 import org.apache.tapestry5.ioc.Messages;
+import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 import org.apache.tapestry5.ioc.internal.util.MessagesImpl;
+import org.apache.tapestry5.services.TransformField;
 import org.apache.tapestry5.services.TransformMethodSignature;
 
 class TransformMessages
@@ -43,24 +45,28 @@
         return MESSAGES.format("cached-no-parameters", method);
     }
 
-    static String illegalNumberOfPageActivationContextHandlers(List<String> fields)
+    static String illegalNumberOfPageActivationContextHandlers(List<TransformField>
fields)
     {
-        return MESSAGES.format("illegal-number-of-page-activation-context-handlers", InternalUtils
-                .joinSorted(fields));
+        List<String> names = CollectionFactory.newList();
+
+        for (TransformField field : fields)
+        {
+            names.add(field.getName());
+        }
+
+        return MESSAGES.format("illegal-number-of-page-activation-context-handlers", InternalUtils.joinSorted(names));
     }
 
     public static String badMixinConstraintLength(MixinClasses mixin, String fieldName)
     {
-        return MESSAGES.format("bad-mixin-constraint-length", mixin.value().length, fieldName,
-                mixin.order().length);
+        return MESSAGES.format("bad-mixin-constraint-length", mixin.value().length, fieldName,
mixin.order().length);
     }
 
     /** @since 5.2.0 */
-    public static String bindParameterOnlyOnMixin(String boundParameterName,
-            ComponentResources resources)
+    public static String bindParameterOnlyOnMixin(String boundParameterName, ComponentResources
resources)
     {
-        return MESSAGES.format("bind-parameter-only-on-mixin", boundParameterName,
-                resources.getComponentModel().getComponentClassName());
+        return MESSAGES.format("bind-parameter-only-on-mixin", boundParameterName, resources.getComponentModel()
+                .getComponentClassName());
     }
 
 }



Mime
View raw message