com.claritysys.jvm.classfile
Class CfMethod

java.lang.Object
  extended by com.claritysys.jvm.classfile.CfEntry
      extended by com.claritysys.jvm.classfile.CfFieldOrMethod
          extended by com.claritysys.jvm.classfile.CfMethod
All Implemented Interfaces:
AttributeHandler

public class CfMethod
extends CfFieldOrMethod
implements AttributeHandler

A method declared within a ClassFile.

Methods are organized into a linked list within our ClassFile, rather than an array, to allow for efficient dynamic growth.

This class differs greatly from the ClassFile representation, in that the members of the Code attribute are represented here rather than having a separate Code class. From a programmer's perspective, a Method has a sequence of instructions, rather than a Method having a Code attribute which then has instructions.


Constructor Summary
CfMethod(ClassFile classFile)
           
 
Method Summary
 void addHandler(ExceptionHandler h)
           
 void addHandler(int startPc, int endPc, int handlerPc, CpClass catchType)
           
 void addLine(LineNumber line)
          Append the given line number to the end of the linked-list of locals for this line number table.
 void addLocal(LocalVariable local)
          Append the given local to the end of the linked-list of locals for this local variable table.
 void createLocalsFromSignature()
          Based on the method signature, automatically build a set of matching LocalVariables.
 byte[] getCode()
           
 Attribute getCodeAttributes()
           
 CodeBuilder getCodeBuilder()
          Return a CodeBuilder for constructing the method code attribute.
 int[] getExceptionCps()
           
 int getHandlerCount()
           
 ExceptionHandler getHandlers()
           
 ExceptionHandler getLastHandler()
           
 LineNumber getLastLine()
           
 LocalVariable getLastLocal()
           
 int getLineCount()
           
 LineNumber getLines()
           
 LocalVariable getLocal(int index)
          Return the first found local variable that occupies the given index.
 LocalVariable getLocals()
           
 int getLocalsCount()
           
 int getMaxLocals()
          Max local variable index as per class file specification.
 int getMaxStack()
           
 CfMethod getNext()
           
 CpRef getRef()
          Return a Cp(Method)Ref for invoking this method.
 int getReturnType()
          Return the return type of this method as one of the JVM.T_XXX constants.
 boolean handleAttribute(java.io.DataInputStream dataIn, java.lang.String name, int length)
          For methods we must recognize: Code, Exceptions, Synthetic, Deprecated.
 void read(java.io.DataInputStream dataIn)
          Read the member from the given stream, overwriting any previous values.
 void setCode(byte[] code)
           
 void setCodeAttributes(Attribute codeAttributes)
           
 void setExceptionCps(int[] exceptionCps)
           
 void setExceptions(java.lang.Class[] exceptions)
           
 void setHandlerCount(int handlerCount)
           
 void setHandlers(ExceptionHandler handlers)
           
 void setLastHandler(ExceptionHandler lastHandler)
           
 void setLastLine(LineNumber lastLine)
           
 void setLastLocal(LocalVariable lastLocal)
           
 void setLineCount(int lineCount)
           
 void setLines(LineNumber lines)
           
 void setLocals(LocalVariable locals)
           
 void setLocalsCount(int localsCount)
           
 void setMaxLocals(int maxLocals)
           
 void setMaxStack(int maxStack)
           
 void setNext(CfMethod next)
           
 void write(java.io.DataOutput dout)
          Write the field or method to a stream in JVM classfile format.
 
Methods inherited from class com.claritysys.jvm.classfile.CfFieldOrMethod
getClassFile, getConstantPool, getDescriptorIndex, getName, getNameIndex, getSignature, isSynthetic, setDescriptorIndex, setNameIndex, setSynthetic
 
Methods inherited from class com.claritysys.jvm.classfile.CfEntry
getAccessFlags, getAttributeCount, getAttributes, isAbstract, isDeprecated, isFinal, isInterface, isNative, isPrivate, isProtected, isPublic, isStatic, isStrict, isSynchronized, isTransient, isVolatile, setAbstract, setAccessFlags, setAttributes, setDeprecated, setFinal, setInterface, setNative, setPrivate, setProtected, setPublic, setStatic, setStrict, setSynchronized, setTransient, setVolatile
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CfMethod

public CfMethod(ClassFile classFile)
Method Detail

getNext

public CfMethod getNext()

setNext

public void setNext(CfMethod next)

read

public void read(java.io.DataInputStream dataIn)
          throws java.io.IOException,
                 ClassFileFormatException
Description copied from class: CfFieldOrMethod
Read the member from the given stream, overwriting any previous values.

Subclasses must override this method, first invoking this one, and then processing attributes.

  field_info {
          u2 access_flags;
          u2 name_index;
          u2 descriptor_index;
          u2 attributes_count;
          attribute_info attributes[attributes_count];
 }

  method_info {
      u2 access_flags;
            u2 name_index;
            u2 descriptor_index;
            u2 attributes_count;
            attribute_info attributes[attributes_count];
 }
 

Overrides:
read in class CfFieldOrMethod
Throws:
java.io.IOException
ClassFileFormatException

handleAttribute

public boolean handleAttribute(java.io.DataInputStream dataIn,
                               java.lang.String name,
                               int length)
                        throws java.io.IOException,
                               ClassFileFormatException
For methods we must recognize: Code, Exceptions, Synthetic, Deprecated. We also handle the attribute table for any encountered Code attribute, so this also must process: LineNumberTable, LocalVariableTable.

Specified by:
handleAttribute in interface AttributeHandler
Parameters:
dataIn - DataInputStream, positioned to attribute's data.
name - Attribute name from constant pool.
length - Length of info[] in bytes.
Returns:
true if the handler processed it, false if not.
Throws:
java.io.IOException - If an I/O error ocurrs.
ClassFileFormatException - If the attribute was processed but was incorrectly formed.

write

public void write(java.io.DataOutput dout)
           throws java.io.IOException
Description copied from class: CfFieldOrMethod
Write the field or method to a stream in JVM classfile format.

Subclasses must override this and extend the behavior by writing the attribute table.

Overrides:
write in class CfFieldOrMethod
Throws:
java.io.IOException

addLocal

public void addLocal(LocalVariable local)
Append the given local to the end of the linked-list of locals for this local variable table.

Parameters:
local - The localvariable to append.

addLine

public void addLine(LineNumber line)
Append the given line number to the end of the linked-list of locals for this line number table.

Parameters:
line - the line number to add.

getReturnType

public int getReturnType()
Return the return type of this method as one of the JVM.T_XXX constants.


getCodeAttributes

public Attribute getCodeAttributes()

setCodeAttributes

public void setCodeAttributes(Attribute codeAttributes)

addHandler

public void addHandler(ExceptionHandler h)

addHandler

public void addHandler(int startPc,
                       int endPc,
                       int handlerPc,
                       CpClass catchType)

getExceptionCps

public int[] getExceptionCps()

setExceptionCps

public void setExceptionCps(int[] exceptionCps)

setExceptions

public void setExceptions(java.lang.Class[] exceptions)

getMaxStack

public int getMaxStack()

setMaxStack

public void setMaxStack(int maxStack)

getMaxLocals

public int getMaxLocals()
Max local variable index as per class file specification.

This is actually the NEXT available local index, as the maximum index for a double or long local variable is maxLocals - 2, and for any other local is maxLocals - 1.

Returns:
Max locals, eg the next available local variable index.

setMaxLocals

public void setMaxLocals(int maxLocals)

getCode

public byte[] getCode()

setCode

public void setCode(byte[] code)

getHandlerCount

public int getHandlerCount()

setHandlerCount

public void setHandlerCount(int handlerCount)

getHandlers

public ExceptionHandler getHandlers()

setHandlers

public void setHandlers(ExceptionHandler handlers)

getLastHandler

public ExceptionHandler getLastHandler()

setLastHandler

public void setLastHandler(ExceptionHandler lastHandler)

getLocalsCount

public int getLocalsCount()

setLocalsCount

public void setLocalsCount(int localsCount)

getLocals

public LocalVariable getLocals()

setLocals

public void setLocals(LocalVariable locals)

createLocalsFromSignature

public void createLocalsFromSignature()
Based on the method signature, automatically build a set of matching LocalVariables.

It is an error to invoke this if locals already are defined. This method is only for the case when a new method is generated and the user wants to work with LocalVariable objects for generating code.


getLocal

public LocalVariable getLocal(int index)
Return the first found local variable that occupies the given index.

Note that multiple locals can use the same index within different scopes. This method just returns the first one it finds, and not in any particular order. Specifically, it does not guarantee this to be the first declared local of this name.


getLastLocal

public LocalVariable getLastLocal()

setLastLocal

public void setLastLocal(LocalVariable lastLocal)

getLineCount

public int getLineCount()

setLineCount

public void setLineCount(int lineCount)

getLines

public LineNumber getLines()

setLines

public void setLines(LineNumber lines)

getLastLine

public LineNumber getLastLine()

setLastLine

public void setLastLine(LineNumber lastLine)

getRef

public CpRef getRef()
Return a Cp(Method)Ref for invoking this method.


getCodeBuilder

public CodeBuilder getCodeBuilder()
Return a CodeBuilder for constructing the method code attribute.

This is the preferred method, since the ClassFile caches a single CodeBuilder to be re-used.



Copyright ? 2000-2003 Clarity Systems Group, LLC. All Rights Reserved.