001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.harmony.unpack200.bytecode;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024/**
025 * Abstracts constant pool entries.
026 */
027public abstract class ConstantPoolEntry extends ClassFileEntry {
028
029    /**
030     * The constant {@value} for a constant pool class.
031     */
032    public static final byte CP_Class = 7;
033
034    /**
035     * The constant {@value} for a constant pool double.
036     */
037    public static final byte CP_Double = 6;
038
039    /**
040     * The constant {@value} for a constant pool field reference.
041     */
042    public static final byte CP_Fieldref = 9;
043
044    /**
045     * The constant {@value} for a constant pool float.
046     */
047    public static final byte CP_Float = 4;
048
049    /**
050     * The constant {@value} for a constant pool int.
051     */
052    public static final byte CP_Integer = 3;
053
054    /*
055     * class MemberRef extends ConstantPoolEntry { private int index; Class(String name) { super(CP_Class); index = pool.indexOf(name); } void
056     * writeBody(DataOutputStream dos) throws IOException { dos.writeShort(index); } }
057     */
058
059    /**
060     * The constant {@value} for a constant pool interface method reference.
061     */
062    public static final byte CP_InterfaceMethodref = 11;
063
064    /**
065     * The constant {@value} for a constant pool long.
066     */
067    public static final byte CP_Long = 5;
068
069    /**
070     * The constant {@value} for a constant pool method reference.
071     */
072    public static final byte CP_Methodref = 10;
073
074    /**
075     * The constant {@value} for a constant pool name and type.
076     */
077    public static final byte CP_NameAndType = 12;
078
079    /**
080     * The constant {@value} for a constant pool string.
081     */
082    public static final byte CP_String = 8;
083
084    /**
085     * The constant {@value} for a constant pool UTF8.
086     */
087    public static final byte CP_UTF8 = 1;
088
089    /**
090     * One-byte tag indicates the kind of constant.
091     */
092    byte tag;
093
094    /**
095     * Global index.
096     */
097    protected int globalIndex;
098
099    /**
100     * Constructs a new instance.
101     *
102     * @param tag One-byte tag indicates the kind of constant.
103     * @param globalIndex Global index.
104     */
105    ConstantPoolEntry(final byte tag, final int globalIndex) {
106        this.tag = tag;
107        this.globalIndex = globalIndex;
108    }
109
110    @Override
111    public void doWrite(final DataOutputStream dos) throws IOException {
112        dos.writeByte(tag);
113        writeBody(dos);
114    }
115
116    @Override
117    public abstract boolean equals(Object obj);
118
119    /**
120     * Gets the global index.
121     *
122     * @return the global index.
123     */
124    public int getGlobalIndex() {
125        return globalIndex;
126    }
127
128    /**
129     * Gets the tag.
130
131     * @return the tag.
132     */
133    public byte getTag() {
134        return tag;
135    }
136
137    @Override
138    public abstract int hashCode();
139
140    /**
141     * Writes this instance to the given output stream.
142     *
143     * @param dos the output stream.
144     * @throws IOException if an I/O error occurs.
145     */
146    protected abstract void writeBody(DataOutputStream dos) throws IOException;
147}