blob: f5f66bf1516c06dba7abaa8f9266544a8cb5d656 [file] [log] [blame]
Damien Martin-Guillerezbf6281d2015-11-19 16:41:33 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Liam Miller-Cushon6e771db2015-10-19 17:13:48 +00002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package test;
16
17import org.objectweb.asm.ClassWriter;
18import org.objectweb.asm.MethodVisitor;
19import org.objectweb.asm.Opcodes;
20
21import java.io.File;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.nio.file.Files;
25import java.nio.file.Paths;
26import java.util.jar.JarOutputStream;
27import java.util.zip.ZipEntry;
28
29public class GenSourceDebugExtension {
30 public static void main(String[] args) throws IOException {
31 try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(args[0]))) {
32 jos.putNextEntry(new ZipEntry("sourcedebugextension/Test.class"));
33 jos.write(dump());
34 }
35 }
36
37 public static byte[] dump() {
38 ClassWriter cw = new ClassWriter(0);
39 MethodVisitor mv;
40
41 cw.visit(
42 52,
43 Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER,
44 "sourcedebugextension/Test",
45 null,
46 "java/lang/Object",
47 null);
48
49 // This is the important part for the test - it's an example SourceDebugExtension found
50 // in Clojure.
51 cw.visitSource(
52 "dispatch.clj",
53 "SMAP\ndispatch.java\nClojure\n*S Clojure\n*F\n+ 1 dispatch.clj\nclojure/pprint/dispatch.clj\n*L\n144#1,8:144\n*E");
54
55 {
56 mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
57 mv.visitCode();
58 mv.visitVarInsn(Opcodes.ALOAD, 0);
59 mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
60 mv.visitInsn(Opcodes.RETURN);
61 mv.visitMaxs(1, 1);
62 mv.visitEnd();
63 }
64 {
65 mv = cw.visitMethod(
66 Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC,
67 "main",
68 "([Ljava/lang/String;)V",
69 null,
70 null);
71 mv.visitCode();
72 mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
73 mv.visitLdcInsn("Hello");
74 mv.visitMethodInsn(
75 Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
76 mv.visitInsn(Opcodes.RETURN);
77 mv.visitMaxs(2, 1);
78 mv.visitEnd();
79 }
80 cw.visitEnd();
81
82 return cw.toByteArray();
83 }
84}