ConstantPoolEntry.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   https://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.harmony.unpack200.bytecode;

  20. import java.io.DataOutputStream;
  21. import java.io.IOException;

  22. /**
  23.  * Abstracts constant pool entries.
  24.  */
  25. public abstract class ConstantPoolEntry extends ClassFileEntry {

  26.     /**
  27.      * The constant {@value} for a constant pool class.
  28.      */
  29.     public static final byte CP_Class = 7;

  30.     /**
  31.      * The constant {@value} for a constant pool double.
  32.      */
  33.     public static final byte CP_Double = 6;

  34.     /**
  35.      * The constant {@value} for a constant pool field reference.
  36.      */
  37.     public static final byte CP_Fieldref = 9;

  38.     /**
  39.      * The constant {@value} for a constant pool float.
  40.      */
  41.     public static final byte CP_Float = 4;

  42.     /**
  43.      * The constant {@value} for a constant pool int.
  44.      */
  45.     public static final byte CP_Integer = 3;

  46.     /*
  47.      * class MemberRef extends ConstantPoolEntry { private int index; Class(String name) { super(CP_Class); index = pool.indexOf(name); } void
  48.      * writeBody(DataOutputStream dos) throws IOException { dos.writeShort(index); } }
  49.      */

  50.     /**
  51.      * The constant {@value} for a constant pool interface method reference.
  52.      */
  53.     public static final byte CP_InterfaceMethodref = 11;

  54.     /**
  55.      * The constant {@value} for a constant pool long.
  56.      */
  57.     public static final byte CP_Long = 5;

  58.     /**
  59.      * The constant {@value} for a constant pool method reference.
  60.      */
  61.     public static final byte CP_Methodref = 10;

  62.     /**
  63.      * The constant {@value} for a constant pool name and type.
  64.      */
  65.     public static final byte CP_NameAndType = 12;

  66.     /**
  67.      * The constant {@value} for a constant pool string.
  68.      */
  69.     public static final byte CP_String = 8;

  70.     /**
  71.      * The constant {@value} for a constant pool UTF8.
  72.      */
  73.     public static final byte CP_UTF8 = 1;

  74.     /**
  75.      * One-byte tag indicates the kind of constant.
  76.      */
  77.     byte tag;

  78.     /**
  79.      * Global index.
  80.      */
  81.     protected int globalIndex;

  82.     /**
  83.      * Constructs a new instance.
  84.      *
  85.      * @param tag One-byte tag indicates the kind of constant.
  86.      * @param globalIndex Global index.
  87.      */
  88.     ConstantPoolEntry(final byte tag, final int globalIndex) {
  89.         this.tag = tag;
  90.         this.globalIndex = globalIndex;
  91.     }

  92.     @Override
  93.     public void doWrite(final DataOutputStream dos) throws IOException {
  94.         dos.writeByte(tag);
  95.         writeBody(dos);
  96.     }

  97.     @Override
  98.     public abstract boolean equals(Object obj);

  99.     /**
  100.      * Gets the global index.
  101.      *
  102.      * @return the global index.
  103.      */
  104.     public int getGlobalIndex() {
  105.         return globalIndex;
  106.     }

  107.     /**
  108.      * Gets the tag.

  109.      * @return the tag.
  110.      */
  111.     public byte getTag() {
  112.         return tag;
  113.     }

  114.     @Override
  115.     public abstract int hashCode();

  116.     /**
  117.      * Writes this instance to the given output stream.
  118.      *
  119.      * @param dos the output stream.
  120.      * @throws IOException if an I/O error occurs.
  121.      */
  122.     protected abstract void writeBody(DataOutputStream dos) throws IOException;
  123. }