001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.classfile.CONSTANT_Utf8_info
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.services.classfile;
023:
024: import org.apache.derby.iapi.services.classfile.VMDescriptor;
025: import java.io.IOException;
026:
027: /** Constant Pool class - pages 92-99 */
028:
029: /** Utf8- page 100 - Section 4.4.7 */
030: public final class CONSTANT_Utf8_info extends ConstantPoolEntry {
031: private final String value;
032: private int asString;
033: private int asCode;
034:
035: CONSTANT_Utf8_info(String value) {
036: super (VMDescriptor.CONSTANT_Utf8);
037: this .value = value;
038: }
039:
040: Object getKey() {
041: return value;
042: }
043:
044: /**
045: We assume here that the String is ASCII, thus this
046: might return a size smaller than actual size.
047: */
048: int classFileSize() {
049: // 1 (tag) + 2 (utf length) + string length
050: return 1 + 2 + value.length();
051: }
052:
053: public String toString() {
054: return value;
055: }
056:
057: // if this returns 0 then the caller must put another CONSTANT_Utf8_info into the
058: // constant pool with no hash table entry and then call setAlternative() with its index.
059: int setAsCode() {
060: if (ClassHolder.isExternalClassName(value)) {
061: if (asString == 0) {
062: // only used as code at the moment
063: asCode = getIndex();
064: }
065:
066: return asCode;
067: }
068: // no dots in the string so it can be used as a JVM internal string and
069: // an external string.
070: return getIndex();
071: }
072:
073: int setAsString() {
074: if (ClassHolder.isExternalClassName(value)) {
075:
076: if (asCode == 0) {
077: // only used as String at the moment
078: asString = getIndex();
079: }
080: return asString;
081: }
082:
083: // no dots in the string so it can be used as a JVM internal string and
084: // an external string.
085: return getIndex();
086: }
087:
088: void setAlternative(int index) {
089:
090: if (asCode == 0)
091: asCode = index;
092: else
093: asString = index;
094: }
095:
096: void put(ClassFormatOutput out) throws IOException {
097: super.put(out);
098:
099: if (getIndex() == asCode) {
100: out.writeUTF(ClassHolder.convertToInternalClassName(value));
101: } else
102: out.writeUTF(value);
103: }
104: }
|