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 8297271
27 * @summary Basic tests of AccessFlag
28 * @run junit BasicAccessFlagTest
29 */
30
31 import java.lang.classfile.ClassFile;
32 import java.lang.reflect.AccessFlag;
33 import java.lang.reflect.ClassFileFormatVersion;
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 org.junit.Test;
43 import org.junit.jupiter.api.Assertions;
44
45 import static org.junit.jupiter.api.Assertions.*;
46
47 public class BasicAccessFlagTest {
48
49 /*
50 * Verify sourceModifier() == true access flags have a
51 * corresponding constant in java.lang.reflect.Modifier.
52 */
53 @Test
54 public void testSourceModifiers() throws Exception {
55 Class<?> modifierClass = Modifier.class;
56
57 for(AccessFlag accessFlag : AccessFlag.values()) {
58 if (accessFlag.sourceModifier()) {
59 // Check for consistency
60 Field f = modifierClass.getField(accessFlag.name());
61 assertEquals(f.getInt(null), accessFlag.mask(), accessFlag + " mask");
94 flags.add(accessFlag);
95 maskToFlags.put(mask, flags);
96 } else {
97 flags.add(accessFlag);
98 }
99 }
100
101 // ...then test for disjointness
102 for (var entry : maskToFlags.entrySet()) {
103 var value = entry.getValue();
104 if (value.size() == 0) {
105 throw new AssertionError("Bad flag set " + entry);
106 } else if (value.size() == 1) {
107 // Need at least two flags to be non-disjointness to
108 // be possible
109 continue;
110 }
111
112 Set<AccessFlag.Location> locations = new HashSet<>();
113 for (var accessFlag : value) {
114 for (var location : accessFlag.locations()) {
115 boolean added = locations.add(location);
116 if (!added) {
117 reportError(location, accessFlag,
118 entry.getKey(), value);
119 }
120 }
121 }
122 }
123 }
124
125 private static void reportError(AccessFlag.Location location,
126 AccessFlag accessFlag,
127 Integer mask, Set<AccessFlag> value) {
128 System.err.println("Location " + location +
129 " from " + accessFlag +
130 " already present for 0x" +
131 Integer.toHexString(mask) + ": " + value);
132 throw new RuntimeException();
133 }
134
135 // For each access flag, make sure it is recognized on every kind
136 // of location it can apply to
137 @Test
138 public 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 for (var cffv : ClassFileFormatVersion.values()) {
150 for (var location : accessFlag.locations(cffv)) {
151 Set<AccessFlag> computedSet =
152 AccessFlag.maskToAccessFlags(accessFlag.mask(), location, cffv);
153 if (!expectedSet.equals(computedSet)) {
154 throw new RuntimeException("Bad set computation on " +
155 accessFlag + ", " + location);
156 }
157 }
158 }
159 }
160 assertEquals(Set.of(AccessFlag.STRICT), AccessFlag.maskToAccessFlags(Modifier.STRICT, AccessFlag.Location.METHOD, ClassFileFormatVersion.RELEASE_8));
161 }
162
163 @Test
164 public void testMaskToAccessFlagsNegative() {
|
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 id=Basic
26 * @bug 8266670 8281463 8293626 8297271
27 * @summary Basic tests of AccessFlag
28 * @modules java.base/jdk.internal.misc
29 * @run junit BasicAccessFlagTest
30 */
31
32 /*
33 * @test id=BasicPreview
34 * @bug 8266670 8281463 8293626 8297271
35 * @summary Basic tests of AccessFlag
36 * @modules java.base/jdk.internal.misc
37 * @enablePreview
38 * @run junit BasicAccessFlagTest
39 */
40
41 import java.lang.classfile.ClassFile;
42 import java.lang.reflect.AccessFlag;
43 import java.lang.reflect.ClassFileFormatVersion;
44 import java.lang.reflect.Field;
45 import java.lang.reflect.Modifier;
46 import java.util.EnumSet;
47 import java.util.Map;
48 import java.util.LinkedHashMap;
49 import java.util.HashSet;
50 import java.util.Set;
51
52 import jdk.internal.misc.PreviewFeatures;
53
54
55 import org.junit.Test;
56 import org.junit.jupiter.api.Assertions;
57
58 import static org.junit.jupiter.api.Assertions.*;
59
60 public class BasicAccessFlagTest {
61
62 /*
63 * Verify sourceModifier() == true access flags have a
64 * corresponding constant in java.lang.reflect.Modifier.
65 */
66 @Test
67 public void testSourceModifiers() throws Exception {
68 Class<?> modifierClass = Modifier.class;
69
70 for(AccessFlag accessFlag : AccessFlag.values()) {
71 if (accessFlag.sourceModifier()) {
72 // Check for consistency
73 Field f = modifierClass.getField(accessFlag.name());
74 assertEquals(f.getInt(null), accessFlag.mask(), accessFlag + " mask");
107 flags.add(accessFlag);
108 maskToFlags.put(mask, flags);
109 } else {
110 flags.add(accessFlag);
111 }
112 }
113
114 // ...then test for disjointness
115 for (var entry : maskToFlags.entrySet()) {
116 var value = entry.getValue();
117 if (value.size() == 0) {
118 throw new AssertionError("Bad flag set " + entry);
119 } else if (value.size() == 1) {
120 // Need at least two flags to be non-disjointness to
121 // be possible
122 continue;
123 }
124
125 Set<AccessFlag.Location> locations = new HashSet<>();
126 for (var accessFlag : value) {
127 if (accessFlag.equals(AccessFlag.SUPER))
128 continue; // SUPER is defined to overlap with IDENTITY
129 for (var location : accessFlag.locations()) {
130 boolean added = locations.add(location);
131 if (!added) {
132 reportError(location, accessFlag,
133 entry.getKey(), value);
134 }
135 }
136 }
137 }
138 }
139
140 private static void reportError(AccessFlag.Location location,
141 AccessFlag accessFlag,
142 Integer mask, Set<AccessFlag> value) {
143 System.err.println("Location " + location +
144 " from " + accessFlag +
145 " already present for 0x" +
146 Integer.toHexString(mask) + ": " + value);
147 throw new RuntimeException();
148 }
149
150 // For each access flag, make sure it is recognized on every kind
151 // of location it can apply to
152 @Test
153 public void testMaskToAccessFlagsPositive() {
154 for (var accessFlag : AccessFlag.values()) {
155 Set<AccessFlag> expectedSet = EnumSet.of(accessFlag);
156 for (var location : accessFlag.locations()) {
157 Set<AccessFlag> computedSet =
158 AccessFlag.maskToAccessFlags(accessFlag.mask(), location);
159 if (!computedSet.containsAll(expectedSet)) {
160 System.out.println("expected: " + expectedSet);
161 System.out.println("computed: " + computedSet);
162 throw new RuntimeException("Bad set computation on " +
163 accessFlag + ", " + location);
164 }
165 }
166 for (var cffv : ClassFileFormatVersion.values()) {
167 for (var location : accessFlag.locations(cffv)) {
168 Set<AccessFlag> computedSet =
169 AccessFlag.maskToAccessFlags(accessFlag.mask(), location, cffv);
170 if (!expectedSet.equals(computedSet)) {
171 throw new RuntimeException("Bad set computation on " +
172 accessFlag + ", " + location);
173 }
174 }
175 }
176 }
177 assertEquals(Set.of(AccessFlag.STRICT), AccessFlag.maskToAccessFlags(Modifier.STRICT, AccessFlag.Location.METHOD, ClassFileFormatVersion.RELEASE_8));
178 }
179
180 @Test
181 public void testMaskToAccessFlagsNegative() {
|