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.

TypeNarrowingHeapTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.structural;
  25. import com.github.dcevm.test.TestUtil;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  29. import static com.github.dcevm.test.util.HotSwapTestHelper.__version__;
  30. import static org.junit.Assert.assertEquals;
  31. /**
  32. * Test case for type narrowing.
  33. *
  34. * @author Thomas Wuerthinger
  35. */
  36. public class TypeNarrowingHeapTest {
  37. // Version 0
  38. public static class A {
  39. int x = 1;
  40. int y = 2;
  41. int z = 3;
  42. public int value() {
  43. return x;
  44. }
  45. }
  46. public static class C {
  47. private A a;
  48. public C(A a) {
  49. this.a = a;
  50. }
  51. }
  52. public static class B extends A {
  53. }
  54. // Version 1
  55. public static class B___1 {
  56. }
  57. @Before
  58. public void setUp() throws Exception {
  59. __toVersion__(0);
  60. A a = new A();
  61. B b = new B();
  62. }
  63. @Test
  64. public void testSimpleTypeNarrowing() {
  65. assert __version__() == 0;
  66. A a = convertBtoA(new B());
  67. assertEquals(1, a.value());
  68. // Cannot do conversion if A object is on the stack!
  69. a = null;
  70. __toVersion__(1);
  71. TestUtil.assertException(NoSuchMethodError.class, new Runnable() {
  72. @Override
  73. public void run() {
  74. B b = new B();
  75. b.value();
  76. }
  77. });
  78. __toVersion__(0);
  79. assert __version__() == 0;
  80. }
  81. @Test
  82. public void testTypeNarrowingWithField() {
  83. C c = new C(new A());
  84. __toVersion__(1);
  85. __toVersion__(0);
  86. c = new C(convertBtoA(new B()));
  87. TestUtil.assertException(UnsupportedOperationException.class, new Runnable() {
  88. @Override
  89. public void run() {
  90. __toVersion__(1);
  91. }
  92. });
  93. assert __version__() == 0;
  94. c.a = null;
  95. __toVersion__(1);
  96. __toVersion__(0);
  97. }
  98. // Method to enforce cast (otherwise bytecodes become invalid in version 2)
  99. public static A convertBtoA(Object b) {
  100. return (A)b;
  101. }
  102. @Test
  103. public void testTypeNarrowingWithArray() {
  104. final B b = new B();
  105. final A[] arr = new A[3];
  106. arr[0] = new A();
  107. assert b instanceof A;
  108. __toVersion__(1);
  109. assert !(b instanceof A);
  110. TestUtil.assertException(ArrayStoreException.class, new Runnable() {
  111. @Override
  112. public void run() {
  113. arr[1] = b;
  114. }
  115. });
  116. __toVersion__(0);
  117. arr[1] = new B();
  118. TestUtil.assertException(UnsupportedOperationException.class, new Runnable() {
  119. @Override
  120. public void run() {
  121. __toVersion__(1);
  122. }
  123. });
  124. assert __version__() == 0;
  125. assert b instanceof A;
  126. arr[1] = new A();
  127. __toVersion__(1);
  128. assert !(b instanceof A);
  129. __toVersion__(0);
  130. assert b instanceof A;
  131. }
  132. }