You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2017/11/08 15:19:00 UTC

svn commit: r1814588 - in /felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin: SCRDescriptorGenerator.java helper/ClassModifier.java

Author: cziegeler
Date: Wed Nov  8 15:19:00 2017
New Revision: 1814588

URL: http://svn.apache.org/viewvc?rev=1814588&view=rev
Log:
FELIX-5729 : NoSuchFieldError when @Reference referenceInterface type different than field type

Modified:
    felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java
    felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/helper/ClassModifier.java

Modified: felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java
URL: http://svn.apache.org/viewvc/felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java?rev=1814588&r1=1814587&r2=1814588&view=diff
==============================================================================
--- felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java (original)
+++ felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java Wed Nov  8 15:19:00 2017
@@ -273,8 +273,9 @@ public class SCRDescriptorGenerator {
                     }
                     ClassModifier.addMethods(container.getClassDescription().getDescribedClass().getName(),
                                     name,
-                                    ref.getField().getName(),
                                     type,
+                                    ref.getField().getName(),
+                                    ref.getField().getType().getName(),
                                     createBind,
                                     createUnbind,
                                     this.project.getClassLoader(),

Modified: felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/helper/ClassModifier.java
URL: http://svn.apache.org/viewvc/felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/helper/ClassModifier.java?rev=1814588&r1=1814587&r2=1814588&view=diff
==============================================================================
--- felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/helper/ClassModifier.java (original)
+++ felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/helper/ClassModifier.java Wed Nov  8 15:19:00 2017
@@ -41,8 +41,9 @@ public abstract class ClassModifier {
      * Add bind/unbind methods
      * @param className       The class name in which the methods are injected
      * @param referenceName   Name of the reference
+     * @param referenceType   Type of the reference
      * @param fieldName       Name of the field
-     * @param typeName        Name of the type
+     * @param fieldType       Type of the field
      * @param createBind      Name of the bind method or null
      * @param createUnbind    Name of the unbind method or null
      * @param outputDirectory Output directory where the class file is stored
@@ -50,8 +51,9 @@ public abstract class ClassModifier {
      */
     public static void addMethods(final String className,
                            final String referenceName,
+                           final String referenceType,
                            final String fieldName,
-                           final String typeName,
+                           final String fieldType,
                            final boolean createBind,
                            final boolean createUnbind,
                            final ClassLoader classLoader,
@@ -105,12 +107,12 @@ public abstract class ClassModifier {
             if ( createBind ) {
                 logger.debug("Adding bind " + className + " " + fieldName);
 
-                createMethod(writer, className, referenceName, fieldName, typeName, true);
+                createMethod(writer, className, referenceName, referenceType, fieldName, fieldType, true);
             }
             if ( createUnbind ) {
                 logger.debug("Adding unbind " + className + " " + fieldName);
 
-                createMethod(writer, className, referenceName, fieldName, typeName, false);
+                createMethod(writer, className, referenceName, referenceType, fieldName, fieldType, false);
             }
 
             final FileOutputStream fos = new FileOutputStream(fileName);
@@ -121,26 +123,27 @@ public abstract class ClassModifier {
                 fos.close();
             }
         } catch (final Exception e) {
-            throw new SCRDescriptorException("Unable to add methods to " + className, typeName, e);
+            throw new SCRDescriptorException("Unable to add methods to " + className, referenceType, e);
         }
     }
 
-    private static void createMethod(final ClassWriter cw, final String className, final String referenceName, final String fieldName, final String typeName, final boolean bind) {
-        final org.objectweb.asm.Type type = org.objectweb.asm.Type.getType("L" + typeName.replace('.', '/') + ";");
+    private static void createMethod(final ClassWriter cw, final String className, final String referenceName, final String referenceTypeName, final String fieldName, final String fieldTypeName, final boolean bind) {
+        final org.objectweb.asm.Type referenceType = org.objectweb.asm.Type.getType("L" + referenceTypeName.replace('.', '/') + ";");
+        final org.objectweb.asm.Type fieldType = org.objectweb.asm.Type.getType("L" + fieldTypeName.replace('.', '/') + ";");
         final String methodName = (bind ? "" : "un") + "bind" + referenceName.substring(0, 1).toUpperCase() + referenceName.substring(1);
-        final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + type.toString() + ")V", null, null);
+        final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + referenceType.toString() + ")V", null, null);
         mv.visitVarInsn(Opcodes.ALOAD, 0);
         if ( bind ) {
-            mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), 1);
-            mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
+            mv.visitVarInsn(referenceType.getOpcode(Opcodes.ILOAD), 1);
+            mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, fieldType.toString());
         } else {
-            mv.visitFieldInsn(Opcodes.GETFIELD, className.replace('.', '/'), fieldName, type.toString());
+            mv.visitFieldInsn(Opcodes.GETFIELD, className.replace('.', '/'), fieldName, fieldType.toString());
             mv.visitVarInsn(Opcodes.ALOAD, 1);
             final Label jmpLabel = new Label();
             mv.visitJumpInsn(Opcodes.IF_ACMPNE, jmpLabel);
             mv.visitVarInsn(Opcodes.ALOAD, 0);
             mv.visitInsn(Opcodes.ACONST_NULL);
-            mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
+            mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, fieldType.toString());
             mv.visitLabel(jmpLabel);
         }
         mv.visitInsn(Opcodes.RETURN);