Path: blob/master/test/jdk/java/lang/StackWalker/GetCallerClassTest.java
41149 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8140450 8152893 818929126* @summary Basic test for StackWalker.getCallerClass()27* @run main/othervm GetCallerClassTest28* @run main/othervm -Djava.security.manager=allow GetCallerClassTest sm29*/3031import static java.lang.StackWalker.Option.*;32import java.lang.invoke.MethodHandle;33import java.lang.invoke.MethodHandles;34import java.lang.invoke.MethodType;35import java.lang.reflect.InvocationTargetException;36import java.lang.reflect.Method;37import java.security.Permission;38import java.security.PermissionCollection;39import java.security.Permissions;40import java.security.Policy;41import java.security.ProtectionDomain;42import java.util.Arrays;43import java.util.EnumSet;44import java.util.List;4546public class GetCallerClassTest {47static final Policy DEFAULT_POLICY = Policy.getPolicy();48private final StackWalker walker;49private final boolean expectUOE;5051public GetCallerClassTest(StackWalker sw, boolean expect) {52this.walker = sw;53this.expectUOE = expect;54}55public static void main(String... args) throws Exception {56if (args.length > 0 && args[0].equals("sm")) {57PermissionCollection perms = new Permissions();58perms.add(new RuntimePermission("getStackWalkerWithClassReference"));59Policy.setPolicy(new Policy() {60@Override61public boolean implies(ProtectionDomain domain, Permission p) {62return perms.implies(p) ||63DEFAULT_POLICY.implies(domain, p);64}65});66System.setSecurityManager(new SecurityManager());67}68new GetCallerClassTest(StackWalker.getInstance(), true).test();69new GetCallerClassTest(StackWalker.getInstance(RETAIN_CLASS_REFERENCE), false).test();70new GetCallerClassTest(StackWalker.getInstance(EnumSet.of(RETAIN_CLASS_REFERENCE,71SHOW_HIDDEN_FRAMES)), false).test();72}7374public void test() {75new TopLevelCaller().run();76new LambdaTest().run();77new Nested().createNestedCaller().run();78new InnerClassCaller().run();79new ReflectionTest().run();8081List<Thread> threads = Arrays.asList(82new Thread(new TopLevelCaller()),83new Thread(new LambdaTest()),84new Thread(new Nested().createNestedCaller()),85new Thread(new InnerClassCaller()),86new Thread(new ReflectionTest())87);88threads.stream().forEach(Thread::start);89threads.stream().forEach(t -> {90try {91t.join();92} catch (InterruptedException e) {93throw new RuntimeException(e);94}95});96}9798public static void staticGetCallerClass(StackWalker stackWalker,99Class<?> expected,100boolean expectUOE) {101try {102Class<?> c = stackWalker.getCallerClass();103assertEquals(c, expected);104if (expectUOE) { // Should have thrown105throw new RuntimeException("Didn't get expected exception");106}107} catch (RuntimeException e) { // also catches UOE108if (expectUOE && causeIsUOE(e)) {109return; /* expected */110}111System.err.println("Unexpected exception:");112throw e;113}114}115116public static void reflectiveGetCallerClass(StackWalker stackWalker,117Class<?> expected,118boolean expectUOE) {119try {120Method m = StackWalker.class.getMethod("getCallerClass");121Class<?> c = (Class<?>) m.invoke(stackWalker);122assertEquals(c, expected);123if (expectUOE) { // Should have thrown124throw new RuntimeException("Didn't get expected exception");125}126} catch (Throwable e) {127if (expectUOE && causeIsUOE(e)) {128return; /* expected */129}130System.err.println("Unexpected exception:");131throw new RuntimeException(e);132}133}134135public static void methodHandleGetCallerClass(StackWalker stackWalker,136Class<?> expected,137boolean expectUOE) {138MethodHandles.Lookup lookup = MethodHandles.lookup();139try {140MethodHandle mh = lookup.findVirtual(StackWalker.class, "getCallerClass",141MethodType.methodType(Class.class));142Class<?> c = (Class<?>) mh.invokeExact(stackWalker);143assertEquals(c, expected);144if (expectUOE) { // Should have thrown145throw new RuntimeException("Didn't get expected exception");146}147} catch (Throwable e) {148if (expectUOE && causeIsUOE(e)) {149return; /* expected */150}151System.err.println("Unexpected exception:");152throw new RuntimeException(e);153}154}155156public static void assertEquals(Class<?> c, Class<?> expected) {157if (expected != c) {158throw new RuntimeException("Got " + c + ", but expected " + expected);159}160}161162/** Is there an UnsupportedOperationException in there? */163public static boolean causeIsUOE(Throwable t) {164while (t != null) {165if (t instanceof UnsupportedOperationException) {166return true;167}168t = t.getCause();169}170return false;171}172173class TopLevelCaller implements Runnable {174public void run() {175GetCallerClassTest.staticGetCallerClass(walker, this.getClass(), expectUOE);176GetCallerClassTest.reflectiveGetCallerClass(walker, this.getClass(), expectUOE);177GetCallerClassTest.methodHandleGetCallerClass(walker, this.getClass(), expectUOE);178}179}180181class LambdaTest implements Runnable {182public void run() {183Runnable lambdaRunnable = () -> {184try {185Class<?> c = walker.getCallerClass();186187assertEquals(c, LambdaTest.class);188if (expectUOE) { // Should have thrown189throw new RuntimeException("Didn't get expected exception");190}191} catch (Throwable e) {192if (expectUOE && causeIsUOE(e)) {193return; /* expected */194}195System.err.println("Unexpected exception:");196throw new RuntimeException(e);197}198};199lambdaRunnable.run();200}201}202203class Nested {204NestedClassCaller createNestedCaller() { return new NestedClassCaller(); }205class NestedClassCaller implements Runnable {206public void run() {207GetCallerClassTest.staticGetCallerClass(walker, this.getClass(), expectUOE);208GetCallerClassTest.reflectiveGetCallerClass(walker, this.getClass(), expectUOE);209GetCallerClassTest.methodHandleGetCallerClass(walker, this.getClass(), expectUOE);210}211}212}213214class InnerClassCaller implements Runnable {215public void run() {216new Inner().test();217}218class Inner {219void test() {220GetCallerClassTest.staticGetCallerClass(walker, this.getClass(), expectUOE);221GetCallerClassTest.reflectiveGetCallerClass(walker, this.getClass(), expectUOE);222GetCallerClassTest.methodHandleGetCallerClass(walker, this.getClass(), expectUOE);223}224}225}226227class ReflectionTest implements Runnable {228final MethodType methodType =229MethodType.methodType(void.class, StackWalker.class, Class.class, boolean.class);230231public void run() {232callMethodHandle();233callMethodHandleRefl();234callMethodInvoke();235callMethodInvokeRefl();236}237void callMethodHandle() {238MethodHandles.Lookup lookup = MethodHandles.publicLookup();239try {240MethodHandle mh = lookup.findStatic(GetCallerClassTest.class,241"staticGetCallerClass",242methodType);243mh.invokeExact(walker, ReflectionTest.class, expectUOE);244} catch (Throwable e) {245throw new RuntimeException(e);246}247}248void callMethodHandleRefl() {249MethodHandles.Lookup lookup = MethodHandles.publicLookup();250try {251MethodHandle mh = lookup.findStatic(GetCallerClassTest.class,252"reflectiveGetCallerClass",253methodType);254mh.invokeExact(walker, ReflectionTest.class, expectUOE);255} catch (Throwable e) {256throw new RuntimeException(e);257}258}259void callMethodInvoke() {260try {261Method m = GetCallerClassTest.class.getMethod("staticGetCallerClass",262StackWalker.class, Class.class, boolean.class);263m.invoke(null, walker, ReflectionTest.class, expectUOE);264} catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException e) {265throw new RuntimeException(e);266}267}268void callMethodInvokeRefl() {269try {270Method m = GetCallerClassTest.class.getMethod("reflectiveGetCallerClass",271StackWalker.class, Class.class, boolean.class);272m.invoke(null, walker, ReflectionTest.class, expectUOE);273} catch (UnsupportedOperationException e) {274throw e;275} catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException e) {276throw new RuntimeException(e);277}278}279}280}281282283