選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DeclareParentsImplementsTest.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Alexandre Vasseur initial implementation
  11. *******************************************************************************/
  12. package ataspectj;
  13. import junit.framework.TestCase;
  14. import org.aspectj.lang.annotation.Aspect;
  15. import org.aspectj.lang.annotation.DeclareParents;
  16. import org.aspectj.lang.annotation.Before;
  17. import java.util.Arrays;
  18. import java.lang.annotation.Retention;
  19. import java.lang.annotation.RetentionPolicy;
  20. import java.lang.annotation.Target;
  21. import java.lang.annotation.ElementType;
  22. import java.lang.reflect.Method;
  23. /**
  24. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  25. */
  26. public class DeclareParentsImplementsTest extends TestCase {
  27. static class Target {
  28. void target() {
  29. log("hello");
  30. }
  31. }
  32. static interface Introduced {
  33. final static int field1 = 1;
  34. @Some
  35. void intro();
  36. }
  37. static class Implementation implements Introduced {
  38. public void intro() {
  39. log("intro-"+field1);
  40. // we cannot copy the raw bytecode as there might be super.* calls, and other OO stuff
  41. }
  42. }
  43. @Aspect
  44. static class TestAspect {
  45. @DeclareParents(value="ataspectj.DeclareParentsImplementsTest.Target",
  46. defaultImpl=Implementation.class)
  47. public static Introduced i;
  48. // will lead to: class Target implements Introduced {
  49. // void intro(args) { delegate to some hidden field, lazy initialized here for now }
  50. // }
  51. @Before("execution(* ataspectj.DeclareParentsImplementsTest.Introduced.intro())")
  52. public void before() {
  53. log("aop");
  54. }
  55. }
  56. static StringBuffer s_log = new StringBuffer();
  57. static void log(String s) {
  58. s_log.append(s).append(" ");
  59. }
  60. public void testDecPInt() {
  61. Class[] intfs = Target.class.getInterfaces();
  62. assertTrue("Was not introduced", Arrays.asList(intfs).contains(Introduced.class));
  63. }
  64. public void testDecPIntAdvised() {
  65. s_log = new StringBuffer();
  66. ((Introduced)new Target()).intro();
  67. assertEquals("aop intro-1 ", s_log.toString());
  68. }
  69. public void testAddedMethodKeepAnnotation() throws Throwable {
  70. Method m = Target.class.getDeclaredMethod("intro");
  71. assertTrue("annotation not retained", m.isAnnotationPresent(Some.class));
  72. }
  73. public static void main(String[] args) {
  74. TestHelper.runAndThrowOnFailure(suite());
  75. }
  76. public static junit.framework.Test suite() {
  77. return new junit.framework.TestSuite(DeclareParentsImplementsTest.class);
  78. }
  79. @Retention(RetentionPolicy.RUNTIME)
  80. static @interface Some {
  81. }
  82. }