You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2007/07/04 17:10:57 UTC

svn commit: r553232 - in /felix/trunk/ipojo: manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ plugin/src/main/resources/archetype-resources/

Author: clement
Date: Wed Jul  4 08:10:55 2007
New Revision: 553232

URL: http://svn.apache.org/viewvc?view=rev&rev=553232
Log:
Solve a bug about local variable inside constructor manipulation.
Update the iPOJO Archetype (Felix-311)

Modified:
    felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ConstructorCodeAdapter.java
    felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/PojoAdapter.java
    felix/trunk/ipojo/plugin/src/main/resources/archetype-resources/pom.xml

Modified: felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ConstructorCodeAdapter.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ConstructorCodeAdapter.java?view=diff&rev=553232&r1=553231&r2=553232
==============================================================================
--- felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ConstructorCodeAdapter.java (original)
+++ felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ConstructorCodeAdapter.java Wed Jul  4 08:10:55 2007
@@ -19,148 +19,159 @@
 package org.apache.felix.ipojo.manipulation;
 
 import org.objectweb.asm.Label;
+import org.objectweb.asm.MethodAdapter;
 import org.objectweb.asm.MethodVisitor;
 import org.objectweb.asm.Opcodes;
-import org.objectweb.asm.commons.GeneratorAdapter;
+
 
 /**
  * Constructor Adapter : add a component manager argument inside a constructor.
- * 
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
-public class ConstructorCodeAdapter extends GeneratorAdapter implements Opcodes {
+public class ConstructorCodeAdapter extends MethodAdapter implements Opcodes {
 
-    /**
-     * The owner class of the field. 
+    /** The owner class of the field.
      * m_owner : String
      */
     private String m_owner;
-
+    
     /**
-     * Is the super call already detected ? 
+     * Is the super call detected ?
      */
     private boolean m_superDetected;
 
-    /**
-     * ConstructorCodeAdapter constructor. 
-     * @param mv : method visitor.
-     * @param access : access level of the method.
-     * @param desc : descriptor of the constructor. 
-     * @param owner : onwer class
-     */
-    public ConstructorCodeAdapter(final MethodVisitor mv, int access,
-            String desc, final String owner) {
-        super(mv, access, "<init>", desc);
+    /** PropertyCodeAdapter constructor.
+     * A new FiledCodeAdapter should be create for each method visit.
+     * @param mv MethodVisitor
+     * @param owner Name of the class
+     */
+    public ConstructorCodeAdapter(final MethodVisitor mv, final String owner) {
+        super(mv);
         m_owner = owner;
-      //  m_superDetected = false;
+        m_superDetected = false;
     }
 
+
+    /** Visit Method for Field instance (GETFIELD).
+     * @see org.objectweb.asm.MethodVisitor#visitFieldInsn(int, String, String, String)
+     * @param opcode : visited operation code
+     * @param owner : owner of the field
+     * @param name : name of the field
+     * @param desc : decriptor of the field
+     */
+    public void visitFieldInsn(
+            final int opcode,
+            final String owner,
+            final String name,
+            final String desc) {
+        if (owner.equals(m_owner)) {
+            if (opcode == GETFIELD) {
+                String gDesc = "()" + desc;
+                visitMethodInsn(INVOKEVIRTUAL, owner, "_get" + name, gDesc);
+                return;
+            } else
+                if (opcode == PUTFIELD) {
+                    String sDesc = "(" + desc + ")V";
+                    visitMethodInsn(INVOKESPECIAL, owner, "_set" + name, sDesc);
+                    return;
+                }
+        }
+        super.visitFieldInsn(opcode, owner, name, desc);
+    }
+    
     /**
-     * Visit a method instruction.
-     * Inject the _setComponentManager invocation just of the super invocation.
+     * Vist a method invocation insruction.
+     * After the super constructor invocation, insert the _setComponentManager invocation.
      * @param opcode : opcode
-     * @param owner : method owner class
+     * @param owner : method owning class
      * @param name : method name
-     * @param desc : method description
-     * @see org.objectweb.asm.commons.AdviceAdapter#visitMethodInsn(int, java.lang.String, java.lang.String, java.lang.String)
+     * @param desc : method descriptor
+     * @see org.objectweb.asm.MethodAdapter#visitMethodInsn(int, java.lang.String, java.lang.String, java.lang.String)
      */
     public void visitMethodInsn(int opcode, String owner, String name, String desc) {
         // A method call is detected, check if it is the super call :
         if (!m_superDetected) {
-            m_superDetected = true;
+            m_superDetected = true; 
             // The first invocation is the super call
             // 1) Visit the super constructor :
             mv.visitVarInsn(ALOAD, 0);
-            mv.visitMethodInsn(opcode, owner, name, desc); // Super constructor
-                                                            // invocation
-
-            // 2) Load the object and the component manager argument
+            mv.visitMethodInsn(opcode, owner, name, desc); // Super constructor invocation
+            
+            // 2) Load the object and the component manager argument 
             mv.visitVarInsn(ALOAD, 0);
-            // mv.visitVarInsn(ALOAD,
-            // Type.getArgumentTypes(m_constructorDesc).length);
-            mv.visitVarInsn(ALOAD, 1); // CM is always the first argument
-            // 3) Initialize the field
-            mv.visitMethodInsn(INVOKESPECIAL, m_owner, "_setComponentManager",
-                    "(Lorg/apache/felix/ipojo/InstanceManager;)V");
-            // insertion finished
-        } else {
-            mv.visitMethodInsn(opcode, owner, name, desc);
+            //mv.visitVarInsn(ALOAD, Type.getArgumentTypes(m_constructorDesc).length);
+            mv.visitVarInsn(ALOAD, 1);  // CM is always the first argument
+            // 3) Initialize the field 
+            mv.visitMethodInsn(INVOKESPECIAL, m_owner, "_setComponentManager", "(Lorg/apache/felix/ipojo/InstanceManager;)V");
+            // insertion finished   
+        } else { 
+            mv.visitMethodInsn(opcode, owner, name, desc); 
         }
     }
-
+    
     /**
-     * Visit an instruction modifying a local variable.
-     * Increment the variable number if != 0.
-     * @param opcode : opcode of the instruction.
-     * @param var : variable number.
-     * @see org.objectweb.asm.commons.AdviceAdapter#visitVarInsn(int, int)
+     * Visit an instruction implying a variable.
+     * For all non-this variable, increment the variable index.
+     * @param opcode : opcode
+     * @param var : variable index  
+     * @see org.objectweb.asm.MethodAdapter#visitVarInsn(int, int)
      */
     public void visitVarInsn(int opcode, int var) {
-        if (var == 0) {
-            mv.visitVarInsn(opcode, var); // ALOAD 0 (THIS)
-        } else {
-            mv.visitVarInsn(opcode, var + 1); // All other variable count
-        } 
+        if (!m_superDetected) { 
+            return; // Do nothing the ALOAD 0 will be injected by visitMethodInsn
+        } else { 
+            if (var == 0) { 
+                mv.visitVarInsn(opcode, var); // ALOAD 0 (THIS)
+            } else { 
+                mv.visitVarInsn(opcode, var + 1); // All other variable count 
+            }     
+        }
     }
-
+    
     /**
      * Visit an increment instruction.
-     * Increment the variable number.
-     * @param var : incremented variable number.
-     * @param increment : increment.
-     * @see org.objectweb.asm.commons.LocalVariablesSorter#visitIincInsn(int, int)
+     * If incrementing a varialbe, increment the variable index.
+     * @param var : variable index
+     * @param increment : increment
+     * @see org.objectweb.asm.MethodAdapter#visitIincInsn(int, int)
      */
     public void visitIincInsn(int var, int increment) {
-        if (var != 0) {
-            mv.visitIincInsn(var + 1, increment);
-        } else {
-            mv.visitIincInsn(var, increment);
-        }
+        if (var != 0) { 
+            mv.visitIincInsn(var + 1, increment); 
+        } else { 
+            mv.visitIincInsn(var, increment); // Increment the current object ???
+        } 
     }
-
+    
     /**
-     * Visit local variable.
-     * @param name : name of the variable.
-     * @param desc : description of the variable.
-     * @param signature : signature of the variable.
-     * @param start : starting label.
-     * @param end : ending label.
-     * @param index : variable index.
-     * @see org.objectweb.asm.commons.LocalVariablesSorter#visitLocalVariable(java.lang.String, java.lang.String, java.lang.String, org.objectweb.asm.Label, org.objectweb.asm.Label, int)
+     * Visit a local variable.
+     * Add _manager and increment variable index.
+     * @param name : variable name
+     * @param desc : variable descriptor
+     * @param signature : variable signature
+     * @param start : beginning label 
+     * @param end : endind label
+     * @param index :variable index
+     * @see org.objectweb.asm.MethodAdapter#visitLocalVariable(java.lang.String, java.lang.String, java.lang.String, org.objectweb.asm.Label, org.objectweb.asm.Label, int)
      */
-    public void visitLocalVariable(String name, String desc, String signature,
-            Label start, Label end, int index) {
+    public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
         if (index == 0) {
             mv.visitLocalVariable(name, desc, signature, start, end, index);
             mv.visitLocalVariable("_manager", "Lorg/apache/felix/ipojo/InstanceManager;", null, start, end, 1);
-        } else {
-            mv.visitLocalVariable(name, desc, signature, start, end, index + 1);   
         }
+        mv.visitLocalVariable(name, desc, signature, start, end, index + 1);
     }
-
+    
     /**
-     * Manage a field operation.
-     * Replace GETFIELD and PUTFILED by getter and setter invocation.
-     * 
-     * @param opcode : visited operation code
-     * @param owner : owner of the field
-     * @param name : name of the field
-     * @param desc : decriptor of the field
-     * @see org.objectweb.asm.commons.AdviceAdapter#visitFieldInsn(int, java.lang.String, java.lang.String, java.lang.String)
+     * Visit max method.
+     * @param maxStack : stack size.
+     * @param maxLocals : local number.
+     * @see org.objectweb.asm.MethodAdapter#visitMaxs(int, int)
      */
-    public void visitFieldInsn(final int opcode, final String owner,
-            final String name, final String desc) {
-        if (owner.equals(m_owner)) {
-            if (opcode == GETFIELD) {
-                String gDesc = "()" + desc;
-                visitMethodInsn(INVOKEVIRTUAL, owner, "_get" + name, gDesc);
-                return;
-            } else if (opcode == PUTFIELD) {
-                String sDesc = "(" + desc + ")V";
-                visitMethodInsn(INVOKESPECIAL, owner, "_set" + name, sDesc);
-                return;
-            }
-        }
-        super.visitFieldInsn(opcode, owner, name, desc);
+    public void visitMaxs(int maxStack, int maxLocals) {
+        mv.visitMaxs(maxStack, maxLocals + 1);
     }
 }
+
+
+

Modified: felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/PojoAdapter.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/PojoAdapter.java?view=diff&rev=553232&r1=553231&r2=553232
==============================================================================
--- felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/PojoAdapter.java (original)
+++ felix/trunk/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/PojoAdapter.java Wed Jul  4 08:10:55 2007
@@ -186,7 +186,8 @@
             if (mv == null) {
                 return null;
             } else {
-                return new ConstructorCodeAdapter(mv, access, desc, m_owner);
+                //return new ConstructorCodeAdapter(mv, access, desc, m_owner);
+                return new ConstructorCodeAdapter(mv, m_owner);
             }
         } else {
             Type[] args = Type.getArgumentTypes(desc);

Modified: felix/trunk/ipojo/plugin/src/main/resources/archetype-resources/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/plugin/src/main/resources/archetype-resources/pom.xml?view=diff&rev=553232&r1=553231&r2=553232
==============================================================================
--- felix/trunk/ipojo/plugin/src/main/resources/archetype-resources/pom.xml (original)
+++ felix/trunk/ipojo/plugin/src/main/resources/archetype-resources/pom.xml Wed Jul  4 08:10:55 2007
@@ -1,6 +1,6 @@
 <project>
   <modelVersion>4.0.0</modelVersion>
-  <packaging>ipojo-bundle</packaging>
+  <packaging>bundle</packaging>
   <groupId>${groupId}</groupId>
   <artifactId>${artifactId}</artifactId>
   <version>${version}</version>