KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)BytecodeLoaderUtil.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.ByteArrayInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32
33 import junit.framework.Assert;
34 import net.sourceforge.groboutils.util.io.v1.ReadByteStream;
35
36 import org.apache.bcel.classfile.ClassParser;
37 import org.apache.bcel.classfile.Code;
38 import org.apache.bcel.classfile.JavaClass;
39 import org.apache.bcel.generic.ConstantPoolGen;
40
41
42 /**
43  * Helper for loading bytecode.
44  *
45  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
46  * @version $Date: 2004/04/15 05:48:27 $
47  * @since January 7, 2003
48  */

49 public class BytecodeLoaderUtil
50 {
51     private static final Class JavaDoc THIS_CLASS = BytecodeLoaderUtil.class;
52
53     public static String JavaDoc getClassFilename( Class JavaDoc c )
54     {
55         Assert.assertNotNull( "Null class.", c );
56         return getClassFilename( c.getName() );
57     }
58     
59     public static String JavaDoc getClassFilename( String JavaDoc name )
60     {
61         Assert.assertNotNull( "Null class name.", name );
62         String JavaDoc filename = name.replace( '.', '/' )+".class";
63         return filename;
64     }
65     
66     public static InputStream JavaDoc loadBytecodeStream( String JavaDoc filename )
67             throws IOException JavaDoc
68     {
69         Assert.assertNotNull( "Null filename.", filename );
70         ClassLoader JavaDoc cl = THIS_CLASS.getClassLoader();
71         InputStream JavaDoc is = cl.getSystemResourceAsStream( filename );
72         Assert.assertNotNull(
73             "resource '"+filename+"' could not be found.",
74             is );
75         return is;
76     }
77     
78     public static byte[] loadBytecode( String JavaDoc filename )
79             throws IOException JavaDoc
80     {
81         Assert.assertNotNull( "Null filename.", filename );
82         InputStream JavaDoc is = loadBytecodeStream( filename );
83         return ReadByteStream.readByteStream( is );
84     }
85     
86     public static JavaClass loadJavaClass( String JavaDoc className )
87             throws IOException JavaDoc
88     {
89         String JavaDoc filename = getClassFilename( className );
90         InputStream JavaDoc is = loadBytecodeStream( filename );
91         ClassParser cp = new ClassParser( is, filename );
92         JavaClass jc = cp.parse();
93         return jc;
94     }
95     
96     public static JavaClass loadJavaClass( String JavaDoc className, byte[] bytecode )
97             throws IOException JavaDoc
98     {
99         String JavaDoc filename = getClassFilename( className );
100         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc( bytecode );
101         ClassParser cp = new ClassParser( is, filename );
102         JavaClass jc = cp.parse();
103         return jc;
104     }
105
106     
107     public static Class JavaDoc loadClassFromBytecode( String JavaDoc className, byte[] b )
108             throws ClassNotFoundException JavaDoc
109     {
110         ArrayClassLoader acl = new ArrayClassLoader();
111         acl.addClass( className, b );
112         Class JavaDoc c = acl.loadClass( className );
113         return c;
114     }
115     
116     
117     public static void runMain( Class JavaDoc cz )
118             throws Exception JavaDoc
119     {
120         String JavaDoc s[] = new String JavaDoc[0];
121         java.lang.reflect.Method JavaDoc m = cz.getMethod( "main",
122             new Class JavaDoc[] { s.getClass() } );
123         m.invoke( null, new Object JavaDoc[] { s } );
124     }
125     
126     
127     public static void verifyClass( String JavaDoc className, byte[] b )
128             throws Exception JavaDoc
129     {
130         JavaClass jc = loadJavaClass( className, b );
131         
132         // ensure the integrety of the class file.
133
org.apache.bcel.generic.ClassGen modClass =
134             new org.apache.bcel.generic.ClassGen( jc );
135         ConstantPoolGen constantPool = modClass.getConstantPool();
136         org.apache.bcel.classfile.Method mL[] = modClass.getMethods();
137         
138         for (int i = 0; i < mL.length; ++i)
139         {
140             verifyMethod( mL[i], constantPool );
141         }
142     }
143     
144     
145     public static void verifyMethod( org.apache.bcel.classfile.Method m,
146             ConstantPoolGen cp )
147             throws Exception JavaDoc
148     {
149         Code c = m.getCode();
150         org.apache.bcel.classfile.CodeException ce[] = c.getExceptionTable();
151         for (int i = 0; i < ce.length; ++i)
152         {
153             /* this isn't an error - it can really happen
154             Assert.assertTrue(
155                 "Method "+m+" includes a '0' catch type in code exception.",
156                 ce[i].getCatchType() != 0);
157             */

158             
159             // a better test is to check this catch type against the
160
// original catch type.
161
}
162     }
163 }
164
165
Popular Tags