KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > CreateMainClassHelper


1 /*
2  * @(#)CreateMainClassHelper.java
3  *
4  * Copyright (C) 2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * 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.ByteArrayOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import org.apache.bcel.Constants;
33 import org.apache.bcel.classfile.JavaClass;
34 import org.apache.bcel.generic.ArrayType;
35 import org.apache.bcel.generic.ClassGen;
36 import org.apache.bcel.generic.ConstantPoolGen;
37 import org.apache.bcel.generic.InstructionFactory;
38 import org.apache.bcel.generic.InstructionList;
39 import org.apache.bcel.generic.MethodGen;
40 import org.apache.bcel.generic.Type;
41
42
43
44 /**
45  * Helper for creating a main class through bytecode.
46  *
47  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
48  * @version $Date: 2004/04/15 05:48:27 $
49  * @since May 2, 2003
50  */

51 public class CreateMainClassHelper
52 {
53     private static final Class JavaDoc THIS_CLASS = CreateMainClassHelper.class;
54     
55     private String JavaDoc className;
56     private ClassGen cg;
57     private MethodGen mg;
58     
59     private JavaClass jc;
60     private byte[] bytecode;
61     
62     public ConstantPoolGen cp;
63     public InstructionList il;
64     public InstructionFactory factory;
65     
66     
67     
68     public CreateMainClassHelper( String JavaDoc className )
69     {
70         this.className = className;
71         
72         this.cg = new ClassGen( className,
73             Object JavaDoc.class.getName(), "<generated>",
74             Constants.ACC_PUBLIC | Constants.ACC_SUPER, null );
75         this.cp = this.cg.getConstantPool();
76         this.il = new InstructionList();
77         this.mg = new MethodGen(
78             Constants.ACC_STATIC | Constants.ACC_PUBLIC, // access flags
79
Type.VOID, // return type
80
new Type[] { // argument types
81
new ArrayType(Type.STRING, 1) },
82             new String JavaDoc[] { "argv" }, // arg names
83
"main", "HelloWorld", // method, class
84
this.il, this.cp );
85         this.factory = new InstructionFactory( this.cg );
86     }
87     
88     
89     public void close()
90             throws IOException JavaDoc
91     {
92         this.mg.setMaxStack();
93         this.cg.addMethod( this.mg.getMethod() );
94         this.il.dispose();
95         this.cg.addEmptyConstructor( Constants.ACC_PUBLIC );
96         
97         this.il = null;
98         this.mg = null;
99         this.cp = null;
100         
101         this.jc = cg.getJavaClass();
102         
103         
104         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
105         this.jc.dump( baos );
106         this.bytecode = baos.toByteArray();
107     }
108     
109     
110     public JavaClass getJavaClass()
111     {
112         return this.jc;
113     }
114     
115     
116     public byte[] getBytecode()
117     {
118         return this.bytecode;
119     }
120     
121     
122     public Class JavaDoc getGenClass()
123             throws ClassNotFoundException JavaDoc
124     {
125         Class JavaDoc cc = BytecodeLoaderUtil.loadClassFromBytecode(
126             this.className, this.bytecode );
127         return cc;
128     }
129     
130 }
131
132
Popular Tags