< prev index next >

test/jdk/java/lang/reflect/AccessFlag/BasicAccessFlagTest.java

Print this page

  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());

 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 }

  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());

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 }
< prev index next >