001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.pack200.bytecode;
018:
019: import java.io.DataOutputStream;
020: import java.io.IOException;
021: import java.io.UnsupportedEncodingException;
022:
023: public class CPUTF8 extends ConstantPoolEntry {
024: private String utf8;
025:
026: public CPUTF8(String utf8, int domain) {
027: super (ConstantPoolEntry.CP_UTF8);
028: this .utf8 = utf8;
029: this .domain = domain;
030: }
031:
032: public boolean equals(Object obj) {
033: if (this == obj)
034: return true;
035: if (obj == null)
036: return false;
037: if (this .getClass() != obj.getClass())
038: return false;
039: final CPUTF8 other = (CPUTF8) obj;
040: if (utf8 == null) {
041: if (other.utf8 != null)
042: return false;
043: } else if (!utf8.equals(other.utf8))
044: return false;
045: return true;
046: }
047:
048: public int hashCode() {
049: final int PRIME = 31;
050: int result = 1;
051: result = PRIME * result
052: + ((utf8 == null) ? 0 : utf8.hashCode());
053: return result;
054: }
055:
056: public String toString() {
057: return "UTF8: " + utf8;
058: }
059:
060: protected void writeBody(DataOutputStream dos) throws IOException {
061: byte[] bytes;
062: try {
063: // TODO Check that this is the right UTF-8 for bytes
064: if (utf8 == null) {
065: bytes = new byte[0];
066: } else {
067: bytes = utf8.getBytes("UTF-8");
068: }
069: } catch (UnsupportedEncodingException e) {
070: throw new RuntimeException("Couldn't convert string "
071: + utf8 + " to UTF-8");
072: }
073: dos.writeShort(bytes.length);
074: dos.write(bytes);
075: }
076:
077: public String underlyingString() {
078: return utf8;
079: }
080:
081: public String comparisonString() {
082: // Should use either normalComparisonString or signatureComparisonString.
083: // If we get here, that might indicate an error.
084: throw new Error(
085: "Should use specific kinds of comparisonString() on CPUTF8s");
086: }
087:
088: public String normalComparisonString() {
089: // TODO: what to do about inner classes?
090: if (utf8 == null) {
091: return "null:utf8 (probably an inner class?)";
092: }
093: ;
094: return utf8;
095: }
096:
097: public String signatureComparisonString() {
098: // TODO: what to do about inner classes?
099: if (utf8 == null) {
100: return "null:utf8 (probably an inner class?)";
101: }
102: ;
103: StringBuffer alphaChars = new StringBuffer();
104: StringBuffer extraChars = new StringBuffer();
105: if (utf8.length() > 0) {
106: if (utf8.charAt(0) == '(') {
107: // Things with return values (which apparently
108: // always begin with '(') sort after things
109: // without return values.
110: // TODO: need a better way for this - possibly in the comparator?
111: alphaChars.append(Character.MAX_VALUE);
112: }
113: }
114: // TODO: need a better way for this - possibly in the comparator?
115: extraChars.append(Character.MAX_VALUE);
116: for (int index = 0; index < utf8.length(); index++) {
117: if ((utf8.charAt(index) == '(')
118: || (utf8.charAt(index) == ')')
119: || (utf8.charAt(index) == '[')
120: || (utf8.charAt(index) == ']')) {
121: extraChars.append(utf8.charAt(index));
122: } else {
123: alphaChars.append(utf8.charAt(index));
124: }
125: }
126: return (alphaChars.toString() + extraChars.toString());
127: }
128: }
|