1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8140450
  27  * @summary Basic test for Thread.getCallerClass()
  28  * @run main GetCallerClassTest
  29  * @run main/othervm GetCallerClassTest sm
  30  */
  31 
  32 import java.lang.StackWalker.Option;
  33 import java.lang.invoke.MethodHandle;
  34 import java.lang.invoke.MethodHandles;
  35 import java.lang.invoke.MethodType;
  36 import java.lang.reflect.InvocationTargetException;
  37 import java.lang.reflect.Method;
  38 import java.security.Permission;
  39 import java.security.PermissionCollection;
  40 import java.security.Permissions;
  41 import java.security.Policy;
  42 import java.security.ProtectionDomain;
  43 import java.util.Arrays;
  44 import java.util.List;
  45 import java.util.Optional;
  46 
  47 public class GetCallerClassTest {
  48     private static StackWalker stackWalker;
  49     public static void main(String... args) throws Exception {
  50         if (args.length > 0 && args[0].equals("sm")) {
  51             PermissionCollection perms = new Permissions();
  52             perms.add(new StackFramePermission("retainClassReference"));
  53             Policy.setPolicy(new Policy() {
  54                 @Override
  55                 public boolean implies(ProtectionDomain domain, Permission p) {
  56                     return perms.implies(p);
  57                 }
  58             });
  59             System.setSecurityManager(new SecurityManager());
  60         }
  61 
  62         stackWalker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE);
  63         TopLevelCaller.test();
  64         Nested.NestedClassCaller.test();
  65         InnerClassCaller.test();
  66         ReflectionTest.test();
  67 
  68         List<Thread> threads = Arrays.asList(
  69                 new Thread(TopLevelCaller::test),
  70                 new Thread(Nested.NestedClassCaller::test),
  71                 new Thread(InnerClassCaller::test),
  72                 new Thread(ReflectionTest::test)
  73         );
  74         threads.stream().forEach(Thread::start);
  75         threads.stream().forEach(t -> {
  76             try {
  77                 t.join();
  78             } catch (InterruptedException e) {
  79                 throw new RuntimeException(e);
  80             }
  81         });
  82     }
  83 
  84     public static void staticGetCallerClass(Class<?> expected) {
  85         Class<?> c = stackWalker.getCallerClass();
  86         assertEquals(c, expected);
  87     }
  88 
  89     public static void reflectiveGetCallerClass(Class<?> expected) {
  90         try {
  91             Method m = StackWalker.class.getMethod("getCallerClass");
  92             Class<?> c = (Class<?>) m.invoke(stackWalker);
  93             assertEquals(c, expected);
  94         } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
  95             throw new RuntimeException(e);
  96         }
  97     }
  98 
  99     public static void methodHandleGetCallerClass(Class<?> expected) {
 100         MethodHandles.Lookup lookup = MethodHandles.lookup();
 101         try {
 102             MethodHandle mh = lookup.findVirtual(StackWalker.class, "getCallerClass",
 103                                                  MethodType.methodType(Class.class));
 104             Class<?> c = (Class<?>) mh.invokeExact(stackWalker);
 105             assertEquals(c, expected);
 106         } catch (Throwable e) {
 107             throw new RuntimeException(e);
 108         }
 109     }
 110 
 111     public static void assertEquals(Class<?> c, Class<?> expected) {
 112         if (expected != c) {
 113             throw new RuntimeException(c + " != " + expected);
 114         }
 115     }
 116 }
 117 
 118 class TopLevelCaller {
 119     static void test() {
 120         GetCallerClassTest.staticGetCallerClass(TopLevelCaller.class);
 121         GetCallerClassTest.reflectiveGetCallerClass(TopLevelCaller.class);
 122         GetCallerClassTest.methodHandleGetCallerClass(TopLevelCaller.class);
 123     }
 124 }
 125 
 126 class Nested {
 127     static class NestedClassCaller {
 128         static void test() {
 129             GetCallerClassTest.staticGetCallerClass(NestedClassCaller.class);
 130             GetCallerClassTest.reflectiveGetCallerClass(NestedClassCaller.class);
 131             GetCallerClassTest.methodHandleGetCallerClass(NestedClassCaller.class);
 132         }
 133     }
 134 }
 135 
 136 class InnerClassCaller {
 137     static void test() {
 138         new Inner().test();
 139     }
 140     static class Inner {
 141         void test() {
 142             GetCallerClassTest.staticGetCallerClass(Inner.class);
 143             GetCallerClassTest.reflectiveGetCallerClass(Inner.class);
 144             GetCallerClassTest.methodHandleGetCallerClass(Inner.class);
 145         }
 146     }
 147 }
 148 
 149 class ReflectionTest {
 150     static void test() {
 151         callMethodInvoke();
 152         callMethodHandle();
 153     }
 154 
 155     static void callMethodHandle() {
 156         MethodHandles.Lookup lookup = MethodHandles.publicLookup();
 157         try {
 158             MethodHandle mh = lookup.findStatic(GetCallerClassTest.class, "staticGetCallerClass",
 159                                                 MethodType.methodType(void.class, Class.class));
 160             mh.invokeExact(ReflectionTest.class);
 161         } catch (Throwable e) {
 162             throw new RuntimeException(e);
 163         }
 164     }
 165 
 166     static void callMethodInvoke() {
 167         try {
 168             Method m = GetCallerClassTest.class.getMethod("staticGetCallerClass", Class.class);
 169             m.invoke(null, ReflectionTest.class);
 170         } catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException e) {
 171             throw new RuntimeException(e);
 172         }
 173     }
 174 }