duke@1: /* duke@1: * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.util; duke@1: duke@1: import java.lang.ref.SoftReference; duke@1: duke@1: duke@1: /** An abstraction for internal compiler strings. For efficiency reasons, duke@1: * GJC uses hashed strings that are stored in a common large buffer. duke@1: * duke@1: *

Names represent unique hashable strings. Two names are equal duke@1: * if their indices are equal. Utf8 representation is used duke@1: * for storing names internally. duke@1: * duke@1: *

This is NOT part of any API supported by Sun Microsystems. If duke@1: * you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class Name implements javax.lang.model.element.Name { duke@1: duke@1: /** The table structure where the name is stored duke@1: */ duke@1: public Table table; duke@1: duke@1: /** The index where the bytes of this name are stored in the global name duke@1: * buffer `names'. duke@1: */ duke@1: public int index; duke@1: duke@1: /** The number of bytes in this name. duke@1: */ duke@1: public int len; duke@1: duke@1: /** The next name occupying the same hash bucket. duke@1: */ duke@1: Name next; duke@1: duke@1: /** The hashcode of a name. duke@1: */ duke@1: private static int hashValue(byte cs[], int start, int len) { duke@1: int h = 0; duke@1: int off = start; duke@1: duke@1: for (int i = 0; i < len; i++) { duke@1: h = (h << 5) - h + cs[off++]; duke@1: } duke@1: return h; duke@1: } duke@1: duke@1: /** Is (the utf8 representation of) name equal to duke@1: * cs[start..start+len-1]? duke@1: */ duke@1: private static boolean equals(byte[] names, int index, duke@1: byte cs[], int start, int len) { duke@1: int i = 0; duke@1: while (i < len && names[index + i] == cs[start + i]) i++; duke@1: return i == len; duke@1: } duke@1: duke@1: /** Create a name from the bytes in cs[start..start+len-1]. duke@1: * Assume that bytes are in utf8 format. duke@1: */ duke@1: public static Name fromUtf(Table table, byte cs[], int start, int len) { duke@1: int h = hashValue(cs, start, len) & table.hashMask; duke@1: Name n = table.hashes[h]; duke@1: byte[] names = table.names; duke@1: while (n != null && duke@1: (n.len != len || !equals(names, n.index, cs, start, len))) duke@1: n = n.next; duke@1: if (n == null) { duke@1: int nc = table.nc; duke@1: while (nc + len > names.length) { duke@1: // System.err.println("doubling name buffer of length + " + names.length + " to fit " + len + " bytes");//DEBUG duke@1: byte[] newnames = new byte[names.length * 2]; duke@1: System.arraycopy(names, 0, newnames, 0, names.length); duke@1: names = table.names = newnames; duke@1: } duke@1: System.arraycopy(cs, start, names, nc, len); duke@1: n = new Name(); duke@1: n.table = table; duke@1: n.index = nc; duke@1: n.len = len; duke@1: n.next = table.hashes[h]; duke@1: table.hashes[h] = n; duke@1: table.nc = nc + len; duke@1: if (len == 0) table.nc++; duke@1: } duke@1: return n; duke@1: } duke@1: duke@1: /** Create a name from the bytes in array cs. duke@1: * Assume that bytes are in utf8 format. duke@1: */ duke@1: public static Name fromUtf(Table table, byte cs[]) { duke@1: return fromUtf(table, cs, 0, cs.length); duke@1: } duke@1: duke@1: /** Create a name from the characters in cs[start..start+len-1]. duke@1: */ duke@1: public static Name fromChars(Table table, char[] cs, int start, int len) { duke@1: int nc = table.nc; duke@1: byte[] names = table.names; duke@1: while (nc + len * 3 >= names.length) { duke@1: // System.err.println("doubling name buffer of length " + names.length + " to fit " + len + " chars");//DEBUG duke@1: byte[] newnames = new byte[names.length * 2]; duke@1: System.arraycopy(names, 0, newnames, 0, names.length); duke@1: names = table.names = newnames; duke@1: } duke@1: int nbytes = duke@1: Convert.chars2utf(cs, start, names, nc, len) - nc; duke@1: int h = hashValue(names, nc, nbytes) & table.hashMask; duke@1: Name n = table.hashes[h]; duke@1: while (n != null && duke@1: (n.len != nbytes || duke@1: !equals(names, n.index, names, nc, nbytes))) duke@1: n = n.next; duke@1: if (n == null) { duke@1: n = new Name(); duke@1: n.table = table; duke@1: n.index = nc; duke@1: n.len = nbytes; duke@1: n.next = table.hashes[h]; duke@1: table.hashes[h] = n; duke@1: table.nc = nc + nbytes; duke@1: if (nbytes == 0) table.nc++; duke@1: } duke@1: return n; duke@1: } duke@1: duke@1: /** Create a name from the characters in string s. duke@1: */ duke@1: public static Name fromString(Table table, String s) { duke@1: char[] cs = s.toCharArray(); duke@1: return fromChars(table, cs, 0, cs.length); duke@1: } duke@1: duke@1: /** Create a name from the characters in char sequence s. duke@1: */ duke@1: public static Name fromString(Table table, CharSequence s) { duke@1: return fromString(table, s.toString()); duke@1: } duke@1: duke@1: /** Return the Utf8 representation of this name. duke@1: */ duke@1: public byte[] toUtf() { duke@1: byte[] bs = new byte[len]; duke@1: System.arraycopy(table.names, index, bs, 0, len); duke@1: return bs; duke@1: } duke@1: duke@1: /** Return the string representation of this name. duke@1: */ duke@1: public String toString() { duke@1: return Convert.utf2string(table.names, index, len); duke@1: } duke@1: duke@1: /** Copy all bytes of this name to buffer cs, starting at start. duke@1: */ duke@1: public void getBytes(byte cs[], int start) { duke@1: System.arraycopy(table.names, index, cs, start, len); duke@1: } duke@1: duke@1: /** Return the hash value of this name. duke@1: */ duke@1: public int hashCode() { duke@1: return index; duke@1: } duke@1: duke@1: /** Is this name equal to other? duke@1: */ duke@1: public boolean equals(Object other) { duke@1: if (other instanceof Name) duke@1: return duke@1: table == ((Name)other).table && index == ((Name)other).index; duke@1: else return false; duke@1: } duke@1: duke@1: /** Compare this name to other name, yielding -1 if smaller, 0 if equal, duke@1: * 1 if greater. duke@1: */ duke@1: public boolean less(Name that) { duke@1: int i = 0; duke@1: while (i < this.len && i < that.len) { duke@1: byte thisb = this.table.names[this.index + i]; duke@1: byte thatb = that.table.names[that.index + i]; duke@1: if (thisb < thatb) return true; duke@1: else if (thisb > thatb) return false; duke@1: else i++; duke@1: } duke@1: return this.len < that.len; duke@1: } duke@1: duke@1: /** Returns the length of this name. duke@1: */ duke@1: public int length() { duke@1: return toString().length(); duke@1: } duke@1: duke@1: /** Returns i'th byte of this name. duke@1: */ duke@1: public byte byteAt(int i) { duke@1: return table.names[index + i]; duke@1: } duke@1: duke@1: /** Returns first occurrence of byte b in this name, len if not found. duke@1: */ duke@1: public int indexOf(byte b) { duke@1: byte[] names = table.names; duke@1: int i = 0; duke@1: while (i < len && names[index + i] != b) i++; duke@1: return i; duke@1: } duke@1: duke@1: /** Returns last occurrence of byte b in this name, -1 if not found. duke@1: */ duke@1: public int lastIndexOf(byte b) { duke@1: byte[] names = table.names; duke@1: int i = len - 1; duke@1: while (i >= 0 && names[index + i] != b) i--; duke@1: return i; duke@1: } duke@1: duke@1: /** Does this name start with prefix? duke@1: */ duke@1: public boolean startsWith(Name prefix) { duke@1: int i = 0; duke@1: while (i < prefix.len && duke@1: i < len && duke@1: table.names[index + i] == prefix.table.names[prefix.index + i]) duke@1: i++; duke@1: return i == prefix.len; duke@1: } duke@1: duke@1: /** Does this name end with suffix? duke@1: */ duke@1: public boolean endsWith(Name suffix) { duke@1: int i = len - 1; duke@1: int j = suffix.len - 1; duke@1: while (j >= 0 && i >= 0 && duke@1: table.names[index + i] == suffix.table.names[suffix.index + j]) { duke@1: i--; j--; duke@1: } duke@1: return j < 0; duke@1: } duke@1: duke@1: /** Returns the sub-name starting at position start, up to and duke@1: * excluding position end. duke@1: */ duke@1: public Name subName(int start, int end) { duke@1: if (end < start) end = start; duke@1: return fromUtf(table, table.names, index + start, end - start); duke@1: } duke@1: duke@1: /** Replace all `from' bytes in this name with `to' bytes. duke@1: */ duke@1: public Name replace(byte from, byte to) { duke@1: byte[] names = table.names; duke@1: int i = 0; duke@1: while (i < len) { duke@1: if (names[index + i] == from) { duke@1: byte[] bs = new byte[len]; duke@1: System.arraycopy(names, index, bs, 0, i); duke@1: bs[i] = to; duke@1: i++; duke@1: while (i < len) { duke@1: byte b = names[index + i]; duke@1: bs[i] = b == from ? to : b; duke@1: i++; duke@1: } duke@1: return fromUtf(table, bs, 0, len); duke@1: } duke@1: i++; duke@1: } duke@1: return this; duke@1: } duke@1: duke@1: /** Return the concatenation of this name and name `n'. duke@1: */ duke@1: public Name append(Name n) { duke@1: byte[] bs = new byte[len + n.len]; duke@1: getBytes(bs, 0); duke@1: n.getBytes(bs, len); duke@1: return fromUtf(table, bs, 0, bs.length); duke@1: } duke@1: duke@1: /** Return the concatenation of this name, the given ASCII duke@1: * character, and name `n'. duke@1: */ duke@1: public Name append(char c, Name n) { duke@1: byte[] bs = new byte[len + n.len + 1]; duke@1: getBytes(bs, 0); duke@1: bs[len] = (byte)c; duke@1: n.getBytes(bs, len+1); duke@1: return fromUtf(table, bs, 0, bs.length); duke@1: } duke@1: duke@1: /** An arbitrary but consistent complete order among all Names. duke@1: */ duke@1: public int compareTo(Name other) { duke@1: return other.index - this.index; duke@1: } duke@1: duke@1: /** Return the concatenation of all names in the array `ns'. duke@1: */ duke@1: public static Name concat(Table table, Name ns[]) { duke@1: int len = 0; duke@1: for (int i = 0; i < ns.length; i++) duke@1: len = len + ns[i].len; duke@1: byte[] bs = new byte[len]; duke@1: len = 0; duke@1: for (int i = 0; i < ns.length; i++) { duke@1: ns[i].getBytes(bs, len); duke@1: len = len + ns[i].len; duke@1: } duke@1: return fromUtf(table, bs, 0, len); duke@1: } duke@1: duke@1: public char charAt(int index) { duke@1: return toString().charAt(index); duke@1: } duke@1: duke@1: public CharSequence subSequence(int start, int end) { duke@1: return toString().subSequence(start, end); duke@1: } duke@1: duke@1: public boolean contentEquals(CharSequence cs) { duke@1: return this.toString().equals(cs.toString()); duke@1: } duke@1: duke@1: public static class Table { duke@1: // maintain a freelist of recently used name tables for reuse. duke@1: private static List> freelist = List.nil(); duke@1: duke@1: static private synchronized Table make() { duke@1: while (freelist.nonEmpty()) { duke@1: Table t = freelist.head.get(); duke@1: freelist = freelist.tail; duke@1: if (t != null) return t; duke@1: } duke@1: return new Table(); duke@1: } duke@1: duke@1: static private synchronized void dispose(Table t) { duke@1: freelist = freelist.prepend(new SoftReference(t)); duke@1: } duke@1: duke@1: public void dispose() { duke@1: dispose(this); duke@1: } duke@1: duke@1: public static final Context.Key
namesKey = duke@1: new Context.Key
(); duke@1: duke@1: public static Table instance(Context context) { duke@1: Table instance = context.get(namesKey); duke@1: if (instance == null) { duke@1: instance = make(); duke@1: context.put(namesKey, instance); duke@1: } duke@1: return instance; duke@1: } duke@1: duke@1: /** The hash table for names. duke@1: */ duke@1: private Name[] hashes; duke@1: duke@1: /** The array holding all encountered names. duke@1: */ duke@1: public byte[] names; duke@1: duke@1: /** The mask to be used for hashing duke@1: */ duke@1: private int hashMask; duke@1: duke@1: /** The number of filled bytes in `names'. duke@1: */ duke@1: private int nc = 0; duke@1: duke@1: /** Allocator duke@1: * @param hashSize the (constant) size to be used for the hash table duke@1: * needs to be a power of two. duke@1: * @param nameSize the initial size of the name table. duke@1: */ duke@1: public Table(int hashSize, int nameSize) { duke@1: hashMask = hashSize - 1; duke@1: hashes = new Name[hashSize]; duke@1: names = new byte[nameSize]; duke@1: duke@1: slash = fromString("/"); duke@1: hyphen = fromString("-"); duke@1: T = fromString("T"); duke@1: slashequals = fromString("/="); duke@1: deprecated = fromString("deprecated"); duke@1: duke@1: init = fromString(""); duke@1: clinit = fromString(""); duke@1: error = fromString(""); duke@1: any = fromString(""); duke@1: empty = fromString(""); duke@1: one = fromString("1"); duke@1: period = fromString("."); duke@1: comma = fromString(","); duke@1: semicolon = fromString(";"); duke@1: asterisk = fromString("*"); duke@1: _this = fromString("this"); duke@1: _super = fromString("super"); duke@1: _default = fromString("default"); duke@1: duke@1: _class = fromString("class"); duke@1: java_lang = fromString("java.lang"); duke@1: java_lang_Object = fromString("java.lang.Object"); duke@1: java_lang_Class = fromString("java.lang.Class"); duke@1: java_lang_Cloneable = fromString("java.lang.Cloneable"); duke@1: java_io_Serializable = fromString("java.io.Serializable"); duke@1: java_lang_Enum = fromString("java.lang.Enum"); duke@1: package_info = fromString("package-info"); duke@1: serialVersionUID = fromString("serialVersionUID"); duke@1: ConstantValue = fromString("ConstantValue"); duke@1: LineNumberTable = fromString("LineNumberTable"); duke@1: LocalVariableTable = fromString("LocalVariableTable"); duke@1: LocalVariableTypeTable = fromString("LocalVariableTypeTable"); duke@1: CharacterRangeTable = fromString("CharacterRangeTable"); duke@1: StackMap = fromString("StackMap"); duke@1: StackMapTable = fromString("StackMapTable"); duke@1: SourceID = fromString("SourceID"); duke@1: CompilationID = fromString("CompilationID"); duke@1: Code = fromString("Code"); duke@1: Exceptions = fromString("Exceptions"); duke@1: SourceFile = fromString("SourceFile"); duke@1: InnerClasses = fromString("InnerClasses"); duke@1: Synthetic = fromString("Synthetic"); duke@1: Bridge= fromString("Bridge"); duke@1: Deprecated = fromString("Deprecated"); duke@1: Enum = fromString("Enum"); duke@1: _name = fromString("name"); duke@1: Signature = fromString("Signature"); duke@1: Varargs = fromString("Varargs"); duke@1: Annotation = fromString("Annotation"); duke@1: RuntimeVisibleAnnotations = fromString("RuntimeVisibleAnnotations"); duke@1: RuntimeInvisibleAnnotations = fromString("RuntimeInvisibleAnnotations"); duke@1: RuntimeVisibleParameterAnnotations = fromString("RuntimeVisibleParameterAnnotations"); duke@1: RuntimeInvisibleParameterAnnotations = fromString("RuntimeInvisibleParameterAnnotations"); duke@1: Value = fromString("Value"); duke@1: EnclosingMethod = fromString("EnclosingMethod"); duke@1: duke@1: desiredAssertionStatus = fromString("desiredAssertionStatus"); duke@1: duke@1: append = fromString("append"); duke@1: family = fromString("family"); duke@1: forName = fromString("forName"); duke@1: toString = fromString("toString"); duke@1: length = fromString("length"); duke@1: valueOf = fromString("valueOf"); duke@1: value = fromString("value"); duke@1: getMessage = fromString("getMessage"); duke@1: getClass = fromString("getClass"); duke@1: duke@1: TYPE = fromString("TYPE"); duke@1: FIELD = fromString("FIELD"); duke@1: METHOD = fromString("METHOD"); duke@1: PARAMETER = fromString("PARAMETER"); duke@1: CONSTRUCTOR = fromString("CONSTRUCTOR"); duke@1: LOCAL_VARIABLE = fromString("LOCAL_VARIABLE"); duke@1: ANNOTATION_TYPE = fromString("ANNOTATION_TYPE"); duke@1: PACKAGE = fromString("PACKAGE"); duke@1: duke@1: SOURCE = fromString("SOURCE"); duke@1: CLASS = fromString("CLASS"); duke@1: RUNTIME = fromString("RUNTIME"); duke@1: duke@1: Array = fromString("Array"); duke@1: Method = fromString("Method"); duke@1: Bound = fromString("Bound"); duke@1: clone = fromString("clone"); duke@1: getComponentType = fromString("getComponentType"); duke@1: getClassLoader = fromString("getClassLoader"); duke@1: initCause = fromString("initCause"); duke@1: values = fromString("values"); duke@1: iterator = fromString("iterator"); duke@1: hasNext = fromString("hasNext"); duke@1: next = fromString("next"); duke@1: AnnotationDefault = fromString("AnnotationDefault"); duke@1: ordinal = fromString("ordinal"); duke@1: equals = fromString("equals"); duke@1: hashCode = fromString("hashCode"); duke@1: compareTo = fromString("compareTo"); duke@1: getDeclaringClass = fromString("getDeclaringClass"); duke@1: ex = fromString("ex"); duke@1: finalize = fromString("finalize"); duke@1: } duke@1: duke@1: public Table() { duke@1: this(0x8000, 0x20000); duke@1: } duke@1: duke@1: /** Create a name from the bytes in cs[start..start+len-1]. duke@1: * Assume that bytes are in utf8 format. duke@1: */ duke@1: public Name fromUtf(byte cs[], int start, int len) { duke@1: return Name.fromUtf(this, cs, start, len); duke@1: } duke@1: duke@1: /** Create a name from the bytes in array cs. duke@1: * Assume that bytes are in utf8 format. duke@1: */ duke@1: public Name fromUtf(byte cs[]) { duke@1: return Name.fromUtf(this, cs, 0, cs.length); duke@1: } duke@1: duke@1: /** Create a name from the characters in cs[start..start+len-1]. duke@1: */ duke@1: public Name fromChars(char[] cs, int start, int len) { duke@1: return Name.fromChars(this, cs, start, len); duke@1: } duke@1: duke@1: /** Create a name from the characters in string s. duke@1: */ duke@1: public Name fromString(CharSequence s) { duke@1: return Name.fromString(this, s); duke@1: } duke@1: duke@1: public final Name slash; duke@1: public final Name hyphen; duke@1: public final Name T; duke@1: public final Name slashequals; duke@1: public final Name deprecated; duke@1: duke@1: public final Name init; duke@1: public final Name clinit; duke@1: public final Name error; duke@1: public final Name any; duke@1: public final Name empty; duke@1: public final Name one; duke@1: public final Name period; duke@1: public final Name comma; duke@1: public final Name semicolon; duke@1: public final Name asterisk; duke@1: public final Name _this; duke@1: public final Name _super; duke@1: public final Name _default; duke@1: duke@1: public final Name _class; duke@1: public final Name java_lang; duke@1: public final Name java_lang_Object; duke@1: public final Name java_lang_Class; duke@1: public final Name java_lang_Cloneable; duke@1: public final Name java_io_Serializable; duke@1: public final Name serialVersionUID; duke@1: public final Name java_lang_Enum; duke@1: public final Name package_info; duke@1: public final Name ConstantValue; duke@1: public final Name LineNumberTable; duke@1: public final Name LocalVariableTable; duke@1: public final Name LocalVariableTypeTable; duke@1: public final Name CharacterRangeTable; duke@1: public final Name StackMap; duke@1: public final Name StackMapTable; duke@1: public final Name SourceID; duke@1: public final Name CompilationID; duke@1: public final Name Code; duke@1: public final Name Exceptions; duke@1: public final Name SourceFile; duke@1: public final Name InnerClasses; duke@1: public final Name Synthetic; duke@1: public final Name Bridge; duke@1: public final Name Deprecated; duke@1: public final Name Enum; duke@1: public final Name _name; duke@1: public final Name Signature; duke@1: public final Name Varargs; duke@1: public final Name Annotation; duke@1: public final Name RuntimeVisibleAnnotations; duke@1: public final Name RuntimeInvisibleAnnotations; duke@1: public final Name RuntimeVisibleParameterAnnotations; duke@1: public final Name RuntimeInvisibleParameterAnnotations; duke@1: duke@1: public final Name Value; duke@1: public final Name EnclosingMethod; duke@1: duke@1: public final Name desiredAssertionStatus; duke@1: duke@1: public final Name append; duke@1: public final Name family; duke@1: public final Name forName; duke@1: public final Name toString; duke@1: public final Name length; duke@1: public final Name valueOf; duke@1: public final Name value; duke@1: public final Name getMessage; duke@1: public final Name getClass; duke@1: duke@1: public final Name TYPE; duke@1: public final Name FIELD; duke@1: public final Name METHOD; duke@1: public final Name PARAMETER; duke@1: public final Name CONSTRUCTOR; duke@1: public final Name LOCAL_VARIABLE; duke@1: public final Name ANNOTATION_TYPE; duke@1: public final Name PACKAGE; duke@1: duke@1: public final Name SOURCE; duke@1: public final Name CLASS; duke@1: public final Name RUNTIME; duke@1: duke@1: public final Name Array; duke@1: public final Name Method; duke@1: public final Name Bound; duke@1: public final Name clone; duke@1: public final Name getComponentType; duke@1: public final Name getClassLoader; duke@1: public final Name initCause; duke@1: public final Name values; duke@1: public final Name iterator; duke@1: public final Name hasNext; duke@1: public final Name next; duke@1: public final Name AnnotationDefault; duke@1: public final Name ordinal; duke@1: public final Name equals; duke@1: public final Name hashCode; duke@1: public final Name compareTo; duke@1: public final Name getDeclaringClass; duke@1: public final Name ex; duke@1: public final Name finalize; duke@1: } duke@1: duke@1: public boolean isEmpty() { duke@1: return len == 0; duke@1: } duke@1: }