You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2011/04/06 21:10:40 UTC

svn commit: r1089574 - in /tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc: ./ internal/ internal/services/

Author: hlship
Date: Wed Apr  6 19:10:39 2011
New Revision: 1089574

URL: http://svn.apache.org/viewvc?rev=1089574&view=rev
Log:
TAP5-83: First pass at re-implementing AspectDecoratorImpl

(note: 9 test failures)

Removed:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AdvisedMethodInvocationBuilder.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ConstantInjector.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ConstantInjectorImpl.java
Modified:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/RegistryBuilder.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AspectDecoratorImpl.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImpl.java

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/RegistryBuilder.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/RegistryBuilder.java?rev=1089574&r1=1089573&r2=1089574&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/RegistryBuilder.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/RegistryBuilder.java Wed Apr  6 19:10:39 2011
@@ -174,7 +174,7 @@ public final class RegistryBuilder
     {
         lock.lock();
 
-        RegistryImpl registry = new RegistryImpl(modules, classFactory, null, loggerSource);
+        RegistryImpl registry = new RegistryImpl(modules, classFactory, proxyFactory, loggerSource);
 
         return new RegistryWrapper(registry);
     }

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java?rev=1089574&r1=1089573&r2=1089574&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java Wed Apr  6 19:10:39 2011
@@ -165,6 +165,11 @@ public class RegistryImpl implements Reg
     public RegistryImpl(Collection<ModuleDef> moduleDefs, ClassFactory classFactory, PlasticProxyFactory proxyFactory,
             LoggerSource loggerSource)
     {
+        assert moduleDefs != null;
+        assert classFactory != null;
+        assert proxyFactory != null;
+        assert loggerSource != null;
+        
         this.loggerSource = loggerSource;
 
         operationTracker = new PerThreadOperationTracker(loggerSource.getLogger(Registry.class));

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AspectDecoratorImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AspectDecoratorImpl.java?rev=1089574&r1=1089573&r2=1089574&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AspectDecoratorImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AspectDecoratorImpl.java Wed Apr  6 19:10:39 2011
@@ -1,4 +1,4 @@
-// Copyright 2008, 2009, 2010 The Apache Software Foundation
+// Copyright 2008, 2009, 2010, 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.
@@ -22,17 +22,17 @@ import org.apache.tapestry5.ioc.internal
 import org.apache.tapestry5.ioc.services.AspectDecorator;
 import org.apache.tapestry5.ioc.services.AspectInterceptorBuilder;
 import org.apache.tapestry5.ioc.services.Builtin;
-import org.apache.tapestry5.ioc.services.ClassFactory;
+import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
 
 @PreventServiceDecoration
 public class AspectDecoratorImpl implements AspectDecorator
 {
-    private final ClassFactory classFactory;
+    private final PlasticProxyFactory proxyFactory;
 
     public AspectDecoratorImpl(@Builtin
-    ClassFactory classFactory)
+    PlasticProxyFactory proxyFactory)
     {
-        this.classFactory = classFactory;
+        this.proxyFactory = proxyFactory;
     }
 
     public <T> T build(Class<T> serviceInterface, T delegate, MethodAdvice advice, String description)
@@ -51,6 +51,10 @@ public class AspectDecoratorImpl impleme
         assert serviceInterface != null;
         assert delegate != null;
         assert InternalUtils.isNonBlank(description);
+
+        // The inner class here prevents the needless creation of the AspectInterceptorBuilderImpl,
+        // and the various Plastic related overhead, until there's some actual advice.
+
         return new AspectInterceptorBuilder<T>()
         {
             private AspectInterceptorBuilder<T> builder;
@@ -78,7 +82,7 @@ public class AspectDecoratorImpl impleme
             private AspectInterceptorBuilder<T> getBuilder()
             {
                 if (builder == null)
-                    builder = new AspectInterceptorBuilderImpl<T>(classFactory, serviceInterface, delegate, description);
+                    builder = new AspectInterceptorBuilderImpl<T>(proxyFactory, serviceInterface, delegate, description);
 
                 return builder;
             }

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImpl.java?rev=1089574&r1=1089573&r2=1089574&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImpl.java Wed Apr  6 19:10:39 2011
@@ -1,4 +1,4 @@
-// Copyright 2008, 2009, 2010 The Apache Software Foundation
+// Copyright 2008, 2009, 2010, 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,54 +23,46 @@ import java.util.Set;
 
 import org.apache.tapestry5.ioc.MethodAdvice;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
+import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 import org.apache.tapestry5.ioc.internal.util.OneShotLock;
 import org.apache.tapestry5.ioc.services.AspectInterceptorBuilder;
 import org.apache.tapestry5.ioc.services.ClassFab;
 import org.apache.tapestry5.ioc.services.ClassFabUtils;
-import org.apache.tapestry5.ioc.services.ClassFactory;
 import org.apache.tapestry5.ioc.services.MethodSignature;
+import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
+import org.apache.tapestry5.plastic.PlasticClass;
+import org.apache.tapestry5.plastic.PlasticClassTransformation;
+import org.apache.tapestry5.plastic.PlasticField;
 
 @SuppressWarnings("all")
 public class AspectInterceptorBuilderImpl<T> implements AspectInterceptorBuilder<T>
 {
-    private final ClassFactory classFactory;
-
     private final Class<T> serviceInterface;
 
-    private final ClassFab interceptorFab;
-
-    private final ConstantInjectorImpl injector;
-
-    private final String delegateFieldName;
-
-    private final String description;
-
-    private final OneShotLock lock = new OneShotLock();
-
-    private final Set<Method> remainingMethods = CollectionFactory.newSet();
+    private final Set<Method> allMethods = CollectionFactory.newSet();
 
-    private final Map<Method, AdvisedMethodInvocationBuilder> methodToBuilder = CollectionFactory.newMap();
+    private final PlasticClassTransformation transformation;
 
-    /**
-     * Set to true if we ever see toString() as a method of the interface; either advised or pass thru. If false at the
-     * end, we add our own implementation.
-     */
-    private boolean sawToString;
+    private final PlasticClass plasticClass;
 
-    public AspectInterceptorBuilderImpl(ClassFactory classFactory, Class<T> serviceInterface, T delegate,
+    public AspectInterceptorBuilderImpl(PlasticProxyFactory plasticProxyFactory, Class<T> serviceInterface, T delegate,
             String description)
     {
-        this.classFactory = classFactory;
         this.serviceInterface = serviceInterface;
-        this.description = description;
 
-        interceptorFab = this.classFactory.newClass(serviceInterface);
+        transformation = plasticProxyFactory.createProxyTransformation(serviceInterface);
+        plasticClass = transformation.getPlasticClass();
 
-        injector = new ConstantInjectorImpl(interceptorFab);
+        plasticClass.addToString(description);
 
-        delegateFieldName = injector.inject(serviceInterface, delegate);
+        allMethods.addAll(Arrays.asList(serviceInterface.getMethods()));
 
-        remainingMethods.addAll(Arrays.asList(serviceInterface.getMethods()));
+        PlasticField delegateField = plasticClass.introduceField(serviceInterface, "delegate").inject(delegate);
+
+        for (Method method : allMethods)
+        {
+            plasticClass.introduceMethod(method).delegateTo(delegateField);
+        }
     }
 
     public void adviseMethod(Method method, MethodAdvice advice)
@@ -78,28 +70,11 @@ public class AspectInterceptorBuilderImp
         assert method != null;
         assert advice != null;
 
-        lock.check();
-
-        AdvisedMethodInvocationBuilder builder = methodToBuilder.get(method);
-
-        if (builder == null)
-        {
-            if (!remainingMethods.contains(method))
-                throw new IllegalArgumentException(String.format("Method %s is not defined for interface %s.", method,
-                        serviceInterface));
-
-            // One less method to pass thru to the delegate
-
-            remainingMethods.remove(method);
+        if (!allMethods.contains(method))
+            throw new IllegalArgumentException(String.format("Method %s is not defined for interface %s.", method,
+                    serviceInterface));
 
-            sawToString |= ClassFabUtils.isToString(method);
-
-            builder = new AdvisedMethodInvocationBuilder(classFactory, serviceInterface, method);
-
-            methodToBuilder.put(method, builder);
-        }
-
-        builder.addAdvice(advice);
+        plasticClass.introduceMethod(method).addAdvice(InternalUtils.toPlasticMethodAdvice(advice));
     }
 
     public void adviseAllMethods(MethodAdvice advice)
@@ -115,55 +90,6 @@ public class AspectInterceptorBuilderImp
 
     public T build()
     {
-        lock.lock();
-
-        // Finish up each method that has been advised
-
-        for (AdvisedMethodInvocationBuilder builder : methodToBuilder.values())
-        {
-            builder.commit(interceptorFab, delegateFieldName, injector);
-        }
-
-        // Hit all the methods that haven't been referenced so far.
-
-        addPassthruMethods();
-
-        // And if we haven't seen a toString(), we can add it now.
-
-        if (!sawToString)
-            interceptorFab.addToString(description);
-
-        injector.implementConstructor();
-
-        Class interceptorClass = interceptorFab.createClass();
-
-        Object[] parameters = injector.getParameters();
-
-        try
-        {
-            Constructor constructor = interceptorClass.getConstructors()[0];
-
-            Object raw = constructor.newInstance(parameters);
-
-            return serviceInterface.cast(raw);
-        }
-        catch (Exception ex)
-        {
-            throw new RuntimeException(ex);
-        }
-    }
-
-    private void addPassthruMethods()
-    {
-        for (Method m : remainingMethods)
-        {
-            sawToString |= ClassFabUtils.isToString(m);
-
-            MethodSignature sig = new MethodSignature(m);
-
-            String body = String.format("return ($r) %s.%s($$);", delegateFieldName, m.getName());
-
-            interceptorFab.addMethod(Modifier.PUBLIC, sig, body);
-        }
+        return (T) transformation.createInstantiator().newInstance();
     }
 }