View Javadoc
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  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  /**
25   * Abstracts constant pool entries.
26   */
27  public abstract class ConstantPoolEntry extends ClassFileEntry {
28  
29      /**
30       * The constant {@value} for a constant pool class.
31       */
32      public static final byte CP_Class = 7;
33  
34      /**
35       * The constant {@value} for a constant pool double.
36       */
37      public static final byte CP_Double = 6;
38  
39      /**
40       * The constant {@value} for a constant pool field reference.
41       */
42      public static final byte CP_Fieldref = 9;
43  
44      /**
45       * The constant {@value} for a constant pool float.
46       */
47      public static final byte CP_Float = 4;
48  
49      /**
50       * The constant {@value} for a constant pool int.
51       */
52      public static final byte CP_Integer = 3;
53  
54      /*
55       * class MemberRef extends ConstantPoolEntry { private int index; Class(String name) { super(CP_Class); index = pool.indexOf(name); } void
56       * writeBody(DataOutputStream dos) throws IOException { dos.writeShort(index); } }
57       */
58  
59      /**
60       * The constant {@value} for a constant pool interface method reference.
61       */
62      public static final byte CP_InterfaceMethodref = 11;
63  
64      /**
65       * The constant {@value} for a constant pool long.
66       */
67      public static final byte CP_Long = 5;
68  
69      /**
70       * The constant {@value} for a constant pool method reference.
71       */
72      public static final byte CP_Methodref = 10;
73  
74      /**
75       * The constant {@value} for a constant pool name and type.
76       */
77      public static final byte CP_NameAndType = 12;
78  
79      /**
80       * The constant {@value} for a constant pool string.
81       */
82      public static final byte CP_String = 8;
83  
84      /**
85       * The constant {@value} for a constant pool UTF8.
86       */
87      public static final byte CP_UTF8 = 1;
88  
89      /**
90       * One-byte tag indicates the kind of constant.
91       */
92      byte tag;
93  
94      /**
95       * Global index.
96       */
97      protected int globalIndex;
98  
99      /**
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 }