View Javadoc
1 package attrib4j.bcel; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.ObjectInputStream; 7 import java.util.ArrayList; 8 import java.util.Arrays; 9 10 import org.apache.bcel.classfile.ClassParser; 11 import org.apache.bcel.classfile.Field; 12 import org.apache.bcel.classfile.JavaClass; 13 import org.apache.bcel.classfile.Method; 14 import org.apache.bcel.classfile.Unknown; 15 16 import attrib4j.Attribute; 17 import attrib4j.AttributeExtractor; 18 import attrib4j.Log; 19 20 /*** 21 * This is the BCEL implementation for extracting attributes from the 22 * class file. 23 * 24 * TODO Consider returning a Set or List 25 * TODO more convenience methods for selection of attributes 26 * TODO code cleanup - put reading of attribute in common util method. 27 * 28 * Created: Thu Oct 17 19:17:15 2002 29 * 30 * @author <a href="mailto:mpollack@speakeasy.net"></a> 31 * @version $Revsion$ $Date: 2003/04/24 05:41:54 $ 32 */ 33 34 public class BCELAttributeExtractor implements AttributeExtractor { 35 36 private JavaClass _javaClass; 37 38 public BCELAttributeExtractor() { 39 40 } 41 42 /*** 43 * Open the classfile and parse it in to the BCEL library. 44 * 45 * @param qualifiedClassname The class name to load. 46 * @param cl the classloader to use to get the inputstream of the .class 47 * file. 48 */ 49 public void open(String qualifiedClassname, ClassLoader cl) { 50 51 String classFileName = qualifiedClassname.replace('.', '/') + ".class"; 52 53 try { 54 InputStream clsInput = cl.getResourceAsStream(classFileName); 55 _javaClass = new ClassParser(clsInput, classFileName).parse(); 56 57 } catch (IOException ioEx) { 58 // Not sure what to do here--repackage and throw? 59 ioEx.printStackTrace(); 60 } 61 } 62 63 public Attribute[] getClassAttributes() { 64 return getClassAttributes(null); 65 } 66 67 public Attribute[] getClassAttributes(Class filter) { 68 ArrayList al = new ArrayList(); 69 org.apache.bcel.classfile.Attribute[] attribs = 70 _javaClass.getAttributes(); 71 for (int i = 0; i < attribs.length; i++) { 72 if (attribs[i] instanceof Unknown) { 73 Unknown unknownAttrib = (Unknown) attribs[i]; 74 Log.log( 75 "Found Class attribute #" 76 + i 77 + " with name = " 78 + unknownAttrib.getName(), 79 Log.INFO, 80 this); 81 byte[] serializedCustomAttribute = unknownAttrib.getBytes(); 82 try { 83 Object customAttr = 84 new ObjectInputStream( 85 new ByteArrayInputStream(serializedCustomAttribute)) 86 .readObject(); 87 if (customAttr instanceof Attribute) { 88 if (filter != null) { 89 if (filter.isInstance(customAttr)) { 90 al.add((Attribute) customAttr); 91 } 92 } else { 93 al.add((Attribute) customAttr); 94 } 95 } 96 } catch (IOException ioEx) { 97 //TODO: repackage and throw common exception..? 98 ioEx.printStackTrace(); 99 } catch (Exception ex) { 100 ex.printStackTrace(); 101 } 102 } 103 } 104 return (Attribute[]) al.toArray(new Attribute[al.size()]); 105 } 106 107 108 /*** 109 * Return all the attributes associated with a method that have a 110 * particular method signature. 111 * 112 * @param methodName The name of the method. 113 * @param methodParamTypes An array of parameter types as given by 114 * the reflection api. 115 * @return An array of attributes. 116 */ 117 public Attribute[] getMethodAttributes( 118 String methodName, 119 String[] methodParamTypes) { 120 return getMethodAttributes(methodName,methodParamTypes,null); 121 } 122 123 /*** 124 * Return all the attributes associated with a method that have a 125 * particular method signature. 126 * 127 * @param methodName The name of the method. 128 * @param methodParamTypes An array of parameter types as given by 129 * the reflection api. 130 * @return An array of attributes. 131 */ 132 public Attribute[] getMethodAttributes( 133 String methodName, 134 String[] methodParamTypes, 135 Class filter ) { 136 ArrayList al = new ArrayList(); 137 138 //TODO: use reflection api to only get method of a particular 139 //signature 140 Method[] classfileMethod = _javaClass.getMethods(); 141 142 for (int i = 0; i < classfileMethod.length; i++) { 143 if (classfileMethod[i].getName().equals(methodName)) { 144 if (Arrays 145 .equals( 146 methodParamTypes, 147 DescriptorUtil.convertToJavaFormat( 148 classfileMethod[i].getSignature()))) { 149 150 org.apache.bcel.classfile.Attribute[] attribs = 151 classfileMethod[i].getAttributes(); 152 Log.log( 153 "Found " + attribs.length + " method attributes", 154 Log.INFO); 155 for (int j = 0; j < attribs.length; j++) { 156 Log.log( 157 "Method attribute is of type = " 158 + attribs[j].getClass().getName(), 159 Log.INFO); 160 161 if (attribs[j] instanceof Unknown) { 162 Unknown unknownAttrib = (Unknown) attribs[j]; 163 Log.log( 164 "Found Method attribute #" 165 + j 166 + " with name = " 167 + unknownAttrib.getName(), 168 Log.INFO); 169 byte[] serializedCustomAttribute = 170 unknownAttrib.getBytes(); 171 try { 172 Object customAttr = 173 new ObjectInputStream( 174 new ByteArrayInputStream(serializedCustomAttribute)) 175 .readObject(); 176 if (customAttr instanceof Attribute) { 177 178 if (filter != null) { 179 if (filter.isInstance(customAttr)) { 180 al.add((Attribute) customAttr); 181 } 182 } else { 183 al.add((Attribute) customAttr); 184 } 185 186 187 } 188 } catch (IOException ioEx) { 189 //TODO: repackage and throw common exception..? 190 ioEx.printStackTrace(); 191 } catch (Exception ex) { 192 ex.printStackTrace(); 193 } 194 } 195 } 196 } //arrays.equals 197 } 198 } 199 return (Attribute[]) al.toArray(new Attribute[al.size()]); 200 } 201 202 /*** 203 * Return all the attributes associated with a field. 204 * 205 * @param fieldName The name of the field. 206 * @return An array of attributes. 207 */ 208 public Attribute[] getFieldAttributes(String fieldName) { 209 ArrayList al = new ArrayList(); 210 Field[] classfileField = _javaClass.getFields(); 211 for (int i = 0; i < classfileField.length; i++) { 212 if (classfileField[i].getName().equals(fieldName)) { 213 org.apache.bcel.classfile.Attribute[] attribs = 214 classfileField[i].getAttributes(); 215 Log.log( 216 "Found " + attribs.length + " field attributes", 217 Log.INFO); 218 for (int j = 0; j < attribs.length; j++) { 219 Log.log( 220 "Field attribute is of type = " 221 + attribs[j].getClass().getName(), 222 Log.INFO); 223 if (attribs[j] instanceof Unknown) { 224 Unknown unknownAttrib = (Unknown) attribs[j]; 225 Log.log( 226 "Found field attribute #" 227 + j 228 + " with name = " 229 + unknownAttrib.getName(), 230 Log.INFO); 231 byte[] serializedCustomAttribute = 232 unknownAttrib.getBytes(); 233 try { 234 Object customAttr = 235 new ObjectInputStream( 236 new ByteArrayInputStream(serializedCustomAttribute)) 237 .readObject(); 238 if (customAttr instanceof Attribute) { 239 al.add(customAttr); 240 } 241 } catch (IOException ioEx) { 242 //TODO: repackage and throw common exception..? 243 ioEx.printStackTrace(); 244 } catch (Exception ex) { 245 ex.printStackTrace(); 246 } 247 } 248 } 249 } 250 } 251 return (Attribute[]) al.toArray(new Attribute[al.size()]); 252 } 253 254 /*** 255 * A do-nothing method for now. 256 * 257 */ 258 public void close() { 259 } 260 261 } // BCELAttributeExtractor

This page was automatically generated by Maven