01: /*
02: * @(#)BCELCreatorUtil.java
03: *
04: * Copyright (C) 2003 Matt Albrecht
05: * groboclown@users.sourceforge.net
06: * http://groboutils.sourceforge.net
07: *
08: * Permission is hereby granted, free of charge, to any person obtaining a
09: * copy of this software and associated documentation files (the "Software"),
10: * to deal in the Software without restriction, including without limitation
11: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12: * and/or sell copies of the Software, and to permit persons to whom the
13: * Software is furnished to do so, subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in
16: * all copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24: * DEALINGS IN THE SOFTWARE.
25: */
26:
27: package net.sourceforge.groboutils.codecoverage.v2;
28:
29: import java.io.IOException;
30:
31: import org.apache.bcel.classfile.JavaClass;
32: import org.apache.bcel.classfile.Method;
33: import org.apache.bcel.generic.ConstantPoolGen;
34: import org.apache.bcel.generic.InstructionHandle;
35: import org.apache.bcel.generic.InstructionList;
36: import org.apache.bcel.generic.LDC;
37: import org.apache.bcel.generic.MethodGen;
38:
39: /**
40: * Helper for creating BCEL objects.
41: *
42: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
43: * @version $Date: 2004/04/15 05:48:27 $
44: * @since January 23, 2003
45: */
46: public class BCELCreatorUtil {
47: private static final Class THIS_CLASS = BCELCreatorUtil.class;
48:
49: public static JavaClass createJavaClass(Class c) throws IOException {
50: JavaClass jc = BytecodeLoaderUtil.loadJavaClass(c.getName());
51: return jc;
52: }
53:
54: public static Method getMethod(JavaClass jc, int methodIndex) {
55: Method mL[] = jc.getMethods();
56: Method m = mL[methodIndex];
57: return m;
58: }
59:
60: public static MethodGen createMethodGen(JavaClass jc,
61: int methodIndex) {
62: MethodGen mg = createMethodGen(jc, getMethod(jc, methodIndex));
63: return mg;
64: }
65:
66: public static MethodGen createMethodGen(JavaClass jc, Method m) {
67: ConstantPoolGen cpg = new ConstantPoolGen(jc.getConstantPool());
68: MethodGen mg = new MethodGen(m, jc.getClassName(), cpg);
69: return mg;
70: }
71:
72: public static InstructionList createInstructionList() {
73: InstructionList il = new InstructionList();
74: return il;
75: }
76:
77: public static InstructionHandle addInstructionHandle(
78: InstructionList il) {
79: InstructionHandle ih = il.append(new LDC(0));
80: return ih;
81: }
82:
83: public static InstructionHandle createInstructionHandle() {
84: return addInstructionHandle(createInstructionList());
85: }
86: }
|