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.

ClassReflectionTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 org.dcevm.test.methods;
  25. import junit.framework.Assert;
  26. import org.dcevm.test.TestUtil;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import java.lang.ref.SoftReference;
  30. import java.lang.reflect.Method;
  31. import static org.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  32. import static org.dcevm.test.util.HotSwapTestHelper.__version__;
  33. import static org.junit.Assert.assertEquals;
  34. /**
  35. * Testing correct reflection functionality after class redefinition.
  36. *
  37. * @author Thomas Wuerthinger
  38. */
  39. public class ClassReflectionTest {
  40. @Before
  41. public void setUp() throws Exception {
  42. __toVersion__(0);
  43. }
  44. // Version 0
  45. public static class A {
  46. public int value() {
  47. return 1;
  48. }
  49. }
  50. public static class B extends A {
  51. @Override
  52. public int value() {
  53. return 2;
  54. }
  55. }
  56. public static class C extends A {
  57. @Override
  58. public int value() {
  59. return 3;
  60. }
  61. }
  62. // Version 1
  63. public static class A___1 {
  64. public int value() {
  65. return 1;
  66. }
  67. public int value2() {
  68. return 2;
  69. }
  70. }
  71. // Version 2
  72. public static class C___2 extends B {
  73. @Override
  74. public int value() {
  75. return super.value();
  76. }
  77. }
  78. private void assertIsSuper(Class<?> s, Class<?> c) {
  79. assertEquals(s, c.getSuperclass());
  80. }
  81. private void assertIsNotSuper(Class<?> s, Class<?> c) {
  82. Assert.assertFalse(s.equals(c.getSuperclass()));
  83. }
  84. @Test
  85. public void testClassReflection() {
  86. checkWeakReference();
  87. assert __version__() == 0;
  88. final A a = new A();
  89. final B b = new B();
  90. final C c = new C();
  91. assertIsSuper(A.class, B.class);
  92. assertIsSuper(A.class, C.class);
  93. assertIsNotSuper(B.class, C.class);
  94. assertEquals(1, a.value());
  95. assertEquals(2, b.value());
  96. assertEquals(3, c.value());
  97. __toVersion__(2);
  98. assertIsSuper(A.class, B.class);
  99. assertIsSuper(B.class, C.class);
  100. assertIsNotSuper(A.class, C.class);
  101. assertEquals(1, a.value());
  102. assertEquals(2, b.value());
  103. assertEquals(2, c.value());
  104. TestUtil.assertUnsupportedWithLight(new Runnable() {
  105. @Override
  106. public void run() {
  107. __toVersion__(0);
  108. assertIsSuper(A.class, B.class);
  109. assertIsSuper(A.class, C.class);
  110. assertIsNotSuper(B.class, C.class);
  111. assertEquals(1, a.value());
  112. assertEquals(2, b.value());
  113. assertEquals(3, c.value());
  114. }
  115. });
  116. }
  117. public void checkWeakReference() {
  118. A a = new A();
  119. Class<?> strongRef = a.getClass();
  120. SoftReference<Class<?>> softRef = new SoftReference<Class<?>>(a.getClass());
  121. assertEquals(1, a.value());
  122. __toVersion__(1);
  123. assertEquals(1, a.value());
  124. Assert.assertTrue(strongRef == softRef.get());
  125. __toVersion__(0);
  126. }
  127. }