jbet
Class ClassInfo

java.lang.Object
  extended by jbet.ClassInfo

public final class ClassInfo
extends java.lang.Object

Represent a Java class file in memory, using data structures that are closely related to the physical layout of a class file on disk, but are more convenient for manipulation. The ClassInfo object knows how to read itself in from disk and to write itself out to disk. This is a central building block for JBET. Abstractly, a JBET operation consists of: 1) Read in one or more ClassInfo objects. 2) Query or modify them (possibly translating them to more abstract data structures). 3) Optionally write out one more more ClassInfo objects. This file contains fields that hold a ClassInfo, and code that: 1) initializes the fields 2) does file i/o 3) follows links to read in inner classes to separate ClassInfos. 4) assembly/disassembly. 5) printout code to display ClassInfo data. Note that, if a class has an inner class, the inner class has its own class file, and thus its own ClassInfo object.


Field Summary
static int ACC_ABSTRACT
           
static int ACC_ALL_INNER_CFLAGS
           
static int ACC_FINAL
           
static int ACC_INTERFACE
           
static int ACC_PUBLIC
           
static int ACC_SUPER
           
 int accessFlags
           
 ClassPathElement classPathElement
           
 jbet.ConstantPool constantPool
           
 boolean deprecated
           
 boolean dirty
           
 java.util.Vector fields
           
 java.util.Vector interfaces
           
static java.lang.String JbetLogFacility
           
 java.util.Vector methods
           
 java.lang.String sourceFile
           
 java.lang.String superClass
           
 boolean synthetic
           
 java.lang.String thisClass
           
 
Constructor Summary
ClassInfo(ClassInfo cr)
          Create a new ClassInfo by copying from another.
ClassInfo(ClassPathElement cpElement, Lexer lexer)
          Supports the JBET assembler/disassembler feature.
ClassInfo(ClassPathElement cpElement, java.lang.String thisClass)
          Construct a new, empty ClassInfo object.
ClassInfo(java.io.InputStream in)
          Create either a single ClassInfo, or a tree of ClassInfo objects.
ClassInfo(java.io.InputStream in, ClassPathElement cpElement)
           
ClassInfo(java.io.InputStream in, ClassPathElement cpElement, java.lang.String filename)
           
 
Method Summary
 void addField(FieldInfo fi)
           
 void addMethod(MethodInfo mi)
           
static java.lang.String allCommonAncestors(java.lang.String classA, java.lang.String classB)
           
 boolean checkAccess(java.lang.String fromPackage)
           
static java.lang.String common_ancestor(java.lang.String classA, java.lang.String classB)
          this should be in Util as well Used by Type.
 jbet.ConstantPool constantPool()
           
 void disassemble(LineWriter out, java.lang.String prefix)
          JBET assembler support that isn't in a constructor.
 FieldInfo fieldAt(int i)
           
 FieldInfo findField(java.lang.String name)
           
 MethodInfo findMethod(java.lang.String name)
           
 MethodInfo findMethod(java.lang.String name, Descriptor type)
           
 MethodInfo findMethod(java.lang.String name, java.lang.String type)
           
 int findMethodIndex(java.lang.String name, Descriptor type)
           
 FieldInfo getField(java.lang.String name)
           
 java.lang.String getPackageName()
           
 ClassPathElement getPathElement()
           
 java.lang.String getSuperName()
           
static boolean hasAncestors(java.lang.String child, java.util.Vector parents)
           
 jbet.InnerClassInfo innerClassAt(int i)
           
 boolean inPackage(java.lang.String packname)
           
 java.lang.String interfaceAt(int i)
           
static boolean isAncestor(java.lang.String child, java.lang.String parent)
          This should really be in Util.
 boolean isAnon()
          Returns true if the class is an anonymous inner class of either flavor.
 boolean isDirty()
           
 boolean isFinal()
           
 boolean isInterface()
           
 MethodInfo methodAt(int i)
           
 java.lang.String name()
           
 int numFields()
           
 int numInterfaces()
           
 int numMethods()
           
 void printAll(LineWriter out)
           
 void printcp(LineWriter out, int v)
           
 void printFields(LineWriter out, boolean header)
           
 void printFields(LineWriter out, int v)
           
 void printMethodNames(LineWriter out)
           
 void printMethods(LineWriter out, int v)
           
 void printout(LineWriter out, int v)
          Functions to print portions of this ClassInfo object.
 void relocate(java.util.Hashtable subs)
          This method is the top level of a name-changing mechanism that spans a number of the core JBET abstractions.
 void removeField(FieldInfo fi)
           
 void removeMethod(MethodInfo fi)
           
 void resolveConstants()
          The purpose of this method, and the methods it calls, is to reconstruct a valid, unique ConstantPool.
 void setDirty()
           
 void setOutdir(ClassPathElement cpe)
           
 java.lang.String toString()
          Return this class's name.
 void write()
          Called by Jbet.
 void writeFile(java.io.DataOutputStream dataOut)
           
 void writeFile(java.io.OutputStream out)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

JbetLogFacility

public static java.lang.String JbetLogFacility

classPathElement

public ClassPathElement classPathElement

dirty

public boolean dirty

constantPool

public jbet.ConstantPool constantPool

accessFlags

public int accessFlags

thisClass

public java.lang.String thisClass

superClass

public java.lang.String superClass

interfaces

public java.util.Vector interfaces

fields

public java.util.Vector fields

methods

public java.util.Vector methods

synthetic

public boolean synthetic

deprecated

public boolean deprecated

sourceFile

public java.lang.String sourceFile

ACC_PUBLIC

public static final int ACC_PUBLIC
See Also:
Constant Field Values

ACC_FINAL

public static final int ACC_FINAL
See Also:
Constant Field Values

ACC_SUPER

public static final int ACC_SUPER
See Also:
Constant Field Values

ACC_INTERFACE

public static final int ACC_INTERFACE
See Also:
Constant Field Values

ACC_ABSTRACT

public static final int ACC_ABSTRACT
See Also:
Constant Field Values

ACC_ALL_INNER_CFLAGS

public static final int ACC_ALL_INNER_CFLAGS
See Also:
Constant Field Values
Constructor Detail

ClassInfo

public ClassInfo(ClassPathElement cpElement,
                 java.lang.String thisClass)
Construct a new, empty ClassInfo object. The object is initialized only with empty or default values. To be meaningful, the initialization must be finalized.

Parameters:
cpElement - Input/Output streams for the new object.
thisClass - The name of the new class.

ClassInfo

public ClassInfo(java.io.InputStream in)
          throws ClassFileException,
                 java.io.IOException
Create either a single ClassInfo, or a tree of ClassInfo objects. A single ClassInfo represents a single .class file. A tree of ClassInfo objects represents a .class file, along with all of its inner classes. When inner classes are read, additional ClassInfo objects are created and pointed to from the root object.

Parameters:
in - The data stream from which to read the root ClassInfo.
cpElement - Input/Output streams for the new object.
parent - Should be set to null if this is the root.
Throws:
ClassFileException
java.io.IOException

ClassInfo

public ClassInfo(java.io.InputStream in,
                 ClassPathElement cpElement)
          throws ClassFileException,
                 java.io.IOException
Throws:
ClassFileException
java.io.IOException

ClassInfo

public ClassInfo(java.io.InputStream in,
                 ClassPathElement cpElement,
                 java.lang.String filename)
          throws ClassFileException,
                 java.io.IOException
Throws:
ClassFileException
java.io.IOException

ClassInfo

public ClassInfo(ClassInfo cr)
Create a new ClassInfo by copying from another. The data structures comprising the new object (e.g., Vectors) are created independently so that subsequent changes to the original object will _not_ affect the new object.

Parameters:
cr - The original ClassInfo.

ClassInfo

public ClassInfo(ClassPathElement cpElement,
                 Lexer lexer)
          throws ClassFileException
Supports the JBET assembler/disassembler feature. Creates a ClassInfo object tree by reading tokens from the lexer. The lexer is reading text written in the JBET Assembler Language, which is described TBD.

Parameters:
cpElement - Input/Output streams for the new object.
lexer - A lexer whose tokens drive the object's construction.
Throws:
ClassFileException
Method Detail

setOutdir

public void setOutdir(ClassPathElement cpe)

getField

public FieldInfo getField(java.lang.String name)
                   throws ElementNotFoundException
Throws:
ElementNotFoundException

write

public void write()
           throws java.lang.Exception
Called by Jbet. Write this object out in a .class file in the correct directory. Reconstructs the constant pool before writing.

Throws:
java.lang.Exception

writeFile

public void writeFile(java.io.OutputStream out)
               throws java.io.IOException
Throws:
java.io.IOException

writeFile

public void writeFile(java.io.DataOutputStream dataOut)
               throws java.io.IOException
Throws:
java.io.IOException

relocate

public void relocate(java.util.Hashtable subs)
This method is the top level of a name-changing mechanism that spans a number of the core JBET abstractions. This mechanism is needed because some operations, like class renaming, have ripple effects. For example, a change to a class's name will affect the class's inner class names, possibly the contents of Java descriptor strings, etc. This method drives appropriate substitutions throughout the JBET system.

Parameters:
subs - A table of string substitution pairs. The key values should be the old names, and the values associated with the keys in the table should be the replacement values. For example, call subs.put(name, newname) prior to calling relocate.

toString

public java.lang.String toString()
Return this class's name.

Overrides:
toString in class java.lang.Object

name

public java.lang.String name()

getPathElement

public ClassPathElement getPathElement()

isAnon

public boolean isAnon()
Returns true if the class is an anonymous inner class of either flavor. TBD - explain the differences among the different kinds of inner class names.


disassemble

public void disassemble(LineWriter out,
                        java.lang.String prefix)
JBET assembler support that isn't in a constructor. Dissassemble an entire class.

Parameters:
out -
prefix -

resolveConstants

public void resolveConstants()
The purpose of this method, and the methods it calls, is to reconstruct a valid, unique ConstantPool. The class file format defines the constant pool records to be unique. Maintaining centralized, unique, data structures to represent simple constant values (such as int, float, and String) during JBET modifications of a binary would complicate JBET. Instead of doing that, we represent simple constants where most convenient within the JBET data structures, and traverse them to construct a valid ConstantPool before writing it out to disk.


getSuperName

public java.lang.String getSuperName()

constantPool

public jbet.ConstantPool constantPool()

numMethods

public int numMethods()

numFields

public int numFields()

numInterfaces

public int numInterfaces()

interfaceAt

public java.lang.String interfaceAt(int i)

fieldAt

public FieldInfo fieldAt(int i)

methodAt

public MethodInfo methodAt(int i)

addMethod

public void addMethod(MethodInfo mi)

addField

public void addField(FieldInfo fi)

removeField

public void removeField(FieldInfo fi)

removeMethod

public void removeMethod(MethodInfo fi)

innerClassAt

public jbet.InnerClassInfo innerClassAt(int i)

findMethod

public MethodInfo findMethod(java.lang.String name,
                             java.lang.String type)

findMethod

public MethodInfo findMethod(java.lang.String name)

findMethod

public MethodInfo findMethod(java.lang.String name,
                             Descriptor type)

findField

public FieldInfo findField(java.lang.String name)

findMethodIndex

public int findMethodIndex(java.lang.String name,
                           Descriptor type)

printout

public void printout(LineWriter out,
                     int v)
Functions to print portions of this ClassInfo object.


printFields

public void printFields(LineWriter out,
                        int v)

printFields

public void printFields(LineWriter out,
                        boolean header)

printMethodNames

public void printMethodNames(LineWriter out)

printMethods

public void printMethods(LineWriter out,
                         int v)

printcp

public void printcp(LineWriter out,
                    int v)

printAll

public void printAll(LineWriter out)

isInterface

public boolean isInterface()

isFinal

public boolean isFinal()

isAncestor

public static boolean isAncestor(java.lang.String child,
                                 java.lang.String parent)
                          throws ClassFileException
This should really be in Util. Used by Type. returns true if child inherits from parent? Supports interfaces.

Parameters:
child -
parent -
Throws:
ClassFileException

common_ancestor

public static java.lang.String common_ancestor(java.lang.String classA,
                                               java.lang.String classB)
                                        throws ClassFileException
this should be in Util as well Used by Type. Return the nearest (in the inheritance tree) common ancestor of two classes. Note that all classes have, java/lang/Object as their common ancestor, but they may share a closer common ancestor. When interfaces are involved, there may be more than one closest common ancestor. In that case, this function returns one of them. allCommonAncestors will return all the closest common ancestors.

Throws:
ClassFileException

hasAncestors

public static boolean hasAncestors(java.lang.String child,
                                   java.util.Vector parents)
                            throws ClassFileException
Throws:
ClassFileException

allCommonAncestors

public static java.lang.String allCommonAncestors(java.lang.String classA,
                                                  java.lang.String classB)
                                           throws ClassFileException
Throws:
ClassFileException

isDirty

public boolean isDirty()

setDirty

public void setDirty()

inPackage

public boolean inPackage(java.lang.String packname)

getPackageName

public java.lang.String getPackageName()

checkAccess

public boolean checkAccess(java.lang.String fromPackage)