00001 package de.fub.bytecode.classfile;
00002
00003 import de.fub.bytecode.Constants;
00004 import java.io.*;
00005
00006
00007
00008
00009
00010
00011
00012 public abstract class FieldOrMethod extends AccessFlags implements Constants, Cloneable {
00013 protected int name_index;
00014 protected int signature_index;
00015 protected int attributes_count;
00016 protected Attribute[] attributes;
00017 protected ConstantPool constant_pool;
00018
00019 FieldOrMethod() {}
00020
00021
00022
00023
00024
00025
00026
00027 protected FieldOrMethod(int access_flags, int name_index, int signature_index,
00028 Attribute[] attributes, ConstantPool constant_pool)
00029 {
00030 this.access_flags = access_flags;
00031 this.name_index = name_index;
00032 this.signature_index = signature_index;
00033 this.constant_pool = constant_pool;
00034
00035 setAttributes(attributes);
00036 }
00037
00038
00039
00040
00041 protected FieldOrMethod(FieldOrMethod c) {
00042 this(c.getAccessFlags(), c.getNameIndex(), c.getSignatureIndex(),
00043 c.getAttributes(), c.getConstantPool());
00044 }
00045
00046
00047
00048
00049
00050
00051 protected FieldOrMethod(DataInputStream file, ConstantPool constant_pool)
00052 throws IOException, ClassFormatError
00053 {
00054 this(file.readUnsignedShort(), file.readUnsignedShort(),
00055 file.readUnsignedShort(), null, constant_pool);
00056
00057 attributes_count = file.readUnsignedShort();
00058 attributes = new Attribute[attributes_count];
00059 for(int i=0; i < attributes_count; i++)
00060 attributes[i] = Attribute.readAttribute(file, constant_pool);
00061 }
00062
00063
00064
00065 protected FieldOrMethod copy_(ConstantPool constant_pool) {
00066 FieldOrMethod c = null;
00067
00068 try {
00069 c = (FieldOrMethod)clone();
00070 } catch(CloneNotSupportedException e) {}
00071
00072 c.constant_pool = constant_pool;
00073 c.attributes = new Attribute[attributes_count];
00074
00075 for(int i=0; i < attributes_count; i++)
00076 c.attributes[i] = attributes[i].copy(constant_pool);
00077
00078 return c;
00079 }
00080
00081
00082
00083
00084
00085
00086 public final void dump(DataOutputStream file) throws IOException
00087 {
00088 file.writeShort(access_flags);
00089 file.writeShort(name_index);
00090 file.writeShort(signature_index);
00091 file.writeShort(attributes_count);
00092
00093 for(int i=0; i < attributes_count; i++)
00094 attributes[i].dump(file);
00095 }
00096
00097
00098
00099 public final Attribute[] getAttributes() { return attributes; }
00100
00101
00102
00103 public final ConstantPool getConstantPool() { return constant_pool; }
00104
00105
00106
00107 public final String getName() {
00108 ConstantUtf8 c;
00109 c = (ConstantUtf8)constant_pool.getConstant(name_index,
00110 CONSTANT_Utf8);
00111 return c.getBytes();
00112 }
00113
00114
00115
00116 public final int getNameIndex() { return name_index; }
00117
00118
00119
00120 public final String getSignature() {
00121 ConstantUtf8 c;
00122 c = (ConstantUtf8)constant_pool.getConstant(signature_index,
00123 CONSTANT_Utf8);
00124 return c.getBytes();
00125 }
00126
00127
00128
00129 public final int getSignatureIndex() { return signature_index; }
00130
00131
00132
00133 public final void setAttributes(Attribute[] attributes) {
00134 this.attributes = attributes;
00135 attributes_count = (attributes == null)? 0 : attributes.length;
00136 }
00137
00138
00139
00140 public final void setConstantPool(ConstantPool constant_pool) {
00141 this.constant_pool = constant_pool;
00142 }
00143
00144
00145
00146 public final void setNameIndex(int name_index) {
00147 this.name_index = name_index;
00148 }
00149
00150
00151
00152 public final void setSignatureIndex(int signature_index) {
00153 this.signature_index = signature_index;
00154 }
00155 }