/* * @test * @run testng/othervm TestDefenderMethodLookup */ import org.testng.annotations.Test; import org.testng.Assert; import org.testng.AssertJUnit; import java.lang.invoke.*; import java.lang.invoke.MethodHandles.Lookup; @Test(groups = { "level.sanity" }) public class TestDefenderMethodLookup { /** * Same as testDirectSuperInterface, but with the findSpecial arguments target and defc switched. * * @throws Throwable No exceptions is expected. Any exception should be treated as an error. */ @Test public void testDirectSuperInterfaceSwitchedTargetDefc() throws Throwable { DefenderInterface impl = new DefenderInterface() { public MethodHandle run() throws Throwable { Lookup l = MethodHandles.lookup(); Class defc = this.getClass(); Class target = DefenderInterface.class; MethodType mt = MethodType.methodType(String.class); // Switched target and defc return l.findSpecial(target, "test", mt, defc); } }; MethodHandle mh = impl.run(); String result = (String)mh.invoke(impl); AssertJUnit.assertEquals("default", result); } /** * Get a SPECIAL MethodHandle for the method "test()V" in the DIRECT super interface DefenderInterface. The method * has a default implementation in DefenderInterface and does ALSO have an implementation in the class. * Invoke the MethodHandle, and assert that the DefenderInterface.test was invoked (should return "default"). * * @throws Throwable No exceptions is expected. Any exception should be treated as an error. */ @Test public void testDirectSuperInterfaceWithOverride() throws Throwable { DefenderInterface impl = new DefenderInterface() { @Test @Override public String test() { return "impl"; } public MethodHandle run() throws Throwable { Lookup l = DefenderInterface.lookup(); Class defc = DefenderInterface.class; Class target = DefenderInterface.class; MethodType mt = MethodType.methodType(String.class); return l.findSpecial(defc, "test", mt, target); } }; MethodHandle mh = impl.run(); String result = (String)mh.invoke(impl); AssertJUnit.assertEquals("default", result); } } interface DefenderInterface { public default String test() { return "default"; } public static Lookup lookup() { return MethodHandles.lookup(); } public MethodHandle run() throws Throwable; }