< prev index next >

test/jdk/java/lang/invoke/lambda/LambdaStackTrace.java

Print this page

  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  * @modules java.base/jdk.internal.org.objectweb.asm
 29  *          jdk.compiler
 30  * @compile -XDignore.symbol.file LUtils.java LambdaStackTrace.java
 31  * @run main LambdaStackTrace

 32  */
 33 
 34 import jdk.internal.org.objectweb.asm.ClassWriter;

 35 
 36 import java.io.File;
 37 import java.io.FileOutputStream;
 38 import java.io.IOException;
 39 import java.lang.reflect.InvocationTargetException;
 40 import java.lang.reflect.Method;



 41 import java.util.ArrayList;
 42 
 43 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_ABSTRACT;
 44 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_INTERFACE;
 45 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
 46 import static jdk.internal.org.objectweb.asm.Opcodes.V1_7;
 47 
 48 public class LambdaStackTrace {
 49 
 50     static File classes = new File(System.getProperty("test.classes"));
 51 
 52     public static void main(String[] args) throws Exception {
 53         testBasic();
 54         testBridgeMethods();
 55     }
 56 
 57     /**
 58      * Test the simple case
 59      */
 60     private static void testBasic() throws Exception {
 61         try {
 62             Runnable r = () -> {
 63                 throw new RuntimeException();
 64             };
 65             r.run();
 66         } catch (Exception ex) {
 67             // Before 8025636 the stacktrace would look like:
 68             //  at LambdaStackTrace.lambda$main$0(LambdaStackTrace.java:37)
 69             //  at LambdaStackTrace$$Lambda/1937396743.run(<Unknown>:1000000)
 70             //  at LambdaStackTrace.testBasic(LambdaStackTrace.java:40)

104         // setup
105         generateInterfaces();
106         compileCaller();
107 
108         // test
109         StackTraceElement[] frames = call("Caller", "callStringMaker");
110         verifyFrames(frames,
111                 "Caller\\..*",
112                 "Caller.callStringMaker");
113 
114         frames = call("Caller", "callMaker");
115         verifyFrames(frames,
116                 "Caller\\..*",
117                 "Caller.callMaker");
118     }
119 
120     private static void generateInterfaces() throws IOException {
121         // We can't let javac compile these interfaces because in > 1.8 it will insert
122         // bridge methods into the interfaces - we want code that looks like <= 1.7,
123         // so we generate it.
124         try (FileOutputStream fw = new FileOutputStream(new File(classes, "Maker.class"))) {
125             fw.write(generateMaker());
126         }
127         try (FileOutputStream fw = new FileOutputStream(new File(classes, "StringMaker.class"))) {
128             fw.write(generateStringMaker());
129         }
130     }
131 
132     private static byte[] generateMaker() {
133         // interface Maker {
134         //   Object make();
135         // }
136         ClassWriter cw = new ClassWriter(0);
137         cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "Maker", null, "java/lang/Object", null);
138         cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
139                 "()Ljava/lang/Object;", null, null);
140         cw.visitEnd();
141         return cw.toByteArray();
142     }
143 
144     private static byte[] generateStringMaker() {
145         // interface StringMaker extends Maker {
146         //   String make();
147         // }
148         ClassWriter cw = new ClassWriter(0);
149         cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "StringMaker", null, "java/lang/Object", new String[]{"Maker"});
150         cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
151                 "()Ljava/lang/String;", null, null);
152         cw.visitEnd();
153         return cw.toByteArray();
154     }
155 
156 
157     static void emitCode(File f) {
158         ArrayList<String> scratch = new ArrayList<>();
159         scratch.add("public class Caller {");
160         scratch.add("    public static void callStringMaker() {");
161         scratch.add("        StringMaker sm = () -> { throw new RuntimeException(); };");
162         scratch.add("        sm.make();");
163         scratch.add("    }");
164         scratch.add("    public static void callMaker() {");
165         scratch.add("        StringMaker sm = () -> { throw new RuntimeException(); };");
166         scratch.add("        ((Maker) sm).make();");  // <-- This will call the bridge method
167         scratch.add("    }");
168         scratch.add("}");
169         LUtils.createFile(f, scratch);

170     }
171 
172     static void compileCaller() {
173         File caller = new File(classes, "Caller.java");



174         emitCode(caller);
175         LUtils.compile("-cp", classes.getAbsolutePath(), "-d", classes.getAbsolutePath(), caller.getAbsolutePath());
176     }
177 
178     private static void verifyFrames(StackTraceElement[] stack, String... patterns) throws Exception {
179         for (int i = 0; i < patterns.length; i++) {
180             String cm = stack[i].getClassName() + "." + stack[i].getMethodName();
181             if (!cm.matches(patterns[i])) {
182                 System.err.println("Actual trace did not match expected trace at frame " + i);
183                 System.err.println("Expected frame patterns:");
184                 for (int j = 0; j < patterns.length; j++) {
185                     System.err.println("  " + j + ": " + patterns[j]);
186                 }
187                 System.err.println("Actual frames:");
188                 for (int j = 0; j < patterns.length; j++) {
189                     System.err.println("  " + j + ": " + stack[j]);
190                 }
191                 throw new Exception("Incorrect stack frames found");
192             }
193         }
194     }
195 

  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  * @library /test/lib/
 28  * @modules java.base/jdk.internal.org.objectweb.asm
 29  *          jdk.compiler
 30  * @compile LambdaStackTrace.java
 31  * @run main LambdaStackTrace
 32  * @summary Synthetic frames should be hidden in exceptions
 33  */
 34 
 35 import jdk.internal.org.objectweb.asm.ClassWriter;
 36 import jdk.test.lib.compiler.CompilerUtils;
 37 


 38 import java.io.IOException;
 39 import java.lang.reflect.InvocationTargetException;
 40 import java.lang.reflect.Method;
 41 import java.nio.charset.Charset;
 42 import java.nio.file.Files;
 43 import java.nio.file.Path;
 44 import java.util.ArrayList;
 45 
 46 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_ABSTRACT;
 47 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_INTERFACE;
 48 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
 49 import static jdk.internal.org.objectweb.asm.Opcodes.V1_7;
 50 
 51 public class LambdaStackTrace {
 52 
 53     static Path CLASSES = Path.of(System.getProperty("test.classes", "."));
 54 
 55     public static void main(String[] args) throws Exception {
 56         testBasic();
 57         testBridgeMethods();
 58     }
 59 
 60     /**
 61      * Test the simple case
 62      */
 63     private static void testBasic() throws Exception {
 64         try {
 65             Runnable r = () -> {
 66                 throw new RuntimeException();
 67             };
 68             r.run();
 69         } catch (Exception ex) {
 70             // Before 8025636 the stacktrace would look like:
 71             //  at LambdaStackTrace.lambda$main$0(LambdaStackTrace.java:37)
 72             //  at LambdaStackTrace$$Lambda/1937396743.run(<Unknown>:1000000)
 73             //  at LambdaStackTrace.testBasic(LambdaStackTrace.java:40)

107         // setup
108         generateInterfaces();
109         compileCaller();
110 
111         // test
112         StackTraceElement[] frames = call("Caller", "callStringMaker");
113         verifyFrames(frames,
114                 "Caller\\..*",
115                 "Caller.callStringMaker");
116 
117         frames = call("Caller", "callMaker");
118         verifyFrames(frames,
119                 "Caller\\..*",
120                 "Caller.callMaker");
121     }
122 
123     private static void generateInterfaces() throws IOException {
124         // We can't let javac compile these interfaces because in > 1.8 it will insert
125         // bridge methods into the interfaces - we want code that looks like <= 1.7,
126         // so we generate it.
127         Files.write(CLASSES.resolve("Maker.class"), generateMaker());
128         Files.write(CLASSES.resolve("StringMaker.class"), generateStringMaker());




129     }
130 
131     private static byte[] generateMaker() {
132         // interface Maker {
133         //   Object make();
134         // }
135         ClassWriter cw = new ClassWriter(0);
136         cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "Maker", null, "java/lang/Object", null);
137         cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
138                 "()Ljava/lang/Object;", null, null);
139         cw.visitEnd();
140         return cw.toByteArray();
141     }
142 
143     private static byte[] generateStringMaker() {
144         // interface StringMaker extends Maker {
145         //   String make();
146         // }
147         ClassWriter cw = new ClassWriter(0);
148         cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "StringMaker", null, "java/lang/Object", new String[]{"Maker"});
149         cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
150                 "()Ljava/lang/String;", null, null);
151         cw.visitEnd();
152         return cw.toByteArray();
153     }
154 
155 
156     static void emitCode(Path file) throws IOException {
157         ArrayList<String> scratch = new ArrayList<>();
158         scratch.add("public class Caller {");
159         scratch.add("    public static void callStringMaker() {");
160         scratch.add("        StringMaker sm = () -> { throw new RuntimeException(); };");
161         scratch.add("        sm.make();");
162         scratch.add("    }");
163         scratch.add("    public static void callMaker() {");
164         scratch.add("        StringMaker sm = () -> { throw new RuntimeException(); };");
165         scratch.add("        ((Maker) sm).make();");  // <-- This will call the bridge method
166         scratch.add("    }");
167         scratch.add("}");
168 
169         Files.write(file, scratch, Charset.defaultCharset());
170     }
171 
172     static void compileCaller() throws IOException {
173         Path src = Path.of("src");
174         Files.createDirectories(src);
175 
176         Path caller = src.resolve("Caller.java");
177         emitCode(caller);
178         CompilerUtils.compile(src, CLASSES, "-cp", CLASSES.toAbsolutePath().toString());
179     }
180 
181     private static void verifyFrames(StackTraceElement[] stack, String... patterns) throws Exception {
182         for (int i = 0; i < patterns.length; i++) {
183             String cm = stack[i].getClassName() + "." + stack[i].getMethodName();
184             if (!cm.matches(patterns[i])) {
185                 System.err.println("Actual trace did not match expected trace at frame " + i);
186                 System.err.println("Expected frame patterns:");
187                 for (int j = 0; j < patterns.length; j++) {
188                     System.err.println("  " + j + ": " + patterns[j]);
189                 }
190                 System.err.println("Actual frames:");
191                 for (int j = 0; j < patterns.length; j++) {
192                     System.err.println("  " + j + ": " + stack[j]);
193                 }
194                 throw new Exception("Incorrect stack frames found");
195             }
196         }
197     }
198 
< prev index next >