LookupTest.java revision 1786:80120e9b3273
1148330Snetchild/* 2148330Snetchild * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3148330Snetchild * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4148330Snetchild * 5148330Snetchild * This code is free software; you can redistribute it and/or modify it 6148330Snetchild * under the terms of the GNU General Public License version 2 only, as 7148330Snetchild * published by the Free Software Foundation. Oracle designates this 8148330Snetchild * particular file as subject to the "Classpath" exception as provided 9148330Snetchild * by Oracle in the LICENSE file that accompanied this code. 10148330Snetchild * 11148330Snetchild * This code is distributed in the hope that it will be useful, but WITHOUT 12148330Snetchild * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13148330Snetchild * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14148543Snetchild * version 2 for more details (a copy is included in the LICENSE file that 15148543Snetchild * accompanied this code). 16148330Snetchild * 17202215Sed * You should have received a copy of the GNU General Public License version 18202215Sed * 2 along with this work; if not, write to the Free Software Foundation, 19202215Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20202215Sed * 21202215Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22202215Sed * or visit www.oracle.com if you need additional information or have any 23202215Sed * questions. 24202215Sed */ 25201546Sdavidxu 26201546Sdavidxupackage jdk.dynalink.linker.support.test; 27201447Santoine 28201447Santoineimport java.lang.invoke.MethodHandle; 29201447Santoineimport java.lang.invoke.MethodHandles; 30201213Straszimport java.lang.invoke.MethodType; 31201213Straszimport jdk.dynalink.linker.support.Lookup; 32201213Straszimport org.testng.Assert; 33201213Straszimport org.testng.annotations.DataProvider; 34201213Straszimport org.testng.annotations.Test; 35201213Strasz 36201213Strasz// Tests for jdk.dynalink.linker.support.Lookup class. 37201213Strasz 38201213Straszpublic class LookupTest { 39201213Strasz private static final MethodHandles.Lookup MY_LOOKUP = MethodHandles.lookup(); 40201213Strasz 41201213Strasz private static MethodHandles.Lookup getLookup(final boolean publicLookup) { 42201213Strasz return publicLookup? MethodHandles.publicLookup() : MY_LOOKUP; 43201213Strasz } 44201213Strasz 45201213Strasz // test constructors, methods used for lookup 46201213Strasz @SuppressWarnings("unused") 47201213Strasz public LookupTest() {} 48201213Strasz 49201213Strasz @SuppressWarnings("unused") 50201213Strasz private LookupTest(final int unused) {} 51201213Strasz 52201213Strasz @SuppressWarnings("unused") 53200028Sume private void privateFunc() {} 54200028Sume 55200028Sume @SuppressWarnings("unused") 56200130Santoine protected void protectedFunc() {} 57199463Sdelphij 58199463Sdelphij @SuppressWarnings("unused") 59200130Santoine private static void privateStaticFunc() {} 60200130Santoine 61198538Skib @SuppressWarnings("unused") 62198538Skib private final int myIntField = 0; 63198443Santoine 64198443Santoine @SuppressWarnings("unused") 65198443Santoine @DataProvider 66197081Sdelphij private static Object[][] flags() { 67197081Sdelphij return new Object[][]{ 68197081Sdelphij {Boolean.FALSE}, 69196787Sremko {Boolean.TRUE} 70196787Sremko }; 71196787Sremko } 72196787Sremko 73196787Sremko @Test(dataProvider = "flags") 74196768Sflz public void unreflectTest(final boolean publicLookup) throws NoSuchMethodException { 75196768Sflz final MethodHandle mh = Lookup.unreflect(getLookup(publicLookup), LookupTest.class.getMethod("unreflectTest", Boolean.TYPE)); 76196768Sflz Assert.assertNotNull(mh); 77196768Sflz } 78198443Santoine 79198443Santoine @Test 80198443Santoine public void unreflectTest2() throws NoSuchMethodException { 81198443Santoine final MethodHandle mh = Lookup.PUBLIC.unreflect(LookupTest.class.getMethod("unreflectTest", Boolean.TYPE)); 82198443Santoine Assert.assertNotNull(mh); 83198443Santoine } 84198443Santoine 85198443Santoine @Test(dataProvider = "flags") 86198443Santoine public void unreflectNegativeTest(final boolean publicLookup) throws NoSuchMethodException { 87198443Santoine try { 88198443Santoine final MethodHandle mh = Lookup.unreflect(getLookup(publicLookup), 89198443Santoine LookupTest.class.getDeclaredMethod("privateFunc")); 90198443Santoine if (publicLookup) { 91198443Santoine throw new RuntimeException("should have thrown Error"); 92198443Santoine } 93198443Santoine Assert.assertNotNull(mh); 94198443Santoine } catch (final Error err) { 95198443Santoine Assert.assertTrue(publicLookup); 96198443Santoine Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 97198443Santoine } 98198443Santoine } 99198443Santoine 100198443Santoine @Test 101198443Santoine public void unreflectNegativeTest2() throws NoSuchMethodException { 102198443Santoine try { 103198443Santoine Lookup.PUBLIC.unreflect(LookupTest.class.getDeclaredMethod("privateFunc")); 104198443Santoine throw new RuntimeException("should have thrown Error"); 105198443Santoine } catch (final Error err) { 106198443Santoine Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 107198443Santoine } 108198443Santoine } 109198443Santoine 110198443Santoine @Test(dataProvider = "flags") 111198443Santoine public void unreflectConstructorTest(final boolean publicLookup) throws NoSuchMethodException { 112198443Santoine final MethodHandle mh = Lookup.unreflectConstructor(getLookup(publicLookup), LookupTest.class.getConstructor()); 113198443Santoine Assert.assertNotNull(mh); 114198443Santoine } 115198443Santoine 116198443Santoine @Test 117198443Santoine public void unreflectConstructorTest2() throws NoSuchMethodException { 118198443Santoine final MethodHandle mh = Lookup.PUBLIC.unreflectConstructor(LookupTest.class.getConstructor()); 119198443Santoine Assert.assertNotNull(mh); 120198443Santoine } 121196019Srwatson 122196019Srwatson @Test(dataProvider = "flags") 123195767Skensmith public void unreflectConstructorNegativeTest(final boolean publicLookup) throws NoSuchMethodException { 124195767Skensmith try { 125195767Skensmith final MethodHandle mh = Lookup.unreflectConstructor(getLookup(publicLookup), 126195767Skensmith LookupTest.class.getDeclaredConstructor(Integer.TYPE)); 127195767Skensmith if (publicLookup) { 128195767Skensmith throw new RuntimeException("should have thrown Error"); 129195767Skensmith } 130195767Skensmith Assert.assertNotNull(mh); 131195767Skensmith } catch (final Error err) { 132195767Skensmith Assert.assertTrue(publicLookup); 133195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 134195767Skensmith } 135195767Skensmith } 136195767Skensmith 137195767Skensmith @Test 138195767Skensmith public void unreflectConstructorNegativeTest2() throws NoSuchMethodException { 139195767Skensmith try { 140195767Skensmith Lookup.PUBLIC.unreflectConstructor( 141195767Skensmith LookupTest.class.getDeclaredConstructor(Integer.TYPE)); 142195767Skensmith throw new RuntimeException("should have thrown Error"); 143195767Skensmith } catch (final Error err) { 144195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 145195767Skensmith } 146195767Skensmith } 147195767Skensmith 148195767Skensmith @Test(dataProvider = "flags") 149195767Skensmith public void findOwnStaticTest(final boolean publicLookup) { 150195767Skensmith try { 151195767Skensmith final MethodHandle mh = Lookup.findOwnStatic(getLookup(publicLookup), "getLookup", 152195767Skensmith MethodHandles.Lookup.class, Boolean.TYPE); 153195767Skensmith if (publicLookup) { 154195767Skensmith throw new RuntimeException("should have thrown Error"); 155195767Skensmith } 156195767Skensmith Assert.assertNotNull(mh); 157195767Skensmith } catch (final Error err) { 158195767Skensmith Assert.assertTrue(publicLookup); 159195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 160195767Skensmith } 161195767Skensmith } 162195767Skensmith 163195767Skensmith @Test 164195767Skensmith public void findOwnStaticTest2() { 165195767Skensmith try { 166195767Skensmith Lookup.PUBLIC.findStatic(LookupTest.class, "getLookup", 167195767Skensmith MethodType.methodType(MethodHandles.Lookup.class, Boolean.TYPE)); 168195767Skensmith throw new RuntimeException("should have thrown Error"); 169195767Skensmith } catch (final Error err) { 170195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 171195767Skensmith } 172195767Skensmith } 173195767Skensmith 174195767Skensmith @Test(dataProvider = "flags") 175195767Skensmith public void findOwnSepcialTest(final boolean publicLookup) { 176195767Skensmith try { 177195767Skensmith final MethodHandle mh = Lookup.findOwnSpecial(getLookup(publicLookup), "privateFunc", Void.TYPE); 178195767Skensmith if (publicLookup) { 179195767Skensmith throw new RuntimeException("should have thrown Error"); 180195767Skensmith } 181195767Skensmith Assert.assertNotNull(mh); 182195767Skensmith } catch (final Error err) { 183195767Skensmith Assert.assertTrue(publicLookup); 184195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 185195767Skensmith } 186195767Skensmith } 187195767Skensmith 188195767Skensmith @Test 189195767Skensmith public void findOwnSepcialTest2() { 190195767Skensmith try { 191195767Skensmith Lookup.PUBLIC.findOwnSpecial("privateFunc", Void.TYPE); 192195767Skensmith throw new RuntimeException("should have thrown Error"); 193195767Skensmith } catch (final Error err) { 194195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 195195767Skensmith } 196195767Skensmith } 197195789Santoine 198195767Skensmith @Test(dataProvider = "flags") 199195767Skensmith public void findGetterTest(final boolean publicLookup) { 200195767Skensmith try { 201195767Skensmith final MethodHandle mh = new Lookup(getLookup(publicLookup)).findGetter(LookupTest.class, "myIntField", Integer.TYPE); 202195767Skensmith if (publicLookup) { 203195767Skensmith throw new RuntimeException("should have thrown Error"); 204195767Skensmith } 205195767Skensmith Assert.assertNotNull(mh); 206195767Skensmith } catch (final Error err) { 207195767Skensmith Assert.assertTrue(publicLookup); 208195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 209195767Skensmith } 210195767Skensmith } 211195767Skensmith 212195767Skensmith @Test 213195767Skensmith public void findGetterTest2() { 214195767Skensmith try { 215195767Skensmith Lookup.PUBLIC.findGetter(LookupTest.class, "myIntField", Integer.TYPE); 216195767Skensmith throw new RuntimeException("should have thrown Error"); 217195767Skensmith } catch (final Error err) { 218195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 219195767Skensmith } 220195767Skensmith } 221195767Skensmith 222195767Skensmith @Test(dataProvider = "flags") 223195767Skensmith public void findVirtualTest(final boolean publicLookup) { 224195767Skensmith try { 225195767Skensmith final MethodHandle mh = new Lookup(getLookup(publicLookup)).findVirtual(LookupTest.class, "protectedFunc", 226195767Skensmith MethodType.methodType(Void.TYPE)); 227195767Skensmith if (publicLookup) { 228195767Skensmith throw new RuntimeException("should have thrown Error"); 229195767Skensmith } 230195767Skensmith Assert.assertNotNull(mh); 231195767Skensmith } catch (final Error err) { 232195767Skensmith Assert.assertTrue(publicLookup); 233195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 234195767Skensmith } 235195767Skensmith } 236195767Skensmith 237195767Skensmith @Test 238195767Skensmith public void findVirtualTest2() { 239195767Skensmith try { 240195767Skensmith Lookup.PUBLIC.findVirtual(LookupTest.class, "protectedFunc", 241195767Skensmith MethodType.methodType(Void.TYPE)); 242195767Skensmith throw new RuntimeException("should have thrown Error"); 243195767Skensmith } catch (final Error err) { 244195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 245195767Skensmith } 246195767Skensmith } 247195767Skensmith 248195767Skensmith @Test(dataProvider = "flags") 249195767Skensmith public void findStaticTest(final boolean publicLookup) { 250195767Skensmith try { 251195767Skensmith final MethodHandle mh = new Lookup(getLookup(publicLookup)).findStatic(LookupTest.class, "privateStaticFunc", 252195767Skensmith MethodType.methodType(Void.TYPE)); 253195767Skensmith if (publicLookup) { 254195767Skensmith throw new RuntimeException("should have thrown Error"); 255195767Skensmith } 256195767Skensmith Assert.assertNotNull(mh); 257195767Skensmith } catch (final Error err) { 258195767Skensmith Assert.assertTrue(publicLookup); 259195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 260195767Skensmith } 261195767Skensmith } 262195767Skensmith 263195767Skensmith @Test 264195767Skensmith public void findStaticTest2() { 265195767Skensmith try { 266195767Skensmith Lookup.PUBLIC.findStatic(LookupTest.class, "privateStaticFunc", 267195767Skensmith MethodType.methodType(Void.TYPE)); 268195767Skensmith throw new RuntimeException("should have thrown Error"); 269195767Skensmith } catch (final Error err) { 270195767Skensmith Assert.assertTrue(err instanceof NoSuchMethodError || err instanceof IllegalAccessError); 271195767Skensmith } 272195767Skensmith } 273195767Skensmith} 274195767Skensmith