224 lines
6.7 KiB
Java
224 lines
6.7 KiB
Java
|
|
package com.de22.javabytecode;
|
||
|
|
|
||
|
|
import java.io.IOException;
|
||
|
|
import com.common.util.*;
|
||
|
|
import com.de22.javabytecode.constant.*;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Copyright Wynne Crisman 1999,2006<p>
|
||
|
|
*/
|
||
|
|
public class Field extends Component {
|
||
|
|
private short accessFlags = 0; //The visibility of the field and the field modifiers.//
|
||
|
|
private short fieldNameIndex = 0; //The index in the constant collection of the name of this field.//
|
||
|
|
private short descriptorIndex = 0; //The index in the constant collection of the descriptor for this field.//
|
||
|
|
private IList attributes = new LiteList(10, 30); //The attributes associated with this field.//
|
||
|
|
/**
|
||
|
|
* Field constructor.
|
||
|
|
*/
|
||
|
|
protected Field(Type type) {
|
||
|
|
super(type);
|
||
|
|
}//Field()//
|
||
|
|
/**
|
||
|
|
* Field constructor.
|
||
|
|
*/
|
||
|
|
public Field(Type type, short accessFlags, short descriptorIndex, short fieldNameIndex) {
|
||
|
|
super(type);
|
||
|
|
|
||
|
|
this.accessFlags = accessFlags;
|
||
|
|
this.descriptorIndex = descriptorIndex;
|
||
|
|
this.fieldNameIndex = fieldNameIndex;
|
||
|
|
}//Field()//
|
||
|
|
/**
|
||
|
|
* Adds an attribute to the method.
|
||
|
|
* An attribute describes some property of the method.
|
||
|
|
* @param attribute The attribute to add to the method. The attribute's name will automatically be added to the constant pool.
|
||
|
|
* @return The index of the attribute in the attribute pool.
|
||
|
|
*/
|
||
|
|
public short addAttribute(CodeAttribute attribute) {
|
||
|
|
short nameIndex = getType().addConstant(new ConstantUtf8(getType(), attribute.getDefaultName()), false);
|
||
|
|
|
||
|
|
attribute.setNameIndex(nameIndex);
|
||
|
|
attributes.add(attribute);
|
||
|
|
|
||
|
|
return (short) (attributes.getSize() - 1);
|
||
|
|
}//addAttribute()//
|
||
|
|
/**
|
||
|
|
* Removes the attribute referenced by the index.
|
||
|
|
* @param index The zero based index of the attribute to remove.
|
||
|
|
* @return The attribute at the index. An array index out of bounds exception will be thrown if there is not an attribute at that index.
|
||
|
|
*/
|
||
|
|
public CodeAttribute removeAttribute(short index) {
|
||
|
|
int constant = index - 1;
|
||
|
|
|
||
|
|
return (CodeAttribute) attributes.remove(constant);
|
||
|
|
}//getAttribute()//
|
||
|
|
/**
|
||
|
|
* Gets the attributes referenced by the index.
|
||
|
|
* @param index The zero based index of the attribute to retrieve.
|
||
|
|
* @return The attribute at the index. An array index out of bounds exception will be thrown if there is not an attribute at that index.
|
||
|
|
*/
|
||
|
|
public CodeAttribute getAttribute(short index) {
|
||
|
|
int constant = index - 1;
|
||
|
|
|
||
|
|
return (CodeAttribute) attributes.get(constant);
|
||
|
|
}//getAttribute()//
|
||
|
|
/**
|
||
|
|
* Gets the count of attributes associated with the type.
|
||
|
|
* @return The number of attributes for this type.
|
||
|
|
*/
|
||
|
|
public int getAttributeCount() {
|
||
|
|
return attributes.getSize();
|
||
|
|
}//getAttributeCount()//
|
||
|
|
/**
|
||
|
|
* Gets the field's access flags.
|
||
|
|
* @return The field's access flags.
|
||
|
|
* @see #ACC_PUBLIC
|
||
|
|
* @see #ACC_PRIVATE
|
||
|
|
* @see #ACC_PROTECTED
|
||
|
|
* @see #ACC_STATIC
|
||
|
|
* @see #ACC_VOLITILE
|
||
|
|
* @see #ACC_FINAL
|
||
|
|
* @see #ACC_TRANSIENT
|
||
|
|
*/
|
||
|
|
public short getAccessFlags() {
|
||
|
|
return accessFlags;
|
||
|
|
}//getAccessFlags()//
|
||
|
|
/**
|
||
|
|
* Gets the index of constant in the classes constant pool for the field descriptor.
|
||
|
|
* @return The field descriptor's constant pool index.
|
||
|
|
*/
|
||
|
|
public short getDescriptorIndex() {
|
||
|
|
return descriptorIndex;
|
||
|
|
}//getDescriptorIndex()//
|
||
|
|
/**
|
||
|
|
* Gets the constant pool index identifying the field name.
|
||
|
|
* @return The field name's index in the constant pool.
|
||
|
|
*/
|
||
|
|
public short getFieldNameIndex() {
|
||
|
|
return fieldNameIndex;
|
||
|
|
}//getFieldNameIndex()//
|
||
|
|
/**
|
||
|
|
* Gets the size in number of bytes of the component and all subcomponents.
|
||
|
|
* @return The number of bytes required to represent the component and all subcomponents.
|
||
|
|
*/
|
||
|
|
public int getSize() {
|
||
|
|
return 0;
|
||
|
|
}//getSize()//
|
||
|
|
/**
|
||
|
|
* Reads this object from the given stream.
|
||
|
|
* @param in The input stream to read this object from.
|
||
|
|
*/
|
||
|
|
public void readExternal(java.io.ObjectInput in) throws IOException, ClassNotFoundException {
|
||
|
|
short count = 0;
|
||
|
|
|
||
|
|
accessFlags = in.readShort();
|
||
|
|
fieldNameIndex = in.readShort();
|
||
|
|
descriptorIndex = in.readShort();
|
||
|
|
count = in.readShort();
|
||
|
|
|
||
|
|
while(count-- != 0) {
|
||
|
|
addAttribute(CodeAttribute.readAttribute(getType(), in));
|
||
|
|
}//while//
|
||
|
|
}//readExternal()//
|
||
|
|
/**
|
||
|
|
* Reads a field from the given stream.
|
||
|
|
* @param type The type object that the field will belong to.
|
||
|
|
* @param in The input stream to read the field from.
|
||
|
|
*/
|
||
|
|
protected static Field readField(Type type, java.io.ObjectInput in) throws java.io.IOException, ClassNotFoundException {
|
||
|
|
Field field = new Field(type);
|
||
|
|
|
||
|
|
field.readExternal(in);
|
||
|
|
|
||
|
|
return field;
|
||
|
|
}//readField()//
|
||
|
|
/**
|
||
|
|
* Sets the field's access flags.
|
||
|
|
* @param accessFlags The field's access flags.
|
||
|
|
* @see #ACC_PUBLIC
|
||
|
|
* @see #ACC_PRIVATE
|
||
|
|
* @see #ACC_PROTECTED
|
||
|
|
* @see #ACC_STATIC
|
||
|
|
* @see #ACC_VOLITILE
|
||
|
|
* @see #ACC_FINAL
|
||
|
|
* @see #ACC_TRANSIENT
|
||
|
|
*/
|
||
|
|
public void setAccessFlags(short accessFlags) {
|
||
|
|
this.accessFlags = accessFlags;
|
||
|
|
}//setAccessFlags()//
|
||
|
|
/**
|
||
|
|
* Sets the index of constant in the classes constant pool for the field descriptor.
|
||
|
|
* @param descriptorIndex The field descriptor's constant pool index.
|
||
|
|
*/
|
||
|
|
public void setDescriptorIndex(short descriptorIndex) {
|
||
|
|
this.descriptorIndex = descriptorIndex;
|
||
|
|
}//setDescriptorIndex()//
|
||
|
|
/**
|
||
|
|
* Sets the constant pool index identifying the field name.
|
||
|
|
* @param nameIndex The field name's index in the constant pool.
|
||
|
|
*/
|
||
|
|
public void setFieldNameIndex(short fieldNameIndex) {
|
||
|
|
this.fieldNameIndex = fieldNameIndex;
|
||
|
|
}//setFieldNameIndex()//
|
||
|
|
/**
|
||
|
|
* @see Component.toString(StringBuffer, int)
|
||
|
|
*/
|
||
|
|
public void toString(StringBuffer buffer, int tabCount) {
|
||
|
|
IIterator iterator = null;
|
||
|
|
|
||
|
|
appendTabs(buffer, tabCount);
|
||
|
|
buffer.append("Field (nameIndex=");
|
||
|
|
buffer.append(fieldNameIndex);
|
||
|
|
buffer.append(" descriptorIndex=");
|
||
|
|
buffer.append(descriptorIndex);
|
||
|
|
buffer.append(" accessFlags=");
|
||
|
|
buffer.append(accessFlags);
|
||
|
|
buffer.append(" descriptor='");
|
||
|
|
buffer.append(getType().getConstant(descriptorIndex));
|
||
|
|
buffer.append("' name='");
|
||
|
|
buffer.append(getType().getConstant(fieldNameIndex));
|
||
|
|
buffer.append("')");
|
||
|
|
|
||
|
|
if(attributes.getSize() > 0) {
|
||
|
|
buffer.append(' ');
|
||
|
|
buffer.append('{');
|
||
|
|
appendEndOfLine(buffer);
|
||
|
|
iterator = attributes.iterator();
|
||
|
|
|
||
|
|
while(iterator.hasNext()) {
|
||
|
|
CodeAttribute attribute = (CodeAttribute) iterator.next();
|
||
|
|
|
||
|
|
attribute.toString(buffer, tabCount + 1);
|
||
|
|
}//while//
|
||
|
|
|
||
|
|
appendTabs(buffer, tabCount);
|
||
|
|
buffer.append('}');
|
||
|
|
}//if//
|
||
|
|
|
||
|
|
appendEndOfLine(buffer);
|
||
|
|
}//toString()//
|
||
|
|
/**
|
||
|
|
* Writes this object to the given stream.
|
||
|
|
* @param out The output stream to write this object to.
|
||
|
|
*/
|
||
|
|
public void writeExternal(java.io.ObjectOutput out) throws IOException {
|
||
|
|
IIterator iterator = null;
|
||
|
|
|
||
|
|
out.writeShort(accessFlags);
|
||
|
|
out.writeShort(fieldNameIndex);
|
||
|
|
out.writeShort(descriptorIndex);
|
||
|
|
|
||
|
|
if(attributes != null) {
|
||
|
|
out.writeShort((short) attributes.getSize());
|
||
|
|
iterator = attributes.iterator();
|
||
|
|
|
||
|
|
while(iterator.hasNext()) {
|
||
|
|
((CodeAttribute) iterator.next()).writeExternal(out);
|
||
|
|
}//while//
|
||
|
|
}//if//
|
||
|
|
else {
|
||
|
|
out.writeShort((short) 0);
|
||
|
|
}//else//
|
||
|
|
}//writeExternal()//
|
||
|
|
}//Field//
|