View Javadoc
1 // Copyright (c) 2003, Chad Woolley, All rights reserved. 2 3 package org.throwableutil; 4 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.OutputStream; 8 import org.apache.bcel.Constants; 9 import org.apache.bcel.generic.ClassGen; 10 import org.apache.bcel.generic.ConstantPoolGen; 11 import org.apache.bcel.generic.InstructionConstants; 12 import org.apache.bcel.generic.InstructionFactory; 13 import org.apache.bcel.generic.InstructionList; 14 import org.apache.bcel.generic.MethodGen; 15 import org.apache.bcel.generic.ObjectType; 16 import org.apache.bcel.generic.Type; 17 18 19 /*** 20 * BCEL class to create the ThrowableUtil class with modified bytecode. 21 * 22 * @author Chad Woolley 23 * @version $Revision: 1.1 $ 24 */ 25 public class ThrowableUtilCreator implements Constants { 26 private ClassGen classGen; 27 private ConstantPoolGen constantPoolGen; 28 private InstructionFactory instructionFactory; 29 30 /*** 31 * Creates a new ThrowableUtilCreator object. 32 */ 33 public ThrowableUtilCreator() { 34 classGen = 35 new ClassGen("org.throwableutil.ThrowableUtil", 36 "java.lang.Object", "ThrowableUtil.java", 37 ACC_PUBLIC | ACC_SUPER, new String[] {}); 38 39 constantPoolGen = classGen.getConstantPool(); 40 instructionFactory = new InstructionFactory(classGen, constantPoolGen); 41 } 42 43 /*** 44 * BCEL generated. 45 * 46 * @param out BCEL generated 47 * 48 * @throws IOException BCEL generated 49 */ 50 public void create(OutputStream out) throws IOException { 51 createMethod0(); 52 createMethod1(); 53 classGen.getJavaClass().dump(out); 54 } 55 56 /*** 57 * BCEL generated. 58 * 59 * @param args one param, which is the path of the output 60 * ThrowableUtil.class file (with "ThrowableUtil.class" 61 * included), relative to the working directory 62 * 63 * @throws Exception BCEL generated 64 */ 65 public static void main(String[] args) throws Exception { 66 String path = args[0]; 67 ThrowableUtilCreator creator = 68 new ThrowableUtilCreator(); 69 creator.create(new FileOutputStream(path)); 70 } 71 72 /*** 73 * BCEL generated. 74 */ 75 private void createMethod0() { 76 InstructionList instructionList = new InstructionList(); 77 MethodGen method = 78 new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, 79 "<init>", "org.throwableutil.ThrowableUtil", 80 instructionList, constantPoolGen); 81 82 instructionList.append(InstructionFactory.createLoad(Type.OBJECT, 0)); 83 instructionList.append(instructionFactory.createInvoke( 84 "java.lang.Object", 85 "<init>", 86 Type.VOID, 87 Type.NO_ARGS, 88 Constants.INVOKESPECIAL)); 89 90 instructionList.append(InstructionFactory.createReturn(Type.VOID)); 91 method.setMaxStack(); 92 method.setMaxLocals(); 93 classGen.addMethod(method.getMethod()); 94 instructionList.dispose(); 95 } 96 97 /*** 98 * BCEL generated. 99 */ 100 private void createMethod1() { 101 InstructionList instructionList = new InstructionList(); 102 MethodGen method = 103 new MethodGen(ACC_PUBLIC | ACC_STATIC, Type.VOID, 104 new Type[] {new ObjectType("java.lang.Throwable")}, 105 new String[] {"arg0"}, "throwAsRuntime", 106 "org.throwableutil.ThrowableUtil", instructionList, 107 constantPoolGen); 108 109 instructionList.append(InstructionFactory.createLoad(Type.OBJECT, 0)); 110 111 /* This instruction is commented. This is what 112 * allows AspectjMethodInterceptorAdvice 113 * to throw any Throwable without the compiler complaining about a 114 * checked exception. 115 il.append(_factory.createCheckCast( 116 new ObjectType("java.lang.RuntimeException"))); 117 */ 118 instructionList.append(InstructionConstants.ATHROW); 119 method.setMaxStack(); 120 method.setMaxLocals(); 121 classGen.addMethod(method.getMethod()); 122 instructionList.dispose(); 123 } 124 }

This page was automatically generated by Maven