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.

MethodReflectionTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. *
  23. */
  24. package com.github.dcevm.test.methods;
  25. import junit.framework.Assert;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import java.lang.reflect.Method;
  29. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  30. import static com.github.dcevm.test.util.HotSwapTestHelper.__version__;
  31. import static org.junit.Assert.assertEquals;
  32. /**
  33. * Testing correct reflection functionality after class redefinition.
  34. *
  35. * @author Thomas Wuerthinger
  36. */
  37. public class MethodReflectionTest {
  38. @Before
  39. public void setUp() throws Exception {
  40. __toVersion__(0);
  41. }
  42. // Version 0
  43. public static class A {
  44. public int value() {
  45. return 1;
  46. }
  47. }
  48. public static class B extends A {
  49. @Override
  50. public int value() {
  51. return 2;
  52. }
  53. }
  54. public static class C extends A {
  55. @Override
  56. public int value() {
  57. return 3;
  58. }
  59. }
  60. // Version 1
  61. public static class A___1 {
  62. public int value() {
  63. return 1;
  64. }
  65. public int value2() {
  66. return 2;
  67. }
  68. }
  69. // Version 2
  70. public static class C___2 extends B {
  71. @Override
  72. public int value() {
  73. return super.value();
  74. }
  75. }
  76. @Test
  77. public void testMethodReflection() {
  78. assert __version__() == 0;
  79. A a = new A();
  80. B b = new B();
  81. C c = new C();
  82. assertEquals(1, a.value());
  83. assertEquals(2, b.value());
  84. assertEquals(3, c.value());
  85. assertContainsMethod(A.class, "value");
  86. assertDoesNotContainMethod(A.class, "value2");
  87. __toVersion__(1);
  88. assertEquals(1, a.value());
  89. assertEquals(2, b.value());
  90. assertEquals(3, c.value());
  91. assertContainsMethod(A.class, "value");
  92. assertContainsMethod(A.class, "value2");
  93. __toVersion__(0);
  94. assertEquals(1, a.value());
  95. assertEquals(2, b.value());
  96. assertEquals(3, c.value());
  97. assertContainsMethod(A.class, "value");
  98. assertDoesNotContainMethod(A.class, "value2");
  99. }
  100. private void assertContainsMethod(Class<?> c, String methodName) {
  101. boolean found = false;
  102. for (Method m : c.getDeclaredMethods()) {
  103. if (m.getName().equals(methodName)) {
  104. found = true;
  105. break;
  106. }
  107. }
  108. Assert.assertTrue(found);
  109. }
  110. private void assertDoesNotContainMethod(Class<?> c, String methodName) {
  111. boolean found = false;
  112. for (Method m : c.getDeclaredMethods()) {
  113. if (m.getName().equals(methodName)) {
  114. found = true;
  115. break;
  116. }
  117. }
  118. Assert.assertFalse(found);
  119. }
  120. }