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.

StaticFieldHandleTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 com.github.dcevm.test.TestUtil;
  26. import org.junit.After;
  27. import org.junit.Assert;
  28. import org.junit.Before;
  29. import org.junit.Ignore;
  30. import org.junit.Test;
  31. import java.lang.invoke.MethodHandle;
  32. import java.lang.invoke.MethodHandles;
  33. import java.lang.invoke.MethodType;
  34. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  35. import static com.github.dcevm.test.util.HotSwapTestHelper.__version__;
  36. import static org.junit.Assert.assertEquals;
  37. /**
  38. * Test for replacing field with MethodHandle pointing to it.
  39. *
  40. * @author Ivan Dubrov
  41. */
  42. public class StaticFieldHandleTest {
  43. // Version 0
  44. public static class A {
  45. public static int fieldA;
  46. public static int fieldB;
  47. }
  48. // Version 1 (fields swapped)
  49. public static class A___1 {
  50. public static int fieldB;
  51. public static int fieldA;
  52. }
  53. // Version 2 (fields removed)
  54. public static class A___2 {
  55. }
  56. // Version 3 (field type changed)
  57. public static class A___3 {
  58. public String fieldA;
  59. public int fieldB;
  60. }
  61. @Before
  62. @After
  63. public void setUp() throws Exception {
  64. __toVersion__(0);
  65. }
  66. @Test
  67. public void testStaticFieldChangeOrder() throws Throwable {
  68. MethodHandle handle = MethodHandles.publicLookup().findStaticGetter(A.class, "fieldA", int.class);
  69. A.fieldA = 3;
  70. A.fieldB = 5;
  71. assertEquals(3, handle.invoke());
  72. // Swap fields A and B
  73. __toVersion__(1);
  74. assertEquals(3, handle.invoke());
  75. }
  76. @Test
  77. @Ignore
  78. public void testStaticFieldRemoved() throws Throwable {
  79. MethodHandle handle = MethodHandles.publicLookup().findStaticGetter(A.class, "fieldA", int.class);
  80. A.fieldA = 3;
  81. A.fieldB = 5;
  82. assertEquals(3, handle.invoke());
  83. // Remove fieldA
  84. __toVersion__(2);
  85. handle.invoke();
  86. }
  87. @Test
  88. @Ignore
  89. public void testStaticFieldTypeChange() throws Throwable {
  90. MethodHandle handle = MethodHandles.publicLookup().findStaticGetter(A.class, "fieldA", int.class);
  91. A.fieldA = 3;
  92. A.fieldB = 5;
  93. assertEquals(3, handle.invoke());
  94. // Remove fieldA
  95. __toVersion__(3);
  96. handle.invoke();
  97. }
  98. }