From 1c0f36ea2026aff22afd9c9d22782db8233c8b95 Mon Sep 17 00:00:00 2001 From: Andy Clement Date: Mon, 19 Mar 2012 12:16:10 -0700 Subject: [PATCH] call jps and indy --- .../apache/bcel/generic/FieldOrMethod.java | 2 +- .../apache/bcel/generic/InvokeDynamic.java | 17 ++- tests/indy/three/Asp.java | 6 ++ tests/indy/three/Code1.java | 21 ++++ tests/indy/three/Generator.java | 101 ++++++++++++++++++ tests/indy/three/indy.jar | Bin 0 -> 409 bytes .../aspectj/systemtest/ajc170/IndyTests.java | 10 +- .../org/aspectj/systemtest/ajc170/indy.xml | 13 +++ 8 files changed, 163 insertions(+), 7 deletions(-) create mode 100644 tests/indy/three/Asp.java create mode 100644 tests/indy/three/Code1.java create mode 100644 tests/indy/three/Generator.java create mode 100644 tests/indy/three/indy.jar diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldOrMethod.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldOrMethod.java index 4987969c3..44b263ffb 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldOrMethod.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldOrMethod.java @@ -68,7 +68,7 @@ import org.aspectj.apache.bcel.classfile.ConstantUtf8; public abstract class FieldOrMethod extends InstructionCP { protected String signature; - private String name; + protected String name; private String classname; protected FieldOrMethod(short opcode, int index) { diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/InvokeDynamic.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/InvokeDynamic.java index 7083500d0..951f9b1b3 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/generic/InvokeDynamic.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/InvokeDynamic.java @@ -58,6 +58,7 @@ import java.io.DataOutputStream; import java.io.IOException; import org.aspectj.apache.bcel.Constants; +import org.aspectj.apache.bcel.classfile.ConstantCP; import org.aspectj.apache.bcel.classfile.ConstantInvokeDynamic; import org.aspectj.apache.bcel.classfile.ConstantNameAndType; import org.aspectj.apache.bcel.classfile.ConstantPool; @@ -108,9 +109,23 @@ public final class InvokeDynamic extends InvokeInstruction { if (signature == null) { ConstantInvokeDynamic cid = (ConstantInvokeDynamic)cp.getConstant(index); ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cid.getNameAndTypeIndex()); - signature = ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getValue(); + signature = cp.getConstantUtf8(cnat.getSignatureIndex()).getValue(); } return signature; } + + @Override + public String getName(ConstantPool cp) { + if (name == null) { + ConstantInvokeDynamic cid = (ConstantInvokeDynamic) cp.getConstant(index); + ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cid.getNameAndTypeIndex()); + name = cp.getConstantUtf8(cnat.getNameIndex()).getValue(); + } + return name; + } + + public String getClassName(ConstantPool cp) { + throw new IllegalStateException("nyi"); + } } diff --git a/tests/indy/three/Asp.java b/tests/indy/three/Asp.java new file mode 100644 index 000000000..f2460c72a --- /dev/null +++ b/tests/indy/three/Asp.java @@ -0,0 +1,6 @@ +aspect Aspect { + + before(): call(* *.*(..)) && !within(Aspect) { + System.out.println(thisJoinPointStaticPart); + } +} diff --git a/tests/indy/three/Code1.java b/tests/indy/three/Code1.java new file mode 100644 index 000000000..2899340d2 --- /dev/null +++ b/tests/indy/three/Code1.java @@ -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/three/Generator.java b/tests/indy/three/Generator.java new file mode 100644 index 000000000..77f1e1807 --- /dev/null +++ b/tests/indy/three/Generator.java @@ -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, "", "()V", null, null); + mv.visitCode(); + mv.visitVarInsn(ALOAD, 0); + mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V"); + mv.visitInsn(RETURN); + mv.visitMaxs(1, 1); + mv.visitEnd(); + } +} \ No newline at end of file diff --git a/tests/indy/three/indy.jar b/tests/indy/three/indy.jar new file mode 100644 index 0000000000000000000000000000000000000000..d6ec561f4a09536fc7aef1a9a991f10818efee70 GIT binary patch literal 409 zcmWIWW@Zs#;Nak3P)jLrU_b)A3@i+ud1d+8sYQCpIf=!^|4uP5lm^)PA2tx#bKQ)S zx3TVnsj{Jpj$jf`(gJP|S02~c88Jy+w-elZR~|W^wMqT8NKu6;lE z{@vrxAIToj8fib66Fes+FE8>9)u%H(%VjXFKC7mj6DPspmPrT1dKmc+j4q zpkOkuv@(~}uyw>&!`5q)!}250D>nmthtzy=QlYGF!f{dG~ScVUM1BAEnLWrnCCqduaYPMfR=!2JST#ey8>t zlr`uFc(ZfdU}0Ur%E-Xb0>lB{j7%a7h)_h91BD_gzzEX-Z&o&t5=J1j2huJ;BN-R~ D;7peX literal 0 HcmV?d00001 diff --git a/tests/src/org/aspectj/systemtest/ajc170/IndyTests.java b/tests/src/org/aspectj/systemtest/ajc170/IndyTests.java index 24503ce1b..b967657ae 100644 --- a/tests/src/org/aspectj/systemtest/ajc170/IndyTests.java +++ b/tests/src/org/aspectj/systemtest/ajc170/IndyTests.java @@ -15,12 +15,7 @@ import java.io.IOException; import junit.framework.Test; -import org.aspectj.apache.bcel.classfile.ClassParser; -import org.aspectj.apache.bcel.classfile.Field; -import org.aspectj.apache.bcel.classfile.JavaClass; import org.aspectj.testing.XMLBasedAjcTestCase; -import org.aspectj.weaver.TypeFactory; -import org.aspectj.weaver.UnresolvedType; /** * @author Andy Clement @@ -36,6 +31,11 @@ public class IndyTests extends org.aspectj.testing.XMLBasedAjcTestCase { public void testInvokeDynamic_execution() throws IOException { runTest("indy - 2"); } + + // call pointcuts on same bytecode + public void testInvokeDynamic_call() throws IOException { + runTest("indy - 3"); + } // --- diff --git a/tests/src/org/aspectj/systemtest/ajc170/indy.xml b/tests/src/org/aspectj/systemtest/ajc170/indy.xml index 0e1b85a12..3bb18608f 100644 --- a/tests/src/org/aspectj/systemtest/ajc170/indy.xml +++ b/tests/src/org/aspectj/systemtest/ajc170/indy.xml @@ -24,5 +24,18 @@ + + + + + + + + + + + + + -- 2.39.5