1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.classfile.CONSTANT_Utf8_info |
4 | |
5 | Copyright 2000, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.services.classfile; |
22 | |
23 | import org.apache.derby.iapi.services.classfile.VMDescriptor; |
24 | import java.io.IOException; |
25 | |
26 | /** Constant Pool class - pages 92-99 */ |
27 | |
28 | /** Utf8- page 100 - Section 4.4.7 */ |
29 | public final class CONSTANT_Utf8_info extends ConstantPoolEntry { |
30 | private final String value; |
31 | private int asString; |
32 | private int asCode; |
33 | |
34 | CONSTANT_Utf8_info(String value) { |
35 | super(VMDescriptor.CONSTANT_Utf8); |
36 | this.value = value; |
37 | } |
38 | |
39 | Object getKey() { |
40 | return value; |
41 | } |
42 | |
43 | /** |
44 | We assume here that the String is ASCII, thus this |
45 | might return a size smaller than actual size. |
46 | */ |
47 | int classFileSize() { |
48 | // 1 (tag) + 2 (utf length) + string length |
49 | return 1 + 2 + value.length(); |
50 | } |
51 | |
52 | public String toString() { |
53 | return value; |
54 | } |
55 | |
56 | // if this returns 0 then the caller must put another CONSTANT_Utf8_info into the |
57 | // constant pool with no hash table entry and then call setAlternative() with its index. |
58 | int setAsCode() { |
59 | if (ClassHolder.isExternalClassName(value)) |
60 | { |
61 | if (asString == 0) { |
62 | // only used as code at the moment |
63 | asCode = getIndex(); |
64 | } |
65 | |
66 | return asCode; |
67 | } |
68 | // no dots in the string so it can be used as a JVM internal string and |
69 | // an external string. |
70 | return getIndex(); |
71 | } |
72 | |
73 | int setAsString() { |
74 | if (ClassHolder.isExternalClassName(value)) |
75 | { |
76 | |
77 | if (asCode == 0) { |
78 | // only used as String at the moment |
79 | asString = getIndex(); |
80 | } |
81 | return asString; |
82 | } |
83 | |
84 | // no dots in the string so it can be used as a JVM internal string and |
85 | // an external string. |
86 | return getIndex(); |
87 | } |
88 | |
89 | void setAlternative(int index) { |
90 | |
91 | if (asCode == 0) |
92 | asCode = index; |
93 | else |
94 | asString = index; |
95 | } |
96 | |
97 | void put(ClassFormatOutput out) throws IOException { |
98 | super.put(out); |
99 | |
100 | if (getIndex() == asCode) |
101 | { |
102 | out.writeUTF(ClassHolder.convertToInternalClassName(value)); |
103 | } |
104 | else |
105 | out.writeUTF(value); |
106 | } |
107 | } |