1 26 27 package net.sourceforge.groboutils.codecoverage.v2; 28 29 import java.io.ByteArrayInputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 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 49 public class BytecodeLoaderUtil 50 { 51 private static final Class THIS_CLASS = BytecodeLoaderUtil.class; 52 53 public static String getClassFilename( Class c ) 54 { 55 Assert.assertNotNull( "Null class.", c ); 56 return getClassFilename( c.getName() ); 57 } 58 59 public static String getClassFilename( String name ) 60 { 61 Assert.assertNotNull( "Null class name.", name ); 62 String filename = name.replace( '.', '/' )+".class"; 63 return filename; 64 } 65 66 public static InputStream loadBytecodeStream( String filename ) 67 throws IOException 68 { 69 Assert.assertNotNull( "Null filename.", filename ); 70 ClassLoader cl = THIS_CLASS.getClassLoader(); 71 InputStream 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 filename ) 79 throws IOException 80 { 81 Assert.assertNotNull( "Null filename.", filename ); 82 InputStream is = loadBytecodeStream( filename ); 83 return ReadByteStream.readByteStream( is ); 84 } 85 86 public static JavaClass loadJavaClass( String className ) 87 throws IOException 88 { 89 String filename = getClassFilename( className ); 90 InputStream 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 className, byte[] bytecode ) 97 throws IOException 98 { 99 String filename = getClassFilename( className ); 100 ByteArrayInputStream is = new ByteArrayInputStream ( bytecode ); 101 ClassParser cp = new ClassParser( is, filename ); 102 JavaClass jc = cp.parse(); 103 return jc; 104 } 105 106 107 public static Class loadClassFromBytecode( String className, byte[] b ) 108 throws ClassNotFoundException 109 { 110 ArrayClassLoader acl = new ArrayClassLoader(); 111 acl.addClass( className, b ); 112 Class c = acl.loadClass( className ); 113 return c; 114 } 115 116 117 public static void runMain( Class cz ) 118 throws Exception 119 { 120 String s[] = new String [0]; 121 java.lang.reflect.Method m = cz.getMethod( "main", 122 new Class [] { s.getClass() } ); 123 m.invoke( null, new Object [] { s } ); 124 } 125 126 127 public static void verifyClass( String className, byte[] b ) 128 throws Exception 129 { 130 JavaClass jc = loadJavaClass( className, b ); 131 132 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 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 158 159 } 162 } 163 } 164 165 | Popular Tags |