LookupTest.java revision 1786:80120e9b3273
155992Swpaul/* 255992Swpaul * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 355992Swpaul * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 455992Swpaul * 555992Swpaul * This code is free software; you can redistribute it and/or modify it 655992Swpaul * under the terms of the GNU General Public License version 2 only, as 755992Swpaul * published by the Free Software Foundation. Oracle designates this 855992Swpaul * particular file as subject to the "Classpath" exception as provided 955992Swpaul * by Oracle in the LICENSE file that accompanied this code. 1055992Swpaul * 1155992Swpaul * This code is distributed in the hope that it will be useful, but WITHOUT 1255992Swpaul * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1355992Swpaul * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1455992Swpaul * version 2 for more details (a copy is included in the LICENSE file that 1555992Swpaul * accompanied this code). 1655992Swpaul * 1755992Swpaul * You should have received a copy of the GNU General Public License version 1855992Swpaul * 2 along with this work; if not, write to the Free Software Foundation, 1955992Swpaul * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2055992Swpaul * 2155992Swpaul * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2255992Swpaul * or visit www.oracle.com if you need additional information or have any 2355992Swpaul * questions. 2455992Swpaul */ 2555992Swpaul 2655992Swpaulpackage jdk.dynalink.linker.support.test; 2755992Swpaul 2855992Swpaulimport java.lang.invoke.MethodHandle; 2955992Swpaulimport java.lang.invoke.MethodHandles; 3055992Swpaulimport java.lang.invoke.MethodType; 3155992Swpaulimport jdk.dynalink.linker.support.Lookup; 3255992Swpaulimport org.testng.Assert; 3355992Swpaulimport org.testng.annotations.DataProvider; 3455992Swpaulimport org.testng.annotations.Test; 3555992Swpaul 3655992Swpaul// Tests for jdk.dynalink.linker.support.Lookup class. 3755992Swpaul 3855992Swpaulpublic class LookupTest { 3955992Swpaul private static final MethodHandles.Lookup MY_LOOKUP = MethodHandles.lookup(); 4055992Swpaul 4155992Swpaul private static MethodHandles.Lookup getLookup(final boolean publicLookup) { 4255992Swpaul return publicLookup? MethodHandles.publicLookup() : MY_LOOKUP; 4355992Swpaul } 4455992Swpaul 4555992Swpaul // test constructors, methods used for lookup 4655992Swpaul @SuppressWarnings("unused") 4755992Swpaul public LookupTest() {} 4855992Swpaul 4955992Swpaul @SuppressWarnings("unused") 5055992Swpaul private LookupTest(final int unused) {} 5155992Swpaul 5255992Swpaul @SuppressWarnings("unused") 5355992Swpaul private void privateFunc() {} 5455992Swpaul 5555992Swpaul @SuppressWarnings("unused") 5655992Swpaul protected void protectedFunc() {} 5755992Swpaul 5855992Swpaul @SuppressWarnings("unused") 5955992Swpaul private static void privateStaticFunc() {} 6055992Swpaul 6155992Swpaul @SuppressWarnings("unused") 6255992Swpaul private final int myIntField = 0; 6355992Swpaul 6455992Swpaul @SuppressWarnings("unused") 6555992Swpaul @DataProvider 6655992Swpaul private static Object[][] flags() { 6755992Swpaul return new Object[][]{ 6855992Swpaul {Boolean.FALSE}, 6955992Swpaul {Boolean.TRUE} 7055992Swpaul }; 7155992Swpaul } 7255992Swpaul 7355992Swpaul @Test(dataProvider = "flags") 7455992Swpaul public void unreflectTest(final boolean publicLookup) throws NoSuchMethodException { 7555992Swpaul final MethodHandle mh = Lookup.unreflect(getLookup(publicLookup), LookupTest.class.getMethod("unreflectTest", Boolean.TYPE)); 7655992Swpaul Assert.assertNotNull(mh); 7755992Swpaul } 7855992Swpaul 7955992Swpaul @Test 8055992Swpaul public void unreflectTest2() throws NoSuchMethodException { 8155992Swpaul final MethodHandle mh = Lookup.PUBLIC.unreflect(LookupTest.class.getMethod("unreflectTest", Boolean.TYPE)); 8255992Swpaul Assert.assertNotNull(mh); 8355992Swpaul } 8455992Swpaul 8555992Swpaul @Test(dataProvider = "flags") 8655992Swpaul public void unreflectNegativeTest(final boolean publicLookup) throws NoSuchMethodException { 8755992Swpaul try { 8855992Swpaul final MethodHandle mh = Lookup.unreflect(getLookup(publicLookup), 8955992Swpaul LookupTest.class.getDeclaredMethod("privateFunc")); 9055992Swpaul if (publicLookup) { 9155992Swpaul throw new RuntimeException("should have thrown Error"); 9255992Swpaul } 9355992Swpaul Assert.assertNotNull(mh); 9455992Swpaul } catch (final Error err) { 9555992Swpaul Assert.assertTrue(publicLookup); 9655992Swpaul Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 9755992Swpaul } 9855992Swpaul } 9955992Swpaul 10055992Swpaul @Test 10155992Swpaul public void unreflectNegativeTest2() throws NoSuchMethodException { 10255992Swpaul try { 10355992Swpaul Lookup.PUBLIC.unreflect(LookupTest.class.getDeclaredMethod("privateFunc")); 10455992Swpaul throw new RuntimeException("should have thrown Error"); 10555992Swpaul } catch (final Error err) { 10655992Swpaul Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 10755992Swpaul } 10855992Swpaul } 10955992Swpaul 11055992Swpaul @Test(dataProvider = "flags") 11155992Swpaul public void unreflectConstructorTest(final boolean publicLookup) throws NoSuchMethodException { 11255992Swpaul final MethodHandle mh = Lookup.unreflectConstructor(getLookup(publicLookup), LookupTest.class.getConstructor()); 11355992Swpaul Assert.assertNotNull(mh); 11455992Swpaul } 11555992Swpaul 11655992Swpaul @Test 11755992Swpaul public void unreflectConstructorTest2() throws NoSuchMethodException { 11855992Swpaul final MethodHandle mh = Lookup.PUBLIC.unreflectConstructor(LookupTest.class.getConstructor()); 11955992Swpaul Assert.assertNotNull(mh); 12055992Swpaul } 12155992Swpaul 12255992Swpaul @Test(dataProvider = "flags") 12355992Swpaul public void unreflectConstructorNegativeTest(final boolean publicLookup) throws NoSuchMethodException { 12455992Swpaul try { 12555992Swpaul final MethodHandle mh = Lookup.unreflectConstructor(getLookup(publicLookup), 12655992Swpaul LookupTest.class.getDeclaredConstructor(Integer.TYPE)); 12755992Swpaul if (publicLookup) { 12855992Swpaul throw new RuntimeException("should have thrown Error"); 12955992Swpaul } 13055992Swpaul Assert.assertNotNull(mh); 13155992Swpaul } catch (final Error err) { 13255992Swpaul Assert.assertTrue(publicLookup); 13355992Swpaul Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 13455992Swpaul } 13555992Swpaul } 13655992Swpaul 13755992Swpaul @Test 13855992Swpaul public void unreflectConstructorNegativeTest2() throws NoSuchMethodException { 13955992Swpaul try { 14055992Swpaul Lookup.PUBLIC.unreflectConstructor( 14155992Swpaul LookupTest.class.getDeclaredConstructor(Integer.TYPE)); 14255992Swpaul throw new RuntimeException("should have thrown Error"); 14355992Swpaul } catch (final Error err) { 14455992Swpaul Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 14555992Swpaul } 14655992Swpaul } 14755992Swpaul 14855992Swpaul @Test(dataProvider = "flags") 14955992Swpaul public void findOwnStaticTest(final boolean publicLookup) { 15055992Swpaul try { 15155992Swpaul final MethodHandle mh = Lookup.findOwnStatic(getLookup(publicLookup), "getLookup", 15255992Swpaul MethodHandles.Lookup.class, Boolean.TYPE); 15355992Swpaul if (publicLookup) { 15455992Swpaul throw new RuntimeException("should have thrown Error"); 15555992Swpaul } 15655992Swpaul Assert.assertNotNull(mh); 157 } catch (final Error err) { 158 Assert.assertTrue(publicLookup); 159 Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 160 } 161 } 162 163 @Test 164 public void findOwnStaticTest2() { 165 try { 166 Lookup.PUBLIC.findStatic(LookupTest.class, "getLookup", 167 MethodType.methodType(MethodHandles.Lookup.class, Boolean.TYPE)); 168 throw new RuntimeException("should have thrown Error"); 169 } catch (final Error err) { 170 Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 171 } 172 } 173 174 @Test(dataProvider = "flags") 175 public void findOwnSepcialTest(final boolean publicLookup) { 176 try { 177 final MethodHandle mh = Lookup.findOwnSpecial(getLookup(publicLookup), "privateFunc", Void.TYPE); 178 if (publicLookup) { 179 throw new RuntimeException("should have thrown Error"); 180 } 181 Assert.assertNotNull(mh); 182 } catch (final Error err) { 183 Assert.assertTrue(publicLookup); 184 Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 185 } 186 } 187 188 @Test 189 public void findOwnSepcialTest2() { 190 try { 191 Lookup.PUBLIC.findOwnSpecial("privateFunc", Void.TYPE); 192 throw new RuntimeException("should have thrown Error"); 193 } catch (final Error err) { 194 Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 195 } 196 } 197 198 @Test(dataProvider = "flags") 199 public void findGetterTest(final boolean publicLookup) { 200 try { 201 final MethodHandle mh = new Lookup(getLookup(publicLookup)).findGetter(LookupTest.class, "myIntField", Integer.TYPE); 202 if (publicLookup) { 203 throw new RuntimeException("should have thrown Error"); 204 } 205 Assert.assertNotNull(mh); 206 } catch (final Error err) { 207 Assert.assertTrue(publicLookup); 208 Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 209 } 210 } 211 212 @Test 213 public void findGetterTest2() { 214 try { 215 Lookup.PUBLIC.findGetter(LookupTest.class, "myIntField", Integer.TYPE); 216 throw new RuntimeException("should have thrown Error"); 217 } catch (final Error err) { 218 Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 219 } 220 } 221 222 @Test(dataProvider = "flags") 223 public void findVirtualTest(final boolean publicLookup) { 224 try { 225 final MethodHandle mh = new Lookup(getLookup(publicLookup)).findVirtual(LookupTest.class, "protectedFunc", 226 MethodType.methodType(Void.TYPE)); 227 if (publicLookup) { 228 throw new RuntimeException("should have thrown Error"); 229 } 230 Assert.assertNotNull(mh); 231 } catch (final Error err) { 232 Assert.assertTrue(publicLookup); 233 Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 234 } 235 } 236 237 @Test 238 public void findVirtualTest2() { 239 try { 240 Lookup.PUBLIC.findVirtual(LookupTest.class, "protectedFunc", 241 MethodType.methodType(Void.TYPE)); 242 throw new RuntimeException("should have thrown Error"); 243 } catch (final Error err) { 244 Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 245 } 246 } 247 248 @Test(dataProvider = "flags") 249 public void findStaticTest(final boolean publicLookup) { 250 try { 251 final MethodHandle mh = new Lookup(getLookup(publicLookup)).findStatic(LookupTest.class, "privateStaticFunc", 252 MethodType.methodType(Void.TYPE)); 253 if (publicLookup) { 254 throw new RuntimeException("should have thrown Error"); 255 } 256 Assert.assertNotNull(mh); 257 } catch (final Error err) { 258 Assert.assertTrue(publicLookup); 259 Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 260 } 261 } 262 263 @Test 264 public void findStaticTest2() { 265 try { 266 Lookup.PUBLIC.findStatic(LookupTest.class, "privateStaticFunc", 267 MethodType.methodType(Void.TYPE)); 268 throw new RuntimeException("should have thrown Error"); 269 } catch (final Error err) { 270 Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 271 } 272 } 273} 274