--- /dev/null 2015-11-09 17:15:03.000000000 -0800 +++ new/jdk/test/java/lang/StackWalker/GetCallerClassTest.java 2015-11-09 17:15:03.000000000 -0800 @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8140450 + * @summary Basic test for Thread.getCallerClass() + * @run main GetCallerClassTest + * @run main/othervm GetCallerClassTest sm + */ + +import java.lang.StackWalker.Option; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.security.Permission; +import java.security.PermissionCollection; +import java.security.Permissions; +import java.security.Policy; +import java.security.ProtectionDomain; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public class GetCallerClassTest { + private static StackWalker stackWalker; + public static void main(String... args) throws Exception { + if (args.length > 0 && args[0].equals("sm")) { + PermissionCollection perms = new Permissions(); + perms.add(new StackFramePermission("retainClassReference")); + Policy.setPolicy(new Policy() { + @Override + public boolean implies(ProtectionDomain domain, Permission p) { + return perms.implies(p); + } + }); + System.setSecurityManager(new SecurityManager()); + } + + stackWalker = StackWalker.create(Option.RETAIN_CLASS_REFERENCE); + TopLevelCaller.test(); + Nested.NestedClassCaller.test(); + InnerClassCaller.test(); + ReflectionTest.test(); + + List threads = Arrays.asList( + new Thread(TopLevelCaller::test), + new Thread(Nested.NestedClassCaller::test), + new Thread(InnerClassCaller::test), + new Thread(ReflectionTest::test) + ); + threads.stream().forEach(Thread::start); + threads.stream().forEach(t -> { + try { + t.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }); + } + + public static void staticGetCallerClass(Class expected) { + Optional> c = stackWalker.getCallerClass(); + assertTrue(c, expected); + } + + public static void reflectiveGetCallerClass(Class expected) { + try { + Method m = StackWalker.class.getMethod("getCallerClass"); + Optional> c = (Optional>) m.invoke(stackWalker); + assertTrue(c, expected); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } + + public static void assertTrue(Optional> c, Class expected) { + if (expected != c.get()) { + throw new RuntimeException(c.get() + " != " + expected); + } + } +} + +class TopLevelCaller { + static void test() { + GetCallerClassTest.staticGetCallerClass(TopLevelCaller.class); + GetCallerClassTest.reflectiveGetCallerClass(TopLevelCaller.class); + } +} + +class Nested { + static class NestedClassCaller { + static void test() { + GetCallerClassTest.staticGetCallerClass(NestedClassCaller.class); + GetCallerClassTest.reflectiveGetCallerClass(NestedClassCaller.class); + } + } +} + +class InnerClassCaller { + static void test() { + new Inner().test(); + } + static class Inner { + void test() { + GetCallerClassTest.staticGetCallerClass(Inner.class); + GetCallerClassTest.reflectiveGetCallerClass(Inner.class); + } + } +} + +class ReflectionTest { + static void test() { + callMethodInvoke(); + callMethodHandle(); + } + + static void callMethodHandle() { + MethodHandles.Lookup lookup = MethodHandles.publicLookup(); + try { + MethodHandle mh = lookup.findStatic(GetCallerClassTest.class, "staticGetCallerClass", + MethodType.methodType(void.class, Class.class)); + mh.invokeExact(ReflectionTest.class); + } catch (Throwable e) { + throw new RuntimeException(e); + } + } + + static void callMethodInvoke() { + try { + Method m = GetCallerClassTest.class.getMethod("staticGetCallerClass", Class.class); + m.invoke(null, ReflectionTest.class); + } catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException e) { + throw new RuntimeException(e); + } + } +}