|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +import org.testng.annotations.DataProvider; |
| 25 | +import org.testng.annotations.Test; |
| 26 | + |
| 27 | +import java.lang.constant.ClassDesc; |
| 28 | +import java.lang.constant.ConstantDesc; |
| 29 | +import java.lang.constant.ConstantDescs; |
| 30 | +import java.lang.constant.DirectMethodHandleDesc; |
| 31 | +import java.lang.constant.DynamicCallSiteDesc; |
| 32 | +import java.lang.constant.DynamicConstantDesc; |
| 33 | +import java.lang.constant.MethodHandleDesc; |
| 34 | +import java.lang.constant.MethodTypeDesc; |
| 35 | +import java.lang.invoke.CallSite; |
| 36 | +import java.lang.invoke.ConstantBootstraps; |
| 37 | +import java.lang.invoke.MethodHandle; |
| 38 | +import java.lang.invoke.MethodHandles; |
| 39 | +import java.lang.invoke.MethodType; |
| 40 | +import java.lang.invoke.VarHandle; |
| 41 | +import java.lang.reflect.AccessFlag; |
| 42 | +import java.lang.reflect.Field; |
| 43 | +import java.util.Collection; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.Set; |
| 47 | +import java.util.stream.Stream; |
| 48 | + |
| 49 | +import static java.lang.constant.ConstantDescs.*; |
| 50 | +import static org.testng.Assert.assertEquals; |
| 51 | + |
| 52 | +/* |
| 53 | + * @test |
| 54 | + * @compile ConstantDescsTest.java |
| 55 | + * @run testng ConstantDescsTest |
| 56 | + * @summary unit tests for java.lang.constant.ConstantDescs |
| 57 | + */ |
| 58 | +public class ConstantDescsTest { |
| 59 | + |
| 60 | + @DataProvider(name = "validateFields") |
| 61 | + public Object[][] knownFieldsData() { |
| 62 | + return new Object[][]{ |
| 63 | + {CD_Object, Object.class}, |
| 64 | + {CD_String, String.class}, |
| 65 | + {CD_Class, Class.class}, |
| 66 | + {CD_Number, Number.class}, |
| 67 | + {CD_Integer, Integer.class}, |
| 68 | + {CD_Long, Long.class}, |
| 69 | + {CD_Float, Float.class}, |
| 70 | + {CD_Double, Double.class}, |
| 71 | + {CD_Short, Short.class}, |
| 72 | + {CD_Byte, Byte.class}, |
| 73 | + {CD_Character, Character.class}, |
| 74 | + {CD_Boolean, Boolean.class}, |
| 75 | + {CD_Void, Void.class}, |
| 76 | + {CD_Exception, Exception.class}, |
| 77 | + {CD_Throwable, Throwable.class}, |
| 78 | + {CD_Enum, Enum.class}, |
| 79 | + {CD_VarHandle, VarHandle.class}, |
| 80 | + {CD_MethodHandles, MethodHandles.class}, |
| 81 | + {CD_MethodHandles_Lookup, MethodHandles.Lookup.class}, |
| 82 | + {CD_MethodHandle, MethodHandle.class}, |
| 83 | + {CD_MethodType, MethodType.class}, |
| 84 | + {CD_CallSite, CallSite.class}, |
| 85 | + {CD_Collection, Collection.class}, |
| 86 | + {CD_List, List.class}, |
| 87 | + {CD_Set, Set.class}, |
| 88 | + {CD_Map, Map.class}, |
| 89 | + {CD_ConstantDesc, ConstantDesc.class}, |
| 90 | + {CD_ClassDesc, ClassDesc.class}, |
| 91 | + {CD_EnumDesc, Enum.EnumDesc.class}, |
| 92 | + {CD_MethodTypeDesc, MethodTypeDesc.class}, |
| 93 | + {CD_MethodHandleDesc, MethodHandleDesc.class}, |
| 94 | + {CD_DirectMethodHandleDesc, DirectMethodHandleDesc.class}, |
| 95 | + {CD_VarHandleDesc, VarHandle.VarHandleDesc.class}, |
| 96 | + {CD_MethodHandleDesc_Kind, DirectMethodHandleDesc.Kind.class}, |
| 97 | + {CD_DynamicConstantDesc, DynamicConstantDesc.class}, |
| 98 | + {CD_DynamicCallSiteDesc, DynamicCallSiteDesc.class}, |
| 99 | + {CD_ConstantBootstraps, ConstantBootstraps.class}, |
| 100 | + {CD_int, int.class}, |
| 101 | + {CD_long, long.class}, |
| 102 | + {CD_float, float.class}, |
| 103 | + {CD_double, double.class}, |
| 104 | + {CD_short, short.class}, |
| 105 | + {CD_byte, byte.class}, |
| 106 | + {CD_char, char.class}, |
| 107 | + {CD_boolean, boolean.class}, |
| 108 | + {CD_void, void.class}, |
| 109 | + {NULL, null}, |
| 110 | + {TRUE, Boolean.TRUE}, |
| 111 | + {FALSE, Boolean.FALSE}, |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Checks that ConstantDescs descriptor fields resolve to the right |
| 117 | + * constants. |
| 118 | + * @throws ReflectiveOperationException if the test fails |
| 119 | + */ |
| 120 | + @Test(dataProvider = "validateFields") |
| 121 | + public void validateFields(ConstantDesc desc, Object value) throws ReflectiveOperationException { |
| 122 | + // Use a minimally-trusted lookup |
| 123 | + assertEquals(desc.resolveConstantDesc(MethodHandles.publicLookup()), value); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Ensures all public static final descriptor fields in ConstantDescs |
| 128 | + * are resolvable. |
| 129 | + * @throws ReflectiveOperationException if the test fails |
| 130 | + */ |
| 131 | + @Test |
| 132 | + public void checkFieldsResolvable() throws ReflectiveOperationException { |
| 133 | + // minimally trusted lookup |
| 134 | + var lookup = MethodHandles.publicLookup(); |
| 135 | + var fields = Stream.of(ConstantDescs.class.getFields()) |
| 136 | + .filter(f -> f.accessFlags().contains(AccessFlag.STATIC) |
| 137 | + && ConstantDesc.class.isAssignableFrom(f.getType())) |
| 138 | + .toArray(Field[]::new); |
| 139 | + for (var field : fields) { |
| 140 | + var desc = (ConstantDesc) field.get(null); |
| 141 | + desc.resolveConstantDesc(lookup); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments