Skip to content

Instantly share code, notes, and snippets.

@monsieurp
Last active October 31, 2015 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monsieurp/87bd89c2317647b75198 to your computer and use it in GitHub Desktop.
Save monsieurp/87bd89c2317647b75198 to your computer and use it in GitHub Desktop.
package org.gentoo.java;
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;
import java.io.File;
import java.io.FileOutputStream;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
public class ExceptionUtils {
private final static String gentooClassDestination = "target/classes/org/codehaus/groovy/runtime/ExceptionUtils.class";
public static void main(String[] args) {
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();
FileOutputStream fos = null;
File f = new File(gentooClassDestination);
f.getParentFile().mkdirs();
try {
fos = new FileOutputStream(f);
fos.write(cw.toByteArray());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment