Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/lang/invoke/lambda/LambdaStackTrace.java
48795 views
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 8025636
27
* @summary Synthetic frames should be hidden in exceptions
28
* @compile -XDignore.symbol.file LUtils.java LambdaStackTrace.java
29
* @run main LambdaStackTrace
30
*/
31
32
import jdk.internal.org.objectweb.asm.ClassWriter;
33
34
import java.io.File;
35
import java.io.FileOutputStream;
36
import java.io.IOException;
37
import java.lang.reflect.InvocationTargetException;
38
import java.lang.reflect.Method;
39
import java.util.ArrayList;
40
41
import static jdk.internal.org.objectweb.asm.Opcodes.ACC_ABSTRACT;
42
import static jdk.internal.org.objectweb.asm.Opcodes.ACC_INTERFACE;
43
import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
44
import static jdk.internal.org.objectweb.asm.Opcodes.V1_7;
45
46
public class LambdaStackTrace {
47
48
static File classes = new File(System.getProperty("test.classes"));
49
50
public static void main(String[] args) throws Exception {
51
testBasic();
52
testBridgeMethods();
53
}
54
55
/**
56
* Test the simple case
57
*/
58
private static void testBasic() throws Exception {
59
try {
60
Runnable r = () -> {
61
throw new RuntimeException();
62
};
63
r.run();
64
} catch (Exception ex) {
65
// Before 8025636 the stacktrace would look like:
66
// at LambdaStackTrace.lambda$main$0(LambdaStackTrace.java:37)
67
// at LambdaStackTrace$$Lambda$1/1937396743.run(<Unknown>:1000000)
68
// at LambdaStackTrace.testBasic(LambdaStackTrace.java:40)
69
// at ...
70
//
71
// We are verifying that the middle frame above is gone.
72
73
verifyFrames(ex.getStackTrace(),
74
"LambdaStackTrace\\..*",
75
"LambdaStackTrace.testBasic");
76
}
77
}
78
79
/**
80
* Test the more complicated case with bridge methods.
81
*
82
* We set up the following interfaces:
83
*
84
* interface Maker {
85
* Object make();
86
* }
87
* interface StringMaker extends Maker {
88
* String make();
89
* }
90
*
91
* And we will use them like so:
92
*
93
* StringMaker sm = () -> { throw new RuntimeException(); };
94
* sm.make();
95
* ((Maker)m).make();
96
*
97
* The first call is a "normal" interface call, the second will use a
98
* bridge method. In both cases the generated lambda frame should
99
* be removed from the stack trace.
100
*/
101
private static void testBridgeMethods() throws Exception {
102
// setup
103
generateInterfaces();
104
compileCaller();
105
106
// test
107
StackTraceElement[] frames = call("Caller", "callStringMaker");
108
verifyFrames(frames,
109
"Caller\\..*",
110
"Caller.callStringMaker");
111
112
frames = call("Caller", "callMaker");
113
verifyFrames(frames,
114
"Caller\\..*",
115
"Caller.callMaker");
116
}
117
118
private static void generateInterfaces() throws IOException {
119
// We can't let javac compile these interfaces because in > 1.8 it will insert
120
// bridge methods into the interfaces - we want code that looks like <= 1.7,
121
// so we generate it.
122
try (FileOutputStream fw = new FileOutputStream(new File(classes, "Maker.class"))) {
123
fw.write(generateMaker());
124
}
125
try (FileOutputStream fw = new FileOutputStream(new File(classes, "StringMaker.class"))) {
126
fw.write(generateStringMaker());
127
}
128
}
129
130
private static byte[] generateMaker() {
131
// interface Maker {
132
// Object make();
133
// }
134
ClassWriter cw = new ClassWriter(0);
135
cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "Maker", null, "java/lang/Object", null);
136
cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
137
"()Ljava/lang/Object;", null, null);
138
cw.visitEnd();
139
return cw.toByteArray();
140
}
141
142
private static byte[] generateStringMaker() {
143
// interface StringMaker extends Maker {
144
// String make();
145
// }
146
ClassWriter cw = new ClassWriter(0);
147
cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "StringMaker", null, "java/lang/Object", new String[]{"Maker"});
148
cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
149
"()Ljava/lang/String;", null, null);
150
cw.visitEnd();
151
return cw.toByteArray();
152
}
153
154
155
static void emitCode(File f) {
156
ArrayList<String> scratch = new ArrayList<>();
157
scratch.add("public class Caller {");
158
scratch.add(" public static void callStringMaker() {");
159
scratch.add(" StringMaker sm = () -> { throw new RuntimeException(); };");
160
scratch.add(" sm.make();");
161
scratch.add(" }");
162
scratch.add(" public static void callMaker() {");
163
scratch.add(" StringMaker sm = () -> { throw new RuntimeException(); };");
164
scratch.add(" ((Maker) sm).make();"); // <-- This will call the bridge method
165
scratch.add(" }");
166
scratch.add("}");
167
LUtils.createFile(f, scratch);
168
}
169
170
static void compileCaller() {
171
File caller = new File(classes, "Caller.java");
172
emitCode(caller);
173
LUtils.compile("-cp", classes.getAbsolutePath(), "-d", classes.getAbsolutePath(), caller.getAbsolutePath());
174
}
175
176
private static void verifyFrames(StackTraceElement[] stack, String... patterns) throws Exception {
177
for (int i = 0; i < patterns.length; i++) {
178
String cm = stack[i].getClassName() + "." + stack[i].getMethodName();
179
if (!cm.matches(patterns[i])) {
180
System.err.println("Actual trace did not match expected trace at frame " + i);
181
System.err.println("Expected frame patterns:");
182
for (int j = 0; j < patterns.length; j++) {
183
System.err.println(" " + j + ": " + patterns[j]);
184
}
185
System.err.println("Actual frames:");
186
for (int j = 0; j < patterns.length; j++) {
187
System.err.println(" " + j + ": " + stack[j]);
188
}
189
throw new Exception("Incorrect stack frames found");
190
}
191
}
192
}
193
194
private static StackTraceElement[] call(String clazz, String method) throws Exception {
195
Class<?> c = Class.forName(clazz);
196
try {
197
Method m = c.getDeclaredMethod(method);
198
m.invoke(null);
199
} catch(InvocationTargetException ex) {
200
return ex.getTargetException().getStackTrace();
201
}
202
throw new Exception("Expected exception to be thrown");
203
}
204
}
205
206