1 18 package org.apache.tools.ant.taskdefs.optional.depend; 19 20 import java.io.DataInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.util.Vector ; 24 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ClassCPInfo; 25 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool; 26 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPoolEntry; 27 28 35 public class ClassFile { 36 37 38 private static final int CLASS_MAGIC = 0xCAFEBABE; 39 40 41 private ConstantPool constantPool; 42 43 44 private String className; 45 46 57 public void read(InputStream stream) throws IOException , ClassFormatError { 58 DataInputStream classStream = new DataInputStream (stream); 59 60 if (classStream.readInt() != CLASS_MAGIC) { 61 throw new ClassFormatError ("No Magic Code Found " 62 + "- probably not a Java class file."); 63 } 64 65 classStream.readUnsignedShort(); 67 classStream.readUnsignedShort(); 68 69 constantPool = new ConstantPool(); 71 72 constantPool.read(classStream); 73 constantPool.resolve(); 74 75 classStream.readUnsignedShort(); 76 int thisClassIndex = classStream.readUnsignedShort(); 77 classStream.readUnsignedShort(); 78 ClassCPInfo classInfo 79 = (ClassCPInfo) constantPool.getEntry(thisClassIndex); 80 className = classInfo.getClassName(); 81 } 82 83 84 89 public Vector getClassRefs() { 90 91 Vector classRefs = new Vector (); 92 93 for (int i = 0; i < constantPool.size(); ++i) { 94 ConstantPoolEntry entry = constantPool.getEntry(i); 95 96 if (entry != null 97 && entry.getTag() == ConstantPoolEntry.CONSTANT_CLASS) { 98 ClassCPInfo classEntry = (ClassCPInfo) entry; 99 100 if (!classEntry.getClassName().equals(className)) { 101 classRefs.addElement( 102 ClassFileUtils.convertSlashName(classEntry.getClassName())); 103 } 104 } 105 } 106 107 return classRefs; 108 } 109 110 115 public String getFullClassName() { 116 return ClassFileUtils.convertSlashName(className); 117 } 118 } 119 120 | Popular Tags |