Commit | Line | Data |
---|---|---|
fdc9170b JL |
1 | From 3dbdc68093e90f0ef9b77b70490d8e0b1dcfbf8f Mon Sep 17 00:00:00 2001 |
2 | From: Julien Lepiller <julien@lepiller.eu> | |
3 | Date: Sun, 17 Sep 2017 21:08:45 +0200 | |
4 | Subject: [PATCH] Add ExceptionUtilsGenerator.java. | |
5 | ||
6 | A gradle task (in gradle/utils.gradle) is normally used to generate an | |
7 | ExceptionUtils class. Since gradle depends on groovy, we cannot use it, so | |
8 | we copy the code from the gradle task to a new file. Running this file then | |
9 | generates the required class. | |
10 | --- | |
11 | .../codehaus/groovy/ExceptionUtilsGenerator.java | 75 ++++++++++++++++++++++ | |
12 | 1 file changed, 75 insertions(+) | |
13 | create mode 100644 config/ant/src/org/codehaus/groovy/ExceptionUtilsGenerator.java | |
14 | ||
15 | diff --git a/config/ant/src/org/codehaus/groovy/ExceptionUtilsGenerator.java b/config/ant/src/org/codehaus/groovy/ExceptionUtilsGenerator.java | |
16 | new file mode 100644 | |
17 | index 0000000..41f006d | |
18 | --- /dev/null | |
19 | +++ b/config/ant/src/org/codehaus/groovy/ExceptionUtilsGenerator.java | |
20 | @@ -0,0 +1,75 @@ | |
21 | +package org.codehaus.groovy; | |
22 | + | |
23 | +import org.objectweb.asm.*; | |
24 | + | |
25 | +import java.io.BufferedOutputStream; | |
26 | +import java.io.File; | |
27 | +import java.io.FileOutputStream; | |
28 | +import java.io.IOException; | |
29 | +import java.util.logging.Logger; | |
30 | + | |
31 | +public class ExceptionUtilsGenerator implements Opcodes { | |
32 | + private final static Logger LOGGER = Logger.getLogger(ExceptionUtilsGenerator.class.getName()); | |
33 | + | |
34 | + public static void main(String... args) { | |
35 | + if (args==null || args.length==0) { | |
36 | + throw new IllegalArgumentException("You must specify at least one file"); | |
37 | + } | |
38 | + | |
39 | + ClassWriter cw = new ClassWriter(0); | |
40 | + MethodVisitor mv; | |
41 | + | |
42 | + cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "org/codehaus/groovy/runtime/ExceptionUtils", null, "java/lang/Object", null); | |
43 | + | |
44 | + cw.visitSource("ExceptionUtils.java", null); | |
45 | + | |
46 | + mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); | |
47 | + mv.visitCode(); | |
48 | + Label l0 = new Label(); | |
49 | + mv.visitLabel(l0); | |
50 | + mv.visitLineNumber(18, l0); | |
51 | + mv.visitVarInsn(ALOAD, 0); | |
52 | + mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); | |
53 | + mv.visitInsn(RETURN); | |
54 | + Label l1 = new Label(); | |
55 | + mv.visitLabel(l1); | |
56 | + mv.visitLocalVariable("this", "Lorg/codehaus/groovy/runtime/ExceptionUtils;", null, l0, l1, 0); | |
57 | + mv.visitMaxs(1, 1); | |
58 | + mv.visitEnd(); | |
59 | + | |
60 | + mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "sneakyThrow", "(Ljava/lang/Throwable;)V", null, null); | |
61 | + mv.visitCode(); | |
62 | + Label l2 = new Label(); | |
63 | + mv.visitLabel(l2); | |
64 | + mv.visitLineNumber(20, l2); | |
65 | + mv.visitVarInsn(ALOAD, 0); | |
66 | + mv.visitInsn(ATHROW); | |
67 | + Label l3 = new Label(); | |
68 | + mv.visitLabel(l3); | |
69 | + mv.visitLocalVariable("e", "Ljava/lang/Throwable;", null, l2, l3, 0); | |
70 | + mv.visitMaxs(1, 1); | |
71 | + mv.visitEnd(); | |
72 | + | |
73 | + cw.visitEnd(); | |
74 | + | |
75 | + LOGGER.info("Generating ExceptionUtils"); | |
76 | + byte[] bytes = cw.toByteArray(); | |
77 | + for (String classFilePath : args) { | |
78 | + File classFile = new File(classFilePath); | |
79 | + if (classFile.getParentFile().exists() || classFile.getParentFile().mkdirs()) { | |
80 | + try { | |
81 | + if (classFile.exists()) { | |
82 | + classFile.delete(); | |
83 | + } | |
84 | + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(classFile)); | |
85 | + bos.write(bytes); | |
86 | + bos.close(); | |
87 | + } catch (IOException e) { | |
88 | + LOGGER.warning("Unable to write file "+classFile); | |
89 | + } | |
90 | + } else { | |
91 | + LOGGER.warning("Unable to create directory "+classFile.getParentFile()); | |
92 | + } | |
93 | + } | |
94 | + } | |
95 | +} | |
96 | -- | |
97 | 2.14.1 | |
98 |