1 /* 2 * Copyright (c) 2022, 2024, 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 /* 25 * @test 26 * @bug 8266670 8281463 8293626 27 * @summary Basic tests of AccessFlag 28 * @modules java.base/jdk.internal.misc 29 * @run main/othervm --enable-preview BasicAccessFlagTest 30 * @run main BasicAccessFlagTest 31 */ 32 33 import java.lang.reflect.AccessFlag; 34 import java.lang.reflect.Field; 35 import java.lang.reflect.Modifier; 36 import java.util.EnumSet; 37 import java.util.Map; 38 import java.util.LinkedHashMap; 39 import java.util.HashSet; 40 import java.util.Set; 41 42 import jdk.internal.misc.PreviewFeatures; 43 44 45 public class BasicAccessFlagTest { 46 public static void main(String... args) throws Exception { 47 testSourceModifiers(); 48 testMaskOrdering(); 49 testDisjoint(); 50 testMaskToAccessFlagsPositive(); 51 testLocationsNullHandling(); 52 } 53 54 /* 55 * Verify sourceModifier() == true access flags have a 56 * corresponding constant in java.lang.reflect.Modifier. 57 */ 58 private static void testSourceModifiers() throws Exception { 59 Class<?> modifierClass = Modifier.class; 60 61 for(AccessFlag accessFlag : AccessFlag.values()) { 62 if (accessFlag.sourceModifier()) { 63 // Check for consistency 64 Field f = modifierClass.getField(accessFlag.name()); 65 if (accessFlag.mask() != f.getInt(null) ) { 66 throw new RuntimeException("Unexpected mask for " + 67 accessFlag); 68 } 69 } 70 } 71 } 72 73 // The mask values of the enum constants must be non-decreasing; 74 // in other words stay the same (for colliding mask values) or go 75 // up. 76 private static void testMaskOrdering() { 77 AccessFlag[] values = AccessFlag.values(); 78 for (int i = 1; i < values.length; i++) { 79 AccessFlag left = values[i-1]; 80 AccessFlag right = values[i]; 81 if (left.mask() > right.mask()) { 82 throw new RuntimeException(left 83 + "has a greater mask than " 84 + right); 85 } 86 } 87 } 88 89 // Test that if access flags have a matching mask, their locations 90 // are disjoint. 91 private static void testDisjoint() { 92 // First build the mask -> access flags map... 93 Map<Integer, Set<AccessFlag>> maskToFlags = new LinkedHashMap<>(); 94 95 for (var accessFlag : AccessFlag.values()) { 96 Integer mask = accessFlag.mask(); 97 Set<AccessFlag> flags = maskToFlags.get(mask); 98 99 if (flags == null ) { 100 flags = new HashSet<>(); 101 flags.add(accessFlag); 102 maskToFlags.put(mask, flags); 103 } else { 104 flags.add(accessFlag); 105 } 106 } 107 108 // ...then test for disjointness 109 for (var entry : maskToFlags.entrySet()) { 110 var value = entry.getValue(); 111 if (value.size() == 0) { 112 throw new AssertionError("Bad flag set " + entry); 113 } else if (value.size() == 1) { 114 // Need at least two flags to be non-disjointness to 115 // be possible 116 continue; 117 } 118 119 Set<AccessFlag.Location> locations = new HashSet<>(); 120 for (var accessFlag : value) { 121 if (accessFlag.equals(AccessFlag.SUPER)) 122 continue; // SUPER is defined to overlap with IDENTITY 123 for (var location : accessFlag.locations()) { 124 boolean added = locations.add(location); 125 if (!added) { 126 reportError(location, accessFlag, 127 entry.getKey(), value); 128 } 129 } 130 } 131 } 132 } 133 134 private static void reportError(AccessFlag.Location location, 135 AccessFlag accessFlag, 136 Integer mask, Set<AccessFlag> value) { 137 System.err.println("Location " + location + 138 " from " + accessFlag + 139 " already present for 0x" + 140 Integer.toHexString(mask) + ": " + value); 141 throw new RuntimeException(); 142 } 143 144 // For each access flag, make sure it is recognized on every kind 145 // of location it can apply to 146 private static void testMaskToAccessFlagsPositive() { 147 for (var accessFlag : AccessFlag.values()) { 148 Set<AccessFlag> expectedSet = EnumSet.of(accessFlag); 149 for (var location : accessFlag.locations()) { 150 Set<AccessFlag> computedSet = 151 AccessFlag.maskToAccessFlags(accessFlag.mask(), location); 152 if (!computedSet.containsAll(expectedSet)) { 153 System.out.println("expected: " + expectedSet); 154 System.out.println("computed: " + computedSet); 155 throw new RuntimeException("Bad set computation on " + 156 accessFlag + ", " + location); 157 } 158 } 159 } 160 } 161 162 private static void testLocationsNullHandling() { 163 for (var flag : AccessFlag.values() ) { 164 try { 165 flag.locations(null); 166 throw new RuntimeException("Did not get NPE on " + flag + ".location(null)"); 167 } catch (NullPointerException npe ) { 168 ; // Expected 169 } 170 } 171 } 172 }