jef.dna.attributes.impl
Class Code

java.lang.Object
  extended by jef.dna.attributes.impl.GenericAttribute
      extended by jef.dna.attributes.impl.Code
All Implemented Interfaces:
java.lang.Cloneable, Attribute, CodeAttribute, BytesCountable

public class Code
extends GenericAttribute
implements CodeAttribute

The Code class represents a Code Attribute as defined by the JVM Class file Specification.

Author:
Francesco Russo

Field Summary
protected static Logger logger
           
static int MAX_CODE_LENGTH_VALUE
           
 
Fields inherited from class jef.dna.attributes.impl.GenericAttribute
attribFactory, attributeNameIndex, dna, instrFactory, length
 
Constructor Summary
Code()
          Constructor.
 
Method Summary
 java.lang.Object clone()
          The clone() method will return a new instance whose reference to the containing Dna instance is set to null.
 void fromStream(java.io.DataInputStream dis, Dna dna, int nameIndex, int length)
          This method fills in the current Code instance reading all the required values from the specified input stream.
 java.util.List<Instruction> generateObjectiveCode(int[] bytecode)
          This method generates an object oriented representation of the bytecode contained within this method.
 int getAttributeCount()
          Gets the number of attributes of the current Code attribute.
 java.util.List<Attribute> getAttributes()
          Gets the set of attributes associated with the current Code attribute.
 int getBytesCount()
          This method must return the overall number of bytes required by the component implementing this interface.
 int[] getCode()
          This method gets this Code Attribute's bytecode, as an int array.
 int getCodeLength()
          Gets the numeber of bytes making up this method's actual code.
 java.util.List<ExceptionTableEntry> getExceptionTable()
          Gets the exception table.
 int getExceptionTableLength()
          Gets the number of entries in stored within the Exceptio Table
 int getMaxLocals()
          Gets the number of local variables allocated upon method invocation.
 int getMaxStack()
          Gets the maximum depth of this method's operand stack during execution.
 java.util.List<Instruction> getObjectiveCode()
          This method returns the object oriented representation of this method's code.
protected  void innerValidations(ConstantPoolTable cpt)
          This method guarantees the validation process' workflow is executed correctly.
 void setCode(int[] code)
          This method sets this Code Attribute's bytecode, as an int array.
 void setDna(Dna dna)
          Set the Dna instance which will contain the current Attribute
 void setExceptionTable(java.util.List<ExceptionTableEntry> table)
          Sets the exception table.
 void setMaxLocals(int maxLocals)
          Sets the number of local variables allocated upon method invocation.
 void setMaxStack(int maxStack)
          Sets the maximum depth of this method's operand stack during execution.
 void toStream(java.io.DataOutputStream dos)
          This method flushes to the given ouput stream all the information related to this object.
 void updateRefs(int offset)
          This method updates all the references that the current attribute holds to the constant pool table.
 void validateRefIntoCode(int ref)
          This method verifies that the given reference is a valid reference to one of the bytecode instructions contained within this Code attribute.
 
Methods inherited from class jef.dna.attributes.impl.GenericAttribute
getDna, getLength, getNameIndex, setNameIndex, validate, validateNameIndex
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface jef.dna.attributes.Attribute
getDna, getLength, getNameIndex, setNameIndex, validate
 

Field Detail

logger

protected static Logger logger

MAX_CODE_LENGTH_VALUE

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

Code

public Code()
Constructor. The default constructor takes no input parameters since they are automatically set via the fromStream method. Neverthless you can manipulate all this class' attributes via the appropriate getters and setters.

Method Detail

setMaxStack

public void setMaxStack(int maxStack)
Sets the maximum depth of this method's operand stack during execution.

Specified by:
setMaxStack in interface CodeAttribute
Parameters:
maxStack - Stack maximum depth

getMaxStack

public int getMaxStack()
Gets the maximum depth of this method's operand stack during execution.

Specified by:
getMaxStack in interface CodeAttribute
Returns:
int Stack maximum depth

setMaxLocals

public void setMaxLocals(int maxLocals)
Sets the number of local variables allocated upon method invocation.

Specified by:
setMaxLocals in interface CodeAttribute
Parameters:
maxLocals - Number of allocated local variables

getMaxLocals

public int getMaxLocals()
Gets the number of local variables allocated upon method invocation.

Specified by:
getMaxLocals in interface CodeAttribute
Returns:
int Number of allocated local variables

getCodeLength

public int getCodeLength()
Gets the numeber of bytes making up this method's actual code. This value must strictly be greater than zero.

Specified by:
getCodeLength in interface CodeAttribute
Returns:
int Number of bytes in the code array

getExceptionTableLength

public int getExceptionTableLength()
Gets the number of entries in stored within the Exceptio Table

Specified by:
getExceptionTableLength in interface CodeAttribute
Returns:
int Number of entries

setExceptionTable

public void setExceptionTable(java.util.List<ExceptionTableEntry> table)
Sets the exception table. This is a table describing exception handlers in the code array. It is represented as a List containing ExceptionTableEntry instances.

Parameters:
table - The Lsit of instances

getExceptionTable

public java.util.List<ExceptionTableEntry> getExceptionTable()
Gets the exception table. This is a table describing exception handlers in the code array. It is represented as a List instance, containing ExceptionTableEntry instances.

Specified by:
getExceptionTable in interface CodeAttribute
Returns:
List The List of instances

getAttributeCount

public int getAttributeCount()
Gets the number of attributes of the current Code attribute.

Specified by:
getAttributeCount in interface CodeAttribute
Returns:
int

getAttributes

public java.util.List<Attribute> getAttributes()
Gets the set of attributes associated with the current Code attribute. This set is represented by a List holding instances of subclasses of the GenericAttribute base class.

Specified by:
getAttributes in interface CodeAttribute
Returns:
List Set of attributes

getObjectiveCode

public java.util.List<Instruction> getObjectiveCode()
This method returns the object oriented representation of this method's code.

Specified by:
getObjectiveCode in interface CodeAttribute
Returns:
List The object or. code representation generated by the generateObjectiveCode method.

setCode

public void setCode(int[] code)
This method sets this Code Attribute's bytecode, as an int array.

Specified by:
setCode in interface CodeAttribute
Parameters:
code - The code as int array

getCode

public int[] getCode()
This method gets this Code Attribute's bytecode, as an int array.

Specified by:
getCode in interface CodeAttribute
Returns:
int[] The code as int array

generateObjectiveCode

public java.util.List<Instruction> generateObjectiveCode(int[] bytecode)
This method generates an object oriented representation of the bytecode contained within this method.

Specified by:
generateObjectiveCode in interface CodeAttribute
Parameters:
bytecode - The method's bytecode
Returns:
List The object oriented code representation

toStream

public void toStream(java.io.DataOutputStream dos)
              throws DnaSerializationException
This method flushes to the given ouput stream all the information related to this object.

Specified by:
toStream in interface Attribute
Parameters:
dos - The ouput stream to write to
Throws:
DnaSerializationException

fromStream

public void fromStream(java.io.DataInputStream dis,
                       Dna dna,
                       int nameIndex,
                       int length)
                throws DnaDeserializationException,
                       MissingDnaContainerException
This method fills in the current Code instance reading all the required values from the specified input stream.

Specified by:
fromStream in interface Attribute
Parameters:
dis - The input stream
dna - A reference to the current Dna instance
attributeNameIndex - Index of the current attribute's name into the constant pool table
length - The length of the attribute but the initial six bytes
Throws:
DnaDeserializationException
MissingDnaContainerException

updateRefs

public void updateRefs(int offset)
This method updates all the references that the current attribute holds to the constant pool table.

Specified by:
updateRefs in interface Attribute
Specified by:
updateRefs in class GenericAttribute
Parameters:
offset - The offset to apply

validateRefIntoCode

public void validateRefIntoCode(int ref)
                         throws UnknownOpCodeException,
                                MissingDnaContainerException,
                                java.lang.IndexOutOfBoundsException
This method verifies that the given reference is a valid reference to one of the bytecode instructions contained within this Code attribute.

Specified by:
validateRefIntoCode in interface CodeAttribute
Parameters:
ref - The reference to be verified
Throws:
UnknownOpCodeException - One of the instructions of the current attribute is not recognized
MissingDnaContainerException - There is no reference to the Dna instance this attribute belongs to
java.lang.IndexOutOfBoundsException - Error during processing

innerValidations

protected void innerValidations(ConstantPoolTable cpt)
                         throws AttributeValidationException,
                                AttributeOutOfRangeException,
                                MissingDnaContainerException,
                                MissingCPTableException
This method guarantees the validation process' workflow is executed correctly.

Overrides:
innerValidations in class GenericAttribute
Parameters:
cpt - The constant pool table
Throws:
MissingCPTableException
MissingDnaContainerException
AttributeValidationException
AttributeOutOfRangeException

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
The clone() method will return a new instance whose reference to the containing Dna instance is set to null. It is up to you appropriately setting it to the correct value.

Specified by:
clone in interface Attribute
Overrides:
clone in class GenericAttribute
Throws:
java.lang.CloneNotSupportedException

setDna

public void setDna(Dna dna)
Description copied from class: GenericAttribute
Set the Dna instance which will contain the current Attribute

Specified by:
setDna in interface Attribute
Overrides:
setDna in class GenericAttribute
Parameters:
dna - The "container" Dna

getBytesCount

public int getBytesCount()
Description copied from interface: BytesCountable
This method must return the overall number of bytes required by the component implementing this interface.

Specified by:
getBytesCount in interface BytesCountable