1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 import  com.sun.org.apache.bcel.internal.Constants;
  25 import java.io.*;
  26 
  27 /**
  28  * Abstract super class for fields and methods.
  29  *
  30  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  31  */
  32 public abstract class FieldOrMethod extends AccessFlags implements Cloneable, Node {
  33   protected int          name_index;      // Points to field name in constant pool
  34   protected int          signature_index; // Points to encoded signature
  35   protected int          attributes_count;// No. of attributes
  36   protected Attribute[]  attributes;      // Collection of attributes
  37   protected ConstantPool constant_pool;
  38 
  39   FieldOrMethod() {}
  40 
  41   /**
  42    * Initialize from another object. Note that both objects use the same
  43    * references (shallow copy). Use clone() for a physical copy.
  44    */
  45   protected FieldOrMethod(FieldOrMethod c) {
  46     this(c.getAccessFlags(), c.getNameIndex(), c.getSignatureIndex(),
  47          c.getAttributes(), c.getConstantPool());
  48   }
  49 
  50   /**
  51    * Construct object from file stream.
  52    * @param file Input stream
  53    * @throws IOException
  54    * @throws ClassFormatException
  55    */
  56   protected FieldOrMethod(DataInputStream file, ConstantPool constant_pool)
  57     throws IOException, ClassFormatException
  58   {
  59     this(file.readUnsignedShort(), file.readUnsignedShort(),
  60          file.readUnsignedShort(), null, constant_pool);
  61 
  62     attributes_count = file.readUnsignedShort();
  63     attributes       = new Attribute[attributes_count];
  64     for(int i=0; i < attributes_count; i++)
  65       attributes[i] = Attribute.readAttribute(file, constant_pool);
  66   }
  67 
  68   /**
  69    * @param access_flags Access rights of method
  70    * @param name_index Points to field name in constant pool
  71    * @param signature_index Points to encoded signature
  72    * @param attributes Collection of attributes
  73    * @param constant_pool Array of constants
  74    */
  75   protected FieldOrMethod(int access_flags, int name_index, int signature_index,
  76                           Attribute[] attributes, ConstantPool constant_pool)
  77   {
  78     this.access_flags    = access_flags;
  79     this.name_index      = name_index;
  80     this.signature_index = signature_index;
  81     this.constant_pool   = constant_pool;
  82 
  83     setAttributes(attributes);
  84   }
  85 
  86   /**
  87    * Dump object to file stream on binary format.
  88    *
  89    * @param file Output file stream
  90    * @throws IOException
  91    */
  92   public final void dump(DataOutputStream file) throws IOException
  93   {
  94     file.writeShort(access_flags);
  95     file.writeShort(name_index);
  96     file.writeShort(signature_index);
  97     file.writeShort(attributes_count);
  98 
  99     for(int i=0; i < attributes_count; i++)
 100       attributes[i].dump(file);
 101   }
 102 
 103   /**
 104    * @return Collection of object attributes.
 105    */
 106   public final Attribute[] getAttributes() { return attributes; }
 107 
 108   /**
 109    * @param attributes Collection of object attributes.
 110    */
 111   public final void setAttributes(Attribute[] attributes) {
 112     this.attributes  = attributes;
 113     attributes_count = (attributes == null)? 0 : attributes.length;
 114   }
 115 
 116   /**
 117    * @return Constant pool used by this object.
 118    */
 119   public final ConstantPool getConstantPool() { return constant_pool; }
 120 
 121   /**
 122    * @param constant_pool Constant pool to be used for this object.
 123    */
 124   public final void setConstantPool(ConstantPool constant_pool) {
 125     this.constant_pool = constant_pool;
 126   }
 127 
 128   /**
 129    * @return Index in constant pool of object's name.
 130    */
 131   public final int getNameIndex() { return name_index; }
 132 
 133   /**
 134    * @param name_index Index in constant pool of object's name.
 135    */
 136   public final void setNameIndex(int name_index) {
 137     this.name_index = name_index;
 138   }
 139 
 140   /**
 141    * @return Index in constant pool of field signature.
 142    */
 143   public final int getSignatureIndex() { return signature_index; }
 144 
 145   /**
 146    * @param signature_index Index in constant pool of field signature.
 147    */
 148   public final void setSignatureIndex(int signature_index) {
 149     this.signature_index = signature_index;
 150   }
 151 
 152   /**
 153    * @return Name of object, i.e., method name or field name
 154    */
 155   public final String getName() {
 156     ConstantUtf8  c;
 157     c = (ConstantUtf8)constant_pool.getConstant(name_index,
 158                                                 Constants.CONSTANT_Utf8);
 159     return c.getBytes();
 160   }
 161 
 162   /**
 163    * @return String representation of object's type signature (java style)
 164    */
 165   public final String getSignature() {
 166     ConstantUtf8  c;
 167     c = (ConstantUtf8)constant_pool.getConstant(signature_index,
 168                                                 Constants.CONSTANT_Utf8);
 169     return c.getBytes();
 170   }
 171 
 172   /**
 173    * @return deep copy of this field
 174    */
 175   protected FieldOrMethod copy_(ConstantPool constant_pool) {
 176     FieldOrMethod c = null;
 177 
 178     try {
 179       c = (FieldOrMethod)clone();
 180     } catch(CloneNotSupportedException e) {}
 181 
 182     c.constant_pool    = constant_pool;
 183     c.attributes       = new Attribute[attributes_count];
 184 
 185     for(int i=0; i < attributes_count; i++)
 186       c.attributes[i] = attributes[i].copy(constant_pool);
 187 
 188     return c;
 189   }
 190 }