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 2013/10/01 18:16:51 UTC
svn commit: r1528135 [1/2] - in /felix/trunk/ipojo:
manipulator/bnd-ipojo-plugin/src/test/java/org/apache/felix/ipojo/bnd/
manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/
mani...
Author: clement
Date: Tue Oct 1 16:16:51 2013
New Revision: 1528135
URL: http://svn.apache.org/r1528135
Log:
Fix:
* FELIX-4254 Specify the method id of methods from inner class
* FELIX-4255 Extend the inner class manipulation to allow method interception
* FELIX-4257 Allow the dependency handler to track the entry and exit of inner class methods
I think the leak is circumvented now, have to think about the test protocol to check it.
Added:
felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassChecker.java
- copied, changed from r1527700, felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java
felix/trunk/ipojo/manipulator/manipulator/src/test/java/test/inner/Example.java
Removed:
felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassManipulator.java
Modified:
felix/trunk/ipojo/manipulator/bnd-ipojo-plugin/src/test/java/org/apache/felix/ipojo/bnd/BndJarResourceStoreTestCase.java
felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/InnerClasses.java
felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/metadata.xml
felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestNestedClasses.java
felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java
felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassAdapter.java
felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java
felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/ManipulationEngine.java
felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/InstanceManager.java
felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ClassCheckerTestCase.java
felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/InnerClassAdapterTest.java
felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatedClassLoader.java
felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java
felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/RemanipulationTest.java
felix/trunk/ipojo/manipulator/manipulator/src/test/java/test/inner/ComponentWithInnerClasses.java
felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
Modified: felix/trunk/ipojo/manipulator/bnd-ipojo-plugin/src/test/java/org/apache/felix/ipojo/bnd/BndJarResourceStoreTestCase.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/bnd-ipojo-plugin/src/test/java/org/apache/felix/ipojo/bnd/BndJarResourceStoreTestCase.java?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/bnd-ipojo-plugin/src/test/java/org/apache/felix/ipojo/bnd/BndJarResourceStoreTestCase.java (original)
+++ felix/trunk/ipojo/manipulator/bnd-ipojo-plugin/src/test/java/org/apache/felix/ipojo/bnd/BndJarResourceStoreTestCase.java Tue Oct 1 16:16:51 2013
@@ -43,11 +43,7 @@ import aQute.service.reporter.Reporter;
import junit.framework.TestCase;
/**
- * Created with IntelliJ IDEA.
- * User: guillaume
- * Date: 04/01/13
- * Time: 15:33
- * To change this template use File | Settings | File Templates.
+ * Checks the Resource Store from the BND plugin.
*/
public class BndJarResourceStoreTestCase extends TestCase {
@Mock
@@ -161,12 +157,13 @@ public class BndJarResourceStoreTestCase
plugin.analyzeJar(analyzer);
- assertContains("component { $classname=\"org.apache.felix.ipojo.bnd.EmptyComponent\" manipulation { method { $name=\"$init\" }}}",
+ assertContains("component { $classname=\"org.apache.felix.ipojo.bnd.EmptyComponent\" manipulation { $classname=\"org.apache.felix.ipojo.bnd.EmptyComponent\" method { $name=\"$init\" }}}",
analyzer.getProperty("IPOJO-Components"));
verify(dot).putResource(eq(path), any(Resource.class));
}
private void assertContains(String expected, String actual) {
+ System.out.println("Actual: " + actual);
assertTrue(actual.contains(expected));
}
Modified: felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/InnerClasses.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/InnerClasses.java?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/InnerClasses.java (original)
+++ felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/InnerClasses.java Tue Oct 1 16:16:51 2013
@@ -45,6 +45,11 @@ public class InnerClasses implements Che
private static int staticint = 6;
+ /**
+ * A fake service.
+ */
+ private Runnable runnable;
+
public boolean check() {
return true;
}
Modified: felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/metadata.xml
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/metadata.xml?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/metadata.xml (original)
+++ felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/main/resources/metadata.xml Tue Oct 1 16:16:51 2013
@@ -39,6 +39,7 @@
<!-- Nested & Inner classes -->
<component name="inners" classname="org.apache.felix.ipojo.runtime.core.components.InnerClasses">
+ <requires field="runnable" optional="true"/>
<provides>
<property field="privateObject"/>
<property field="privateInt"/>
Modified: felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestNestedClasses.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestNestedClasses.java?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestNestedClasses.java (original)
+++ felix/trunk/ipojo/manipulator/manipulator-it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestNestedClasses.java Tue Oct 1 16:16:51 2013
@@ -124,7 +124,7 @@ public class TestNestedClasses extends B
public void testPublicInnerClass() {
Map data = (Map) service.getProps().get("publicInner");
assertNotNull("Check data existence", data);
-
+ System.out.println(data);
assertEquals("Check public object", "publicObject", data.get("publicObject"));
assertEquals("Check public int", new Integer(0), data.get("publicInt"));
assertEquals("Check protected object", "protectedObject", data.get("protectedObject"));
Modified: felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java (original)
+++ felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java Tue Oct 1 16:16:51 2013
@@ -98,7 +98,7 @@ public class ClassChecker extends EmptyV
public FieldVisitor visitField(int access, String name, String desc,
String signature, Object value) {
- if (access == ACC_PRIVATE && name.equals(MethodCreator.IM_FIELD)
+ if (access == 0 && name.equals(MethodCreator.IM_FIELD)
&& desc.equals("Lorg/apache/felix/ipojo/InstanceManager;")) {
m_isAlreadyManipulated = true;
} else if (name.startsWith("class$")) { // Does not add class$* field generated by 'x.class'
@@ -191,9 +191,9 @@ public class ClassChecker extends EmptyV
m_superClass = superName.replace('/', '.');
}
- for (int i = 0; i < interfaces.length; i++) {
- if (! interfaces[i].equals("org/apache/felix/ipojo/Pojo")) {
- m_itfs.add(interfaces[i].replace('/', '.'));
+ for (String anInterface : interfaces) {
+ if (!anInterface.equals("org/apache/felix/ipojo/Pojo")) {
+ m_itfs.add(anInterface.replace('/', '.'));
}
}
@@ -245,7 +245,7 @@ public class ClassChecker extends EmptyV
&& Type.getType("Lorg/apache/felix/ipojo/InstanceManager;").equals(types[0]);
}
- private boolean isGeneratedMethod(String name, String desc) {
+ public static boolean isGeneratedMethod(String name, String desc) {
return isGetterMethod(name, desc)
|| isSetterMethod(name, desc)
|| isSetInstanceManagerMethod(name)
@@ -253,7 +253,7 @@ public class ClassChecker extends EmptyV
|| isManipulatedMethod(name);
}
- private boolean isGetterMethod(String name, String desc) {
+ private static boolean isGetterMethod(String name, String desc) {
// TYPE __getXXX()
Type[] arguments = Type.getArgumentTypes(desc);
return (name.startsWith("__get")
@@ -261,7 +261,7 @@ public class ClassChecker extends EmptyV
&& !Type.VOID_TYPE.equals(Type.getReturnType(desc)));
}
- private boolean isSetterMethod(String name, String desc) {
+ private static boolean isSetterMethod(String name, String desc) {
// void __setXXX(TYPE)
Type[] arguments = Type.getArgumentTypes(desc);
return (name.startsWith("__set")
@@ -269,16 +269,16 @@ public class ClassChecker extends EmptyV
&& Type.VOID_TYPE.equals(Type.getReturnType(desc)));
}
- private boolean isSetInstanceManagerMethod(String name) {
+ private static boolean isSetInstanceManagerMethod(String name) {
return name.startsWith("_setInstanceManager");
}
- private boolean isGetComponentInstanceMethod(String name, String desc) {
+ private static boolean isGetComponentInstanceMethod(String name, String desc) {
return (name.startsWith("getComponentInstance")
&& Type.getType("Lorg/apache/felix/ipojo/ComponentInstance;").equals(Type.getReturnType(desc)));
}
- private boolean isManipulatedMethod(String name) {
+ private static boolean isManipulatedMethod(String name) {
return (name.startsWith(MethodCreator.PREFIX));
}
Modified: felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassAdapter.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassAdapter.java?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassAdapter.java (original)
+++ felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassAdapter.java Tue Oct 1 16:16:51 2013
@@ -20,7 +20,12 @@
package org.apache.felix.ipojo.manipulation;
import org.objectweb.asm.*;
+import org.objectweb.asm.commons.GeneratorAdapter;
+import org.objectweb.asm.tree.LocalVariableNode;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
import java.util.Set;
/**
@@ -33,8 +38,22 @@ import java.util.Set;
*/
public class InnerClassAdapter extends ClassAdapter implements Opcodes {
+ /**
+ * The manipulator having manipulated the outer class.
+ * We add method descriptions to this manipulator.
+ */
private final Manipulator m_manipulator;
+
+ /**
+ * The name of the inner class. This name is only define in the outer class.
+ */
private final String m_name;
+
+ /**
+ * The ismple name of the class.
+ */
+ private final String m_simpleName;
+
/**
* Implementation class name.
*/
@@ -47,21 +66,22 @@ public class InnerClassAdapter extends C
/**
* Creates the inner class adapter.
*
- * @param name the inner class name
+ * @param name the inner class name (internal name)
* @param arg0 parent class visitor
- * @param outerClass outer class (implementation class)
- * @param fields fields of the implementation class
+ * @param outerClassName outer class (implementation class)
* @param manipulator the manipulator having manipulated the outer class.
*/
- public InnerClassAdapter(String name, ClassVisitor arg0, String outerClass, Set<String> fields,
+ public InnerClassAdapter(String name, ClassVisitor arg0, String outerClassName,
Manipulator manipulator) {
super(arg0);
m_name = name;
- m_outer = outerClass;
- m_fields = fields;
+ m_simpleName = m_name.substring(m_name.indexOf("$") + 1);
+ m_outer = outerClassName;
m_manipulator = manipulator;
+ m_fields = manipulator.getFields().keySet();
}
+
/**
* Visits a method.
* This methods create a code visitor manipulating outer class field accesses.
@@ -75,10 +95,6 @@ public class InnerClassAdapter extends C
* @see org.objectweb.asm.ClassAdapter#visitMethod(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
*/
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
-
- final MethodDescriptor md = new MethodDescriptor(name, desc, (access & ACC_STATIC) == ACC_STATIC);
- m_manipulator.addMethodToInnerClass(m_name, md);
-
// Do nothing on static methods, should not happen in non-static inner classes.
if ((access & ACC_STATIC) == ACC_STATIC) {
return super.visitMethod(access, name, desc, signature, exceptions);
@@ -89,14 +105,240 @@ public class InnerClassAdapter extends C
return super.visitMethod(access, name, desc, signature, exceptions);
}
- MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
+
+ // Do not re-manipulate.
if (! m_manipulator.isAlreadyManipulated()) {
- // Do not re-manipulate.
- return new MethodCodeAdapter(mv, m_outer, access, name, desc, m_fields);
+
+ if (name.equals("<init>")) {
+ // We change the field access from the constructor, but we don't generate the wrapper.
+ MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
+ return new MethodCodeAdapter(mv, m_outer, access, name, desc, m_fields);
+ }
+
+ // For all non constructor methods
+
+ MethodDescriptor md = getMethodDescriptor(name, desc);
+ if (md == null) {
+ generateMethodWrapper(access, name, desc, signature, exceptions, null, null,
+ null);
+ } else {
+ generateMethodWrapper(access, name, desc, signature, exceptions,
+ md.getArgumentLocalVariables(),
+ md.getAnnotations(), md.getParameterAnnotations());
+ }
+
+ // The new name is the method name prefixed by the PREFIX.
+ MethodVisitor mv = super.visitMethod(access, MethodCreator.PREFIX + name, desc, signature,
+ exceptions);
+ return new MethodCodeAdapter(mv, m_outer, access, MethodCreator.PREFIX + name, desc, m_fields);
+ } else {
+ return super.visitMethod(access, name, desc, signature, exceptions);
+ }
+ }
+
+ private String getMethodFlagName(String name, String desc) {
+ return MethodCreator.METHOD_FLAG_PREFIX + getMethodId(name, desc);
+ }
+
+ private String getMethodId(String name, String desc) {
+ StringBuilder id = new StringBuilder(m_simpleName);
+ id.append("___"); // Separator
+ id.append(name);
+
+ Type[] args = Type.getArgumentTypes(desc);
+ for (Type type : args) {
+ String arg = type.getClassName();
+ if (arg.endsWith("[]")) {
+ // We have to replace all []
+ String acc = "";
+ while (arg.endsWith("[]")) {
+ arg = arg.substring(0, arg.length() - 2);
+ acc += "__";
+ }
+ id.append("$").append(arg.replace('.', '_')).append(acc);
+ } else {
+ id.append("$").append(arg.replace('.', '_'));
+ }
+ }
+ return id.toString();
+ }
+
+ /**
+ * 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.
+ *
+ * The instance manager and flag are accessed using method calls.
+ * @param access : access flag.
+ * @param name : method name.
+ * @param desc : method descriptor.
+ * @param signature : method signature.
+ * @param exceptions : declared exceptions.
+ * @param localVariables : the local variable nodes.
+ * @param annotations : the annotations to move to this method.
+ * @param paramAnnotations : the parameter annotations to move to this method.
+ */
+ private void generateMethodWrapper(int access, String name, String desc, String signature, String[] exceptions,
+ List<LocalVariableNode> localVariables, List<ClassChecker.AnnotationDescriptor> annotations,
+ Map<Integer, List<ClassChecker.AnnotationDescriptor>> paramAnnotations) {
+ GeneratorAdapter mv = new GeneratorAdapter(cv.visitMethod(access, name, desc, signature, exceptions), access, name, desc);
+
+ // If we have variables, we wraps the code within labels. The `lifetime` of the variables are bound to those
+ // two variables.
+ boolean hasArgumentLabels = localVariables != null && !localVariables.isEmpty();
+ Label start = null;
+ if (hasArgumentLabels) {
+ start = new Label();
+ mv.visitLabel(start);
+ }
+
+ mv.visitCode();
+
+ Type returnType = Type.getReturnType(desc);
+
+ // Compute result and exception stack location
+ int result = -1;
+ int exception = -1;
+
+ //int arguments = mv.newLocal(Type.getType((new Object[0]).getClass()));
+
+ if (returnType.getSort() != Type.VOID) {
+ // The method returns something
+ result = mv.newLocal(returnType);
+ exception = mv.newLocal(Type.getType(Throwable.class));
+ } else {
+ exception = mv.newLocal(Type.getType(Throwable.class));
+ }
+
+ Label l0 = new Label();
+ Label l1 = new Label();
+ Label l2 = new Label();
+
+ mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Throwable");
+
+ // Access the flag from the outer class
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitFieldInsn(GETFIELD, m_name, "this$0", "L" + m_outer + ";");
+ mv.visitFieldInsn(GETFIELD, m_outer, getMethodFlagName(name, desc), "Z");
+ mv.visitJumpInsn(IFNE, l0);
+
+ mv.visitVarInsn(ALOAD, 0);
+ mv.loadArgs();
+ mv.visitMethodInsn(INVOKESPECIAL, m_name, MethodCreator.PREFIX + name, desc);
+ mv.visitInsn(returnType.getOpcode(IRETURN));
+
+ // end of the non intercepted method invocation.
+
+ mv.visitLabel(l0);
+
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitFieldInsn(GETFIELD, m_name, "this$0", "L" + m_outer + ";");
+ mv.visitFieldInsn(GETFIELD, m_outer, MethodCreator.IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitLdcInsn(getMethodId(name, desc));
+ mv.loadArgArray();
+ mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", MethodCreator.ENTRY,
+ "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)V");
+
+ mv.visitVarInsn(ALOAD, 0);
+
+ // Do not allow argument modification : just reload arguments.
+ mv.loadArgs();
+ mv.visitMethodInsn(INVOKESPECIAL, m_name, MethodCreator.PREFIX + name, desc);
+
+ if (returnType.getSort() != Type.VOID) {
+ mv.visitVarInsn(returnType.getOpcode(ISTORE), result);
+ }
+
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitFieldInsn(GETFIELD, m_name, "this$0", "L" + m_outer + ";");
+ mv.visitFieldInsn(GETFIELD, m_outer, MethodCreator.IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitLdcInsn(getMethodId(name, desc));
+ if (returnType.getSort() != Type.VOID) {
+ mv.visitVarInsn(returnType.getOpcode(ILOAD), result);
+ mv.box(returnType);
} else {
- return mv;
+ mv.visitInsn(ACONST_NULL);
+ }
+ mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", MethodCreator.EXIT, "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V");
+
+ mv.visitLabel(l1);
+ Label l7 = new Label();
+ mv.visitJumpInsn(GOTO, l7);
+ mv.visitLabel(l2);
+
+ mv.visitVarInsn(ASTORE, exception);
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitFieldInsn(GETFIELD, m_name, "this$0", "L" + m_outer + ";");
+ mv.visitFieldInsn(GETFIELD, m_outer, MethodCreator.IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitLdcInsn(getMethodId(name, desc));
+ mv.visitVarInsn(ALOAD, exception);
+ mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", MethodCreator.ERROR, "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Throwable;)V");
+ mv.visitVarInsn(ALOAD, exception);
+ mv.visitInsn(ATHROW);
+
+ mv.visitLabel(l7);
+ if (returnType.getSort() != Type.VOID) {
+ mv.visitVarInsn(returnType.getOpcode(ILOAD), result);
}
+ mv.visitInsn(returnType.getOpcode(IRETURN));
+
+ // If we had arguments, we mark the end of the lifetime.
+ Label end = null;
+ if (hasArgumentLabels) {
+ end = new Label();
+ mv.visitLabel(end);
+ }
+
+ // Move annotations
+ if (annotations != null) {
+ for (int i = 0; i < annotations.size(); i++) {
+ ClassChecker.AnnotationDescriptor ad = annotations.get(i);
+ ad.visitAnnotation(mv);
+ }
+ }
+
+ // Move parameter annotations
+ if (paramAnnotations != null && ! paramAnnotations.isEmpty()) {
+ Iterator<Integer> ids = paramAnnotations.keySet().iterator();
+ while(ids.hasNext()) {
+ Integer id = ids.next();
+ List<ClassChecker.AnnotationDescriptor> ads = paramAnnotations.get(id);
+ for (int i = 0; i < ads.size(); i++) {
+ ClassChecker.AnnotationDescriptor ad = ads.get(i);
+ ad.visitParameterAnnotation(id, mv);
+ }
+ }
+ }
+
+ // Write the arguments name.
+ if (hasArgumentLabels) {
+ for (LocalVariableNode var : localVariables) {
+ mv.visitLocalVariable(var.name, var.desc, var.signature, start, end, var.index);
+ }
+ }
+
+ mv.visitMaxs(0, 0);
+ mv.visitEnd();
}
+ /**
+ * Gets the method descriptor for the specified name and descriptor.
+ * The method descriptor is looked inside the
+ * {@link MethodCreator#m_visitedMethods}
+ * @param name the name of the method
+ * @param desc the descriptor of the method
+ * @return the method descriptor or <code>null</code> if not found.
+ */
+ private MethodDescriptor getMethodDescriptor(String name, String desc) {
+ for (MethodDescriptor md : m_manipulator.getMethodsFromInnerClass(m_name)) {
+ if (md.getName().equals(name) && md.getDescriptor().equals(desc)) {
+ return md;
+ }
+ }
+ return null;
+ }
}
Copied: felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassChecker.java (from r1527700, felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java)
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassChecker.java?p2=felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassChecker.java&p1=felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java&r1=1527700&r2=1528135&rev=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ClassChecker.java (original)
+++ felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/InnerClassChecker.java Tue Oct 1 16:16:51 2013
@@ -19,773 +19,44 @@
package org.apache.felix.ipojo.manipulation;
-import java.util.*;
-
-import org.objectweb.asm.AnnotationVisitor;
-import org.objectweb.asm.ClassVisitor;
-import org.objectweb.asm.FieldVisitor;
-import org.objectweb.asm.Label;
-import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Opcodes;
-import org.objectweb.asm.Type;
+import org.objectweb.asm.*;
import org.objectweb.asm.commons.EmptyVisitor;
+import java.util.*;
+
/**
- * Checks that a POJO is already manipulated or not.
- * Moreover it allows to get manipulation data about this class.
+ * Analyze an inner class.
+ * This visit collects the methods from the inner class.
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
-public class ClassChecker extends EmptyVisitor implements ClassVisitor, Opcodes {
-
- /**
- * True if the class is already manipulated.
- */
- private boolean m_isAlreadyManipulated = false;
-
- /**
- * Interfaces implemented by the component.
- */
- private List<String> m_itfs = new ArrayList<String>();
-
- /**
- * Field map [field name, type] discovered in the component class.
- */
- private Map<String, String> m_fields = new TreeMap<String, String>();
-
- /**
- * Method List of method descriptor discovered in the component class.
- */
- private List<MethodDescriptor> m_methods = new ArrayList<MethodDescriptor>();
-
- /**
- * Super class if not java.lang.Object.
- */
- private String m_superClass;
-
- /**
- * Class name.
- */
- private String m_className;
-
- /**
- * List of visited inner class owned by the implementation class.
- */
- private Map<String, List<MethodDescriptor>> m_inners = new LinkedHashMap<String, List<MethodDescriptor>>();
-
- /**
- * <code>true</code> if the class supports annotations.
- * This enables the analysis of the code to find and moves annotations.
- */
- private boolean m_supportAnnotation = false;
-
- /**
- * Class Version.
- * Used to determine the frame format.
- */
- private int m_classVersion;
-
- /**
- * Check if the _cm field already exists.
- * Update the field list.
- * @param access : access of the field
- * @param name : name of the field
- * @param desc : description of the field
- * @param signature : signature of the field
- * @param value : value of the field (for static field only)
- * @return the field visitor
- * @see org.objectweb.asm.ClassVisitor#visitField(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object)
- */
- public FieldVisitor visitField(int access, String name, String desc,
- String signature, Object value) {
-
- if (access == ACC_PRIVATE && name.equals(MethodCreator.IM_FIELD)
- && desc.equals("Lorg/apache/felix/ipojo/InstanceManager;")) {
- m_isAlreadyManipulated = true;
- } else if (name.startsWith("class$")) { // Does not add class$* field generated by 'x.class'
- return null;
- } else if ((access & ACC_STATIC) == ACC_STATIC) {
- return null;
- }
-
- if (isManipulatedField(name)) {
- return null;
- }
-
- Type type = Type.getType(desc);
- if (type.getSort() == Type.ARRAY) {
- if (type.getInternalName().startsWith("L")) {
- String internalType = type.getInternalName().substring(1);
- String nameType = internalType.replace('/', '.');
- m_fields.put(name, nameType + "[]");
- } else {
- String nameType = type.getClassName().substring(0,
- type.getClassName().length() - 2);
- m_fields.put(name, nameType + "[]");
- }
- } else {
- m_fields.put(name, type.getClassName());
- }
-
- return null;
- }
-
- private boolean isManipulatedField(String name) {
- return ((MethodCreator.IM_FIELD.equals(name))
- || (name.startsWith(MethodCreator.FIELD_FLAG_PREFIX))
- || (name.startsWith(MethodCreator.METHOD_FLAG_PREFIX)));
- }
-
- /**
- * Add the inner class to the list of inner class to manipulate.
- * The method checks that the inner class is really owned by the implementation class.
- * @param name inner class qualified name
- * @param outerName outer class name (may be null for anonymous class)
- * @param innerName inner class simple (i.e. short) name
- * @param access inner class visibility
- * @see org.objectweb.asm.commons.EmptyVisitor#visitInnerClass(java.lang.String, java.lang.String, java.lang.String, int)
- */
- public void visitInnerClass(String name, String outerName, String innerName, int access) {
- if (m_className.equals(outerName) || outerName == null) { // Anonymous classes does not have an outer class.
- // Do not include inner static class
- if (! ((access & ACC_STATIC) == ACC_STATIC)) {
- m_inners.put(name, new ArrayList<MethodDescriptor>());
- }
- }
- }
-
-
- /**
- * Check if the class was already manipulated.
- * @return true if the class is already manipulated.
- */
- public boolean isAlreadyManipulated() {
- return m_isAlreadyManipulated;
- }
-
- /**
- * Gets the extracted class version
- * @return the class version.
- */
- public int getClassVersion() {
- return m_classVersion;
- }
-
- /**
- * Visit the class.
- * Update the implemented interface list.
- * @param version : version of the class
- * @param access : access of the class
- * @param name : name of the class
- * @param signature : signature of the class
- * @param superName : super class of the class
- * @param interfaces : implemented interfaces.
- * @see org.objectweb.asm.ClassVisitor#visit(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
- */
- public void visit(int version, int access, String name, String signature,
- String superName, String[] interfaces) {
-
- m_classVersion = version;
- m_supportAnnotation = version > V1_4 && version < V1_1;
-
- if (! superName.equals("java/lang/Object")) {
- m_superClass = superName.replace('/', '.');
- }
-
- for (int i = 0; i < interfaces.length; i++) {
- if (! interfaces[i].equals("org/apache/felix/ipojo/Pojo")) {
- m_itfs.add(interfaces[i].replace('/', '.'));
- }
- }
-
- m_className = name;
- }
-
- /**
- * Visit a method.
- * Update the method list (except if it init or clinit.
- * @param access - the method's access flags (see Opcodes). This parameter also indicates if the method is synthetic and/or deprecated.
- * @param name - the method's name.
- * @param desc - the method's descriptor (see Type).
- * @param signature - the method's signature. May be null if the method parameters, return type and exceptions do not use generic types.
- * @param exceptions - the internal names of the method's exception classes (see getInternalName). May be null.
- * @return nothing.
- * @see org.objectweb.asm.ClassVisitor#visitMethod(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
- */
- public MethodVisitor visitMethod(int access, String name, String desc,
- String signature, String[] exceptions) {
- if (!name.equals("<clinit>")) {
-
- if (name.equals("<init>")) {
- if (!isGeneratedConstructor(name, desc)) {
- final MethodDescriptor md = new MethodDescriptor("$init", desc, (access & ACC_STATIC) == ACC_STATIC);
- m_methods.add(md);
- return new MethodInfoCollector(md);
- }
- } else {
- // no constructors.
- if (!isGeneratedMethod(name, desc)) {
- final MethodDescriptor md = new MethodDescriptor(name, desc, (access & ACC_STATIC) == ACC_STATIC);
- m_methods.add(md);
- return new MethodInfoCollector(md);
- }
- }
-
- }
-
- return null;
- }
-
- public static boolean isGeneratedConstructor(String name, String desc) {
- return ("<init>".equals(name) && isFirstArgumentInstanceManager(desc));
- }
-
- public static boolean isFirstArgumentInstanceManager(String desc) {
- Type[] types = Type.getArgumentTypes(desc);
- return types != null && (types.length >= 1)
- && Type.getType("Lorg/apache/felix/ipojo/InstanceManager;").equals(types[0]);
- }
-
- private boolean isGeneratedMethod(String name, String desc) {
- return isGetterMethod(name, desc)
- || isSetterMethod(name, desc)
- || isSetInstanceManagerMethod(name)
- || isGetComponentInstanceMethod(name, desc)
- || isManipulatedMethod(name);
- }
-
- private boolean isGetterMethod(String name, String desc) {
- // TYPE __getXXX()
- Type[] arguments = Type.getArgumentTypes(desc);
- return (name.startsWith("__get")
- && (arguments.length == 0)
- && !Type.VOID_TYPE.equals(Type.getReturnType(desc)));
- }
-
- private boolean isSetterMethod(String name, String desc) {
- // void __setXXX(TYPE)
- Type[] arguments = Type.getArgumentTypes(desc);
- return (name.startsWith("__set")
- && (arguments.length == 1)
- && Type.VOID_TYPE.equals(Type.getReturnType(desc)));
- }
-
- private boolean isSetInstanceManagerMethod(String name) {
- return name.startsWith("_setInstanceManager");
- }
-
- private boolean isGetComponentInstanceMethod(String name, String desc) {
- return (name.startsWith("getComponentInstance")
- && Type.getType("Lorg/apache/felix/ipojo/ComponentInstance;").equals(Type.getReturnType(desc)));
- }
-
- private boolean isManipulatedMethod(String name) {
- return (name.startsWith(MethodCreator.PREFIX));
- }
-
- /**
- * Get collected interfaces.
- * @return the interfaces implemented by the component class.
- */
- public List<String> getInterfaces() {
- return m_itfs;
- }
-
- /**
- * Get collected fields.
- * @return the field map [field_name, type].
- */
- public Map<String, String> getFields() {
- return m_fields;
- }
-
- /**
- * Get collected methods.
- * @return the method list of [method, signature].
- */
- public List<MethodDescriptor> getMethods() {
- return m_methods;
- }
-
- public String getSuperClass() {
- return m_superClass;
- }
-
- public Collection<String> getInnerClasses() {
- return m_inners.keySet();
- }
+public class InnerClassChecker extends EmptyVisitor implements ClassVisitor, Opcodes {
- public Map<String, List<MethodDescriptor>> getInnerClassesAndMethods() {
- return m_inners;
- }
+ private final String m_name;
+ private final Manipulator m_manipulator;
- public String getClassName() {
- return m_className;
+ public InnerClassChecker(String name, Manipulator manipulator) {
+ m_name = name;
+ m_manipulator = manipulator;
}
- /**
- * This class collects annotations in a method.
- * This class creates an {@link AnnotationDescriptor}
- * if an annotation is found during the visit.
- * It also collects local variables definition.
- * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
- */
- private final class MethodInfoCollector extends EmptyVisitor {
- /**
- * The method descriptor of the visited method.
- */
- private MethodDescriptor m_method;
-
- /**
- * Creates an annotation collector.
- * @param md the method descriptor of the visited method.
- */
- private MethodInfoCollector(MethodDescriptor md) {
- m_method = md;
- }
+ @Override
+ public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
+ // Do not collect static and native method.
- /**
- * Visits an annotation.
- * This class checks the visibility. If the annotation is visible,
- * creates the {@link AnnotationDescriptor} corresponding to this annotation
- * to visit this annotation. This {@link AnnotationDescriptor} is added to
- * the {@link MethodDescriptor} of the visited method.
- * @param name the name of the annotation
- * @param visible is the annotation visible at runtime
- * @return the {@link AnnotationDescriptor} to visit this annotation or
- * <code>null</code> if the annotation is not visible.
- * @see org.objectweb.asm.commons.EmptyVisitor#visitAnnotation(java.lang.String, boolean)
- */
- public AnnotationVisitor visitAnnotation(String name, boolean visible) {
- if (visible) {
- AnnotationDescriptor ann = new AnnotationDescriptor(name, visible);
- m_method.addAnnotation(ann);
- return ann;
- }
+ if ((access & ACC_STATIC) == ACC_STATIC) {
return null;
}
-
- public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
- m_method.addLocalVariable(name, desc, signature, index);
- }
-
- public void visitEnd() {
- m_method.end();
- }
- public AnnotationVisitor visitParameterAnnotation(int id,
- String name, boolean visible) {
- if (visible) {
- AnnotationDescriptor ann = new AnnotationDescriptor(name, visible);
- m_method.addParameterAnnotation(id, ann);
- return ann;
- }
-
- /*
- * It is harmless to keep injected parameter annotations on original constructor
- * for correct property resolution in case of re-manipulation
- */
- if(m_method.getName().equals("$init"))
- {
- AnnotationDescriptor ann = new AnnotationDescriptor(name, visible);
- m_method.addParameterAnnotation(id, ann);
- return ann;
- }
-
+ if ((access & ACC_NATIVE) == ACC_NATIVE) {
return null;
}
-
- }
-
- /**
- * Describes a method or constructor annotation.
- * This allows creating a copy of the annotations found in the original class
- * to move them on inserted method. This class implements an
- * {@link AnnotationVisitor} in order to create the copy.
- * This class contains a <code>visit</code> method re-injecting the
- * annotation in the generated method.
- * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
- */
- public class AnnotationDescriptor implements AnnotationVisitor {
- /**
- * The name of the annotation.
- */
- private String m_name;
- /**
- * Is the annotation visible at runtime?
- */
- private boolean m_visible;
- /**
- * The description of the annotation.
- * This attribute is set only for nested annotations.
- */
- private String m_desc;
- /**
- * The list of 'simple' attributes.
- */
- private List<SimpleAttribute> m_simples = new ArrayList<SimpleAttribute>(0);
- /**
- * The list of attribute containing an
- * enumeration value.
- */
- private List<EnumAttribute> m_enums = new ArrayList<EnumAttribute>(0);
- /**
- * The list of attribute which are
- * annotations.
- */
- private List<AnnotationDescriptor> m_nested = new ArrayList<AnnotationDescriptor>(0);
- /**
- * The list of attribute which are
- * arrays.
- */
- private List<ArrayAttribute> m_arrays = new ArrayList<ArrayAttribute>(0);
-
-
- /**
- * Creates an annotation descriptor.
- * This constructor is used for 'root' annotations.
- * @param name the name of the annotation
- * @param visible the visibility of the annotation at runtime
- */
- public AnnotationDescriptor(String name, boolean visible) {
- m_name = name;
- m_visible = visible;
- }
-
- /**
- * Creates an annotation descriptor.
- * This constructor is used for nested annotations.
- * @param name the name of the annotation
- * @param desc the descriptor of the annotation
- */
- public AnnotationDescriptor(String name, String desc) {
- m_name = name;
- m_visible = true;
- m_desc = desc;
- }
-
-
- /**
- * Visits a simple attribute.
- * @param arg0 the attribute name
- * @param arg1 the attribute value
- * @see org.objectweb.asm.AnnotationVisitor#visit(java.lang.String, java.lang.Object)
- */
- public void visit(String arg0, Object arg1) {
- m_simples.add(new SimpleAttribute(arg0, arg1));
- }
-
-
- /**
- * Visits a nested annotation.
- * @param arg0 the attribute name
- * @param arg1 the annotation descriptor
- * @return the annotation visitor parsing the nested annotation
- * @see org.objectweb.asm.AnnotationVisitor#visitAnnotation(java.lang.String, java.lang.String)
- */
- public AnnotationVisitor visitAnnotation(String arg0, String arg1) {
- AnnotationDescriptor ad = new AnnotationDescriptor(arg0, arg1);
- m_nested.add(ad);
- return ad;
- }
-
-
- /**
- * Visits an array attribute.
- * @param arg0 the name of the attribute
- * @return the annotation visitor parsing the content of the array,
- * uses a specific {@link ArrayAttribute} to parse this array
- * @see org.objectweb.asm.AnnotationVisitor#visitArray(java.lang.String)
- */
- public AnnotationVisitor visitArray(String arg0) {
- ArrayAttribute aa = new ArrayAttribute(arg0);
- m_arrays.add(aa);
- return aa;
- }
-
-
- /**
- * End of the visit.
- * @see org.objectweb.asm.AnnotationVisitor#visitEnd()
- */
- public void visitEnd() { }
-
-
- /**
- * Visits an enumeration attribute.
- * @param arg0 the attribute name
- * @param arg1 the enumeration descriptor
- * @param arg2 the attribute value
- * @see org.objectweb.asm.AnnotationVisitor#visitEnum(java.lang.String, java.lang.String, java.lang.String)
- */
- public void visitEnum(String arg0, String arg1, String arg2) {
- m_enums.add(new EnumAttribute(arg0, arg1, arg2));
- }
-
- /**
- * Methods allowing to recreate the visited (stored) annotation
- * into the destination method.
- * This method recreate the annotations itself and any other
- * attributes.
- * @param mv the method visitor visiting the destination method.
- */
- public void visitAnnotation(MethodVisitor mv) {
- AnnotationVisitor av = mv.visitAnnotation(m_name, m_visible);
- for (int i = 0; i < m_simples.size(); i++) {
- m_simples.get(i).visit(av);
- }
- for (int i = 0; i < m_enums.size(); i++) {
- m_enums.get(i).visit(av);
- }
- for (int i = 0; i < m_nested.size(); i++) {
- m_nested.get(i).visit(av);
- }
- for (int i = 0; i < m_arrays.size(); i++) {
- m_arrays.get(i).visit(av);
- }
- av.visitEnd();
- }
-
- /**
- * Methods allowing to recreate the visited (stored) parameter annotations
- * into the destination method.
- * This method recreate the annotations itself and any other
- * attributes.
- * @param id the paramter id
- * @param mv the method visitor visiting the destination method.
- */
- public void visitParameterAnnotation(int id, MethodVisitor mv) {
- AnnotationVisitor av = mv.visitParameterAnnotation(id, m_name, m_visible);
- for (int i = 0; i < m_simples.size(); i++) {
- m_simples.get(i).visit(av);
- }
- for (int i = 0; i < m_enums.size(); i++) {
- m_enums.get(i).visit(av);
- }
- for (int i = 0; i < m_nested.size(); i++) {
- m_nested.get(i).visit(av);
- }
- for (int i = 0; i < m_arrays.size(); i++) {
- m_arrays.get(i).visit(av);
- }
- av.visitEnd();
- }
-
- /**
- * Method allowing to recreate the visited (stored) annotation
- * into the destination annotation. This method is used only
- * for nested annotation.
- * @param mv the annotation visitor to populate with the stored
- * annotation
- */
- public void visit(AnnotationVisitor mv) {
- AnnotationVisitor av = mv.visitAnnotation(m_name, m_desc);
- for (int i = 0; i < m_simples.size(); i++) {
- m_simples.get(i).visit(av);
- }
- for (int i = 0; i < m_enums.size(); i++) {
- m_enums.get(i).visit(av);
- }
- for (int i = 0; i < m_nested.size(); i++) {
- m_nested.get(i).visit(av);
- }
- for (int i = 0; i < m_arrays.size(); i++) {
- m_arrays.get(i).visit(av);
- }
- av.visitEnd();
- }
-
-
- }
-
- /**
- * Describes an array attribute.
- * This class is able to visit an annotation array attribute, and to
- * recreate this array on another annotation.
- * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
- */
- public class ArrayAttribute implements AnnotationVisitor {
- /**
- * The name of the attribute.
- */
- private String m_name;
- /**
- * The content of the parsed array.
- */
- private List<Object> m_content = new ArrayList<Object>();
-
- /**
- * Creates an array attribute.
- * @param name the name of the attribute.
- */
- public ArrayAttribute(String name) {
- m_name = name;
- }
-
- /**
- * Visits the content of the array. This method is called for
- * simple values.
- * @param arg0 <code>null</code>
- * @param arg1 the value
- * @see org.objectweb.asm.AnnotationVisitor#visit(java.lang.String, java.lang.Object)
- */
- public void visit(String arg0, Object arg1) {
- m_content.add(arg1);
- }
-
- /**
- * Visits the content of the array. This method is called for
- * nested annotations (annotations contained in the array).
- * @param arg0 <code>null</code>
- * @param arg1 the annotation descriptor
- * @return an {@link AnnotationDescriptor} which creates a copy of
- * the contained annotation.
- * @see org.objectweb.asm.AnnotationVisitor#visitAnnotation(String, String)
- */
- public AnnotationVisitor visitAnnotation(String arg0, String arg1) {
- AnnotationDescriptor ad = new AnnotationDescriptor(null, arg1);
- m_content.add(ad);
- return ad;
- }
-
- /**
- * Visits the content of the array. This method is called for
- * nested arrays (arrays contained in the array).
- * @param arg0 <code>null</code>
- * @return an {@link AnnotationVisitor} which creates a copy of
- * the contained array.
- * @see org.objectweb.asm.AnnotationVisitor#visitArray(String)
- */
- public AnnotationVisitor visitArray(String arg0) {
- ArrayAttribute aa = new ArrayAttribute(null);
- m_content.add(aa);
- return aa;
- }
-
- /**
- * End of the array attribute visit.
- * @see org.objectweb.asm.AnnotationVisitor#visitEnd()
- */
- public void visitEnd() { }
-
- /**
- * Visits the content of the array. This method is called for
- * enumeration values.
- * @param arg0 <code>null</code>
- * @param arg1 the enumeration descriptor
- * @param arg2 the value
- * @see org.objectweb.asm.AnnotationVisitor#visitEnum(String, String, String)
- */
- public void visitEnum(String arg0, String arg1, String arg2) {
- EnumAttribute ea = new EnumAttribute(null, arg1, arg2);
- m_content.add(ea);
- }
-
- /**
- * Recreates the visited array attribute. This method
- * handle the generation of the object embedded in the
- * array.
- * @param av the annotation visitor on which the array attribute
- * needs to be injected.
- */
- public void visit(AnnotationVisitor av) {
- AnnotationVisitor content = av.visitArray(m_name);
- for (int i = 0; i < m_content.size(); i++) {
- Object component = m_content.get(i);
- if (component instanceof AnnotationDescriptor) {
- ((AnnotationDescriptor) component).visit(content);
- } else if (component instanceof EnumAttribute) {
- ((EnumAttribute) component).visit(content);
- } else if (component instanceof ArrayAttribute) {
- ((ArrayAttribute) component).visit(content);
- } else { // Simple
- content.visit(null, component);
- }
- }
- content.visitEnd();
- }
-
- }
-
- /**
- * Describes a simple attribute.
- * This class is able to visit an annotation simple attribute, and to
- * recreate this attribute on another annotation.
- * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
- */
- public static final class SimpleAttribute {
- /**
- * The name of the attribute.
- */
- private String m_name;
- /**
- * The value of the attribute.
- */
- private Object m_value;
-
- /**
- * Creates a simple attribute.
- * @param name the name of the attribute
- * @param object the value of the attribute
- */
- private SimpleAttribute(String name, Object object) {
- m_name = name;
- m_value = object;
- }
-
- /**
- * Recreates the attribute on the given annotation.
- * @param visitor the visitor on which the attribute needs
- * to be injected.
- */
- public void visit(AnnotationVisitor visitor) {
- visitor.visit(m_name, m_value);
- }
- }
-
- /**
- * Describes an attribute. The value of this attribute is an enumerated
- * value.
- * This class is able to visit an annotation enumeration attribute, and to
- * recreate this attribute on another annotation.
- * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
- */
- public static final class EnumAttribute {
- /**
- * The name of the attribute.
- */
- private String m_name;
- /**
- * The descriptor of the enumeration.
- */
- private String m_desc;
- /**
- * The value of the attribute.
- */
- private String m_value;
-
- /**
- * Creates a enumeration attribute.
- * @param name the name of the attribute.
- * @param desc the descriptor of the {@link Enum}
- * @param value the enumerated value
- */
- private EnumAttribute(String name, String desc, String value) {
- m_name = name;
- m_value = value;
- m_desc = desc;
- }
-
- /**
- * Recreates the attribute on the given annotation.
- * @param visitor the visitor on which the attribute needs
- * to be injected.
- */
- public void visit(AnnotationVisitor visitor) {
- visitor.visitEnum(m_name, m_desc, m_value);
+ // Don't add generated methods, and constructors
+ if (!ClassChecker.isGeneratedMethod(name, desc) && ! name.endsWith("<init>")) {
+ final MethodDescriptor md = new MethodDescriptor(name, desc, (access & ACC_STATIC) == ACC_STATIC);
+ m_manipulator.addMethodToInnerClass(m_name, md);
}
+ return null;
}
-
-
}
Modified: felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java (original)
+++ felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java Tue Oct 1 16:16:51 2013
@@ -77,19 +77,18 @@ public class Manipulator {
private String m_className;
/**
- * Manipulate the given byte array.
- * @param origin : original class.
- * @return the manipulated class, if the class is already manipulated, the original class.
- * @throws IOException : if an error occurs during the manipulation.
+ * Checks the given bytecode, determines if the class was already manipulated, and collect the metadata about the
+ * class.
+ * @param origin the bytecode
*/
- public byte[] manipulate(byte[] origin) throws IOException {
- InputStream is1 = new ByteArrayInputStream(origin);
+ public void prepare(byte[] origin) throws IOException {
+ InputStream is = new ByteArrayInputStream(origin);
// First check if the class is already manipulated :
- ClassReader ckReader = new ClassReader(is1);
+ ClassReader ckReader = new ClassReader(is);
ClassChecker ck = new ClassChecker();
ckReader.accept(ck, ClassReader.SKIP_FRAMES);
- is1.close();
+ is.close();
m_fields = ck.getFields(); // Get visited fields (contains only POJO fields)
m_className = ck.getClassName();
@@ -101,36 +100,37 @@ public class Manipulator {
// Get the methods list
m_methods = ck.getMethods();
+ // Methods are not yet collected, but the structure is ready.
m_inners = ck.getInnerClassesAndMethods();
m_version = ck.getClassVersion();
- ClassWriter finalWriter = null;
-
m_alreadyManipulated = ck.isAlreadyManipulated();
+ }
+ /**
+ * Manipulate the given byte array.
+ * @param origin : original class.
+ * @return the manipulated class, if the class is already manipulated, the original class.
+ * @throws IOException : if an error occurs during the manipulation.
+ */
+ public byte[] manipulate(byte[] origin) throws IOException {
+ ClassWriter finalWriter = null;
if (!m_alreadyManipulated) {
- // Manipulation ->
- // Add the _setComponentManager method
- // Instrument all fields
InputStream is2 = new ByteArrayInputStream(origin);
ClassReader cr0 = new ClassReader(is2);
ClassWriter cw0 = new ClassWriter(ClassWriter.COMPUTE_MAXS);
//CheckClassAdapter ch = new CheckClassAdapter(cw0);
- MethodCreator process = new MethodCreator(cw0, m_fields, m_methods);
- if (ck.getClassVersion() >= Opcodes.V1_6) {
+ MethodCreator process = new MethodCreator(cw0, this);
+ if (m_version >= Opcodes.V1_6) {
cr0.accept(process, ClassReader.EXPAND_FRAMES);
} else {
cr0.accept(process, 0);
}
is2.close();
- finalWriter = cw0;
- }
- // The file is in the bundle
- if (m_alreadyManipulated) {
- return origin;
+ return cw0.toByteArray();
} else {
- return finalWriter.toByteArray();
+ return origin;
}
}
@@ -210,6 +210,10 @@ public class Manipulator {
return m_fields;
}
+ public List<MethodDescriptor> getMethods() {
+ return m_methods;
+ }
+
public Collection<String> getInnerClasses() {
return new ArrayList<String>(m_inners.keySet());
}
@@ -231,4 +235,61 @@ public class Manipulator {
}
list.add(md);
}
+
+ /**
+ * Analyzes the given inner class.
+ * @param inner the inner class name
+ * @param bytecode the bytecode of the inner class
+ */
+ public void prepareInnerClass(String inner, byte[] bytecode) throws IOException {
+ InputStream is = new ByteArrayInputStream(bytecode);
+ ClassReader ckReader = new ClassReader(is);
+ InnerClassChecker ck = new InnerClassChecker(inner, this);
+ ckReader.accept(ck, ClassReader.SKIP_FRAMES);
+ is.close();
+ // The metadata are collected during the visit.
+ }
+
+ /**
+ * Manipulates the inner class. If the outer class was already manipulated does not re-manipulate the inner class.
+ * We consider that the manipulation cycle of the outer and inner classes are the same.
+ * @param inner the inner class name
+ * @param bytecode input (i.e. original) class
+ * @return the manipulated class
+ * @throws IOException the class cannot be read correctly
+ */
+ public byte[] manipulateInnerClass(String inner, byte[] bytecode) throws IOException {
+ if (!m_alreadyManipulated) {
+ InputStream is1 = new ByteArrayInputStream(bytecode);
+
+ ClassReader cr = new ClassReader(is1);
+ ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
+ InnerClassAdapter adapter = new InnerClassAdapter(inner, cw, m_className, this);
+ if (m_version >= Opcodes.V1_6) {
+ cr.accept(adapter, ClassReader.EXPAND_FRAMES);
+ } else {
+ cr.accept(adapter, 0);
+ }
+ is1.close();
+
+ return cw.toByteArray();
+ } else {
+ // Return the unchanged inner class
+ return bytecode;
+ }
+ }
+
+ public List<MethodDescriptor> getMethodsFromInnerClass(String innerClassInternalName) {
+ return m_inners.get(innerClassInternalName);
+ }
+
+ public Map<String, List<MethodDescriptor>> getInnerClassesAndMethods() {
+ // Transform the map to use the simple name of the inner classes.
+ Map<String, List<MethodDescriptor>> map = new HashMap<String, List<MethodDescriptor>>();
+ for (Map.Entry<String, List<MethodDescriptor>> entry : m_inners.entrySet()) {
+ String name = extractInnerClassName(toQualifiedName(entry.getKey()));
+ map.put(name, entry.getValue());
+ }
+ return map;
+ }
}
Modified: felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java (original)
+++ felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java Tue Oct 1 16:16:51 2013
@@ -31,6 +31,7 @@ import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.GeneratorAdapter;
import org.objectweb.asm.tree.LocalVariableNode;
+import org.objectweb.asm.tree.MethodNode;
/**
* iPOJO Class Adapter.
@@ -67,17 +68,17 @@ public class MethodCreator extends Class
/**
* onEntry method name.
*/
- private static final String ENTRY = "onEntry";
+ public static final String ENTRY = "onEntry";
/**
* onExit method name.
*/
- private static final String EXIT = "onExit";
+ public static final String EXIT = "onExit";
/**
* on Error method name.
*/
- private static final String ERROR = "onError";
+ public static final String ERROR = "onError";
/**
* onGet method name.
@@ -90,6 +91,11 @@ public class MethodCreator extends Class
private static final String SET = "onSet";
/**
+ * The manipulator. It has already collected all the metadata about the class.
+ */
+ private final Manipulator m_manipulator;
+
+ /**
* Name of the current manipulated class.
*/
private String m_owner;
@@ -134,13 +140,13 @@ public class MethodCreator extends Class
/**
* Constructor.
* @param arg0 : class visitor.
- * @param fields : fields map detected during the previous class analysis.
- * @param methods : the list of the detected method during the previous class analysis.
+ * @param manipulator : the manipulator having analyzed the class.
*/
- public MethodCreator(ClassVisitor arg0, Map<String, String> fields, List<MethodDescriptor> methods) {
+ public MethodCreator(ClassVisitor arg0, Manipulator manipulator) {
super(arg0);
- m_fields = fields.keySet();
- m_visitedMethods = methods;
+ m_manipulator = manipulator;
+ m_fields = manipulator.getFields().keySet();
+ m_visitedMethods = manipulator.getMethods();
}
/**
@@ -161,6 +167,7 @@ public class MethodCreator extends Class
m_superclass = superName;
addPOJOInterface(version, access, name, signature, superName, interfaces);
addIMField();
+ addFlagsForInnerClassMethods();
}
/**
@@ -223,9 +230,10 @@ public class MethodCreator extends Class
md.getAnnotations(), md.getParameterAnnotations());
}
+ // TODO Also add the method flags for inner class methods.
String id = generateMethodFlag(name, desc);
if (! m_methodFlags.contains(id)) {
- FieldVisitor flagField = cv.visitField(Opcodes.ACC_PRIVATE, id, "Z", null, null);
+ FieldVisitor flagField = cv.visitField(0, id, "Z", null, null);
flagField.visitEnd();
m_methodFlags.add(id);
}
@@ -243,8 +251,7 @@ public class MethodCreator extends Class
* @return the method descriptor or <code>null</code> if not found.
*/
private MethodDescriptor getMethodDescriptor(String name, String desc) {
- for (int i = 0; i < m_visitedMethods.size(); i++) {
- MethodDescriptor md = m_visitedMethods.get(i);
+ for (MethodDescriptor md : m_visitedMethods) {
if (md.getName().equals(name) && md.getDescriptor().equals(desc)) {
return md;
}
@@ -527,6 +534,10 @@ public class MethodCreator extends Class
return METHOD_FLAG_PREFIX + generateMethodId(name, desc);
}
+ private String generateMethodFlagForMethodFromInnerClass(String name, String desc, String inner) {
+ return METHOD_FLAG_PREFIX + generateMethodIdForMethodFromInnerClass(name, desc, inner);
+ }
+
/**
* Generate the method id based on the given method name and method descriptor.
* The method Id is unique for this method and serves to create the flag field (so
@@ -536,10 +547,36 @@ public class MethodCreator extends Class
* @return method ID
*/
private String generateMethodId(String name, String desc) {
- StringBuffer id = new StringBuffer(name);
+ StringBuilder id = new StringBuilder(name);
+ Type[] args = Type.getArgumentTypes(desc);
+ for (Type type : args) {
+ String arg = type.getClassName();
+ if (arg.endsWith("[]")) {
+ // We have to replace all []
+ String acc = "";
+ while (arg.endsWith("[]")) {
+ arg = arg.substring(0, arg.length() - 2);
+ acc += "__";
+ }
+ id.append("$").append(arg.replace('.', '_')).append(acc);
+ } else {
+ id.append("$").append(arg.replace('.', '_'));
+ }
+ }
+ if (!m_methods.contains(id.toString())) {
+ m_methods.add(id.toString());
+ }
+ return id.toString();
+ }
+
+ private String generateMethodIdForMethodFromInnerClass(String name, String desc, String inner) {
+ StringBuilder id = new StringBuilder(inner);
+ id.append("___"); // Separator
+ id.append(name);
+
Type[] args = Type.getArgumentTypes(desc);
- for (int i = 0; i < args.length; i++) {
- String arg = args[i].getClassName();
+ for (Type type : args) {
+ String arg = type.getClassName();
if (arg.endsWith("[]")) {
// We have to replace all []
String acc = "";
@@ -547,14 +584,16 @@ public class MethodCreator extends Class
arg = arg.substring(0, arg.length() - 2);
acc += "__";
}
- id.append("$" + arg.replace('.', '_') + acc);
+ id.append("$").append(arg.replace('.', '_')).append(acc);
} else {
- id.append("$" + arg.replace('.', '_'));
+ id.append("$").append(arg.replace('.', '_'));
}
}
+
if (!m_methods.contains(id.toString())) {
m_methods.add(id.toString());
}
+
return id.toString();
}
@@ -562,11 +601,30 @@ public class MethodCreator extends Class
* Add the instance manager field (__im).
*/
private void addIMField() {
- FieldVisitor fv = super.visitField(ACC_PRIVATE, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;", null, null);
+ FieldVisitor fv = super.visitField(0, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;", null, null);
fv.visitEnd();
}
/**
+ * Add the boolean flag fields for methods from inner classes.
+ */
+ private void addFlagsForInnerClassMethods() {
+ for (Map.Entry<String, List<MethodDescriptor>> entry : m_manipulator.getInnerClassesAndMethods().entrySet()) {
+ for (MethodDescriptor descriptor : entry.getValue()) {
+ String id = generateMethodFlagForMethodFromInnerClass(
+ descriptor.getName(),
+ descriptor.getDescriptor(),
+ entry.getKey());
+ if (! m_methodFlags.contains(id)) {
+ FieldVisitor flagField = cv.visitField(0, id, "Z", null, null);
+ flagField.visitEnd();
+ m_methodFlags.add(id);
+ }
+ }
+ }
+ }
+
+ /**
* Add the POJO interface to the visited class.
* @param version : class version
* @param access : class access
Modified: felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/ManipulationEngine.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/ManipulationEngine.java?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/ManipulationEngine.java (original)
+++ felix/trunk/ipojo/manipulator/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/ManipulationEngine.java Tue Oct 1 16:16:51 2013
@@ -19,13 +19,12 @@
package org.apache.felix.ipojo.manipulator;
+import org.apache.felix.ipojo.manipulation.Manipulator;
+
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
-import org.apache.felix.ipojo.manipulation.InnerClassManipulator;
-import org.apache.felix.ipojo.manipulation.Manipulator;
-
/**
* A {@code ManipulationEngine} is responsible to drive the component's
* classes manipulation.
@@ -106,9 +105,32 @@ public class ManipulationEngine {
if (result != null) {
// Should always be the case
- // Manipulate the original bytecode and store the modified one
+ // Manipulation preparation
Manipulator manipulator = new Manipulator();
try {
+ manipulator.prepare(bytecode);
+ } catch (IOException e) {
+ m_reporter.error("Cannot analyze the class " + info.getClassName() + " : " + e.getMessage());
+ return;
+ }
+
+ // Inner class preparation
+ for (String inner : manipulator.getInnerClasses()) {
+ // Get the bytecode and start manipulation
+ String resourcePath = inner + ".class";
+ String outerClassInternalName = info.getClassName().replace('.', '/');
+ byte[] innerClassBytecode;
+ try {
+ innerClassBytecode = m_store.read(resourcePath);
+ manipulator.prepareInnerClass(inner, innerClassBytecode);
+ } catch (IOException e) {
+ m_reporter.error("Cannot find or analyze inner class '" + resourcePath + "'");
+ return;
+ }
+ }
+
+ // Now manipulate the classes.
+ try {
byte[] out = manipulator.manipulate(bytecode);
// Call the visitor
result.visitManipulatedResource(info.getResourcePath(), out);
@@ -117,7 +139,6 @@ public class ManipulationEngine {
return;
}
-
// Visit inner classes
for (String inner : manipulator.getInnerClasses()) {
// Get the bytecode and start manipulation
@@ -136,10 +157,7 @@ public class ManipulationEngine {
// discovered in the main class instead of re-parsing the inner class to find
// its own class version
try {
- InnerClassManipulator innerManipulator = new InnerClassManipulator(inner,
- outerClassInternalName,
- manipulator);
- byte[] manipulated = innerManipulator.manipulate(innerClassBytecode, manipulator.getClassVersion());
+ byte[] manipulated = manipulator.manipulateInnerClass(inner, innerClassBytecode);
// Propagate manipulated resource
result.visitManipulatedResource(resourcePath, manipulated);
} catch (IOException e) {
Modified: felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/InstanceManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/InstanceManager.java?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/InstanceManager.java (original)
+++ felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/InstanceManager.java Tue Oct 1 16:16:51 2013
@@ -36,4 +36,16 @@ public class InstanceManager {
return null;
}
+ public void onEntry(Object pojo, String methodId, Object[] args) {
+
+ }
+
+ public void onExit(Object pojo, String methodId, Object[] args) {
+
+ }
+
+ public void onError(Object pojo, String methodId, Throwable error) {
+
+ }
+
}
Modified: felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ClassCheckerTestCase.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ClassCheckerTestCase.java?rev=1528135&r1=1528134&r2=1528135&view=diff
==============================================================================
--- felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ClassCheckerTestCase.java (original)
+++ felix/trunk/ipojo/manipulator/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ClassCheckerTestCase.java Tue Oct 1 16:16:51 2013
@@ -110,6 +110,7 @@ public class ClassCheckerTestCase extend
private byte[] manipulate(byte[] input) throws Exception {
Manipulator manipulator = new Manipulator();
+ manipulator.prepare(input);
return manipulator.manipulate(input);
}