javar
Class JavaByteCodeFile

java.lang.Object
  |
  +--javar.JavaByteCodeFile

public class JavaByteCodeFile
extends Object

This class will represent the structure of a class file. It is constructed using a physical location of a class file.

Author:
Ben Litchfield

Field Summary
static int MAGIC_NUMBER
          CAFEBABE the cafe where magic happens
static short MAJOR_NUMBER
          These is the default major number.
static short MINOR_NUMBER
          These is the default minor number.
 
Constructor Summary
JavaByteCodeFile(File theFile)
          Constructor
JavaByteCodeFile(InputStream stream)
          This will take in input stream and create a java file representation from that.
JavaByteCodeFile(String cName)
          Constructor, uses java.lang.Object as its parent
JavaByteCodeFile(String cName, String pName)
          Constructor
 
Method Summary
 void addMethod(Method method)
          This will add a method to the class
 int getAccessFlags()
          This will return the access flags for this class
 long getClassName()
          This will return index of the name of the class
 String getClassNameAsString()
          This will return the fully qualified class name that contains both the name of the class and the package for the class.
 ConstantPool getConstantPool()
          This will return the constant pool for this class.
 int getFieldCount()
          This will return the number of fields that this class has
 int getInterfaceCount()
          This will return the number of interfaces that this class deals with
 int getMagicNumber()
          This will return the magic number for this class this should always return 0xCAFEBABE which is 3405691582 as an integer.
 int getMajorNumber()
          This will return the major number for this class this should always return 3 which is the current major number for java class files.
 int getMethodCount()
          This will return the number of methods that are in this class
 int getMinorNumber()
          This will return the minor number for this class this should always return 45 which is the current major number for java class files.
 String getPackageName()
          This will return the name of this classes package.
 long getParentClassName()
          This will return the index of the name of the parent Class
 String getParentClassNameAsString()
          This will return the fully qualified super class name that contains both the name of the class and the package for the class.
 String getShortClassName()
          This will return the name of this class without the package.
 String getShortParentClassName()
          This will return the name of this class without the package.
 boolean isInterface()
          This will tell if this class is an interface or not.
 Iterator listFields()
          This will list all of the interfaces for this class.
 Iterator listInterfaces()
          This will list all of the interfaces for this class.
 Iterator listMethods()
          This will get all the methods that are part of this class file
 void setAccessFlags(int val)
          This will set the access flags for this class
 void setClassName(int val)
          This will set index of the name of the class
 void setConstantPool(ConstantPool pool)
          This will set the constant pool for this class
 void setMagicNumber(int number)
          This will set the magic number for this class.
 void setMajorNumber(int number)
          This will set the major number for this class.
 void setMinorNumber(int number)
          This will set the minor number for this class.
 void setParentClassName(int val)
          This will set the index of the name of the parent Class
 byte[] toBytes()
          This will convert this byte code file into an array of class bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAGIC_NUMBER

public static final int MAGIC_NUMBER
CAFEBABE the cafe where magic happens

See Also:
Constant Field Values

MAJOR_NUMBER

public static final short MAJOR_NUMBER
These is the default major number. It was a number used in a version of the JDK.

See Also:
Constant Field Values

MINOR_NUMBER

public static final short MINOR_NUMBER
These is the default minor number. It was a number used in a version of the JDK.

See Also:
Constant Field Values
Constructor Detail

JavaByteCodeFile

public JavaByteCodeFile(String cName)
Constructor, uses java.lang.Object as its parent

Parameters:
cName - The name of the class to create

JavaByteCodeFile

public JavaByteCodeFile(String cName,
                        String pName)
Constructor

Parameters:
cName - The name of the class to create
pName - The name of the super class

JavaByteCodeFile

public JavaByteCodeFile(File theFile)
                 throws FileNotFoundException,
                        JavarParseException
Constructor

Parameters:
theFile -
Throws:
FileNotFoundException
JavarParseException

JavaByteCodeFile

public JavaByteCodeFile(InputStream stream)
                 throws JavarParseException
This will take in input stream and create a java file representation from that.

Parameters:
stream - A stream that can be used to initialize all values of this bytecodefile
Method Detail

addMethod

public void addMethod(Method method)
This will add a method to the class

Parameters:
method - The method to add

listMethods

public Iterator listMethods()
This will get all the methods that are part of this class file

Returns:
a List of all methods

getMagicNumber

public int getMagicNumber()
This will return the magic number for this class this should always return 0xCAFEBABE which is 3405691582 as an integer. If the magic number has not been set -1 will be returned.

Returns:
the magic number

getMajorNumber

public int getMajorNumber()
This will return the major number for this class this should always return 3 which is the current major number for java class files.

Returns:
the major number

getMinorNumber

public int getMinorNumber()
This will return the minor number for this class this should always return 45 which is the current major number for java class files.

Returns:
the major number

getConstantPool

public ConstantPool getConstantPool()
This will return the constant pool for this class.

Returns:
the constant pool

getAccessFlags

public int getAccessFlags()
This will return the access flags for this class

Returns:
the access flags

getClassName

public long getClassName()
This will return index of the name of the class

Returns:
index

getClassNameAsString

public String getClassNameAsString()
This will return the fully qualified class name that contains both the name of the class and the package for the class.

Returns:
The name of the class.

getShortClassName

public String getShortClassName()
This will return the name of this class without the package.

Returns:
The name of this class;

getPackageName

public String getPackageName()
This will return the name of this classes package. A non-null empty string will be returned if there is no package.

Returns:
The name of the package.

getParentClassName

public long getParentClassName()
This will return the index of the name of the parent Class

Returns:
parent class index

getParentClassNameAsString

public String getParentClassNameAsString()
This will return the fully qualified super class name that contains both the name of the class and the package for the class.

Returns:
The name of the class that this class extends.

getShortParentClassName

public String getShortParentClassName()
This will return the name of this class without the package.

Returns:
The name of this class;

getInterfaceCount

public int getInterfaceCount()
This will return the number of interfaces that this class deals with

Returns:
number of class interfaces

listInterfaces

public Iterator listInterfaces()
This will list all of the interfaces for this class.

Returns:
The list of interfaces that this class implements.

getFieldCount

public int getFieldCount()
This will return the number of fields that this class has

Returns:
field count

listFields

public Iterator listFields()
This will list all of the interfaces for this class.

Returns:
The list of interfaces that this class implements.

getMethodCount

public int getMethodCount()
This will return the number of methods that are in this class

Returns:
method count

isInterface

public boolean isInterface()
This will tell if this class is an interface or not.

Returns:
true if this class is an interface

setMagicNumber

public void setMagicNumber(int number)
This will set the magic number for this class.

Parameters:
number - the magic number to set for this class

setMajorNumber

public void setMajorNumber(int number)
This will set the major number for this class.

Parameters:
number - the major number to set for this class

setMinorNumber

public void setMinorNumber(int number)
This will set the minor number for this class.

Parameters:
number - the minor number to set for this class

setConstantPool

public void setConstantPool(ConstantPool pool)
This will set the constant pool for this class


setAccessFlags

public void setAccessFlags(int val)
This will set the access flags for this class


setClassName

public void setClassName(int val)
This will set index of the name of the class


setParentClassName

public void setParentClassName(int val)
This will set the index of the name of the parent Class


toBytes

public byte[] toBytes()
               throws JavarIOException
This will convert this byte code file into an array of class bytes.

Returns:
a byte array that conforms to the class file specification
Throws:
JavarIOException - if there was an error writing the data