Path: blob/master/test/functional/Jsr292/src/com/ibm/j9/jsr292/LookupInTests.java
6007 views
/*******************************************************************************1* Copyright (c) 2001, 2019 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.j9.jsr292;22import org.testng.annotations.Test;23import org.testng.Assert;2425import java.lang.invoke.MethodHandles;26import static java.lang.invoke.MethodHandles.Lookup;27import static java.lang.invoke.MethodHandles.lookup;28import static java.lang.invoke.MethodHandles.publicLookup;2930import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass;31import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2;32import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass2;33import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass2.SamePackageInnerClass2_Nested_Level2;34import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass_Protected;35import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass_Static;3637import examples.PackageExamples;3839/**40*This test case verifies lookup modes being permitted by the Lookup factory object for various Lookup classes (callers)41*using test scenarios involving Lookup classes(callers) candidates of various access restriction enforcement.42*/43public class LookupInTests {4445static final int NO_ACCESS = 0;46static final int PUBLIC_PACKAGE_MODE = MethodHandles.Lookup.PUBLIC | MethodHandles.Lookup.PACKAGE; // 947static final int PUBLIC_PACKAGE_PRIVATE_MODE = MethodHandles.Lookup.PUBLIC | MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PACKAGE; //1148static final int PUBLIC_PACKAGE_PROTECTED_MODE = MethodHandles.Lookup.PUBLIC | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PROTECTED; //1349static final int FULL_ACCESS_MODE = MethodHandles.Lookup.PUBLIC | MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED | MethodHandles.Lookup.PACKAGE; //155051final Lookup localLookup = lookup();52final Lookup localPublicLookup = publicLookup();53final Lookup packageExamplesLookup = PackageExamples.getLookup();54final Lookup samePackageExampleLookup = SamePackageExample.getLookup();55final Lookup samePackageExample2Lookup = SamePackageExample2.getLookup();56final Lookup samePackageExampleSubclassLookup = SamePackageExampleSubclass.getLookup();5758SamePackageExample spe = new SamePackageExample();59SamePackageInnerClass innerClassD = spe.new SamePackageInnerClass();60final Lookup samePackageInnerClassLookup = spe.new SamePackageInnerClass().getLookup();61final Lookup samePackageInnerClass_Nested_Level2Lookup = innerClassD.new SamePackageInnerClass_Nested_Level2().getLookup();6263/******************************************64* Basic Sanity tests for Lookup modes65* ****************************************/6667/**68* Validates public access mode of a Lookup factory created by a call to MethodHandles.publicLookup()69* @throws Throwable70*/71@Test(groups = { "level.extended" })72public void testPublicLookup() throws Throwable {73assertClassAndMode(localPublicLookup, Object.class, MethodHandles.Lookup.PUBLIC);74Assert.assertEquals(localPublicLookup.lookupModes(), MethodHandles.Lookup.PUBLIC);7576Lookup newLookup = localPublicLookup.in(int[].class);77assertClassAndMode(newLookup, int[].class, localPublicLookup.lookupModes());78}7980/**81* Validates access restriction stored in a Lookup factory object that are applied to its own lookup class.82* @throws Throwable83*/84@Test(groups = { "level.extended" })85public void testLookup_inObject_Self() throws Throwable {86Lookup newLookup = localLookup.in(LookupInTests.class);87assertClassAndMode(newLookup, LookupInTests.class, FULL_ACCESS_MODE);88}8990/**91* Validates access restrictions stored in a new Lookup object created from an old Lookup object92* where the new lookup class is java.lang.Object.93* @throws Throwable94*/95@Test(groups = { "level.extended" })96public void testLookup_inObject() throws Throwable {97Lookup inObject = packageExamplesLookup.in(Object.class);98assertClassAndMode(inObject, Object.class, MethodHandles.Lookup.PUBLIC);99}100101/**102* Using an old lookup object that was created from a call to MethodHandles.publicLookup(),103* validates access restrictions stored in a new Lookup object created from an old Lookup object104* where the new lookup class is java.lang.Object.105* @throws Throwable106*/107@Test(groups = { "level.extended" })108public void testLookup_inObject_Using_publicLookup() throws Throwable {109Lookup lookup = PackageExamples.getPublicLookup();110Lookup inObject = lookup.in(int.class);111112/*NOTE:113* This is equivalent of the failing test case above, but this one works.114* The only difference is here we are using MethodHandles.publicLookup()115* instead of MethodHandles.lookup() to obtain the original Lookup object.116* */117assertClassAndMode(inObject, int.class, MethodHandles.Lookup.PUBLIC);118}119120/**121* Validates access restrictions stored in a new Lookup object created from the Lookup object of this class (LookupInTests)122* where the new lookup class is junit.framework.Assert.123* @throws Throwable124*/125@Test(groups = { "level.extended" })126public void testLookup_in_Assert() throws Throwable {127Lookup newLookup = localLookup.in(Assert.class);128assertClassAndMode(newLookup, Assert.class, MethodHandles.Lookup.PUBLIC);129}130131/*******************************************************************************132* Tests involving same package, cross package, super class, subclass lookups133* *****************************************************************************/134135/**136* Validates access restrictions stored in a new Lookup object created from an old Lookup object137* where the new lookup class is a different class but under the same package as the lookup class138* of the original Lookup object.139* @throws Throwable140*/141@Test(groups = { "level.extended" })142public void testLookup_SamePackageLookup() throws Throwable {143Lookup inObject = samePackageExampleLookup.in(SamePackageSingleMethodInterfaceExample.class);144assertClassAndMode(inObject, SamePackageSingleMethodInterfaceExample.class, PUBLIC_PACKAGE_MODE);145}146147/**148* Validates that, if a new lookup class differs from the old one, protected members will not be149* accessible by virtue of inheritance. The test creates a new Lookup object from an old Lookup object150* where the new Lookup class is a subclass of the original lookup class.151* @throws Throwable152*/153@Test(groups = { "level.extended" })154public void testLookup_SamePackageLookup_Subclass() throws Throwable {155Lookup inObject = samePackageExampleLookup.in(SamePackageExampleSubclass.class);156assertClassAndMode(inObject, SamePackageExampleSubclass.class, PUBLIC_PACKAGE_MODE);157}158159/**160* Validates access restrictions stored in a new Lookup object created from an old Lookup object161* where the new lookup class is the super-class of the original Lookup class.162* @throws Throwable163*/164@Test(groups = { "level.extended" })165public void testLookup_SamePackageLookup_Superclass() throws Throwable {166Lookup inObject = samePackageExampleSubclassLookup.in(SamePackageExample.class);167assertClassAndMode(inObject, SamePackageExample.class, FULL_ACCESS_MODE);168}169170/**171* Validates access restrictions stored in a new Lookup object created from an old Lookup object172* where the new lookup class is in a different package than the old lookup class.173* @throws Throwable174*/175@Test(groups = { "level.extended" })176public void testLookup_CrossPackageLookup() throws Throwable {177Lookup inObject = samePackageExampleLookup.in(PackageExamples.class);178assertClassAndMode(inObject, PackageExamples.class, MethodHandles.Lookup.PUBLIC);179}180181/**182* Validates access restrictions stored in a new Lookup object created from an old Lookup object183* where the new lookup class is a subclass of the original lookup class but is residing in a184* different package than the original lookup class.185* @throws Throwable186*/187@Test(groups = { "level.extended" })188public void testLookup_CrossPackageLookup_Subclass() throws Throwable {189Lookup inObject = packageExamplesLookup.in(CrossPackageExampleSubclass.class);190assertClassAndMode(inObject, CrossPackageExampleSubclass.class, MethodHandles.Lookup.PUBLIC);191}192193/**194* Validates access restrictions stored in a new Lookup object created from an old Lookup object195* where the new lookup class is the superclass of the old lookup class but is residing in a196* different package than the old lookup class.197* @throws Throwable198*/199@Test(groups = { "level.extended" })200public void testLookup_CrossPackageLookup_Superclass() throws Throwable {201Lookup lookup = CrossPackageExampleSubclass.getLookup();202Lookup inObject = lookup.in(PackageExamples.class);203assertClassAndMode(inObject, PackageExamples.class, Lookup.PUBLIC);204}205206/**207* Validates access restrictions stored in a new Lookup object created from an old Lookup object208* where the new lookup class is a non-public outer class in the same Java source file as the old lookup class209* @throws Throwable210*/211@Test(groups = { "level.extended" })212public void testLookup_PrivateOuterClassLookup() throws Throwable {213Lookup inObj = localLookup.in(PackageClass.class);214assertClassAndMode(inObj, PackageClass.class, PUBLIC_PACKAGE_PRIVATE_MODE);215}216217/**218* Validates access restrictions stored in a new Lookup object created from an old Lookup object219* where the new lookup class is a non-public outer class belonging to the same package as the old lookup class.220* @throws Throwable221*/222@Test(groups = { "level.extended" })223public void testLookup_SamePackageLookup_PrivateOuterClass() throws Throwable {224Lookup inObj = samePackageExampleLookup.in(PackageClass.class);225assertClassAndMode(inObj, PackageClass.class, PUBLIC_PACKAGE_MODE);226}227228/**229* Validates access restrictions stored in a new Lookup object created from an old Lookup object230* where the new lookup class is a non-public outer class belonging to a different package than the old lookup class.231* @throws Throwable232*/233@Test(groups = { "level.extended" })234public void testLookup_CrossPackageLookup_PrivateOuterClass() throws Throwable {235Lookup inObj = packageExamplesLookup.in(PackageClass.class);236assertClassAndMode(inObj, PackageClass.class, NO_ACCESS);237}238239/***************************************************240* Tests involving public inner classes241* ************************************************/242243/**244* Validates access restrictions stored in a new Lookup object created from an old Lookup object245* where the new lookup class is a public inner class under the old lookup class.246* @throws Throwable247*/248@Test(groups = { "level.extended" })249public void testLookup_PublicInnerClassLookup() throws Throwable {250Lookup inObj = samePackageExampleLookup.in(SamePackageInnerClass.class);251assertClassAndMode(inObj, SamePackageInnerClass.class, PUBLIC_PACKAGE_PRIVATE_MODE);252}253254/**255* Validates access restrictions stored in a new Lookup object created from an old Lookup object256* where the new lookup class is the outer class of the old lookup class.257* @throws Throwable258*/259@Test(groups = { "level.extended" })260public void testLookup_PublicOuterClassLookup() throws Throwable {261Lookup inObj = samePackageInnerClassLookup.in(SamePackageExample.class);262assertClassAndMode(inObj, SamePackageExample.class, PUBLIC_PACKAGE_PRIVATE_MODE);263}264265/**266* Validates access restrictions stored in a new Lookup object created from an old Lookup object267* where the new lookup class is a public inner class under the super-class of the old lookup class.268* @throws Throwable269*/270@Test(groups = { "level.extended" })271public void testLookup_PublicInnerClassLookup_Subclass() throws Throwable {272Lookup inObj = samePackageExampleSubclassLookup.in(SamePackageInnerClass.class);273assertClassAndMode(inObj, SamePackageInnerClass.class, PUBLIC_PACKAGE_PRIVATE_MODE);274}275276/**277* Validates access restrictions stored in a new Lookup object created from an old Lookup object278* where the new lookup class is a public inner class inside a top level class belonging to the same279* package as the old lookup class.280* @throws Throwable281*/282@Test(groups = { "level.extended" })283public void testLookup_PublicInnerClassLookup_SamePackage() throws Throwable {284Lookup inObj = samePackageExample2Lookup.in(SamePackageInnerClass.class);285assertClassAndMode(inObj, SamePackageInnerClass.class, PUBLIC_PACKAGE_MODE);286}287288/**289* Validates access restrictions stored in a new Lookup object created from an old Lookup object290* where the new lookup class is a public inner class inside a top level class belonging to a different291* package than the old lookup class.292* @throws Throwable293*/294@Test(groups = { "level.extended" })295public void testLookup_PublicInnerClassLookup_CrossPackage() throws Throwable {296Lookup inObj = packageExamplesLookup.in(SamePackageInnerClass.class);297assertClassAndMode(inObj, SamePackageInnerClass.class, MethodHandles.Lookup.PUBLIC);298}299300/***************************************************301* Tests involving protected inner classes302* ************************************************/303304/**305* Validates access restrictions stored in a new Lookup object created from an old Lookup object306* where the new lookup class is a protected inner class under the old lookup class.307* @throws Throwable308*/309@Test(groups = { "level.extended" })310public void testLookup_ProtectedInnerClassLookup() throws Throwable {311Lookup inObj = samePackageExampleLookup.in(SamePackageInnerClass_Protected.class);312assertClassAndMode(inObj, SamePackageInnerClass_Protected.class, PUBLIC_PACKAGE_PRIVATE_MODE);313}314315/**316* Validates access restrictions stored in a new Lookup object created from an old Lookup object317* where the new lookup class is the outer class of the original lookup class.318* @throws Throwable319*/320@Test(groups = { "level.extended" })321public void testLookup_ProtectedOuterClassLookup() throws Throwable {322SamePackageExample spe = new SamePackageExample();323Lookup lookup = spe.new SamePackageInnerClass_Protected().getLookup();324Lookup inObj = lookup.in(SamePackageExample.class);325assertClassAndMode(inObj, SamePackageExample.class, PUBLIC_PACKAGE_PRIVATE_MODE);326}327328/**329* Validates access restrictions stored in a new Lookup object created from an old Lookup object330* where the new lookup class is a protected inner class under the super-class of the old lookup class.331* @throws Throwable332*/333@Test(groups = { "level.extended" })334public void testLookup_ProtectedInnerClassLookup_Subclass() throws Throwable {335Lookup inObj = samePackageExampleSubclassLookup.in(SamePackageInnerClass_Protected.class);336assertClassAndMode(inObj, SamePackageInnerClass_Protected.class, PUBLIC_PACKAGE_PRIVATE_MODE);337}338339/**340* Validates access restrictions stored in a new Lookup object created from an old Lookup object341* where the new lookup class is a protected inner class inside a top level class belonging to the same342* package as the old lookup class.343* @throws Throwable344*/345@Test(groups = { "level.extended" })346public void testLookup_ProtectedInnerClassLookup_SamePackage() throws Throwable {347Lookup inObj = samePackageExample2Lookup.in(SamePackageInnerClass_Protected.class);348assertClassAndMode(inObj, SamePackageInnerClass_Protected.class, PUBLIC_PACKAGE_MODE);349}350351/**352* Validates access restrictions stored in a new Lookup object created from an old Lookup object353* where the new lookup class is a protected inner class inside a top level class belonging to a different354* package than the old lookup class.355* Note: the access flag of a protected class is set to public when compiled to a class file.356* @throws Throwable357*/358@Test(groups = { "level.extended" })359public void testLookup_ProtectedInnerClassLookup_CrossPackage() throws Throwable {360Lookup inObj = packageExamplesLookup.in(SamePackageInnerClass_Protected.class);361assertClassAndMode(inObj, SamePackageInnerClass_Protected.class, MethodHandles.Lookup.PUBLIC);362}363364/***************************************************365* Tests involving static inner classes366* ************************************************/367368/**369* Validates access restrictions stored in a new Lookup object created from an old Lookup object370* where the new lookup class is a static inner class under the old lookup class.371* @throws Throwable372*/373@Test(groups = { "level.extended" })374public void testLookup_StaticInnerClassLookup() throws Throwable {375Lookup inObj = samePackageExampleLookup.in(SamePackageInnerClass_Static.class);376assertClassAndMode(inObj, SamePackageInnerClass_Static.class, PUBLIC_PACKAGE_PRIVATE_MODE);377}378379/**380* Validates access restrictions stored in a new Lookup object created from an old Lookup object381* where the new lookup class is the outer class of the original lookup class.382* @throws Throwable383*/384@Test(groups = { "level.extended" })385public void testLookup_StaticdOuterClassLookup() throws Throwable {386Lookup lookup = SamePackageExample.SamePackageInnerClass_Static.getLookup();387Lookup inObj = lookup.in(SamePackageExample.class);388assertClassAndMode(inObj, SamePackageExample.class, PUBLIC_PACKAGE_PRIVATE_MODE);389}390391/**392* Validates access restrictions stored in a new Lookup object created from an old Lookup object393* where the new lookup class is a static inner class under the super-class of the old lookup class.394* @throws Throwable395*/396@Test(groups = { "level.extended" })397public void testLookup_StaticInnerClassLookup_Subclass() throws Throwable {398Lookup inObj = samePackageExampleSubclassLookup.in(SamePackageInnerClass_Static.class);399assertClassAndMode(inObj, SamePackageInnerClass_Static.class, PUBLIC_PACKAGE_PRIVATE_MODE);400}401402/**403* Validates access restrictions stored in a new Lookup object created from an old Lookup object404* where the new lookup class is a static inner class inside a top level class belonging to the same405* package as the old lookup class.406* @throws Throwable407*/408@Test(groups = { "level.extended" })409public void testLookup_StaticInnerClassLookup_SamePackage() throws Throwable {410Lookup inObj = samePackageExample2Lookup.in(SamePackageInnerClass_Static.class);411assertClassAndMode(inObj, SamePackageInnerClass_Static.class, PUBLIC_PACKAGE_MODE);412}413414/**415* Validates access restrictions stored in a new Lookup object created from an old Lookup object416* where the new lookup class is a static inner class inside a top level class belonging to a different417* package than the old lookup class.418* @throws Throwable419*/420@Test(groups = { "level.extended" })421public void testLookup_StaticInnerClassLookup_CrossPackage() throws Throwable {422Lookup inObj = packageExamplesLookup.in(SamePackageInnerClass_Static.class);423assertClassAndMode(inObj, SamePackageInnerClass_Static.class, NO_ACCESS);424}425426/***************************************************427* Tests involving nested public inner classes428* ************************************************/429430/**431* Validates access restrictions stored in a new Lookup object created from an old Lookup object432* where the new lookup class is a second level inner class under the old lookup class which is a first level inner class.433* Basically we validate that a nested class C.D can access private members within another nested class C.D.E.434* @throws Throwable435*/436@Test(groups = { "level.extended" })437public void testLookup_PublicNestedInnerClassLookup_Level1() throws Throwable {438Lookup inObj = samePackageInnerClassLookup.in(SamePackageInnerClass_Nested_Level2.class);439assertClassAndMode(inObj, SamePackageInnerClass_Nested_Level2.class, PUBLIC_PACKAGE_PRIVATE_MODE);440}441442/**443* Validates access restrictions stored in a new Lookup object created from an old Lookup object444* where the new lookup class is a second level inner class under the old lookup class which is the top level outer class.445* Basically we validate that a top level class C can access private members within a nested class C.D.E.446* @throws Throwable447*/448@Test(groups = { "level.extended" })449public void testLookup_PublicNestedInnerClassLookup_Level2() throws Throwable {450Lookup inObj = samePackageExampleLookup.in(SamePackageInnerClass_Nested_Level2.class);451assertClassAndMode(inObj, SamePackageInnerClass_Nested_Level2.class, PUBLIC_PACKAGE_PRIVATE_MODE);452}453454/**455* Validates access restrictions stored in a new Lookup object created from an old Lookup object456* where the new lookup class is an inner class that shares the same outer class as the old lookup class which is457* another inner class. Basically we validate that a nested class C.D can access private members within another458* nested class C.B.459* @throws Throwable460*/461@Test(groups = { "level.extended" })462public void testLookup_PublicInnerClassLookup_ParallelInnerClasses_Level1() throws Throwable {463Lookup inObj = samePackageInnerClassLookup.in(SamePackageInnerClass2.class);464assertClassAndMode(inObj, SamePackageInnerClass2.class, PUBLIC_PACKAGE_PRIVATE_MODE);465}466467/**468* Validates access restrictions stored in a new Lookup object created from an old Lookup object469* where the new lookup class is an second level inner class that shares the same top level outer class as the old lookup class which is470* another second level inner class. Basically we validate that a nested class C.D.E can access private members within another471* nested class C.B.F472* @throws Throwable473*/474@Test(groups = { "level.extended" })475public void testLookup_PublicInnerClassLookup_ParallelInnerClasses_Level2() throws Throwable {476SamePackageExample spe = new SamePackageExample();477SamePackageInnerClass spei_level1 = spe.new SamePackageInnerClass();478SamePackageInnerClass_Nested_Level2 spei_level2 = spei_level1.new SamePackageInnerClass_Nested_Level2();479480Lookup lookup = spei_level2.getLookup();481Lookup inObj = lookup.in(SamePackageInnerClass2_Nested_Level2.class);482assertClassAndMode(inObj, SamePackageInnerClass2_Nested_Level2.class, PUBLIC_PACKAGE_PRIVATE_MODE);483}484485/**486* Validates access restrictions stored in a new Lookup object created from an old Lookup object487* where the new lookup class is a first level inner class on top of the old lookup class which is the second488* level inner class. Basically we validate that a nested class C.D.E can access private members within489* another nested class C.D.490* @throws Throwable491*/492@Test(groups = { "level.extended" })493public void testLookup_PublicNestedOuterClassLookup_Level2() throws Throwable {494Lookup inObj = samePackageInnerClass_Nested_Level2Lookup.in(SamePackageInnerClass.class);495assertClassAndMode(inObj, SamePackageInnerClass.class, PUBLIC_PACKAGE_PRIVATE_MODE);496}497498/**499* Validates access restrictions stored in a new Lookup object created from an old Lookup object500* where the new lookup class is the top level outer class on top of the old lookup class which is the second501* level inner class. Basically we validate that a nested class C.D.E can access private members within502* the top level outer class C.503* @throws Throwable504*/505@Test(groups = { "level.extended" })506public void testLookup_PublicNestedOuterClassLookup_Level1() throws Throwable {507Lookup inObj = samePackageInnerClass_Nested_Level2Lookup.in(SamePackageExample.class);508assertClassAndMode(inObj, SamePackageExample.class, PUBLIC_PACKAGE_PRIVATE_MODE);509}510511512/****************************************************513* Tests involving cross class loaders514* ***************************************************/515516/**517* Validates access restrictions stored in a new Lookup object created from an old Lookup object518* where the new lookup class was loaded using a custom class loader unrelated to the class loader519* that loaded the original lookup class.520* @throws Throwable521*/522@Test(groups = { "level.extended" })523public void testLookup_UnrelatedClassLoaders() throws Throwable {524ParentCustomClassLoader unrelatedClassLoader = new ParentCustomClassLoader(LookupInTests.class.getClassLoader());525Object customLoadedClass = unrelatedClassLoader.loadClass("com.ibm.j9.jsr292.CustomLoadedClass1" ).newInstance();526527Lookup inObject = samePackageExampleLookup.in(customLoadedClass.getClass());528assertClassAndMode(inObject, customLoadedClass.getClass(), MethodHandles.Lookup.PUBLIC);529}530531/**532* Validates access restrictions stored in a new Lookup object created from an old Lookup object533* where the new lookup class was loaded using a custom class loader which is the parent class loader534* of the class loader that loaded the original lookup class.535* @throws Throwable536*/537@Test(groups = { "level.extended" })538public void testLookup_RelatedClassLoaders_ChildLookupInParent() throws Throwable {539ParentCustomClassLoader parentCustomCL = new ParentCustomClassLoader(LookupInTests.class.getClassLoader());540Object customLoadedClass1 = parentCustomCL.loadClass("com.ibm.j9.jsr292.CustomLoadedClass1" ).newInstance();541542Assert.assertTrue(customLoadedClass1.getClass().getClassLoader() instanceof ParentCustomClassLoader);543544ChildCustomClassLoader childCustomCL = new ChildCustomClassLoader(parentCustomCL);545ICustomLoadedClass customLoadedClass2 = (ICustomLoadedClass) childCustomCL.loadClass("com.ibm.j9.jsr292.CustomLoadedClass2" ).newInstance();546547Assert.assertTrue(customLoadedClass2.getClass().getClassLoader() instanceof ChildCustomClassLoader);548549Lookup lookup = customLoadedClass2.getLookup();550Lookup inObject = lookup.in(customLoadedClass1.getClass());551552assertClassAndMode(inObject, customLoadedClass1.getClass(), MethodHandles.Lookup.PUBLIC);553}554555/**556* Validates access restrictions stored in a new Lookup object created from an old Lookup object557* where the new lookup class was loaded using a custom class loader which is the child class loader558* of the class loader that loaded the original lookup class.559* @throws Throwable560*/561@Test(groups = { "level.extended" })562public void testLookup_RelatedClassLoaders_ParentLookupInChild() throws Throwable {563ParentCustomClassLoader parentCustomCL = new ParentCustomClassLoader(LookupInTests.class.getClassLoader());564ICustomLoadedClass customLoadedClass1 = (ICustomLoadedClass) parentCustomCL.loadClass("com.ibm.j9.jsr292.CustomLoadedClass1" ).newInstance();565566Assert.assertTrue(customLoadedClass1.getClass().getClassLoader() instanceof ParentCustomClassLoader);567568ChildCustomClassLoader childCustomCL = new ChildCustomClassLoader(parentCustomCL);569ICustomLoadedClass customLoadedClass2 = (ICustomLoadedClass) childCustomCL.loadClass("com.ibm.j9.jsr292.CustomLoadedClass2" ).newInstance();570571Assert.assertTrue(customLoadedClass2.getClass().getClassLoader() instanceof ChildCustomClassLoader);572573Lookup lookup = customLoadedClass1.getLookup();574Lookup inObject = lookup.in(customLoadedClass2.getClass());575576assertClassAndMode(inObject, customLoadedClass2.getClass(), MethodHandles.Lookup.PUBLIC);577}578579/**580*Test for MethodHandles.Lookup.toString() class where we check output depending on various access modes.581*/582@Test(groups = { "level.extended" })583public void test_Lookup_toString() {584Lookup publicLookup = MethodHandles.publicLookup();585Assert.assertEquals(publicLookup.toString(), "java.lang.Object/public");586587Lookup lookup = PackageExamples.getLookup();588Lookup inObject = lookup.in(Object.class);589Assert.assertEquals(inObject.toString(), "java.lang.Object/public");590591lookup = MethodHandles.lookup();592Lookup inAssertObject = lookup.in(Assert.class);593Assert.assertEquals(inAssertObject.toString(), "org.testng.Assert/public");594595lookup = SamePackageExample.getLookup();596Lookup inCrossPackageClass = lookup.in(PackageExamples.class);597Assert.assertEquals(inCrossPackageClass.toString(), "examples.PackageExamples/public");598599lookup = SamePackageExample.getLookup();600Lookup inSamePackageClass = lookup.in(SamePackageSingleMethodInterfaceExample.class);601Assert.assertEquals(inSamePackageClass.toString(), "com.ibm.j9.jsr292.SamePackageSingleMethodInterfaceExample/package");602603lookup = MethodHandles.lookup();604Lookup inPrivateClass = lookup.in(PackageClass.class);605Assert.assertEquals(inPrivateClass.toString(), "com.ibm.j9.jsr292.LookupInTests$PackageClass/private");606607lookup = MethodHandles.publicLookup();608Lookup inNoAccess = lookup.in(PackageClass.class);609Assert.assertEquals(inNoAccess.toString(), "com.ibm.j9.jsr292.LookupInTests$PackageClass/noaccess");610}611612/**613*Non-public outer class used in tests614*/615class PackageClass { }616617/**618* Helper validation method. Validates the lookup class and lookup modes of the Lookup object being tested.619* @param lookup - Lookup object being tested620* @param c - Expected Lookup class621* @param mode - Expected Lookup Modes622*/623protected void assertClassAndMode(Lookup lookup, Class<?> c, int mode) {624Assert.assertTrue(lookup.lookupClass().equals(c), "Lookup class mismatch. Expected: " + c + ", found: " + lookup.lookupClass());625Assert.assertTrue(lookup.lookupModes() == mode, "Lookup mode mismatch. Expected: " + mode + ", found: " + lookup.lookupModes());626}627}628629630