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 25 /* 26 * @test 27 * @summary test reflection on value types 28 * @compile -XDenableValueTypes Point.java 29 * @run main/othervm -XX:+EnableValhalla Reflection 30 */ 31 32 import java.lang.reflect.*; 33 34 public class Reflection { 35 public static void main(String... args) throws Exception { 36 Reflection test = new Reflection("Point"); 37 test.newInstance(); 38 test.accessField(); 39 } 40 41 private final Class<?> c; 42 Reflection(String cn) throws Exception { 43 this.c = Class.forName(cn); 44 if (!c.isValue()) { 45 throw new RuntimeException(cn + " is not a value class"); 46 } 47 } 48 49 void accessField() throws Exception { 50 Point o = Point.origin; 51 Field x = c.getField("x"); 52 if (x.getInt(o) != o.x) { 53 throw new RuntimeException("Unexpected Point.x value: " + x.getInt(o)); 54 } 55 56 try { 57 x.setInt(o, 100); 58 throw new RuntimeException("IllegalAccessException not thrown"); 59 } catch (IllegalAccessException e) {} 60 } 61 62 void newInstance() throws Exception { 63 if (c == null) return; 64 65 try { 66 Object o = c.newInstance(); 67 throw new RuntimeException("newInstance expected to be unsupported on value class"); 68 } catch (UnsupportedOperationException e) {} 69 } 70 } | 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 25 /* 26 * @test 27 * @summary test reflection on value types 28 * @compile -XDenableValueTypes Point.java 29 * @run main/othervm -Xverify:none Reflection 30 */ 31 32 import java.lang.reflect.*; 33 34 public class Reflection { 35 public static void main(String... args) throws Exception { 36 Reflection test = new Reflection("Point"); 37 test.newInstance(); 38 test.constructor(); 39 test.accessField(); 40 test.setAccessible(); 41 test.trySetAccessible(); 42 test.staticField(); 43 } 44 45 private final Class<?> c; 46 private final Constructor<?> ctor; 47 private final Field field; 48 Reflection(String cn) throws Exception { 49 this.c = Class.forName(cn); 50 if (!c.isValue()) { 51 throw new RuntimeException(cn + " is not a value class"); 52 } 53 54 this.ctor = Point.class.getDeclaredConstructor(); 55 this.field = c.getField("x"); 56 } 57 58 void accessField() throws Exception { 59 Point o = Point.origin; 60 if (field.getInt(o) != o.x) { 61 throw new RuntimeException("Unexpected Point.x value: " + field.getInt(o)); 62 } 63 64 try { 65 field.setInt(o, 100); 66 throw new RuntimeException("IllegalAccessException not thrown"); 67 } catch (IllegalAccessException e) {} 68 } 69 70 void newInstance() throws Exception { 71 try { 72 Object o = c.newInstance(); 73 throw new RuntimeException("newInstance expected to be unsupported on value class"); 74 } catch (IllegalAccessException e) {} 75 } 76 77 void constructor() throws Exception { 78 try { 79 ctor.newInstance(); 80 throw new RuntimeException("IllegalAccessException not thrown"); 81 } catch (IllegalAccessException e) { } 82 } 83 84 void setAccessible() throws Exception { 85 try { 86 ctor.setAccessible(true); 87 throw new RuntimeException("InaccessibleObjectException not thrown"); 88 } catch (InaccessibleObjectException e) { e.printStackTrace(); } 89 try { 90 field.setAccessible(true); 91 throw new RuntimeException("InaccessibleObjectException not thrown"); 92 } catch (InaccessibleObjectException e) { e.printStackTrace(); } 93 } 94 95 void trySetAccessible() throws Exception { 96 if (ctor.trySetAccessible()) { 97 throw new RuntimeException("trySetAccessible should not succeed"); 98 } 99 if (field.trySetAccessible()) { 100 throw new RuntimeException("trySetAccessible should not succeed"); 101 } 102 } 103 104 void staticField() throws Exception { 105 Field f = Point.class.getDeclaredField("origin"); 106 if (f.trySetAccessible()) { 107 throw new RuntimeException("trySetAccessible should not succeed"); 108 } 109 try { 110 f.setAccessible(true); 111 throw new RuntimeException("IllegalAccessException not thrown"); 112 } catch (InaccessibleObjectException e) { } 113 } 114 } |