1 /*
2 * Copyright (c) 2016, 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 */
89 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE));
90
91 lookup = lookup.dropLookupMode(PACKAGE);
92 assertTrue(lookup.lookupClass() == lc);
93 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE));
94
95 lookup = lookup.dropLookupMode(MODULE);
96 assertTrue(lookup.lookupClass() == lc);
97 assertTrue(lookup.lookupModes() == PUBLIC);
98
99 lookup = lookup.dropLookupMode(PUBLIC);
100 assertTrue(lookup.lookupClass() == lc);
101 assertTrue(lookup.lookupModes() == 0);
102
103 // repeat with lookup has no access
104 lookup = lookup.dropLookupMode(PUBLIC);
105 assertTrue(lookup.lookupClass() == lc);
106 assertTrue(lookup.lookupModes() == 0);
107 }
108
109 /**
110 * Test dropLookupMode on the public Lookup.
111 */
112 public void testPublicLookup() {
113 final Lookup publicLookup = MethodHandles.publicLookup();
114 final Class<?> lc = publicLookup.lookupClass();
115 assertTrue(publicLookup.lookupModes() == (PUBLIC|UNCONDITIONAL));
116
117 Lookup lookup = publicLookup.dropLookupMode(PRIVATE);
118 assertTrue(lookup.lookupClass() == lc);
119 assertTrue(lookup.lookupModes() == PUBLIC);
120
121 lookup = publicLookup.dropLookupMode(PROTECTED);
122 assertTrue(lookup.lookupClass() == lc);
123 assertTrue(lookup.lookupModes() == PUBLIC);
124
125 lookup = publicLookup.dropLookupMode(PACKAGE);
126 assertTrue(lookup.lookupClass() == lc);
127 assertTrue(lookup.lookupModes() == PUBLIC);
128
129 lookup = publicLookup.dropLookupMode(MODULE);
130 assertTrue(lookup.lookupClass() == lc);
131 assertTrue(lookup.lookupModes() == PUBLIC);
132
133 lookup = publicLookup.dropLookupMode(PUBLIC);
134 assertTrue(lookup.lookupClass() == lc);
135 assertTrue(lookup.lookupModes() == 0);
136
137 lookup = publicLookup.dropLookupMode(UNCONDITIONAL);
138 assertTrue(lookup.lookupClass() == lc);
139 assertTrue(lookup.lookupModes() == PUBLIC);
140 }
141
142 @DataProvider(name = "badInput")
143 public Object[][] badInput() {
144 return new Object[][] {
145 { 0, null },
146 { (PACKAGE|PRIVATE), null }, // two modes
147 { Integer.MAX_VALUE, null },
148 { Integer.MIN_VALUE, null },
149 };
150 }
151
152 /**
153 * Check that IllegalArgumentException is thrown for bad input
154 */
155 @Test(dataProvider = "badInput", expectedExceptions = {IllegalArgumentException.class})
156 public void testBadInput(Integer modeToDrop, Object ignore) {
157 MethodHandles.lookup().dropLookupMode(modeToDrop);
158 }
159
|
1 /*
2 * Copyright (c) 2016, 2019, 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 */
89 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE));
90
91 lookup = lookup.dropLookupMode(PACKAGE);
92 assertTrue(lookup.lookupClass() == lc);
93 assertTrue(lookup.lookupModes() == (PUBLIC|MODULE));
94
95 lookup = lookup.dropLookupMode(MODULE);
96 assertTrue(lookup.lookupClass() == lc);
97 assertTrue(lookup.lookupModes() == PUBLIC);
98
99 lookup = lookup.dropLookupMode(PUBLIC);
100 assertTrue(lookup.lookupClass() == lc);
101 assertTrue(lookup.lookupModes() == 0);
102
103 // repeat with lookup has no access
104 lookup = lookup.dropLookupMode(PUBLIC);
105 assertTrue(lookup.lookupClass() == lc);
106 assertTrue(lookup.lookupModes() == 0);
107 }
108
109 @DataProvider(name = "unconditionals")
110 public Object[][] unconditionals() {
111 Lookup publicLookup = MethodHandles.publicLookup();
112 return new Object[][] {
113 { publicLookup, Object.class },
114 { publicLookup.in(String.class), String.class },
115 { publicLookup.in(DropLookupModeTest.class), DropLookupModeTest.class },
116 };
117 }
118
119 /**
120 * Test dropLookupMode on the lookup with public lookup
121 * and UNCONDITIONAL
122 */
123 @Test(dataProvider = "unconditionals")
124 public void testUnconditionalLookup(Lookup unconditionalLookup, Class<?> expected) {
125 assertTrue(unconditionalLookup.lookupModes() == UNCONDITIONAL);
126
127 assertPublicLookup(unconditionalLookup.dropLookupMode(PRIVATE), expected);
128 assertPublicLookup(unconditionalLookup.dropLookupMode(PROTECTED), expected);
129 assertPublicLookup(unconditionalLookup.dropLookupMode(PACKAGE), expected);
130 assertPublicLookup(unconditionalLookup.dropLookupMode(MODULE), expected);
131 assertPublicLookup(unconditionalLookup.dropLookupMode(PUBLIC), expected);
132
133 // drop all access
134 Lookup lookup = unconditionalLookup.dropLookupMode(UNCONDITIONAL);
135 assertTrue(lookup.lookupClass() == expected);
136 assertTrue(lookup.lookupModes() == 0);
137 }
138
139 private void assertPublicLookup(Lookup lookup, Class<?> expected) {
140 assertTrue(lookup.lookupClass() == expected);
141 assertTrue(lookup.lookupModes() == UNCONDITIONAL);
142 }
143
144 @DataProvider(name = "badInput")
145 public Object[][] badInput() {
146 return new Object[][] {
147 { 0, null },
148 { (PACKAGE|PRIVATE), null }, // two modes
149 { Integer.MAX_VALUE, null },
150 { Integer.MIN_VALUE, null },
151 };
152 }
153
154 /**
155 * Check that IllegalArgumentException is thrown for bad input
156 */
157 @Test(dataProvider = "badInput", expectedExceptions = {IllegalArgumentException.class})
158 public void testBadInput(Integer modeToDrop, Object ignore) {
159 MethodHandles.lookup().dropLookupMode(modeToDrop);
160 }
161
|