Java API by
Zvi Har’El

cf
Class ClassFile

java.lang.Object
  extended by cf.ClassFile
Direct Known Subclasses:
Obfuscate, View

public class ClassFile
extends Object

An instance of class ClassFile is an in-core representation of a Java class file.

Version:
$Id: ClassFile.java,v 1.135 2003/11/23 06:58:30 rl Exp $
Author:
Zvi Har’El
See Also:
Class source code

Nested Class Summary
protected static class ClassFile.ByteArray
          An instance of the inner class ByteArray is an immutable wrapper to a byte[] object.
 
Field Summary
(package private)  short access
          Class access modifiers.
(package private)  Vector attributes
          Attributes of this class.
(package private)  Vector cP
          Constant pool.
(package private)  short cPCode
          UTF8 constant for “Code”.
(package private)  Hashtable cPHash
          Constant pool hash table.
(package private)  Vector fields
          Fields declared by this class.
(package private)  Vector interfaces
          Superinterfaces of this class.
(package private)  short major
          Major version number of this class file.
(package private)  Vector methods
          Methods declared by this class.
(package private)  short minor
          Minor version number of this class file.
(package private)  short superClass
          The direct superclass of this class.
(package private)  short thisClass
          The class defined by this class file.
 
Constructor Summary
ClassFile()
          Constructs an empty ClassFile object.
ClassFile(DataInputStream in)
          Constructs a ClassFile object by reading an input stream.
 
Method Summary
 void addInterface(String name)
          Adds a name to the list of interfaces implemented by this class.
private  short cp(byte[] b)
          Inserts a constant to the pool.
(package private)  short cp(byte tag, double c)
          Inserts a DOUBLE constant.
(package private)  short cp(byte tag, float c)
          Inserts a FLOAT constant.
(package private)  short cp(byte tag, int c)
          Inserts an INTEGER constant.
(package private)  short cp(byte tag, long c)
          Inserts a LONG constant.
private  short cp(byte tag, short i)
          Inserts a constant.
private  short cp(byte tag, short i, short j)
          Inserts a constant.
(package private)  short cp(byte tag, short i, String name, String desc)
          Inserts a FIELDREF, METHODREF or INTERFACEMETHODREF constant.
(package private)  short cp(byte tag, String s)
          Inserts a UTF8, CLASS or STRING constant.
(package private)  short cp(byte tag, String name, String desc)
          Inserts a NAMEANDTYPE constant.
(package private)  short cp(byte tag, String s, String name, String desc)
          Inserts a FIELDREF, METHODREF or INTERFACEMETHODREF constant.
(package private) static String desc(Class type)
          Returns a descriptor of a field.
(package private) static String desc(Class[] param, Class type)
          Returns a descriptor of a method.
static InputStream findClassInPath(String name)
          Finds a class file in the user class path.
(package private)  short findUTF8(String s)
          Finds a UTF8 constant in the pool.
 byte[] getBytes()
          Writes the ClassFile object on a byte array.
static ClassFile getClassFile(String name)
          Gets a ClassFile object for a named class by reading a class file from the user class path.
(package private)  Object readAttribute(DataInputStream in)
          Reads an attribute table from the input stream.
(package private)  void renameClass(short i, String name)
          Renames a class.
 void setName(String name)
          Sets the name of this class.
 void setSuper(String name)
          Sets the name of the direct superclass of this class.
 void strip()
          Strips attributes.
(package private) static byte[] toUTF8(String s)
          Computes the UTF-8 encoding of a string.
 void write(DataOutputStream out)
          Writes the ClassFile object on an output stream.
 void write(String s)
          Writes the ClassFile object on a file.
(package private) static void writeAttribute(DataOutputStream out, byte[] b)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cP

final Vector cP
Constant pool. Each entry is a ClassFile.ByteArray object.


cPHash

final Hashtable cPHash
Constant pool hash table. Maps byte arrays to pool indices.


cPCode

short cPCode
UTF8 constant for “Code”.


minor

short minor
Minor version number of this class file.


major

short major
Major version number of this class file.


access

short access
Class access modifiers.


thisClass

short thisClass
The class defined by this class file. An index to a CLASS constant.


superClass

short superClass
The direct superclass of this class. An index to a CLASS constant.


interfaces

final Vector interfaces
Superinterfaces of this class. Each entry is a Short object wrapping an index to a CLASS constant.


fields

final Vector fields
Fields declared by this class. Each entry is a MemberInfo object.


methods

final Vector methods
Methods declared by this class. Each entry is a MemberInfo object.


attributes

final Vector attributes
Attributes of this class. The only attribute possible at this level is “SourceFile”. Each entry is a byte array.

Constructor Detail

ClassFile

ClassFile()
Constructs an empty ClassFile object.


ClassFile

public ClassFile(DataInputStream in)
          throws IOException
Constructs a ClassFile object by reading an input stream.

Parameters:
in - the input stream
Throws:
ClassFormatError - if input cannot be interpreted as a class file
IOException - if input error occurs
Method Detail

getClassFile

public static ClassFile getClassFile(String name)
                              throws IOException,
                                     ClassNotFoundException
Gets a ClassFile object for a named class by reading a class file from the user class path.

Parameters:
name - the class name
Throws:
IOException - if input error occurs
ClassFormatError - if input cannot be interpreted as a class file
ClassNotFoundException - if class file is not found

findClassInPath

public static final InputStream findClassInPath(String name)
                                         throws ClassNotFoundException
Finds a class file in the user class path.

Parameters:
name - the class name
Returns:
an input stream
Throws:
ClassNotFoundException - if class file is not found

write

public final void write(DataOutputStream out)
                 throws IOException
Writes the ClassFile object on an output stream.

Parameters:
out - the output stream
Throws:
IOException - if output error occurs

write

public final void write(String s)
                 throws IOException
Writes the ClassFile object on a file.

Parameters:
s - a file name
Throws:
IOException - if output error occurs

getBytes

public final byte[] getBytes()
                      throws IOException
Writes the ClassFile object on a byte array.

Returns:
the byte array
Throws:
IOException - if output error occurs

strip

public final void strip()
Strips attributes.


cp

private final short cp(byte[] b)
Inserts a constant to the pool.

Parameters:
b - a byte array
Returns:
the index of the new constant

cp

private final short cp(byte tag,
                       short i)
Inserts a constant.

Parameters:
tag - a constant pool tag
i - an index to constant pool
Returns:
the index of the new constant

cp

private final short cp(byte tag,
                       short i,
                       short j)
Inserts a constant.

Parameters:
tag - a constant pool tag
i - an index to constant pool
j - an index to constant pool
Returns:
the index of the new constant

cp

final short cp(byte tag,
               String s)
Inserts a UTF8, CLASS or STRING constant. In the latter cases, an intermediate UTF8 constant is created.

Parameters:
tag - a constant pool tag
s - a String value
Returns:
the index of the new constant
See Also:
JVM Spec §4.4.7, §4.4.1, §4.4.3

cp

final short cp(byte tag,
               String name,
               String desc)
Inserts a NAMEANDTYPE constant. Two intermediate UTF8 constants are created.

Parameters:
tag - a constant pool tag
name - a member name
desc - a member descriptor
Returns:
the index of the new the constant
See Also:
JVM Spec §4.4.6

cp

final short cp(byte tag,
               String s,
               String name,
               String desc)
Inserts a FIELDREF, METHODREF or INTERFACEMETHODREF constant. Two intermediate CLASS and NAMEANDTYPE constants are created.

Parameters:
tag - a constant pool tag
s - a class name in internal form
name - a member name
desc - a member descriptor
Returns:
the index of the new constant
See Also:
JVM Spec §4.4.2

cp

final short cp(byte tag,
               short i,
               String name,
               String desc)
Inserts a FIELDREF, METHODREF or INTERFACEMETHODREF constant. An intermediate NAMEANDTYPE constant is created.

Parameters:
tag - a constant pool tag
i - an index to a CLASS constant
name - a member name
desc - a member descriptor
Returns:
the index of the new constant
See Also:
JVM Spec §4.4.2

cp

final short cp(byte tag,
               int c)
Inserts an INTEGER constant. Also called internally to insert a four-byte constant which contains a float value.

Parameters:
tag - a constant pool tag
c - an int value
Returns:
the index of the new constant
See Also:
JVM Spec §4.4.4

cp

final short cp(byte tag,
               float c)
Inserts a FLOAT constant.

Parameters:
tag - a constant pool tag
c - a float value
Returns:
the index of the new constant
See Also:
JVM Spec §4.4.4

cp

final short cp(byte tag,
               long c)
Inserts a LONG constant. Also called internally to insert an eight-byte constant which contains a double value. The next pool entry is marked invalid.
Note:
In retrospect, making eight-byte constants take two constant pool entries was a poor choice.

Parameters:
tag - a constant pool tag
c - a long value
Returns:
the index of the new constant
See Also:
JVM Spec §4.4.5, footnote 2 therein

cp

final short cp(byte tag,
               double c)
Inserts a DOUBLE constant.

Parameters:
tag - a constant pool tag
c - a double value
Returns:
the index of the new constant
See Also:
JVM Spec §4.4.5

toUTF8

static final byte[] toUTF8(String s)
Computes the UTF-8 encoding of a string.

Parameters:
s - a String value
Returns:
the equivalent byte array
See Also:
DataOutput.writeUTF(java.lang.String), RFC2279

findUTF8

final short findUTF8(String s)
Finds a UTF8 constant in the pool.

Parameters:
s - a String value
Returns:
the index of the constant, or zero if not found

setName

public final void setName(String name)
Sets the name of this class.

Parameters:
name - a class name.

setSuper

public final void setSuper(String name)
Sets the name of the direct superclass of this class.

Parameters:
name - a class name.

renameClass

final void renameClass(short i,
                       String name)
Renames a class. The CLASS constant is removed from the pool and a new CLASS constant is inserted in the same place.

Parameters:
i - an index to a CLASS constant
name - a class name in internal form.
See Also:
JVM Spec §4.2

addInterface

public final void addInterface(String name)
Adds a name to the list of interfaces implemented by this class.

Parameters:
name - a class name.

readAttribute

final Object readAttribute(DataInputStream in)
                    throws IOException
Reads an attribute table from the input stream. The attribute is stored in a byte array. Its length is not stored. “Code” attributes are transformed into CodeAttribute objects.

Parameters:
in - the input stream
Returns:
the attribute
Throws:
IOException - if an input error occurs.
See Also:
JVM Spec §4.7

writeAttribute

static final void writeAttribute(DataOutputStream out,
                                 byte[] b)
                          throws IOException
Throws:
IOException

desc

static final String desc(Class type)
Returns a descriptor of a field.

Parameters:
type - the field type
Returns:
the descriptor
See Also:
JVM Spec §4.3.2

desc

static final String desc(Class[] param,
                         Class type)
Returns a descriptor of a method. If the method has no parameters, either null or an empty Class array are acceptable as the parameter types.

Parameters:
param - the parameter types
type - the return type
Returns:
the descriptor
See Also:
JVM Spec §4.3.3

Java API by
Zvi Har’El

Copyright © Zvi Har’El
$Date: 2007/10/15 13:50:30$