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/14 00:22:34 UTC

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

Author: hlship
Date: Wed Apr 13 22:22:33 2011
New Revision: 1091934

URL: http://svn.apache.org/viewvc?rev=1091934&view=rev
Log:
TAP5-853: Convert StrategyBuilderImpl to use PlasticProxyFactory

Modified:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/StrategyBuilderImpl.java

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/StrategyBuilderImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/StrategyBuilderImpl.java?rev=1091934&r1=1091933&r2=1091934&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/StrategyBuilderImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/StrategyBuilderImpl.java Wed Apr 13 22:22:33 2011
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007, 2008 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 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
+// 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,40 +14,34 @@
 
 package org.apache.tapestry5.ioc.internal.services;
 
-import org.apache.tapestry5.ioc.services.*;
-import org.apache.tapestry5.ioc.util.BodyBuilder;
-import org.apache.tapestry5.ioc.util.StrategyRegistry;
-
-import java.lang.reflect.Modifier;
+import java.lang.reflect.Method;
 import java.util.Map;
 
+import org.apache.tapestry5.ioc.services.Builtin;
+import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
+import org.apache.tapestry5.ioc.services.StrategyBuilder;
+import org.apache.tapestry5.ioc.util.StrategyRegistry;
+import org.apache.tapestry5.plastic.ClassInstantiator;
+import org.apache.tapestry5.plastic.InstructionBuilder;
+import org.apache.tapestry5.plastic.InstructionBuilderCallback;
+import org.apache.tapestry5.plastic.PlasticClass;
+import org.apache.tapestry5.plastic.PlasticClassTransformer;
+import org.apache.tapestry5.plastic.PlasticField;
+import org.apache.tapestry5.plastic.PlasticUtils;
+
 public class StrategyBuilderImpl implements StrategyBuilder
 {
-    private final ClassFactory classFactory;
+    private final PlasticProxyFactory proxyFactory;
 
-    public StrategyBuilderImpl(@Builtin ClassFactory classFactory)
+    public StrategyBuilderImpl(@Builtin
+    PlasticProxyFactory proxyFactory)
     {
-        this.classFactory = classFactory;
+        this.proxyFactory = proxyFactory;
     }
 
     public <S> S build(StrategyRegistry<S> registry)
     {
-        Class<S> interfaceClass = registry.getAdapterType();
-
-        // TODO: Could cache the implClass by interfaceClass ...
-
-        Class implClass = createImplClass(interfaceClass);
-
-        try
-        {
-            Object raw = implClass.getConstructors()[0].newInstance(registry);
-
-            return interfaceClass.cast(raw);
-        }
-        catch (Exception ex)
-        {
-            throw new RuntimeException(ex);
-        }
+        return createProxy(registry.getAdapterType(), registry);
     }
 
     public <S> S build(Class<S> adapterType, Map<Class, S> registrations)
@@ -57,44 +51,49 @@ public class StrategyBuilderImpl impleme
         return build(registry);
     }
 
-    private Class createImplClass(Class interfaceClass)
+    private <S> S createProxy(final Class<S> interfaceType, final StrategyRegistry<S> registry)
     {
-        ClassFab cf = classFactory.newClass(interfaceClass);
-
-        String interfaceClassName = interfaceClass.getName();
-
-        cf.addField("_registry", Modifier.PRIVATE | Modifier.FINAL, StrategyRegistry.class);
-        cf.addConstructor(new Class[]
-                {StrategyRegistry.class}, null, "_registry = $1;");
-
-        BodyBuilder builder = new BodyBuilder();
-
-        MethodIterator mi = new MethodIterator(interfaceClass);
-
-        while (mi.hasNext())
+        ClassInstantiator instantiator = proxyFactory.createProxy(interfaceType, new PlasticClassTransformer()
         {
-            MethodSignature sig = mi.next();
-
-            // TODO: Checks for methods that don't have at least one parameter, or don't have a
-            // compatible 1st parameter. Currently, we'll get a Javassist exception.
-
-            builder.clear();
-            builder.begin();
-
-            builder.addln("Object selector = $1;");
-            builder.addln(
-                    "%s adapter = (%<s) _registry.getByInstance(selector);",
-                    interfaceClassName);
-            builder.addln("return ($r) adapter.%s($$);", sig.getName());
-
-            builder.end();
-
-            cf.addMethod(Modifier.PUBLIC, sig, builder.toString());
-        }
-
-        if (!mi.getToString())
-            cf.addToString(String.format("<Strategy for %s>", interfaceClassName));
+            public void transform(PlasticClass plasticClass)
+            {
+                final PlasticField registryField = plasticClass.introduceField(StrategyRegistry.class, "registry")
+                        .inject(registry);
+
+                for (final Method method : interfaceType.getMethods())
+                {
+                    plasticClass.introduceMethod(method).changeImplementation(new InstructionBuilderCallback()
+                    {
+                        public void doBuild(InstructionBuilder builder)
+                        {
+                            Class returnType = method.getReturnType();
+
+                            builder.loadThis().getField(registryField);
+
+                            // Argument 0 is the selector used to find the adapter and should be an object reference,
+                            // not a primitive.
+
+                            builder.loadArgument(0);
+
+                            // Use the StrategyRegistry to get the adapter to re-invoke the method on
+                            builder.invoke(StrategyRegistry.class, Object.class, "getByInstance", Object.class)
+                                    .checkcast(interfaceType);
+
+                            // That leaves the correct adapter on top of the stack. Get the
+                            // selector and the rest of the arguments in place and invoke the method.
+
+                            builder.loadArguments().invoke(interfaceType, returnType, method.getName(),
+                                    method.getParameterTypes());
+
+                            builder.returnResult();
+                        }
+                    });
+                }
+
+                plasticClass.addToString(String.format("<Strategy for %s>", interfaceType.getName()));
+            }
+        });
 
-        return cf.createClass();
+        return interfaceType.cast(instantiator.newInstance());
     }
 }