001: /*
002: * @(#)CreateMainClassHelper.java
003: *
004: * Copyright (C) 2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2;
028:
029: import java.io.ByteArrayOutputStream;
030: import java.io.IOException;
031:
032: import org.apache.bcel.Constants;
033: import org.apache.bcel.classfile.JavaClass;
034: import org.apache.bcel.generic.ArrayType;
035: import org.apache.bcel.generic.ClassGen;
036: import org.apache.bcel.generic.ConstantPoolGen;
037: import org.apache.bcel.generic.InstructionFactory;
038: import org.apache.bcel.generic.InstructionList;
039: import org.apache.bcel.generic.MethodGen;
040: import org.apache.bcel.generic.Type;
041:
042: /**
043: * Helper for creating a main class through bytecode.
044: *
045: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
046: * @version $Date: 2004/04/15 05:48:27 $
047: * @since May 2, 2003
048: */
049: public class CreateMainClassHelper {
050: private static final Class THIS_CLASS = CreateMainClassHelper.class;
051:
052: private String className;
053: private ClassGen cg;
054: private MethodGen mg;
055:
056: private JavaClass jc;
057: private byte[] bytecode;
058:
059: public ConstantPoolGen cp;
060: public InstructionList il;
061: public InstructionFactory factory;
062:
063: public CreateMainClassHelper(String className) {
064: this .className = className;
065:
066: this .cg = new ClassGen(className, Object.class.getName(),
067: "<generated>", Constants.ACC_PUBLIC
068: | Constants.ACC_SUPER, null);
069: this .cp = this .cg.getConstantPool();
070: this .il = new InstructionList();
071: this .mg = new MethodGen(
072: Constants.ACC_STATIC | Constants.ACC_PUBLIC, // access flags
073: Type.VOID, // return type
074: new Type[] { // argument types
075: new ArrayType(Type.STRING, 1) },
076: new String[] { "argv" }, // arg names
077: "main", "HelloWorld", // method, class
078: this .il, this .cp);
079: this .factory = new InstructionFactory(this .cg);
080: }
081:
082: public void close() throws IOException {
083: this .mg.setMaxStack();
084: this .cg.addMethod(this .mg.getMethod());
085: this .il.dispose();
086: this .cg.addEmptyConstructor(Constants.ACC_PUBLIC);
087:
088: this .il = null;
089: this .mg = null;
090: this .cp = null;
091:
092: this .jc = cg.getJavaClass();
093:
094: ByteArrayOutputStream baos = new ByteArrayOutputStream();
095: this .jc.dump(baos);
096: this .bytecode = baos.toByteArray();
097: }
098:
099: public JavaClass getJavaClass() {
100: return this .jc;
101: }
102:
103: public byte[] getBytecode() {
104: return this .bytecode;
105: }
106:
107: public Class getGenClass() throws ClassNotFoundException {
108: Class cc = BytecodeLoaderUtil.loadClassFromBytecode(
109: this.className, this.bytecode);
110: return cc;
111: }
112:
113: }
|