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