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.

InstanceFieldHandleTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.fields;
  25. import org.junit.After;
  26. import org.junit.Assert;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import java.lang.invoke.MethodHandle;
  30. import java.lang.invoke.MethodHandles;
  31. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  32. import static org.junit.Assert.assertEquals;
  33. /**
  34. * Test for replacing field with MethodHandle pointing to it.
  35. *
  36. * Technically, should work for Java 7, but currently is not supported in Java 7.
  37. *
  38. * @author Ivan Dubrov
  39. */
  40. public class InstanceFieldHandleTest {
  41. // Version 0
  42. public static class A {
  43. public int fieldA;
  44. public int fieldB;
  45. public int getFieldA() {
  46. return -1;
  47. }
  48. }
  49. // Version 1 (fields swapped and new one is added)
  50. public static class A___1 {
  51. public int fieldB;
  52. public int fieldA;
  53. public String fieldC;
  54. public int getFieldA() {
  55. return fieldA;
  56. }
  57. }
  58. // Version 2 (fields removed)
  59. public static class A___2 {
  60. }
  61. // Version 3 (field type changed)
  62. public static class A___3 {
  63. public String fieldA;
  64. public int fieldB;
  65. }
  66. @Before
  67. @After
  68. public void setUp() throws Exception {
  69. __toVersion__(0);
  70. }
  71. @Test
  72. public void testFieldChangeOrder() throws Throwable {
  73. A a = new A();
  74. MethodHandle getter = MethodHandles.publicLookup().findGetter(A.class, "fieldA", int.class);
  75. MethodHandle setter = MethodHandles.publicLookup().findSetter(A.class, "fieldA", int.class);
  76. a.fieldA = 3;
  77. assertEquals(3, getter.invoke(a));
  78. // Swap fields
  79. __toVersion__(1);
  80. assertEquals(3, getter.invoke(a));
  81. setter.invoke(a, 53);
  82. assertEquals(53, a.getFieldA());
  83. assertEquals(53, getter.invoke(a));
  84. }
  85. @Test
  86. public void testFieldRemoved() throws Throwable {
  87. A a = new A();
  88. MethodHandle getter = MethodHandles.publicLookup().findGetter(A.class, "fieldA", int.class);
  89. MethodHandle setter = MethodHandles.publicLookup().findSetter(A.class, "fieldA", int.class);
  90. a.fieldA = 3;
  91. assertEquals(3, getter.invoke(a));
  92. // Remove fieldA
  93. __toVersion__(2);
  94. try {
  95. getter.invoke(a);
  96. Assert.fail("Handle should have been cleared!");
  97. } catch (NullPointerException e) {
  98. // Handle was cleared!
  99. }
  100. try {
  101. setter.invoke(a, 10);
  102. Assert.fail("Handle should have been cleared!");
  103. } catch (NullPointerException e) {
  104. // Handle was cleared!
  105. }
  106. }
  107. @Test
  108. public void testFieldTypeChange() throws Throwable {
  109. A a = new A();
  110. MethodHandle getter = MethodHandles.publicLookup().findGetter(A.class, "fieldA", int.class);
  111. MethodHandle setter = MethodHandles.publicLookup().findSetter(A.class, "fieldA", int.class);
  112. a.fieldA = 3;
  113. assertEquals(3, getter.invoke(a));
  114. // Remove fieldA
  115. __toVersion__(3);
  116. try {
  117. getter.invoke(a);
  118. Assert.fail("Handle should have been cleared!");
  119. } catch (NullPointerException e) {
  120. // Handle was cleared!
  121. }
  122. try {
  123. setter.invoke(a, 10);
  124. Assert.fail("Handle should have been cleared!");
  125. } catch (NullPointerException e) {
  126. // Handle was cleared!
  127. }
  128. }
  129. }