You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnweavableTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Alexandre Vasseur initial implementation
  11. *******************************************************************************/
  12. package ataspectj;
  13. import junit.framework.TestCase;
  14. import java.lang.reflect.Proxy;
  15. import java.lang.reflect.InvocationHandler;
  16. import java.lang.reflect.Method;
  17. import java.lang.annotation.Retention;
  18. import java.lang.annotation.RetentionPolicy;
  19. import java.io.Serializable;
  20. import org.aspectj.lang.annotation.Aspect;
  21. import org.aspectj.lang.annotation.Before;
  22. import org.objectweb.asm.ClassWriter;
  23. import org.objectweb.asm.Opcodes;
  24. import org.objectweb.asm.MethodVisitor;
  25. import org.objectweb.asm.AnnotationVisitor;
  26. /**
  27. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  28. */
  29. public class UnweavableTest extends TestCase {
  30. public void testUnweavableProxy() {
  31. TestAspect.I = 0;
  32. ISome some = getProxy();
  33. some.giveOne();
  34. assertEquals(1, TestAspect.I);
  35. }
  36. interface ISome {
  37. int giveOne();
  38. }
  39. ISome getProxy() {
  40. return (ISome) Proxy.newProxyInstance(
  41. ISome.class.getClassLoader(),
  42. new Class[]{ISome.class},
  43. new InvocationHandler() {
  44. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  45. return 1;
  46. }
  47. }
  48. );
  49. }
  50. @Aspect
  51. public static class TestAspect {
  52. static int I = 0;
  53. @Before("execution(* ataspectj.UnweavableTest.ISome+.giveOne())")
  54. public void before() {
  55. I++;
  56. }
  57. }
  58. public void testJit() {
  59. TestAspect.I = 0;
  60. TestAspect2.I = 0;
  61. ISome some = getJit();
  62. assertNotNull(some.getClass().getAnnotation(ASome.class));
  63. assertEquals(2, some.giveOne());
  64. assertEquals(1, TestAspect.I);
  65. assertEquals(1, TestAspect2.I);
  66. }
  67. public void testJitNotMatched() {
  68. // just load a jit to make sure the weaver does not complains for classes coming from nowhere
  69. Serializable serial = getJitNoMatch();
  70. assertEquals(0, serial.getClass().getDeclaredMethods().length);
  71. }
  72. @Retention(RetentionPolicy.RUNTIME)
  73. @interface ASome {}
  74. ISome getJit() {
  75. ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
  76. cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "ataspectj/ISomeGen", null, "java/lang/Object", new String[]{"ataspectj/UnweavableTest$ISome"});
  77. AnnotationVisitor av = cw.visitAnnotation("Lataspectj/UnweavableTest$ASome;", true);
  78. av.visitEnd();
  79. MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
  80. mv.visitVarInsn(Opcodes.ALOAD, 0);
  81. mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
  82. mv.visitInsn(Opcodes.RETURN);
  83. mv.visitMaxs(0, 0);
  84. mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "giveOne", "()I", null, new String[0]);
  85. mv.visitInsn(Opcodes.ICONST_2);
  86. mv.visitInsn(Opcodes.IRETURN);
  87. mv.visitMaxs(0, 0);
  88. cw.visitEnd();
  89. try {
  90. ClassLoader loader = this.getClass().getClassLoader();
  91. // Needs "--add-opens java.base/java.lang=ALL-UNNAMED" on the JVM command line, injected in Ant build via
  92. // aj.addOpensKey and aj.addOpensValue variables. See also AntSpec::execute.
  93. Method def = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
  94. def.setAccessible(true);
  95. Class<?> gen = (Class<?>) def.invoke(loader, "ataspectj.ISomeGen", cw.toByteArray(), 0, cw.toByteArray().length);
  96. return (ISome) gen.getDeclaredConstructor().newInstance();
  97. } catch (Throwable t) {
  98. fail(t.toString());
  99. return null;
  100. }
  101. }
  102. Serializable getJitNoMatch() {
  103. ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
  104. cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "ataspectj/unmatched/Gen", null, "java/lang/Object", new String[]{"java/io/Serializable"});
  105. MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
  106. mv.visitVarInsn(Opcodes.ALOAD, 0);
  107. mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
  108. mv.visitInsn(Opcodes.RETURN);
  109. mv.visitMaxs(0, 0);
  110. cw.visitEnd();
  111. try {
  112. ClassLoader loader = this.getClass().getClassLoader();
  113. // Needs "--add-opens java.base/java.lang=ALL-UNNAMED" on the JVM command line, injected in Ant build via
  114. // aj.addOpensKey and aj.addOpensValue variables. See also AntSpec::execute.
  115. Method def = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
  116. def.setAccessible(true);
  117. Class<?> gen = (Class<?>) def.invoke(loader, "ataspectj.unmatched.Gen", cw.toByteArray(), 0, cw.toByteArray().length);
  118. return (Serializable) gen.getDeclaredConstructor().newInstance();
  119. } catch (Throwable t) {
  120. fail(t.toString());
  121. return null;
  122. }
  123. }
  124. @Aspect
  125. public static class TestAspect2 {
  126. static int I = 0;
  127. @Before("execution(* @ataspectj.UnweavableTest$ASome ataspectj..*.giveOne())")
  128. public void before() {
  129. I++;
  130. }
  131. }
  132. public static void main(String[] args) throws Throwable {
  133. TestHelper.runAndThrowOnFailure(suite());
  134. }
  135. public static junit.framework.Test suite() {
  136. return new junit.framework.TestSuite(UnweavableTest.class);
  137. }
  138. }