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.

AnonymousClassInMethodTest.java 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package com.github.dcevm.test.structural;
  2. import org.junit.Assert;
  3. import org.junit.Before;
  4. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  5. import static com.github.dcevm.test.util.HotSwapTestHelper.__version__;
  6. /**
  7. * Test insertion and swap of anonymous classes.
  8. */
  9. public class AnonymousClassInMethodTest {
  10. public static interface I {
  11. public boolean m();
  12. };
  13. public static interface I2 {};
  14. // Version 0
  15. public static class A {
  16. public boolean test() {
  17. I anonymous = new I() {
  18. @Override
  19. public boolean m() {
  20. return true;
  21. }
  22. };
  23. return anonymous.m();
  24. }
  25. }
  26. // Version 1
  27. public static class A___1 {
  28. public boolean test() {
  29. I2 insertedAnonymous = new I2() {};
  30. I anonymous = new I() {
  31. @Override
  32. public boolean m() {
  33. return false;
  34. }
  35. };
  36. return anonymous.m();
  37. }
  38. }
  39. @Before
  40. public void setUp() throws Exception {
  41. __toVersion__(0);
  42. }
  43. // TODO this test fails, because conent of A$1 is now interface I2 instead of interface I (not compatible change)
  44. // HotswapAgent plugin AnonymousClassPatch solves this on Java instrumentation level by exchanging content of class files.
  45. // @see https://github.com/HotswapProjects/HotswapAgent/tree/master/HotswapAgent/src/main/java/org/hotswap/agent/plugin/jvm
  46. //@Test
  47. public void testAnonymous() {
  48. assert __version__() == 0;
  49. Assert.assertTrue(new A().test());
  50. __toVersion__(1);
  51. Assert.assertFalse(new A().test());
  52. __toVersion__(0);
  53. Assert.assertTrue(new A().test());
  54. }
  55. }