aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/taskdefs/optional/depend/constantpool/ConstantPoolEntry.java
blob: 26a0d094cdf32c91afbc59560f6275fb077b88ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.taskdefs.optional.depend.constantpool;

import java.io.DataInputStream;
import java.io.IOException;

/**
 * An entry in the constant pool. This class contains a representation of the
 * constant pool entries. It is an abstract base class for all the different
 * forms of constant pool entry.
 *
 * @see ConstantPool
 */
public abstract class ConstantPoolEntry {

    /** Tag value for UTF8 entries. */
    public static final int CONSTANT_UTF8 = 1;

    /** Tag value for Integer entries. */
    public static final int CONSTANT_INTEGER = 3;

    /** Tag value for Float entries. */
    public static final int CONSTANT_FLOAT = 4;

    /** Tag value for Long entries. */
    public static final int CONSTANT_LONG = 5;

    /** Tag value for Double entries. */
    public static final int CONSTANT_DOUBLE = 6;

    /** Tag value for Class entries. */
    public static final int CONSTANT_CLASS = 7;

    /** Tag value for String entries. */
    public static final int CONSTANT_STRING = 8;

    /** Tag value for Field Reference entries. */
    public static final int CONSTANT_FIELDREF = 9;

    /** Tag value for Method Reference entries. */
    public static final int CONSTANT_METHODREF = 10;

    /** Tag value for Interface Method Reference entries. */
    public static final int CONSTANT_INTERFACEMETHODREF = 11;

    /** Tag value for Name and Type entries. */
    public static final int CONSTANT_NAMEANDTYPE = 12;

    /** Tag value for Method Handle entries */
    public static final int CONSTANT_METHODHANDLE  = 15;

    /** Tag value for Method Type entries */
    public static final int CONSTANT_METHODTYPE = 16;

    /** Tag value for InvokeDynamic entries*/
    public static final int CONSTANT_INVOKEDYNAMIC = 18;

    /**
     * This entry's tag which identifies the type of this constant pool
     * entry.
     */
    private int tag;

    /**
     * The number of slots in the constant pool, occupied by this entry.
     */
    private int numEntries;

    /**
     * A flag which indicates if this entry has been resolved or not.
     */
    private boolean resolved;

    /**
     * Initialise the constant pool entry.
     *
     * @param tagValue the tag value which identifies which type of constant
     *      pool entry this is.
     * @param entries the number of constant pool entry slots this entry
     *      occupies.
     */
    public ConstantPoolEntry(int tagValue, int entries) {
        tag = tagValue;
        numEntries = entries;
        resolved = false;
    }

    /**
     * Read a constant pool entry from a stream. This is a factory method
     * which reads a constant pool entry form a stream and returns the
     * appropriate subclass for the entry.
     *
     * @param cpStream the stream from which the constant pool entry is to
     *      be read.
     * @return the appropriate ConstantPoolEntry subclass representing the
     *      constant pool entry from the stream.
     * @exception IOException if the constant pool entry cannot be read
     *      from the stream
     */
    public static ConstantPoolEntry readEntry(DataInputStream cpStream)
         throws IOException {
        ConstantPoolEntry cpInfo = null;
        int cpTag = cpStream.readUnsignedByte();

        switch (cpTag) {

            case CONSTANT_UTF8:
                cpInfo = new Utf8CPInfo();

                break;
            case CONSTANT_INTEGER:
                cpInfo = new IntegerCPInfo();

                break;
            case CONSTANT_FLOAT:
                cpInfo = new FloatCPInfo();

                break;
            case CONSTANT_LONG:
                cpInfo = new LongCPInfo();

                break;
            case CONSTANT_DOUBLE:
                cpInfo = new DoubleCPInfo();

                break;
            case CONSTANT_CLASS:
                cpInfo = new ClassCPInfo();

                break;
            case CONSTANT_STRING:
                cpInfo = new StringCPInfo();

                break;
            case CONSTANT_FIELDREF:
                cpInfo = new FieldRefCPInfo();

                break;
            case CONSTANT_METHODREF:
                cpInfo = new MethodRefCPInfo();

                break;
            case CONSTANT_INTERFACEMETHODREF:
                cpInfo = new InterfaceMethodRefCPInfo();

                break;
            case CONSTANT_NAMEANDTYPE:
                cpInfo = new NameAndTypeCPInfo();

                break;
            case CONSTANT_METHODHANDLE:
                cpInfo = new MethodHandleCPInfo();

                break;
            case CONSTANT_METHODTYPE:
                cpInfo = new MethodTypeCPInfo();

                break;
            case CONSTANT_INVOKEDYNAMIC:
                cpInfo = new InvokeDynamicCPInfo();

                break;
            default:
                throw new ClassFormatError("Invalid Constant Pool entry Type "
                     + cpTag);
        }

        cpInfo.read(cpStream);

        return cpInfo;
    }

    /**
     * Indicates whether this entry has been resolved. In general a constant
     * pool entry can reference another constant pool entry by its index
     * value. Resolution involves replacing this index value with the
     * constant pool entry at that index.
     *
     * @return true if this entry has been resolved.
     */
    public boolean isResolved() {
        return resolved;
    }

    /**
     * Resolve this constant pool entry with respect to its dependents in
     * the constant pool.
     *
     * @param constantPool the constant pool of which this entry is a member
     *      and against which this entry is to be resolved.
     */
    public void resolve(ConstantPool constantPool) {
        resolved = true;
    }

    /**
     * read a constant pool entry from a class stream.
     *
     * @param cpStream the DataInputStream which contains the constant pool
     *      entry to be read.
     * @exception IOException if there is a problem reading the entry from
     *      the stream.
     */
    public abstract void read(DataInputStream cpStream) throws IOException;

    /**
     * Get the Entry's type tag.
     *
     * @return The Tag value of this entry
     */
    public int getTag() {
        return tag;
    }

    /**
     * Get the number of Constant Pool Entry slots within the constant pool
     * occupied by this entry.
     *
     * @return the number of slots used.
     */
    public final int getNumEntries() {
        return numEntries;
    }

}