Thu, 12 Oct 2017 19:44:07 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2010, 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.codemodel.internal; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.HashSet; |
aoqi@0 | 29 | import java.util.regex.Matcher; |
aoqi@0 | 30 | import java.util.regex.Pattern; |
aoqi@0 | 31 | |
aoqi@0 | 32 | /** |
aoqi@0 | 33 | * Utility methods that convert arbitrary strings into Java identifiers. |
aoqi@0 | 34 | */ |
aoqi@0 | 35 | public class JJavaName { |
aoqi@0 | 36 | |
aoqi@0 | 37 | |
aoqi@0 | 38 | /** |
aoqi@0 | 39 | * Checks if a given string is usable as a Java identifier. |
aoqi@0 | 40 | */ |
aoqi@0 | 41 | public static boolean isJavaIdentifier(String s) { |
aoqi@0 | 42 | if(s.length()==0) return false; |
aoqi@0 | 43 | if( reservedKeywords.contains(s) ) return false; |
aoqi@0 | 44 | |
aoqi@0 | 45 | if(!Character.isJavaIdentifierStart(s.charAt(0))) return false; |
aoqi@0 | 46 | |
aoqi@0 | 47 | for (int i = 1; i < s.length(); i++) |
aoqi@0 | 48 | if (!Character.isJavaIdentifierPart(s.charAt(i))) |
aoqi@0 | 49 | return false; |
aoqi@0 | 50 | |
aoqi@0 | 51 | return true; |
aoqi@0 | 52 | } |
aoqi@0 | 53 | |
aoqi@0 | 54 | /** |
aoqi@0 | 55 | * Checks if the given string is a valid fully qualified name. |
aoqi@0 | 56 | */ |
aoqi@0 | 57 | public static boolean isFullyQualifiedClassName(String s) { |
aoqi@0 | 58 | return isJavaPackageName(s); |
aoqi@0 | 59 | } |
aoqi@0 | 60 | |
aoqi@0 | 61 | /** |
aoqi@0 | 62 | * Checks if the given string is a valid Java package name. |
aoqi@0 | 63 | */ |
aoqi@0 | 64 | public static boolean isJavaPackageName(String s) { |
aoqi@0 | 65 | while(s.length()!=0) { |
aoqi@0 | 66 | int idx = s.indexOf('.'); |
aoqi@0 | 67 | if(idx==-1) idx=s.length(); |
aoqi@0 | 68 | if( !isJavaIdentifier(s.substring(0,idx)) ) |
aoqi@0 | 69 | return false; |
aoqi@0 | 70 | |
aoqi@0 | 71 | s = s.substring(idx); |
aoqi@0 | 72 | if(s.length()!=0) s = s.substring(1); // remove '.' |
aoqi@0 | 73 | } |
aoqi@0 | 74 | return true; |
aoqi@0 | 75 | } |
aoqi@0 | 76 | |
aoqi@0 | 77 | /** |
aoqi@0 | 78 | * <b>Experimental API:</b> converts an English word into a plural form. |
aoqi@0 | 79 | * |
aoqi@0 | 80 | * @param word |
aoqi@0 | 81 | * a word, such as "child", "apple". Must not be null. |
aoqi@0 | 82 | * It accepts word concatanation forms |
aoqi@0 | 83 | * that are common in programming languages, such as "my_child", "MyChild", |
aoqi@0 | 84 | * "myChild", "MY-CHILD", "CODE003-child", etc, and mostly tries to do the right thing. |
aoqi@0 | 85 | * ("my_children","MyChildren","myChildren", and "MY-CHILDREN", "CODE003-children" respectively) |
aoqi@0 | 86 | * <p> |
aoqi@0 | 87 | * Although this method only works for English words, it handles non-English |
aoqi@0 | 88 | * words gracefully (by just returning it as-is.) For example, 日本語 |
aoqi@0 | 89 | * will be returned as-is without modified, not "日本語s" |
aoqi@0 | 90 | * <p> |
aoqi@0 | 91 | * This method doesn't handle suffixes very well. For example, passing |
aoqi@0 | 92 | * "person56" will return "person56s", not "people56". |
aoqi@0 | 93 | * |
aoqi@0 | 94 | * @return |
aoqi@0 | 95 | * always non-null. |
aoqi@0 | 96 | */ |
aoqi@0 | 97 | public static String getPluralForm(String word) { |
aoqi@0 | 98 | // remember the casing of the word |
aoqi@0 | 99 | boolean allUpper = true; |
aoqi@0 | 100 | |
aoqi@0 | 101 | // check if the word looks like an English word. |
aoqi@0 | 102 | // if we see non-ASCII characters, abort |
aoqi@0 | 103 | for(int i=0; i<word.length(); i++ ) { |
aoqi@0 | 104 | char ch = word.charAt(i); |
aoqi@0 | 105 | if(ch >=0x80) |
aoqi@0 | 106 | return word; |
aoqi@0 | 107 | |
aoqi@0 | 108 | // note that this isn't the same as allUpper &= Character.isUpperCase(ch); |
aoqi@0 | 109 | allUpper &= !Character.isLowerCase(ch); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | for (Entry e : TABLE) { |
aoqi@0 | 113 | String r = e.apply(word); |
aoqi@0 | 114 | if(r!=null) { |
aoqi@0 | 115 | if(allUpper) r=r.toUpperCase(); |
aoqi@0 | 116 | return r; |
aoqi@0 | 117 | } |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | // failed |
aoqi@0 | 121 | return word; |
aoqi@0 | 122 | } |
aoqi@0 | 123 | |
aoqi@0 | 124 | |
aoqi@0 | 125 | /** All reserved keywords of Java. */ |
aoqi@0 | 126 | private static HashSet<String> reservedKeywords = new HashSet<String>(); |
aoqi@0 | 127 | |
aoqi@0 | 128 | static { |
aoqi@0 | 129 | // see http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html |
aoqi@0 | 130 | String[] words = new String[]{ |
aoqi@0 | 131 | "abstract", |
aoqi@0 | 132 | "boolean", |
aoqi@0 | 133 | "break", |
aoqi@0 | 134 | "byte", |
aoqi@0 | 135 | "case", |
aoqi@0 | 136 | "catch", |
aoqi@0 | 137 | "char", |
aoqi@0 | 138 | "class", |
aoqi@0 | 139 | "const", |
aoqi@0 | 140 | "continue", |
aoqi@0 | 141 | "default", |
aoqi@0 | 142 | "do", |
aoqi@0 | 143 | "double", |
aoqi@0 | 144 | "else", |
aoqi@0 | 145 | "extends", |
aoqi@0 | 146 | "final", |
aoqi@0 | 147 | "finally", |
aoqi@0 | 148 | "float", |
aoqi@0 | 149 | "for", |
aoqi@0 | 150 | "goto", |
aoqi@0 | 151 | "if", |
aoqi@0 | 152 | "implements", |
aoqi@0 | 153 | "import", |
aoqi@0 | 154 | "instanceof", |
aoqi@0 | 155 | "int", |
aoqi@0 | 156 | "interface", |
aoqi@0 | 157 | "long", |
aoqi@0 | 158 | "native", |
aoqi@0 | 159 | "new", |
aoqi@0 | 160 | "package", |
aoqi@0 | 161 | "private", |
aoqi@0 | 162 | "protected", |
aoqi@0 | 163 | "public", |
aoqi@0 | 164 | "return", |
aoqi@0 | 165 | "short", |
aoqi@0 | 166 | "static", |
aoqi@0 | 167 | "strictfp", |
aoqi@0 | 168 | "super", |
aoqi@0 | 169 | "switch", |
aoqi@0 | 170 | "synchronized", |
aoqi@0 | 171 | "this", |
aoqi@0 | 172 | "throw", |
aoqi@0 | 173 | "throws", |
aoqi@0 | 174 | "transient", |
aoqi@0 | 175 | "try", |
aoqi@0 | 176 | "void", |
aoqi@0 | 177 | "volatile", |
aoqi@0 | 178 | "while", |
aoqi@0 | 179 | |
aoqi@0 | 180 | // technically these are not reserved words but they cannot be used as identifiers. |
aoqi@0 | 181 | "true", |
aoqi@0 | 182 | "false", |
aoqi@0 | 183 | "null", |
aoqi@0 | 184 | |
aoqi@0 | 185 | // and I believe assert is also a new keyword |
aoqi@0 | 186 | "assert", |
aoqi@0 | 187 | |
aoqi@0 | 188 | // and 5.0 keywords |
aoqi@0 | 189 | "enum" |
aoqi@0 | 190 | }; |
aoqi@0 | 191 | for (String w : words) |
aoqi@0 | 192 | reservedKeywords.add(w); |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | |
aoqi@0 | 196 | private static class Entry { |
aoqi@0 | 197 | private final Pattern pattern; |
aoqi@0 | 198 | private final String replacement; |
aoqi@0 | 199 | |
aoqi@0 | 200 | public Entry(String pattern, String replacement) { |
aoqi@0 | 201 | this.pattern = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE); |
aoqi@0 | 202 | this.replacement = replacement; |
aoqi@0 | 203 | } |
aoqi@0 | 204 | |
aoqi@0 | 205 | String apply(String word) { |
aoqi@0 | 206 | Matcher m = pattern.matcher(word); |
aoqi@0 | 207 | if(m.matches()) { |
aoqi@0 | 208 | StringBuffer buf = new StringBuffer(); |
aoqi@0 | 209 | m.appendReplacement(buf,replacement); |
aoqi@0 | 210 | return buf.toString(); |
aoqi@0 | 211 | } else { |
aoqi@0 | 212 | return null; |
aoqi@0 | 213 | } |
aoqi@0 | 214 | } |
aoqi@0 | 215 | } |
aoqi@0 | 216 | |
aoqi@0 | 217 | private static final Entry[] TABLE; |
aoqi@0 | 218 | |
aoqi@0 | 219 | static { |
aoqi@0 | 220 | String[] source = { |
aoqi@0 | 221 | "(.*)child","$1children", |
aoqi@0 | 222 | "(.+)fe","$1ves", |
aoqi@0 | 223 | "(.*)mouse","$1mise", |
aoqi@0 | 224 | "(.+)f","$1ves", |
aoqi@0 | 225 | "(.+)ch","$1ches", |
aoqi@0 | 226 | "(.+)sh","$1shes", |
aoqi@0 | 227 | "(.*)tooth","$1teeth", |
aoqi@0 | 228 | "(.+)um","$1a", |
aoqi@0 | 229 | "(.+)an","$1en", |
aoqi@0 | 230 | "(.+)ato","$1atoes", |
aoqi@0 | 231 | "(.*)basis","$1bases", |
aoqi@0 | 232 | "(.*)axis","$1axes", |
aoqi@0 | 233 | "(.+)is","$1ises", |
aoqi@0 | 234 | "(.+)ss","$1sses", |
aoqi@0 | 235 | "(.+)us","$1uses", |
aoqi@0 | 236 | "(.+)s","$1s", |
aoqi@0 | 237 | "(.*)foot","$1feet", |
aoqi@0 | 238 | "(.+)ix","$1ixes", |
aoqi@0 | 239 | "(.+)ex","$1ices", |
aoqi@0 | 240 | "(.+)nx","$1nxes", |
aoqi@0 | 241 | "(.+)x","$1xes", |
aoqi@0 | 242 | "(.+)y","$1ies", |
aoqi@0 | 243 | "(.+)","$1s", |
aoqi@0 | 244 | }; |
aoqi@0 | 245 | |
aoqi@0 | 246 | TABLE = new Entry[source.length/2]; |
aoqi@0 | 247 | |
aoqi@0 | 248 | for( int i=0; i<source.length; i+=2 ) { |
aoqi@0 | 249 | TABLE[i/2] = new Entry(source[i],source[i+1]); |
aoqi@0 | 250 | } |
aoqi@0 | 251 | } |
aoqi@0 | 252 | } |