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