Class dec.trek.ClassFileA .class file or classfile is the executable form of a class. The ClassFile object allows you to manipulate classfiles. This class does not have a public constructor. To reach a treks classfiles, you must use firstClassFile() or lastClassFile() in the Trek class, or override its atStartOf(ClassFile) or atEndOf(ClassFile) methods. You can also add classfiles to a trek using getClassFile and insertClassFile. Method Summary
Fieldspublic Object userDefined Field that can be used by an application developer to store arbitrary data about this object during the trek. Example: Suppose your trek could reach a classfile more than once. The following code fragment would insure that the classfiles methods are scanned only once. if (cf.userDefined!=null) return; Methodspublic void addInterface(String interface) Adds the specified interface to this classfile. This is mainly useful for adding empty interfaces like Serializable. Parameters: interface fully qualified class name to add. Its classfile need not exist yet. Outputs the class and member declarations of this classfile to <classname>.dmp in the current directory. Additionally outputs the types of classfile content specified by flags. Parameters: flags 0, or one or more of the DMP constants defined in the Trek class. Returns the Field of the alphabetically first field name defined within this classfile, or null if it has no fields. public FieldRef firstFieldRef() Returns the FieldRef of the first field reference in this classfile's constant pool, or null if there are none. Returns the Method of the alphabetically first method name defined within this classfile, or null if it has no methods. (Because a classfiles static initializer method is actually named <clinit> and its constructors are actually named <init>, they are always at the front of the list). public MethodRef firstMethodRef() Returns the MethodRef of the first method reference in this classfile's constant pool, or null if there are none. Returns the TypeRef of the first class or array reference in this classfile's constant pool, or null if there are none. public ValueRef firstValueRef() Returns the ValueRef of the first value constant in this classfile's constant pool, or null if there are none. Returns the number of bytes in this classfile Returns the Trek object that this classfile is within public boolean hasModifier(int flags) Returns true if the classfiles class was declared with any of the specified by flags. Parameters: flags one or more of the ACC constants defined in the Trek class. Example: For a program containing: private static myclass extends Object { } The following code in a Trek application would set acc to true. ClassFile cf = myclasss ClassFile object; boolean acc = cf.hasModifier(ACC_STATIC|ACC_PUBLIC) public boolean isAncestor(ClassFile cf) Returns true if this is one of the superclasses of the specified class, and false if it is not. However if isAncestor cannot access a classfile that is needed to help make this determination, an exception is thrown. Parameters: cf the ClassFile to check Returns the Field of the alphabetically last field name defined within this classfile. Returns the Method of the alphabetically last method name defined within this classfile. Returns the ClassFile of the alphabetically next classfile in the trek. Returns the ClassFile of the alphabetically previous classfile in the trek. public static ClassFile read(DataInputStream stream, Trek trek) Reads a classfile from the specified stream and returns a ClassFile object for it. But if there is an error reading or interpreting data in the stream, an exception is thrown instead. The returned ClassFile is freestanding; it is neither cached in the trek nor inserted into the trek's list of classes. So if and when you clear all your references to it, it will be garbage collected. However you can insert the returned ClassFile into a trek's class list by calling insertClassFile. Parameters: stream the stream to read from trek this trek is made the parent of the created ClassFile object public boolean references(Member mem) Returns true if this classfile has code that uses the specified field or method. Parameters: mem the Method or Field object to check. (Member is the superclass of these two classes). Checks the time of last modification of the classfile, and rereads it if it has changed since the last time it was read in. This includes the case of a new file being in an earlier directory of the classpath. Any unsaved instrumentations are lost when a classfile is reread. Conversely the classfile is marked as unmodified. References to the ClassFile object itself remain valid. However if you have cached a reference to any code object within the classfile, the cached reference is now invalid and must be updated or discarded. public String[] toInterfaces() Returns an array containing the fully qualified name of each interface that this class implements. If this class implements no interfaces, the returned array's size is 0. Returns the unqualified name of this classfiles class (eg. Trek). Returns a persistent value that identifies this classfile. A reference to this classfile can be acquired by calling getObject on a trek that this classfile is within. Returns the package of this classfiles class. If the class is not in a package, it returns the null string. public String toQualifiedName() Returns the fully qualified name of this classfiles class (eg. dec.trek.Trek). Returns the source statement that defined this classfiles class. Returns the fully qualified name of this classfiles superclass. If this classfile has no superclass, it returns null. public void write(DataOutputStream stream, boolean clean) Writes the classfile to the specified stream, and marks its in-memory representation as unmodified. Parameters: stream the output stream to write to clean true says to remove all changes made to the classfile's in-memory representation Example: This method can be used in conjunction with a class loader: ByteArrayOutputStream bst = new ByteArrayOutputStream(); DataOutputStream dst = new DataOutputStream(bst); classfile.write(dst, false); String name = classfile.toQualifiedName(); byte data[] = bst.toByteArray(); classloader.defineClass(name, data, 0, data.length); |