src/share/classes/com/sun/tools/javac/util/Name.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.util;
aoqi@0 27
aoqi@0 28 /** An abstraction for internal compiler strings. They are stored in
aoqi@0 29 * Utf8 format. Names are stored in a Name.Table, and are unique within
aoqi@0 30 * that table.
aoqi@0 31 *
aoqi@0 32 * <p><b>This is NOT part of any supported API.
aoqi@0 33 * If you write code that depends on this, you do so at your own risk.
aoqi@0 34 * This code and its internal interfaces are subject to change or
aoqi@0 35 * deletion without notice.</b>
aoqi@0 36 */
aoqi@0 37 public abstract class Name implements javax.lang.model.element.Name {
aoqi@0 38
aoqi@0 39 public final Table table;
aoqi@0 40
aoqi@0 41 protected Name(Table table) {
aoqi@0 42 this.table = table;
aoqi@0 43 }
aoqi@0 44
aoqi@0 45 /**
aoqi@0 46 * {@inheritDoc}
aoqi@0 47 */
aoqi@0 48 public boolean contentEquals(CharSequence cs) {
aoqi@0 49 return toString().equals(cs.toString());
aoqi@0 50 }
aoqi@0 51
aoqi@0 52 /**
aoqi@0 53 * {@inheritDoc}
aoqi@0 54 */
aoqi@0 55 public int length() {
aoqi@0 56 return toString().length();
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * {@inheritDoc}
aoqi@0 61 */
aoqi@0 62 public char charAt(int index) {
aoqi@0 63 return toString().charAt(index);
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 /**
aoqi@0 67 * {@inheritDoc}
aoqi@0 68 */
aoqi@0 69 public CharSequence subSequence(int start, int end) {
aoqi@0 70 return toString().subSequence(start, end);
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 /** Return the concatenation of this name and name `n'.
aoqi@0 74 */
aoqi@0 75 public Name append(Name n) {
aoqi@0 76 int len = getByteLength();
aoqi@0 77 byte[] bs = new byte[len + n.getByteLength()];
aoqi@0 78 getBytes(bs, 0);
aoqi@0 79 n.getBytes(bs, len);
aoqi@0 80 return table.fromUtf(bs, 0, bs.length);
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 /** Return the concatenation of this name, the given ASCII
aoqi@0 84 * character, and name `n'.
aoqi@0 85 */
aoqi@0 86 public Name append(char c, Name n) {
aoqi@0 87 int len = getByteLength();
aoqi@0 88 byte[] bs = new byte[len + 1 + n.getByteLength()];
aoqi@0 89 getBytes(bs, 0);
aoqi@0 90 bs[len] = (byte) c;
aoqi@0 91 n.getBytes(bs, len+1);
aoqi@0 92 return table.fromUtf(bs, 0, bs.length);
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 /** An arbitrary but consistent complete order among all Names.
aoqi@0 96 */
aoqi@0 97 public int compareTo(Name other) {
aoqi@0 98 return other.getIndex() - this.getIndex();
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 /** Return true if this is the empty name.
aoqi@0 102 */
aoqi@0 103 public boolean isEmpty() {
aoqi@0 104 return getByteLength() == 0;
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 /** Returns last occurrence of byte b in this name, -1 if not found.
aoqi@0 108 */
aoqi@0 109 public int lastIndexOf(byte b) {
aoqi@0 110 byte[] bytes = getByteArray();
aoqi@0 111 int offset = getByteOffset();
aoqi@0 112 int i = getByteLength() - 1;
aoqi@0 113 while (i >= 0 && bytes[offset + i] != b) i--;
aoqi@0 114 return i;
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 /** Does this name start with prefix?
aoqi@0 118 */
aoqi@0 119 public boolean startsWith(Name prefix) {
aoqi@0 120 byte[] thisBytes = this.getByteArray();
aoqi@0 121 int thisOffset = this.getByteOffset();
aoqi@0 122 int thisLength = this.getByteLength();
aoqi@0 123 byte[] prefixBytes = prefix.getByteArray();
aoqi@0 124 int prefixOffset = prefix.getByteOffset();
aoqi@0 125 int prefixLength = prefix.getByteLength();
aoqi@0 126
aoqi@0 127 int i = 0;
aoqi@0 128 while (i < prefixLength &&
aoqi@0 129 i < thisLength &&
aoqi@0 130 thisBytes[thisOffset + i] == prefixBytes[prefixOffset + i])
aoqi@0 131 i++;
aoqi@0 132 return i == prefixLength;
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 /** Returns the sub-name starting at position start, up to and
aoqi@0 136 * excluding position end.
aoqi@0 137 */
aoqi@0 138 public Name subName(int start, int end) {
aoqi@0 139 if (end < start) end = start;
aoqi@0 140 return table.fromUtf(getByteArray(), getByteOffset() + start, end - start);
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 /** Return the string representation of this name.
aoqi@0 144 */
aoqi@0 145 @Override
aoqi@0 146 public String toString() {
aoqi@0 147 return Convert.utf2string(getByteArray(), getByteOffset(), getByteLength());
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 /** Return the Utf8 representation of this name.
aoqi@0 151 */
aoqi@0 152 public byte[] toUtf() {
aoqi@0 153 byte[] bs = new byte[getByteLength()];
aoqi@0 154 getBytes(bs, 0);
aoqi@0 155 return bs;
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 /* Get a "reasonably small" value that uniquely identifies this name
aoqi@0 159 * within its name table.
aoqi@0 160 */
aoqi@0 161 public abstract int getIndex();
aoqi@0 162
aoqi@0 163 /** Get the length (in bytes) of this name.
aoqi@0 164 */
aoqi@0 165 public abstract int getByteLength();
aoqi@0 166
aoqi@0 167 /** Returns i'th byte of this name.
aoqi@0 168 */
aoqi@0 169 public abstract byte getByteAt(int i);
aoqi@0 170
aoqi@0 171 /** Copy all bytes of this name to buffer cs, starting at start.
aoqi@0 172 */
aoqi@0 173 public void getBytes(byte cs[], int start) {
aoqi@0 174 System.arraycopy(getByteArray(), getByteOffset(), cs, start, getByteLength());
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 /** Get the underlying byte array for this name. The contents of the
aoqi@0 178 * array must not be modified.
aoqi@0 179 */
aoqi@0 180 public abstract byte[] getByteArray();
aoqi@0 181
aoqi@0 182 /** Get the start offset of this name within its byte array.
aoqi@0 183 */
aoqi@0 184 public abstract int getByteOffset();
aoqi@0 185
aoqi@0 186 /** An abstraction for the hash table used to create unique Name instances.
aoqi@0 187 */
aoqi@0 188 public static abstract class Table {
aoqi@0 189 /** Standard name table.
aoqi@0 190 */
aoqi@0 191 public final Names names;
aoqi@0 192
aoqi@0 193 Table(Names names) {
aoqi@0 194 this.names = names;
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 /** Get the name from the characters in cs[start..start+len-1].
aoqi@0 198 */
aoqi@0 199 public abstract Name fromChars(char[] cs, int start, int len);
aoqi@0 200
aoqi@0 201 /** Get the name for the characters in string s.
aoqi@0 202 */
aoqi@0 203 public Name fromString(String s) {
aoqi@0 204 char[] cs = s.toCharArray();
aoqi@0 205 return fromChars(cs, 0, cs.length);
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 /** Get the name for the bytes in array cs.
aoqi@0 209 * Assume that bytes are in utf8 format.
aoqi@0 210 */
aoqi@0 211 public Name fromUtf(byte[] cs) {
aoqi@0 212 return fromUtf(cs, 0, cs.length);
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 /** get the name for the bytes in cs[start..start+len-1].
aoqi@0 216 * Assume that bytes are in utf8 format.
aoqi@0 217 */
aoqi@0 218 public abstract Name fromUtf(byte[] cs, int start, int len);
aoqi@0 219
aoqi@0 220 /** Release any resources used by this table.
aoqi@0 221 */
aoqi@0 222 public abstract void dispose();
aoqi@0 223
aoqi@0 224 /** The hashcode of a name.
aoqi@0 225 */
aoqi@0 226 protected static int hashValue(byte bytes[], int offset, int length) {
aoqi@0 227 int h = 0;
aoqi@0 228 int off = offset;
aoqi@0 229
aoqi@0 230 for (int i = 0; i < length; i++) {
aoqi@0 231 h = (h << 5) - h + bytes[off++];
aoqi@0 232 }
aoqi@0 233 return h;
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 /** Compare two subarrays
aoqi@0 237 */
aoqi@0 238 protected static boolean equals(byte[] bytes1, int offset1,
aoqi@0 239 byte[] bytes2, int offset2, int length) {
aoqi@0 240 int i = 0;
aoqi@0 241 while (i < length && bytes1[offset1 + i] == bytes2[offset2 + i]) {
aoqi@0 242 i++;
aoqi@0 243 }
aoqi@0 244 return i == length;
aoqi@0 245 }
aoqi@0 246 }
aoqi@0 247 }

mercurial