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 2010/12/11 18:28:54 UTC

svn commit: r1044683 - /felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java

Author: clement
Date: Sat Dec 11 17:28:54 2010
New Revision: 1044683

URL: http://svn.apache.org/viewvc?rev=1044683&view=rev
Log:
Clean code
Move annotation parameters on constructors.

Modified:
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java

Modified: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java?rev=1044683&r1=1044682&r2=1044683&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java (original)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java Sat Dec 11 17:28:54 2010
@@ -193,7 +193,7 @@ public class MethodCreator extends Class
             Type[] args = Type.getArgumentTypes(desc);
 
             // TODO HERE ! => All constructor matches, no distinction between the different constructors.
-            generateConstructor(access, desc, signature, exceptions, md.getAnnotations());
+            generateConstructor(access, desc, signature, exceptions, md.getAnnotations(), md.getParameterAnnotations());
 
             if (args.length == 0) {
                 m_foundSuitableConstructor = true;
@@ -295,13 +295,27 @@ public class MethodCreator extends Class
         return cv.visitField(access, name, desc, signature, value);
     }
 
-    private void generateConstructor(int access, String descriptor, String signature, String[] exceptions, List annotations) {
-         GeneratorAdapter mv = new GeneratorAdapter(cv.visitMethod(access, "<init>", descriptor, signature, exceptions), access, "<init>", descriptor);
+    /**
+     * Modify the given constructor to be something like:
+     * <code>
+     * this(null, params...);
+     * return;
+     * </code>
+     * The actual constructor is modified to support the instance manager argument.
+     * @param access : access flag
+     * @param descriptor : the original constructor descriptor
+     * @param signature : method signature
+     * @param exceptions : declared exception
+     * @param annotations : the annotations to move to this constructor.
+     */
+    private void generateConstructor(int access, String descriptor, String signature, String[] exceptions, List annotations, Map paramAnnotations) {
+         GeneratorAdapter mv = new GeneratorAdapter(
+        		 cv.visitMethod(access, "<init>", descriptor, signature, exceptions),
+        		 access, "<init>", descriptor);
     	 // Compute the new signature
     	 String newDesc = descriptor.substring(1); // Remove the first (
          newDesc = "(Lorg/apache/felix/ipojo/InstanceManager;" + newDesc;
 
-
          mv.visitCode();
          mv.visitVarInsn(ALOAD, 0);
          mv.visitInsn(ACONST_NULL);
@@ -317,72 +331,24 @@ public class MethodCreator extends Class
              }
          }
 
+         // Move parameter annotations if any
+         if (paramAnnotations != null  && ! paramAnnotations.isEmpty()) {
+             Iterator ids = paramAnnotations.keySet().iterator();
+             while(ids.hasNext()) {
+                 Integer id = (Integer) ids.next();
+                 List ads = (List) paramAnnotations.get(id);
+                 for (int i = 0; i < ads.size(); i++) {
+                     AnnotationDescriptor ad = (AnnotationDescriptor) ads.get(i);
+                     ad.visitParameterAnnotation(id.intValue(), mv);
+                 }
+             }
+         }
+
          mv.visitMaxs(0, 0);
          mv.visitEnd();
     }
 
     /**
-     * Create a constructor to call the manipulated constructor.
-     * This constructor does not have any argument. It will call the manipulated
-     * constructor with a null instance manager.
-     * @param access : access flag
-     * @param signature : method signature
-     * @param exceptions : declared exception
-     * @param annotations : the annotations to move to this constructor.
-     */
-    private void generateEmptyConstructor(int access, String signature, String[] exceptions, List annotations) {
-        MethodVisitor mv = cv.visitMethod(access, "<init>", "()V", signature, exceptions);
-        mv.visitCode();
-        mv.visitVarInsn(ALOAD, 0);
-        mv.visitInsn(ACONST_NULL);
-        mv.visitMethodInsn(INVOKESPECIAL, m_owner, "<init>", "(Lorg/apache/felix/ipojo/InstanceManager;)V");
-        mv.visitInsn(RETURN);
-
-        // Move annotations
-        if (annotations != null) {
-            for (int i = 0; i < annotations.size(); i++) {
-                AnnotationDescriptor ad = (AnnotationDescriptor) annotations.get(i);
-                ad.visitAnnotation(mv);
-            }
-        }
-
-        mv.visitMaxs(0, 0);
-        mv.visitEnd();
-    }
-
-    /**
-     * Create a constructor to call the manipulated constructor.
-     * This constructor has one argument (the bundle context). It will call the manipulated
-     * constructor with a null instance manager.
-     * @param access : access flag
-     * @param signature : method signature
-     * @param exceptions : declared exception
-     * @param annotations : the annotations to move to this constructor.
-     */
-    private void generateBCConstructor(int access, String signature, String[] exceptions, List annotations) {
-        MethodVisitor mv = cv.visitMethod(access, "<init>", "(Lorg/osgi/framework/BundleContext;)V", signature, exceptions);
-        mv.visitCode();
-        Label l0 = new Label();
-        mv.visitLabel(l0);
-        mv.visitVarInsn(ALOAD, 0);
-        mv.visitInsn(ACONST_NULL);
-        mv.visitVarInsn(ALOAD, 1);
-        mv.visitMethodInsn(INVOKESPECIAL, m_owner, "<init>", "(Lorg/apache/felix/ipojo/InstanceManager;Lorg/osgi/framework/BundleContext;)V");
-        mv.visitInsn(RETURN);
-
-        // Move annotations
-        if (annotations != null) {
-            for (int i = 0; i < annotations.size(); i++) {
-                AnnotationDescriptor ad = (AnnotationDescriptor) annotations.get(i);
-                ad.visitAnnotation(mv);
-            }
-        }
-
-        mv.visitMaxs(0, 0);
-        mv.visitEnd();
-    }
-
-    /**
      * Generate the method header of a POJO method.
      * This method header encapsulate the POJO method call to
      * signal entry exit and error to the container.