File: utils.gradle

package info (click to toggle)
groovy 2.4.21-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 30,708 kB
  • sloc: java: 157,114; xml: 728; sh: 357; makefile: 64; javascript: 15
file content (97 lines) | stat: -rw-r--r-- 3,438 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.Label

import static org.objectweb.asm.Opcodes.ACC_PUBLIC
import static org.objectweb.asm.Opcodes.ACC_STATIC
import static org.objectweb.asm.Opcodes.ACC_SUPER
import static org.objectweb.asm.Opcodes.ALOAD
import static org.objectweb.asm.Opcodes.ATHROW
import static org.objectweb.asm.Opcodes.INVOKESPECIAL
import static org.objectweb.asm.Opcodes.RETURN
import static org.objectweb.asm.Opcodes.V1_5

buildscript {
    repositories {
        maven { url "file:///usr/share/maven-repo" }
    }
    dependencies {
        classpath "org.ow2.asm:asm:debian"
    }
}

/**
 * This tasks generates an utility class which allows sneaky throwing.
 */
task exceptionUtils {
    ext.classFiles = [
            "${buildDir}/generated-classes/org/codehaus/groovy/runtime/ExceptionUtils.class",
            "${compileJava.destinationDir}/org/codehaus/groovy/runtime/ExceptionUtils.class"]
    outputs.files classFiles

    doLast {
        ClassWriter cw = new ClassWriter(0);
        MethodVisitor mv;

        cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, 'org/codehaus/groovy/runtime/ExceptionUtils', null, 'java/lang/Object', null);

        cw.visitSource('ExceptionUtils.java', null);

        mv = cw.visitMethod(ACC_PUBLIC, '<init>', '()V', null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(18, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, 'java/lang/Object', '<init>', '()V', false);
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable('this', 'Lorg/codehaus/groovy/runtime/ExceptionUtils;', null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, 'sneakyThrow', '(Ljava/lang/Throwable;)V', null, null);
        mv.visitCode();
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLineNumber(20, l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ATHROW);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLocalVariable('e', 'Ljava/lang/Throwable;', null, l2, l3, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();

        cw.visitEnd();

        logger.lifecycle('Generating ExceptionUtils')
        classFiles.each { classFile ->
            def output = file(classFile)
            output.parentFile.mkdirs()
            output.withOutputStream {
                it << cw.toByteArray()
            }
        }
    }
}