1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/util/Name.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,247 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.util; 1.30 + 1.31 +/** An abstraction for internal compiler strings. They are stored in 1.32 + * Utf8 format. Names are stored in a Name.Table, and are unique within 1.33 + * that table. 1.34 + * 1.35 + * <p><b>This is NOT part of any supported API. 1.36 + * If you write code that depends on this, you do so at your own risk. 1.37 + * This code and its internal interfaces are subject to change or 1.38 + * deletion without notice.</b> 1.39 + */ 1.40 +public abstract class Name implements javax.lang.model.element.Name { 1.41 + 1.42 + public final Table table; 1.43 + 1.44 + protected Name(Table table) { 1.45 + this.table = table; 1.46 + } 1.47 + 1.48 + /** 1.49 + * {@inheritDoc} 1.50 + */ 1.51 + public boolean contentEquals(CharSequence cs) { 1.52 + return toString().equals(cs.toString()); 1.53 + } 1.54 + 1.55 + /** 1.56 + * {@inheritDoc} 1.57 + */ 1.58 + public int length() { 1.59 + return toString().length(); 1.60 + } 1.61 + 1.62 + /** 1.63 + * {@inheritDoc} 1.64 + */ 1.65 + public char charAt(int index) { 1.66 + return toString().charAt(index); 1.67 + } 1.68 + 1.69 + /** 1.70 + * {@inheritDoc} 1.71 + */ 1.72 + public CharSequence subSequence(int start, int end) { 1.73 + return toString().subSequence(start, end); 1.74 + } 1.75 + 1.76 + /** Return the concatenation of this name and name `n'. 1.77 + */ 1.78 + public Name append(Name n) { 1.79 + int len = getByteLength(); 1.80 + byte[] bs = new byte[len + n.getByteLength()]; 1.81 + getBytes(bs, 0); 1.82 + n.getBytes(bs, len); 1.83 + return table.fromUtf(bs, 0, bs.length); 1.84 + } 1.85 + 1.86 + /** Return the concatenation of this name, the given ASCII 1.87 + * character, and name `n'. 1.88 + */ 1.89 + public Name append(char c, Name n) { 1.90 + int len = getByteLength(); 1.91 + byte[] bs = new byte[len + 1 + n.getByteLength()]; 1.92 + getBytes(bs, 0); 1.93 + bs[len] = (byte) c; 1.94 + n.getBytes(bs, len+1); 1.95 + return table.fromUtf(bs, 0, bs.length); 1.96 + } 1.97 + 1.98 + /** An arbitrary but consistent complete order among all Names. 1.99 + */ 1.100 + public int compareTo(Name other) { 1.101 + return other.getIndex() - this.getIndex(); 1.102 + } 1.103 + 1.104 + /** Return true if this is the empty name. 1.105 + */ 1.106 + public boolean isEmpty() { 1.107 + return getByteLength() == 0; 1.108 + } 1.109 + 1.110 + /** Returns last occurrence of byte b in this name, -1 if not found. 1.111 + */ 1.112 + public int lastIndexOf(byte b) { 1.113 + byte[] bytes = getByteArray(); 1.114 + int offset = getByteOffset(); 1.115 + int i = getByteLength() - 1; 1.116 + while (i >= 0 && bytes[offset + i] != b) i--; 1.117 + return i; 1.118 + } 1.119 + 1.120 + /** Does this name start with prefix? 1.121 + */ 1.122 + public boolean startsWith(Name prefix) { 1.123 + byte[] thisBytes = this.getByteArray(); 1.124 + int thisOffset = this.getByteOffset(); 1.125 + int thisLength = this.getByteLength(); 1.126 + byte[] prefixBytes = prefix.getByteArray(); 1.127 + int prefixOffset = prefix.getByteOffset(); 1.128 + int prefixLength = prefix.getByteLength(); 1.129 + 1.130 + int i = 0; 1.131 + while (i < prefixLength && 1.132 + i < thisLength && 1.133 + thisBytes[thisOffset + i] == prefixBytes[prefixOffset + i]) 1.134 + i++; 1.135 + return i == prefixLength; 1.136 + } 1.137 + 1.138 + /** Returns the sub-name starting at position start, up to and 1.139 + * excluding position end. 1.140 + */ 1.141 + public Name subName(int start, int end) { 1.142 + if (end < start) end = start; 1.143 + return table.fromUtf(getByteArray(), getByteOffset() + start, end - start); 1.144 + } 1.145 + 1.146 + /** Return the string representation of this name. 1.147 + */ 1.148 + @Override 1.149 + public String toString() { 1.150 + return Convert.utf2string(getByteArray(), getByteOffset(), getByteLength()); 1.151 + } 1.152 + 1.153 + /** Return the Utf8 representation of this name. 1.154 + */ 1.155 + public byte[] toUtf() { 1.156 + byte[] bs = new byte[getByteLength()]; 1.157 + getBytes(bs, 0); 1.158 + return bs; 1.159 + } 1.160 + 1.161 + /* Get a "reasonably small" value that uniquely identifies this name 1.162 + * within its name table. 1.163 + */ 1.164 + public abstract int getIndex(); 1.165 + 1.166 + /** Get the length (in bytes) of this name. 1.167 + */ 1.168 + public abstract int getByteLength(); 1.169 + 1.170 + /** Returns i'th byte of this name. 1.171 + */ 1.172 + public abstract byte getByteAt(int i); 1.173 + 1.174 + /** Copy all bytes of this name to buffer cs, starting at start. 1.175 + */ 1.176 + public void getBytes(byte cs[], int start) { 1.177 + System.arraycopy(getByteArray(), getByteOffset(), cs, start, getByteLength()); 1.178 + } 1.179 + 1.180 + /** Get the underlying byte array for this name. The contents of the 1.181 + * array must not be modified. 1.182 + */ 1.183 + public abstract byte[] getByteArray(); 1.184 + 1.185 + /** Get the start offset of this name within its byte array. 1.186 + */ 1.187 + public abstract int getByteOffset(); 1.188 + 1.189 + /** An abstraction for the hash table used to create unique Name instances. 1.190 + */ 1.191 + public static abstract class Table { 1.192 + /** Standard name table. 1.193 + */ 1.194 + public final Names names; 1.195 + 1.196 + Table(Names names) { 1.197 + this.names = names; 1.198 + } 1.199 + 1.200 + /** Get the name from the characters in cs[start..start+len-1]. 1.201 + */ 1.202 + public abstract Name fromChars(char[] cs, int start, int len); 1.203 + 1.204 + /** Get the name for the characters in string s. 1.205 + */ 1.206 + public Name fromString(String s) { 1.207 + char[] cs = s.toCharArray(); 1.208 + return fromChars(cs, 0, cs.length); 1.209 + } 1.210 + 1.211 + /** Get the name for the bytes in array cs. 1.212 + * Assume that bytes are in utf8 format. 1.213 + */ 1.214 + public Name fromUtf(byte[] cs) { 1.215 + return fromUtf(cs, 0, cs.length); 1.216 + } 1.217 + 1.218 + /** get the name for the bytes in cs[start..start+len-1]. 1.219 + * Assume that bytes are in utf8 format. 1.220 + */ 1.221 + public abstract Name fromUtf(byte[] cs, int start, int len); 1.222 + 1.223 + /** Release any resources used by this table. 1.224 + */ 1.225 + public abstract void dispose(); 1.226 + 1.227 + /** The hashcode of a name. 1.228 + */ 1.229 + protected static int hashValue(byte bytes[], int offset, int length) { 1.230 + int h = 0; 1.231 + int off = offset; 1.232 + 1.233 + for (int i = 0; i < length; i++) { 1.234 + h = (h << 5) - h + bytes[off++]; 1.235 + } 1.236 + return h; 1.237 + } 1.238 + 1.239 + /** Compare two subarrays 1.240 + */ 1.241 + protected static boolean equals(byte[] bytes1, int offset1, 1.242 + byte[] bytes2, int offset2, int length) { 1.243 + int i = 0; 1.244 + while (i < length && bytes1[offset1 + i] == bytes2[offset2 + i]) { 1.245 + i++; 1.246 + } 1.247 + return i == length; 1.248 + } 1.249 + } 1.250 +}