KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > classfile > CONSTANT_Utf8_info


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

21
22 package org.apache.derby.iapi.services.classfile;
23
24 import org.apache.derby.iapi.services.classfile.VMDescriptor;
25 import java.io.IOException JavaDoc;
26
27 /** Constant Pool class - pages 92-99 */
28
29 /** Utf8- page 100 - Section 4.4.7 */
30 public final class CONSTANT_Utf8_info extends ConstantPoolEntry {
31     private final String JavaDoc value;
32     private int asString;
33     private int asCode;
34     
35     CONSTANT_Utf8_info(String JavaDoc value) {
36         super(VMDescriptor.CONSTANT_Utf8);
37         this.value = value;
38     }
39
40     Object JavaDoc getKey() {
41         return value;
42     }
43
44     /**
45         We assume here that the String is ASCII, thus this
46         might return a size smaller than actual size.
47     */

48     int classFileSize() {
49         // 1 (tag) + 2 (utf length) + string length
50
return 1 + 2 + value.length();
51     }
52
53     public String JavaDoc toString() {
54         return value;
55     }
56
57     // if this returns 0 then the caller must put another CONSTANT_Utf8_info into the
58
// constant pool with no hash table entry and then call setAlternative() with its index.
59
int setAsCode() {
60         if (ClassHolder.isExternalClassName(value))
61         {
62             if (asString == 0) {
63                 // only used as code at the moment
64
asCode = getIndex();
65             }
66
67             return asCode;
68         }
69         // no dots in the string so it can be used as a JVM internal string and
70
// an external string.
71
return getIndex();
72     }
73
74     int setAsString() {
75         if (ClassHolder.isExternalClassName(value))
76         {
77
78             if (asCode == 0) {
79                 // only used as String at the moment
80
asString = getIndex();
81             }
82             return asString;
83         }
84         
85         // no dots in the string so it can be used as a JVM internal string and
86
// an external string.
87
return getIndex();
88     }
89
90     void setAlternative(int index) {
91
92         if (asCode == 0)
93             asCode = index;
94         else
95             asString = index;
96     }
97
98     void put(ClassFormatOutput out) throws IOException JavaDoc {
99         super.put(out);
100
101         if (getIndex() == asCode)
102         {
103             out.writeUTF(ClassHolder.convertToInternalClassName(value));
104         }
105         else
106             out.writeUTF(value);
107     }
108 }
109
Popular Tags