Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/lang/invoke/lambda/LambdaStackTrace.java
48795 views
/*1* Copyright (c) 2015, 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 802563626* @summary Synthetic frames should be hidden in exceptions27* @compile -XDignore.symbol.file LUtils.java LambdaStackTrace.java28* @run main LambdaStackTrace29*/3031import jdk.internal.org.objectweb.asm.ClassWriter;3233import java.io.File;34import java.io.FileOutputStream;35import java.io.IOException;36import java.lang.reflect.InvocationTargetException;37import java.lang.reflect.Method;38import java.util.ArrayList;3940import static jdk.internal.org.objectweb.asm.Opcodes.ACC_ABSTRACT;41import static jdk.internal.org.objectweb.asm.Opcodes.ACC_INTERFACE;42import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;43import static jdk.internal.org.objectweb.asm.Opcodes.V1_7;4445public class LambdaStackTrace {4647static File classes = new File(System.getProperty("test.classes"));4849public static void main(String[] args) throws Exception {50testBasic();51testBridgeMethods();52}5354/**55* Test the simple case56*/57private static void testBasic() throws Exception {58try {59Runnable r = () -> {60throw new RuntimeException();61};62r.run();63} catch (Exception ex) {64// Before 8025636 the stacktrace would look like:65// at LambdaStackTrace.lambda$main$0(LambdaStackTrace.java:37)66// at LambdaStackTrace$$Lambda$1/1937396743.run(<Unknown>:1000000)67// at LambdaStackTrace.testBasic(LambdaStackTrace.java:40)68// at ...69//70// We are verifying that the middle frame above is gone.7172verifyFrames(ex.getStackTrace(),73"LambdaStackTrace\\..*",74"LambdaStackTrace.testBasic");75}76}7778/**79* Test the more complicated case with bridge methods.80*81* We set up the following interfaces:82*83* interface Maker {84* Object make();85* }86* interface StringMaker extends Maker {87* String make();88* }89*90* And we will use them like so:91*92* StringMaker sm = () -> { throw new RuntimeException(); };93* sm.make();94* ((Maker)m).make();95*96* The first call is a "normal" interface call, the second will use a97* bridge method. In both cases the generated lambda frame should98* be removed from the stack trace.99*/100private static void testBridgeMethods() throws Exception {101// setup102generateInterfaces();103compileCaller();104105// test106StackTraceElement[] frames = call("Caller", "callStringMaker");107verifyFrames(frames,108"Caller\\..*",109"Caller.callStringMaker");110111frames = call("Caller", "callMaker");112verifyFrames(frames,113"Caller\\..*",114"Caller.callMaker");115}116117private static void generateInterfaces() throws IOException {118// We can't let javac compile these interfaces because in > 1.8 it will insert119// bridge methods into the interfaces - we want code that looks like <= 1.7,120// so we generate it.121try (FileOutputStream fw = new FileOutputStream(new File(classes, "Maker.class"))) {122fw.write(generateMaker());123}124try (FileOutputStream fw = new FileOutputStream(new File(classes, "StringMaker.class"))) {125fw.write(generateStringMaker());126}127}128129private static byte[] generateMaker() {130// interface Maker {131// Object make();132// }133ClassWriter cw = new ClassWriter(0);134cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "Maker", null, "java/lang/Object", null);135cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",136"()Ljava/lang/Object;", null, null);137cw.visitEnd();138return cw.toByteArray();139}140141private static byte[] generateStringMaker() {142// interface StringMaker extends Maker {143// String make();144// }145ClassWriter cw = new ClassWriter(0);146cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "StringMaker", null, "java/lang/Object", new String[]{"Maker"});147cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",148"()Ljava/lang/String;", null, null);149cw.visitEnd();150return cw.toByteArray();151}152153154static void emitCode(File f) {155ArrayList<String> scratch = new ArrayList<>();156scratch.add("public class Caller {");157scratch.add(" public static void callStringMaker() {");158scratch.add(" StringMaker sm = () -> { throw new RuntimeException(); };");159scratch.add(" sm.make();");160scratch.add(" }");161scratch.add(" public static void callMaker() {");162scratch.add(" StringMaker sm = () -> { throw new RuntimeException(); };");163scratch.add(" ((Maker) sm).make();"); // <-- This will call the bridge method164scratch.add(" }");165scratch.add("}");166LUtils.createFile(f, scratch);167}168169static void compileCaller() {170File caller = new File(classes, "Caller.java");171emitCode(caller);172LUtils.compile("-cp", classes.getAbsolutePath(), "-d", classes.getAbsolutePath(), caller.getAbsolutePath());173}174175private static void verifyFrames(StackTraceElement[] stack, String... patterns) throws Exception {176for (int i = 0; i < patterns.length; i++) {177String cm = stack[i].getClassName() + "." + stack[i].getMethodName();178if (!cm.matches(patterns[i])) {179System.err.println("Actual trace did not match expected trace at frame " + i);180System.err.println("Expected frame patterns:");181for (int j = 0; j < patterns.length; j++) {182System.err.println(" " + j + ": " + patterns[j]);183}184System.err.println("Actual frames:");185for (int j = 0; j < patterns.length; j++) {186System.err.println(" " + j + ": " + stack[j]);187}188throw new Exception("Incorrect stack frames found");189}190}191}192193private static StackTraceElement[] call(String clazz, String method) throws Exception {194Class<?> c = Class.forName(clazz);195try {196Method m = c.getDeclaredMethod(method);197m.invoke(null);198} catch(InvocationTargetException ex) {199return ex.getTargetException().getStackTrace();200}201throw new Exception("Expected exception to be thrown");202}203}204205206