1 /* 2 Copyright (c) 2003, Laurent Caillette. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without modifica- 6 tion, are permitted provided that the following conditions are met: 7 8 1. Redistributions of source code must retain the above copyright notice, 9 this list of conditions and the following disclaimer. 10 11 2. Redistributions in binary form must reproduce the above copyright notice, 12 this list of conditions and the following disclaimer in the documentation 13 and/or other materials provided with the distribution. 14 15 3. The end-user documentation included with the redistribution, if any, must 16 include the following acknowledgment: "This product includes software 17 written by Laurent Caillette." 18 Alternately, this acknowledgment may appear in the software itself, if 19 and wherever such third-party acknowledgments normally appear. 20 21 4. The name "Laurent Caillette" must not be used to endorse or 22 promote products derived from this software without 23 prior written permission. For written permission, please contact 24 laurent.caillette@laposte.net 25 26 5. Products derived from this software may not be called 27 "Laurent Caillette", nor may "Laurent Caillette" appear 28 in their name, without prior written permission of the 29 author. 30 31 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 32 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 33 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 34 AUTHOR (LAURENT CAILLETTE) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 36 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 37 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 41 */ 42 package tweed.system.interceptor; 43 44 import org.apache.bcel.generic.*; 45 import org.apache.bcel.classfile.*; 46 import org.apache.bcel.*; 47 import java.io.*; 48 49 public class HardCodedBuilder implements Constants { 50 51 52 private InstructionFactory _factory; 53 private ConstantPoolGen _cp; 54 private ClassGen _cg; 55 56 public HardCodedBuilder() { 57 _cg = new ClassGen("tweed.system.interceptor.MySimpleBean$Interceptor", "tweed.system.interceptor.MySimpleBean", "MySimpleBeanInterceptor.java", ACC_PUBLIC | ACC_SUPER, new String[] { }); 58 59 _cp = _cg.getConstantPool(); 60 _factory = new InstructionFactory(_cg, _cp); 61 } 62 63 public void create(OutputStream out) throws IOException { 64 createFields(); 65 createMethod_0(); 66 createMethod_1(); 67 createMethod_2(); 68 _cg.getJavaClass().dump(out); 69 } 70 71 private void createFields() { 72 FieldGen field; 73 74 field = new FieldGen(ACC_PRIVATE | ACC_FINAL, new ObjectType("tweed.system.interceptor.MySimpleBean"), "subject", _cp); 75 _cg.addField(field.getField()); 76 77 field = new FieldGen(ACC_PRIVATE | ACC_FINAL, new ObjectType("tweed.system.interceptor.InterceptionHandler"), "interceptor", _cp); 78 _cg.addField(field.getField()); 79 80 field = new FieldGen(ACC_PUBLIC | ACC_FINAL, new ArrayType(new ObjectType("tweed.system.interceptor.MethodDescriptor"), 1), "methodDescriptors", _cp); 81 _cg.addField(field.getField()); 82 } 83 84 private void createMethod_0() { 85 InstructionList il = new InstructionList(); 86 MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, new Type[] { new ObjectType("tweed.system.interceptor.MySimpleBean"), new ObjectType("tweed.system.interceptor.InterceptionHandler") }, new String[] { "arg0", "arg1" }, "<init>", "tweed.system.interceptor.MySimpleBean$Interceptor", il, _cp); 87 88 InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0)); 89 il.append(_factory.createInvoke("tweed.system.interceptor.MySimpleBean", "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL)); 90 InstructionHandle ih_4 = il.append(_factory.createLoad(Type.OBJECT, 0)); 91 il.append(new PUSH(_cp, 2)); 92 il.append(_factory.createNewArray(new ObjectType("tweed.system.interceptor.MethodDescriptor"), (short) 1)); 93 il.append(_factory.createFieldAccess("tweed.system.interceptor.MySimpleBean$Interceptor", "methodDescriptors", new ArrayType(new ObjectType("tweed.system.interceptor.MethodDescriptor"), 1), Constants.PUTFIELD)); 94 InstructionHandle ih_12 = il.append(_factory.createLoad(Type.OBJECT, 0)); 95 il.append(_factory.createLoad(Type.OBJECT, 1)); 96 il.append(_factory.createFieldAccess("tweed.system.interceptor.MySimpleBean$Interceptor", "subject", new ObjectType("tweed.system.interceptor.MySimpleBean"), Constants.PUTFIELD)); 97 InstructionHandle ih_17 = il.append(_factory.createLoad(Type.OBJECT, 0)); 98 il.append(_factory.createLoad(Type.OBJECT, 2)); 99 il.append(_factory.createFieldAccess("tweed.system.interceptor.MySimpleBean$Interceptor", "interceptor", new ObjectType("tweed.system.interceptor.InterceptionHandler"), Constants.PUTFIELD)); 100 InstructionHandle ih_22 = il.append(_factory.createReturn(Type.VOID)); 101 method.setMaxStack(); 102 method.setMaxLocals(); 103 _cg.addMethod(method.getMethod()); 104 il.dispose(); 105 } 106 107 private void createMethod_1() { 108 InstructionList il = new InstructionList(); 109 MethodGen method = new MethodGen(ACC_PUBLIC, Type.OBJECT, Type.NO_ARGS, new String[] { }, "getMyObject", "tweed.system.interceptor.MySimpleBean$Interceptor", il, _cp); 110 111 InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0)); 112 il.append(_factory.createFieldAccess("tweed.system.interceptor.MySimpleBean$Interceptor", "interceptor", new ObjectType("tweed.system.interceptor.InterceptionHandler"), Constants.GETFIELD)); 113 il.append(_factory.createLoad(Type.OBJECT, 0)); 114 il.append(_factory.createFieldAccess("tweed.system.interceptor.MySimpleBean$Interceptor", "subject", new ObjectType("tweed.system.interceptor.MySimpleBean"), Constants.GETFIELD)); 115 il.append(_factory.createLoad(Type.OBJECT, 0)); 116 il.append(_factory.createFieldAccess("tweed.system.interceptor.MySimpleBean$Interceptor", "methodDescriptors", new ArrayType(new ObjectType("tweed.system.interceptor.MethodDescriptor"), 1), Constants.GETFIELD)); 117 il.append(new PUSH(_cp, 0)); 118 il.append(InstructionConstants.AALOAD); 119 il.append(_factory.createInvoke("tweed.system.interceptor.InterceptionHandler", "handleMethod", Type.VOID, new Type[] { Type.OBJECT, new ObjectType("tweed.system.interceptor.MethodDescriptor") }, Constants.INVOKEVIRTUAL)); 120 InstructionHandle ih_17 = il.append(_factory.createLoad(Type.OBJECT, 0)); 121 il.append(_factory.createFieldAccess("tweed.system.interceptor.MySimpleBean$Interceptor", "subject", new ObjectType("tweed.system.interceptor.MySimpleBean"), Constants.GETFIELD)); 122 il.append(_factory.createInvoke("tweed.system.interceptor.MySimpleBean", "getMyObject", Type.OBJECT, Type.NO_ARGS, Constants.INVOKEVIRTUAL)); 123 InstructionHandle ih_24 = il.append(_factory.createReturn(Type.OBJECT)); 124 method.setMaxStack(); 125 method.setMaxLocals(); 126 _cg.addMethod(method.getMethod()); 127 il.dispose(); 128 } 129 130 private void createMethod_2() { 131 InstructionList il = new InstructionList(); 132 MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, new Type[] { Type.OBJECT }, new String[] { "arg0" }, "setMyObject", "tweed.system.interceptor.MySimpleBean$Interceptor", il, _cp); 133 134 InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0)); 135 il.append(_factory.createFieldAccess("tweed.system.interceptor.MySimpleBean$Interceptor", "subject", new ObjectType("tweed.system.interceptor.MySimpleBean"), Constants.GETFIELD)); 136 il.append(_factory.createLoad(Type.OBJECT, 1)); 137 il.append(_factory.createInvoke("tweed.system.interceptor.MySimpleBean", "setMyObject", Type.VOID, new Type[] { Type.OBJECT }, Constants.INVOKEVIRTUAL)); 138 InstructionHandle ih_8 = il.append(_factory.createReturn(Type.VOID)); 139 method.setMaxStack(); 140 method.setMaxLocals(); 141 _cg.addMethod(method.getMethod()); 142 il.dispose(); 143 } 144 145 }

This page was automatically generated by Maven