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.create(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         Optional<Class<?>> c = stackWalker.getCallerClass();
  86         assertTrue(c, expected);
  87     }
  88 
  89     public static void reflectiveGetCallerClass(Class<?> expected) {
  90         try {
  91             Method m = StackWalker.class.getMethod("getCallerClass");
  92             Optional<Class<?>> c = (Optional<Class<?>>) m.invoke(stackWalker);
  93             assertTrue(c, expected);
  94         } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
  95             throw new RuntimeException(e);
  96         }
  97     }
  98 
  99     public static void assertTrue(Optional<Class<?>> c, Class<?> expected) {
 100         if (expected != c.get()) {
 101             throw new RuntimeException(c.get() + " != " + expected);
 102         }
 103     }
 104 }
 105 
 106 class TopLevelCaller {
 107     static void test() {
 108         GetCallerClassTest.staticGetCallerClass(TopLevelCaller.class);
 109         GetCallerClassTest.reflectiveGetCallerClass(TopLevelCaller.class);
 110     }
 111 }
 112 
 113 class Nested {
 114     static class NestedClassCaller {
 115         static void test() {
 116             GetCallerClassTest.staticGetCallerClass(NestedClassCaller.class);
 117             GetCallerClassTest.reflectiveGetCallerClass(NestedClassCaller.class);
 118         }
 119     }
 120 }
 121 
 122 class InnerClassCaller {
 123     static void test() {
 124         new Inner().test();
 125     }
 126     static class Inner {
 127         void test() {
 128             GetCallerClassTest.staticGetCallerClass(Inner.class);
 129             GetCallerClassTest.reflectiveGetCallerClass(Inner.class);
 130         }
 131     }
 132 }
 133 
 134 class ReflectionTest {
 135     static void test() {
 136         callMethodInvoke();
 137         callMethodHandle();
 138     }
 139 
 140     static void callMethodHandle() {
 141         MethodHandles.Lookup lookup = MethodHandles.publicLookup();
 142         try {
 143             MethodHandle mh = lookup.findStatic(GetCallerClassTest.class, "staticGetCallerClass",
 144                                                 MethodType.methodType(void.class, Class.class));
 145             mh.invokeExact(ReflectionTest.class);
 146         } catch (Throwable e) {
 147             throw new RuntimeException(e);
 148         }
 149     }
 150 
 151     static void callMethodInvoke() {
 152         try {
 153             Method m = GetCallerClassTest.class.getMethod("staticGetCallerClass", Class.class);
 154             m.invoke(null, ReflectionTest.class);
 155         } catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException e) {
 156             throw new RuntimeException(e);
 157         }
 158     }
 159 }