]> source.dussan.org Git - aspectj.git/commitdiff
additional test to show all joinpoints in code using indy
authorAndy Clement <aclement@vmware.com>
Mon, 19 Mar 2012 21:06:19 +0000 (14:06 -0700)
committerAndy Clement <aclement@vmware.com>
Mon, 19 Mar 2012 21:06:19 +0000 (14:06 -0700)
tests/indy/four/Asp.java [new file with mode: 0644]
tests/indy/four/Code1.java [new file with mode: 0644]
tests/indy/four/Generator.java [new file with mode: 0644]
tests/indy/four/indy.jar [new file with mode: 0644]
tests/src/org/aspectj/systemtest/ajc170/IndyTests.java
tests/src/org/aspectj/systemtest/ajc170/indy.xml

diff --git a/tests/indy/four/Asp.java b/tests/indy/four/Asp.java
new file mode 100644 (file)
index 0000000..78f86a3
--- /dev/null
@@ -0,0 +1,6 @@
+aspect Aspect {
+
+  before(): within(Code1) {
+    System.out.println(thisJoinPointStaticPart);
+  }
+}
diff --git a/tests/indy/four/Code1.java b/tests/indy/four/Code1.java
new file mode 100644 (file)
index 0000000..2899340
--- /dev/null
@@ -0,0 +1,21 @@
+import java.lang.invoke.CallSite;
+import java.lang.invoke.ConstantCallSite;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+
+public class Code1 {
+
+       // Called via invokedynamic from a generated class
+       private static void foo() {
+               System.out.println("foo() is running");
+       }
+
+       public static CallSite bootstrap(MethodHandles.Lookup caller, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
+               MethodHandles.Lookup lookup = MethodHandles.lookup();
+               Class<?> thisClass = lookup.lookupClass();
+               MethodHandle mh = lookup.findStatic(thisClass, name, type);
+               return new ConstantCallSite(mh);//mh.asType(type));
+       }
+
+}
\ No newline at end of file
diff --git a/tests/indy/four/Generator.java b/tests/indy/four/Generator.java
new file mode 100644 (file)
index 0000000..77f1e18
--- /dev/null
@@ -0,0 +1,101 @@
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.lang.invoke.CallSite;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+
+import org.objectweb.asm.*;
+
+public class Generator implements Opcodes {
+
+       private String generatedRunnerTypename,linkClassName,dynMethodName,dynMethodDescriptor;
+       private byte[] bytes;
+       
+       /**
+        * Main entry point generates a default thing, in this case a class called 'Invoker' that will use the bootstrap method on Code1 to run 'foo()'
+        * @param args
+        * @throws Throwable
+        */
+       public static void main(String[] args) throws Throwable {
+               Generator g = new Generator("Invoker","Code1","foo","()V");
+               g.dump();
+       }
+       
+       public byte[] getBytes() {
+               return bytes;
+       }
+
+       public Generator(String generatedRunnerTypename, String linkClassName, String dynMethodName, String dynMethodDescriptor) {
+               this.generatedRunnerTypename = generatedRunnerTypename;
+               this.linkClassName = linkClassName;
+               this.dynMethodName = dynMethodName;
+               this.dynMethodDescriptor = dynMethodDescriptor;
+               this.bytes = generateClass();
+       }
+
+       public void dump() {
+               try {
+                       FileOutputStream fos
+                        = new FileOutputStream(new File(generatedRunnerTypename+".class"));
+                       fos.write(bytes);
+                       fos.close();
+               } catch (FileNotFoundException e) {
+                       e.printStackTrace();
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
+       
+       public byte[] generateClass() {
+               ClassWriter cw = new ClassWriter(0);
+               cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, generatedRunnerTypename, null, "java/lang/Object", null);
+               createConstructor(cw);
+               createMain(cw);
+               cw.visitEnd();
+               return cw.toByteArray();
+       }
+
+       private void createMain(ClassWriter cw) {
+               MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
+               mv.visitCode();
+               MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
+                               MethodType.class);
+               Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, this.linkClassName, "bootstrap",
+                               mt.toMethodDescriptorString());
+               int maxStackSize = 0;//addMethodParameters(mv);
+               mv.visitInvokeDynamicInsn(dynMethodName, dynMethodDescriptor, bootstrap);
+               mv.visitInsn(RETURN);
+               mv.visitMaxs(maxStackSize, 1);
+               mv.visitEnd();
+       }
+       
+//     public byte[] dump(String dynamicInvokerClassName, String dynamicLinkageClassName, String bootstrapMethodName, String targetMethodDescriptor)
+//                     throws Exception {
+//             ClassWriter cw = new ClassWriter(0);
+//             cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, dynamicInvokerClassName, null, "java/lang/Object", null);
+//             createConstructor(cw);
+//             createMain(dynamicLinkageClassName, bootstrapMethodName, targetMethodDescriptor, cw);
+//             cw.visitEnd();
+//             return cw.toByteArray();
+//     }
+
+
+
+//     protected int addMethodParameters(MethodVisitor mv) {
+//             return 0;
+//     }
+
+
+       private void createConstructor(ClassWriter cw) {
+               MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
+               mv.visitCode();
+               mv.visitVarInsn(ALOAD, 0);
+               mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
+               mv.visitInsn(RETURN);
+               mv.visitMaxs(1, 1);
+               mv.visitEnd();
+       }
+}
\ No newline at end of file
diff --git a/tests/indy/four/indy.jar b/tests/indy/four/indy.jar
new file mode 100644 (file)
index 0000000..d6ec561
Binary files /dev/null and b/tests/indy/four/indy.jar differ
index b967657ae99552a75513d1b04039440105b7e433..5846a66fd078ab689f1af9ec7a355bb5f2318d4f 100644 (file)
@@ -36,6 +36,11 @@ public class IndyTests extends org.aspectj.testing.XMLBasedAjcTestCase {
        public void testInvokeDynamic_call() throws IOException {
                runTest("indy - 3");
        }
+       
+       // within(Code1) on same bytecode
+       public void testInvokeDynamic_target() throws IOException {
+               runTest("indy - 4");
+       }
 
        // ---
 
index 3bb18608f428d81cd160e34fb1b1c2c7f83bd14f..0c4bbe5805cecb7a6a617b46b769ab7c08b0c853 100644 (file)
       </stdout>
       </run>
     </ajc-test>
+    
+    <ajc-test dir="indy/four" title="indy - 4">
+      <compile files="Code1.java,Asp.java" inpath="indy.jar" options="-1.7"/>
+      <run class="Invoker">
+      <stdout>
+      <line text="staticinitialization(Code1.&lt;clinit&gt;)"/>
+      <line text="execution(CallSite Code1.bootstrap(MethodHandles.Lookup, String, MethodType))"/>
+      <line text="call(MethodHandles.Lookup java.lang.invoke.MethodHandles.lookup())"/>
+      <line text="call(Class java.lang.invoke.MethodHandles.Lookup.lookupClass())"/>
+      <line text="call(MethodHandle java.lang.invoke.MethodHandles.Lookup.findStatic(Class, String, MethodType))"/>
+      <line text="call(java.lang.invoke.ConstantCallSite(MethodHandle))"/>
+      <line text="execution(void Code1.foo())"/>
+      <line text="get(PrintStream java.lang.System.out)"/>
+      <line text="call(void java.io.PrintStream.println(String))"/>
+      <line text="foo() is running"/>
+      </stdout>
+      </run>
+    </ajc-test>
    
 </suite>