tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject svn commit: r1098835 - in /tapestry/tapestry5/trunk: plastic/src/main/java/org/apache/tapestry5/internal/plastic/ tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/ tapestry-ioc/src/...
Date Mon, 02 May 2011 22:55:04 GMT
Author: hlship
Date: Mon May  2 22:55:04 2011
New Revision: 1098835

URL: http://svn.apache.org/viewvc?rev=1098835&view=rev
Log:
TAP5-853: Create an implementation of PlasticProxyFactory.getMethodLocation() and getConstructorLocation()
based on ASM Tree API (not ClassFactory)

Modified:
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassPool.java
    tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java
    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/services/PlasticProxyFactoryImpl.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java

Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassPool.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassPool.java?rev=1098835&r1=1098834&r2=1098835&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassPool.java
(original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticClassPool.java
Mon May  2 22:55:04 2011
@@ -14,9 +14,6 @@
 
 package org.apache.tapestry5.internal.plastic;
 
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
@@ -73,7 +70,7 @@ public class PlasticClassPool implements
         }
     };
 
-    class BaseClassDef
+    static class BaseClassDef
     {
         final MethodBundle methodBundle;
 
@@ -382,65 +379,14 @@ public class PlasticClassPool implements
         if (bytecode == null)
             return null;
 
-        return convertBytecodeToClassNode(bytecode);
-    }
-
-    private ClassNode convertBytecodeToClassNode(byte[] bytecode)
-    {
-        ClassReader cr = new ClassReader(bytecode);
-
-        ClassNode result = new ClassNode();
-
-        cr.accept(result, 0);
-
-        return result;
+        return PlasticInternalUtils.convertBytecodeToClassNode(bytecode);
     }
 
     private byte[] readBytecode(String className)
     {
         ClassLoader parentClassLoader = loader.getParent();
 
-        String path = PlasticInternalUtils.toClassPath(className);
-
-        InputStream stream = parentClassLoader.getResourceAsStream(path);
-
-        if (stream == null) { throw new RuntimeException(String.format(
-                "Unable to locate class file for '%s' in class loader %s.", className, parentClassLoader));
}
-
-        try
-        {
-            return readBytestream(stream);
-        }
-        catch (IOException ex)
-        {
-            throw new RuntimeException(String.format("Failure reading bytecode for class
%s: %s", className,
-                    PlasticInternalUtils.toMessage(ex)), ex);
-        }
-        finally
-        {
-            PlasticInternalUtils.close(stream);
-        }
-    }
-
-    private byte[] readBytestream(InputStream stream) throws IOException
-    {
-        byte[] buffer = new byte[5000];
-
-        ByteArrayOutputStream bos = new ByteArrayOutputStream();
-
-        while (true)
-        {
-            int length = stream.read(buffer);
-
-            if (length < 0)
-                break;
-
-            bos.write(buffer, 0, length);
-        }
-
-        bos.close();
-
-        return bos.toByteArray();
+        return PlasticInternalUtils.readBytecodeForClass(parentClassLoader, className, true);
     }
 
     public PlasticClassTransformation createTransformation(String baseClassName, String newClassName)

Modified: tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java?rev=1098835&r1=1098834&r2=1098835&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java
(original)
+++ tapestry/tapestry5/trunk/plastic/src/main/java/org/apache/tapestry5/internal/plastic/PlasticInternalUtils.java
Mon May  2 22:55:04 2011
@@ -14,8 +14,10 @@
 
 package org.apache.tapestry5.internal.plastic;
 
+import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.lang.reflect.Array;
@@ -28,6 +30,7 @@ import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.tapestry5.internal.plastic.asm.ClassReader;
 import org.apache.tapestry5.internal.plastic.asm.Type;
 import org.apache.tapestry5.internal.plastic.asm.tree.ClassNode;
 import org.apache.tapestry5.internal.plastic.asm.tree.MethodNode;
@@ -349,4 +352,66 @@ public class PlasticInternalUtils
     {
         return left == right || (left != null && left.equals(right));
     }
+
+    static byte[] readBytestream(InputStream stream) throws IOException
+    {
+        byte[] buffer = new byte[5000];
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+        while (true)
+        {
+            int length = stream.read(buffer);
+
+            if (length < 0)
+                break;
+
+            bos.write(buffer, 0, length);
+        }
+
+        bos.close();
+
+        return bos.toByteArray();
+    }
+
+    public static byte[] readBytecodeForClass(ClassLoader loader, String className, boolean
mustExist)
+    {
+        String path = toClassPath(className);
+
+        InputStream stream = loader.getResourceAsStream(path);
+
+        if (stream == null)
+        {
+            if (mustExist)
+                throw new RuntimeException(String.format("Unable to locate class file for
'%s' in class loader %s.",
+                        className, loader));
+
+            return null;
+        }
+
+        try
+        {
+            return readBytestream(stream);
+        }
+        catch (IOException ex)
+        {
+            throw new RuntimeException(String.format("Failure reading bytecode for class
%s: %s", className,
+                    toMessage(ex)), ex);
+        }
+        finally
+        {
+            close(stream);
+        }
+    }
+
+    public static ClassNode convertBytecodeToClassNode(byte[] bytecode)
+    {
+        ClassReader cr = new ClassReader(bytecode);
+
+        ClassNode result = new ClassNode();
+
+        cr.accept(result, 0);
+
+        return result;
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java?rev=1098835&r1=1098834&r2=1098835&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java
Mon May  2 22:55:04 2011
@@ -175,7 +175,7 @@ public final class ComponentInstantiator
 
         classFactory = new ClassFactoryImpl(manager.getClassLoader(), logger);
 
-        proxyFactory = new PlasticProxyFactoryImpl(classFactory, manager.getClassLoader(),
logger);
+        proxyFactory = new PlasticProxyFactoryImpl(manager.getClassLoader(), logger);
 
         classToInstantiator.clear();
         classToModel.clear();

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=1098835&r1=1098834&r2=1098835&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
Mon May  2 22:55:04 2011
@@ -83,7 +83,7 @@ public final class RegistryBuilder
         Logger proxyFactoryLogger = loggerSource.getLogger(TapestryIOCModule.class.getName()
+ ".PlasticProxyFactory");
 
         classFactory = new ClassFactoryImpl(this.classLoader, classFactoryLogger);
-        proxyFactory = new PlasticProxyFactoryImpl(classFactory, this.classLoader, proxyFactoryLogger);
+        proxyFactory = new PlasticProxyFactoryImpl(this.classLoader, proxyFactoryLogger);
 
         add(TapestryIOCModule.class);
     }

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PlasticProxyFactoryImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PlasticProxyFactoryImpl.java?rev=1098835&r1=1098834&r2=1098835&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PlasticProxyFactoryImpl.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PlasticProxyFactoryImpl.java
Mon May  2 22:55:04 2011
@@ -15,12 +15,20 @@
 package org.apache.tapestry5.ioc.internal.services;
 
 import java.lang.reflect.Constructor;
+import java.lang.reflect.Member;
 import java.lang.reflect.Method;
+import java.util.List;
 
+import org.apache.tapestry5.internal.plastic.PlasticInternalUtils;
+import org.apache.tapestry5.internal.plastic.asm.Type;
+import org.apache.tapestry5.internal.plastic.asm.tree.AbstractInsnNode;
+import org.apache.tapestry5.internal.plastic.asm.tree.ClassNode;
+import org.apache.tapestry5.internal.plastic.asm.tree.InsnList;
+import org.apache.tapestry5.internal.plastic.asm.tree.LineNumberNode;
+import org.apache.tapestry5.internal.plastic.asm.tree.MethodNode;
 import org.apache.tapestry5.ioc.Location;
 import org.apache.tapestry5.ioc.ObjectCreator;
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
-import org.apache.tapestry5.ioc.services.ClassFactory;
 import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
 import org.apache.tapestry5.plastic.ClassInstantiator;
 import org.apache.tapestry5.plastic.InstructionBuilder;
@@ -36,15 +44,15 @@ import org.slf4j.Logger;
 
 public class PlasticProxyFactoryImpl implements PlasticProxyFactory
 {
-    private final ClassFactory classFactory;
-
     private final Logger logger;
 
     private final PlasticManager manager;
 
-    public PlasticProxyFactoryImpl(ClassFactory classFactory, ClassLoader parentClassLoader,
Logger logger)
+    private final ClassLoader loader;
+
+    public PlasticProxyFactoryImpl(ClassLoader parentClassLoader, Logger logger)
     {
-        this.classFactory = classFactory;
+        this.loader = parentClassLoader;
         this.logger = logger;
 
         manager = new PlasticManager(parentClassLoader);
@@ -107,14 +115,83 @@ public class PlasticProxyFactoryImpl imp
         return interfaceType.cast(instantiator.newInstance());
     }
 
+    private ClassNode readClassNode(Class clazz)
+    {
+        byte[] bytecode = PlasticInternalUtils.readBytecodeForClass(loader, clazz.getName(),
false);
+
+        return bytecode == null ? null : PlasticInternalUtils.convertBytecodeToClassNode(bytecode);
+    }
+
     public Location getMethodLocation(Method method)
     {
-        return classFactory.getMethodLocation(method);
+        return getMemberLocation(method, method.getName(), Type.getMethodDescriptor(method),
+                InternalUtils.asString(method));
     }
 
     public Location getConstructorLocation(Constructor constructor)
     {
-        return classFactory.getConstructorLocation(constructor);
+        StringBuilder builder = new StringBuilder(constructor.getDeclaringClass().getName()).append("(");
+        String sep = "";
+
+        for (Class parameterType : constructor.getParameterTypes())
+        {
+            builder.append(sep);
+            builder.append(parameterType.getSimpleName());
+
+            sep = ", ";
+        }
+
+        builder.append(")");
+
+        String constructorDescription = builder.toString();
+
+        Location location = getMemberLocation(constructor, "<init>", Type.getConstructorDescriptor(constructor),
+                constructorDescription);
+
+        if (location != null)
+            return location;
+
+        return new StringLocation(builder.toString(), 0);
+    }
+
+    public Location getMemberLocation(Member member, String methodName, String memberTypeDesc,
String textDescription)
+    {
+        ClassNode classNode = readClassNode(member.getDeclaringClass());
+
+        if (classNode == null)
+            return null;
+
+        if (classNode.sourceFile == null)
+            return null;
+
+        for (MethodNode mn : (List<MethodNode>) classNode.methods)
+        {
+            if (mn.name.equals(methodName) && mn.desc.equals(memberTypeDesc))
+            {
+                int lineNumber = findFirstLineNumber(mn.instructions);
+
+                if (lineNumber < 1)
+                    return null;
+
+                String description = String.format("%s (at %s:%d)", textDescription, classNode.sourceFile,
lineNumber);
+
+                return new StringLocation(description, lineNumber);
+            }
+        }
+
+        // Didn't find it. Odd.
+
+        return null;
+    }
+
+    private int findFirstLineNumber(InsnList instructions)
+    {
+        for (AbstractInsnNode node = instructions.getFirst(); node != null; node = node.getNext())
+        {
+            if (node instanceof LineNumberNode) { return ((LineNumberNode) node).line; }
+        }
+
+        return -1;
     }
 
     public void addPlasticClassListener(PlasticClassListener listener)

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java?rev=1098835&r1=1098834&r2=1098835&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java
Mon May  2 22:55:04 2011
@@ -52,7 +52,7 @@ public class DefaultModuleDefImplTest ex
     public void setup()
     {
         classFactory = new ClassFactoryImpl();
-        proxyFactory = new PlasticProxyFactoryImpl(classFactory, Thread.currentThread().getContextClassLoader(),
null);
+        proxyFactory = new PlasticProxyFactoryImpl(Thread.currentThread().getContextClassLoader(),
null);
     }
 
     @AfterClass



Mime
View raw message