// Here are the details of the matching. We track the current brace depth, and we artificially // consider that the whole file is at brace depth 1 inside a pseudo-class whose name is the // name of the package. When we see a class definition, we push the fully-qualified name of the // class on a stack so that we can associate abstract methods we find with the possibly-nested // class they belong to. A class definition must occur at brace depth one more than the class // containing it, which is equivalent to saying that the brace depth must be the same as the // class stack depth. This check excludes local class definitions within methods and // initializers. If we meet these constraints and we see the word "class" followed by an // identifier Foo, then we consider that we are entering the definition of class Foo. We determine // the fully-qualified name of Foo, which is container.Foo, where container is the current top of // the class stack (initially, the package name). We push this new fully-qualified name on the // class stack. We have not yet seen the left brace with the class definition so at this point the // class stack depth is one more than the brace depth. When we subsequently see a right brace that // takes us back to this situation then we know we have completed the definition of Foo and we can // pop it from the class stack. // // We check that the token after "class" is indeed an identifier to avoid confusion // with Foo.class. Even though the tokenizer does not distinguish between identifiers and // keywords, it is enough to exclude the single word "instanceof" because that is the only word // that can legally appear after Foo.class (though in a legal program the resultant expression // will always be true). // // Again, we are at the top level of a class when the brace depth is equal to the class stack // depth. If we then see the word "abstract" then that is the start either of an abstract class // definition or of an abstract method. We record that we have seen "abstract" and we cancel // that indication as soon as we see a left brace, to exclude the abstract class case, and also // the case of interfaces or @interfaces redundantly declared abstract. Now, when // we see an identifier that is preceded by an uncanceled "abstract" and followed by a left paren // then we have found an abstract method of the class on the top of the class stack. We record it // in the list of abstract methods of the class on the top of the class stack. We don't bother // checking that the method has no parameters, because an @AutoValue class will cause a compiler // error if there are abstract methods with parameters, since the @AutoValue processor doesn't // know how to implement them in the concrete subclass it generates. Map<String, List<String>> abstractMethods(JavaTokenizer tokenizer, String packageName) { Map<String, List<String>> abstractMethods = new HashMap<String, List<String>>(); Deque<String> classStack = new ArrayDeque<String>(); classStack.addLast(packageName); int braceDepth = 1; boolean sawAbstract = false; String className = null; for (String previousToken = "", token = tokenizer.nextToken(); token != null; previousToken = token, token = tokenizer.nextToken()) { boolean topLevel = (braceDepth == classStack.size()); if (className != null) { // get last term in fully-qualified class name (e.g. "class some.package.Bar { ...") if (token.equals(".")) { className = tokenizer.nextToken(); continue; } else { if (Character.isJavaIdentifierStart(className.charAt(0)) && !className.equals("instanceof")) { String container = classStack.getLast(); // container might be empty in the case of a packageless class classStack.add(container.isEmpty() ? className : container + "." + className); } className = null; } } if (token.equals("{")) { braceDepth++; sawAbstract = false; } else if (token.equals("}")) { braceDepth--; if (topLevel) { classStack.removeLast(); } } else if (topLevel) { if (token.equals("class") || token.equals("interface")) { className = tokenizer.nextToken(); } else if (token.equals("abstract")) { sawAbstract = true; } else if (token.equals("(")) { if (sawAbstract && Character.isJavaIdentifierStart(previousToken.charAt(0))) { List<String> methods = abstractMethods.get(classStack.getLast()); if (methods == null) { methods = new ArrayList<String>(); abstractMethods.put(classStack.getLast(), methods); } methods.add(previousToken); } sawAbstract = false; } } } return abstractMethods; }
/** * Checks that the given string is a valid Java identifier. * * @param version * the class version. * @param name * the string to be checked. * @param msg * a message to be used in case of error. */ static void checkMethodIdentifier(int version, final String name, final String msg) { if (name == null || name.length() == 0) { throw new IllegalArgumentException("Invalid " + msg + " (must not be null or empty)"); } if ((version & 0xFFFF) >= Opcodes.V1_5) { for (int i = 0; i < name.length(); ++i) { if (".;[/<>".indexOf(name.charAt(i)) != -1) { throw new IllegalArgumentException("Invalid " + msg + " (must be a valid unqualified name): " + name); } } return; } if (!Character.isJavaIdentifierStart(name.charAt(0))) { throw new IllegalArgumentException( "Invalid " + msg + " (must be a '<init>', '<clinit>' or a valid Java identifier): " + name); } for (int i = 1; i < name.length(); ++i) { if (!Character.isJavaIdentifierPart(name.charAt(i))) { throw new IllegalArgumentException( "Invalid " + msg + " (must be '<init>' or '<clinit>' or a valid Java identifier): " + name); } } }
@NotNull public static ContractsContext getContext(@NotNull String name) { ContractsContext context; if (name.equals(ROOT_NAME)) { return ROOT; } synchronized (CONTEXTS) { context = CONTEXTS.get(name); if (context != null) { return context; } if (!ROOT_NAME.equals(name)) { boolean firstChar = true; for (int i = 0; i < name.length(); i++) { char c = name.charAt(i); if (firstChar) { if (!Character.isJavaIdentifierStart(c)) { throw new IllegalArgumentException("Illegal policy name: '" + name + "'"); } firstChar = false; } else { if (c == '.') { firstChar = true; } else if (!Character.isJavaIdentifierPart(c)) { throw new IllegalArgumentException("Illegal policy name: '" + name + "'"); } } } if (firstChar) { throw new IllegalArgumentException("Illegal policy name: '" + name + "'"); } } return getContext0(name); } }
// Parse a single line from the given configuration file, adding the name // on the line to the names list. // private int parseLine(Class<?> service, URL u, BufferedReader r, int lc, List<String> names) throws IOException, ServiceConfigurationError { String ln = r.readLine(); if (ln == null) { return -1; } int ci = ln.indexOf('#'); if (ci >= 0) ln = ln.substring(0, ci); ln = ln.trim(); int n = ln.length(); if (n != 0) { if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) fail(service, u, lc, "Illegal configuration-file syntax"); int cp = ln.codePointAt(0); if (!Character.isJavaIdentifierStart(cp)) fail(service, u, lc, "Illegal provider-class name: " + ln); for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) { cp = ln.codePointAt(i); if (!Character.isJavaIdentifierPart(cp) && (cp != '.')) fail(service, u, lc, "Illegal provider-class name: " + ln); } if (!providers.containsKey(ln) && !names.contains(ln)) names.add(ln); } return lc + 1; }
public static boolean isIdentifier(Token token) { if (token.type != token.IDENTIFIER) return false; if (!Character.isJavaIdentifierStart(token.text[token.textOffset])) return false; for (int i = 1; i < token.textCount; i++) if (!Character.isJavaIdentifierPart(token.text[token.textOffset + i])) return false; return true; }
private boolean checkForDynamicImport(String className) { if (packageImports == null) return false; if (!Character.isJavaIdentifierStart(className.charAt(0))) return false; if (nonValidImports != null && nonValidImports.contains(className)) return false; int found = 0; Class cls = null; for (String pkg : packageImports) { try { cls = Class.forName(pkg + "." + className, true, getClassLoader()); found++; } catch (ClassNotFoundException e) { // do nothing. } catch (NoClassDefFoundError e) { if (PropertyTools.contains(e.getMessage(), "wrong name")) { // do nothing. this is a weirdness in the jvm. // see MVEL-43 } else { throw e; } } } if (found > 1) throw new RuntimeException("ambiguous class name: " + className); if (found == 1) { addImport(className, cls); return true; } cacheNegativeHitForDynamicImport(className); return false; }
public IToken evaluate(ICharacterScanner scanner) { StringBuffer sb = new StringBuffer(); int c = -1; int readCount = 0; while (true) { c = scanner.read(); if (c == EOF) { scanner.unread(); return Token.EOF; } sb.append((char) c); readCount++; if (!Character.isJavaIdentifierStart(c) && c != '<' && c != '>') { for (int j = 0; j < readCount; j++) { scanner.unread(); } return Token.UNDEFINED; } else if (c == '>') { return fToken; } } }
private static boolean isValidPackageName(String name) { if (name.length() == 0) { // Fast check of default pkg return true; } StringTokenizer tk = new StringTokenizer(name, ".", true); // NOI18N boolean delimExpected = false; while (tk.hasMoreTokens()) { String namePart = tk.nextToken(); if (!delimExpected) { if (namePart.equals(".")) { // NOI18N return false; } for (int i = 0; i < namePart.length(); i++) { char c = namePart.charAt(i); if (i == 0) { if (!Character.isJavaIdentifierStart(c)) { return false; } } else { if (!Character.isJavaIdentifierPart(c)) { return false; } } } } else { if (!namePart.equals(".")) { // NOI18N return false; } } delimExpected = !delimExpected; } return delimExpected; }
/** * Validates that the name of a processor option conforms to the grammar defined by <code> * javax.annotation.processing.Processor.getSupportedOptions()</code>. * * @param optionName * @return <code>true</code> if the name conforms to the grammar, <code>false</code> if not. */ public static boolean isValidOptionName(String optionName) { if (optionName == null) { return false; } boolean startExpected = true; int codePoint; for (int i = 0; i < optionName.length(); i += Character.charCount(codePoint)) { codePoint = optionName.codePointAt(i); if (startExpected) { if (!Character.isJavaIdentifierStart(codePoint)) { return false; } startExpected = false; } else { if (codePoint == '.') { startExpected = true; } else if (!Character.isJavaIdentifierPart(codePoint)) { return false; } } } return !startExpected; }
private static String[] getSuggestionsByValue(final String stringValue) { List<String> result = new ArrayList<String>(); StringBuffer currentWord = new StringBuffer(); boolean prevIsUpperCase = false; for (int i = 0; i < stringValue.length(); i++) { final char c = stringValue.charAt(i); if (Character.isUpperCase(c)) { if (currentWord.length() > 0 && !prevIsUpperCase) { result.add(currentWord.toString()); currentWord = new StringBuffer(); } currentWord.append(c); } else if (Character.isLowerCase(c)) { currentWord.append(Character.toUpperCase(c)); } else if (Character.isJavaIdentifierPart(c) && c != '_') { if (Character.isJavaIdentifierStart(c) || currentWord.length() > 0 || !result.isEmpty()) { currentWord.append(c); } } else { if (currentWord.length() > 0) { result.add(currentWord.toString()); currentWord = new StringBuffer(); } } prevIsUpperCase = Character.isUpperCase(c); } if (currentWord.length() > 0) { result.add(currentWord.toString()); } return ArrayUtil.toStringArray(result); }
private boolean isQuotedName(final String name) { final int nameLength = name.length(); final char[] namechars = new char[nameLength]; name.getChars(0, nameLength, namechars, 0); return !Character.isJavaIdentifierStart(namechars[0]) && namechars[0] == namechars[nameLength - 1]; }
private static String parse(String query, Map<String, List<Integer>> paramMap) { int length = query.length(); StringBuilder parsedQuery = new StringBuilder(length); boolean inSingleQuote = false; boolean inDoubleQuote = false; int index = 1; for (int i = 0; i < length; i++) { char c = query.charAt(i); // String end if (inSingleQuote) { if (c == '\'') { inSingleQuote = false; } } else if (inDoubleQuote) { if (c == '"') { inDoubleQuote = false; } } else { // String begin if (c == '\'') { inSingleQuote = true; } else if (c == '"') { inDoubleQuote = true; } else if (c == ':' && i + 1 < length && Character.isJavaIdentifierStart(query.charAt(i + 1))) { // Identifier name int j = i + 2; while (j < length && Character.isJavaIdentifierPart(query.charAt(j))) { j++; } String name = query.substring(i + 1, j); c = '?'; i += name.length(); name = name.toLowerCase(); // Add to list List<Integer> indexList = paramMap.get(name); if (indexList == null) { indexList = new LinkedList<>(); paramMap.put(name, indexList); } indexList.add(index); index++; } } parsedQuery.append(c); } return parsedQuery.toString(); }
public static void main(String[] args) { char a[] = { 'a', '5', '?', 'A', ' ', '$', 'жа' }; for (int i = 0; i < a.length; i++) { if (Character.isDigit(a[i])) System.out.println(a[i] + " is a digit."); if (Character.isLetter(a[i])) System.out.println(a[i] + " is a letter."); if (Character.isWhitespace(a[i])) System.out.println(a[i] + " is whitespace."); if (Character.isUpperCase(a[i])) System.out.println(a[i] + " is uppercase."); if (Character.isLowerCase(a[i])) System.out.println(a[i] + " is lowercase."); if (Character.isJavaIdentifierPart(a[i])) System.out.println(a[i] + " may be part of java Identifier part."); if (Character.isJavaIdentifierStart(a[i])) System.out.println(a[i] + " may be part of java Identifier Start."); if (Character.isUnicodeIdentifierPart(a[i])) System.out.println(a[i] + " may be part of a Unicode identifier ."); if (Character.isUnicodeIdentifierStart(a[i])) System.out .println(a[i] + " may be the first character in a Unicode identifier."); } }
public void trySearch(SearchState state) throws RaiseException { // no library or extension found, try to load directly as a class Script script; String className = buildClassName(state.searchFile); int lastSlashIndex = className.lastIndexOf('/'); if (lastSlashIndex > -1 && lastSlashIndex < className.length() - 1 && !Character.isJavaIdentifierStart(className.charAt(lastSlashIndex + 1))) { if (lastSlashIndex == -1) { className = "_" + className; } else { className = className.substring(0, lastSlashIndex + 1) + "_" + className.substring(lastSlashIndex + 1); } } className = className.replace('/', '.'); try { Class scriptClass = Class.forName(className); script = (Script) scriptClass.newInstance(); } catch (Exception cnfe) { throw runtime.newLoadError("no such file to load -- " + state.searchFile); } state.library = new ScriptClassLibrary(script); }
public static void main(String args[]) { char c = 'è'; if (Character.isJavaIdentifierStart(c)) System.out.println("char\'" + c + "\'is right"); else System.out.println("char\'" + c + "\'isn\'t right"); if (Character.isJavaIdentifierPart(c)) System.out.println("char\'" + c + "\'is right chu shou"); else System.out.println("char\'" + c + "\'isn\'t right shou"); }
/** * Parses a type name token T (which can be potentially of the form Tr&ly;T1,T2,...>, or "? * extends/super T".) * * @return the index of the character next to T. */ JClass parseTypeName() throws ClassNotFoundException { int start = idx; if (s.charAt(idx) == '?') { // wildcard idx++; ws(); String head = s.substring(idx); if (head.startsWith("extends")) { idx += 7; ws(); return parseTypeName().wildcard(); } else if (head.startsWith("super")) { throw new UnsupportedOperationException("? super T not implemented"); } else { // not supported throw new IllegalArgumentException( "only extends/super can follow ?, but found " + s.substring(idx)); } } while (idx < s.length()) { char ch = s.charAt(idx); if (Character.isJavaIdentifierStart(ch) || Character.isJavaIdentifierPart(ch) || ch == '.') idx++; else break; } JClass clazz = ref(s.substring(start, idx)); return parseSuffix(clazz); }
@Override public boolean consume(CodeReader code, Lexer lexer) { if (!Character.isJavaIdentifierStart(code.peek())) { return false; } int line = code.getCursor().getLine(); int column = code.getCursor().getColumn(); while (Character.isJavaIdentifierPart(code.peek())) { tmpBuilder.append((char) code.pop()); } String word = tmpBuilder.toString(); TokenType keywordType = keywordsMap.get(word); Token token = tokenBuilder .setType(keywordType == null ? GenericTokenType.IDENTIFIER : keywordType) .setValueAndOriginalValue(word, word) .setURI(lexer.getURI()) .setLine(line) .setColumn(column) .build(); lexer.addToken(token); tmpBuilder.delete(0, tmpBuilder.length()); return true; }
public static boolean isSingleWord(final String string) { for (final char c : string.toCharArray()) { if (!Character.isJavaIdentifierStart(c)) { return false; } } return true; }
private static String extractMemberName(String part) { checkArgument(Character.isJavaIdentifierStart(part.charAt(0)), "not an identifier: %s", part); for (int i = 1; i <= part.length(); i++) { if (!SourceVersion.isIdentifier(part.substring(0, i))) { return part.substring(0, i - 1); } } return part; }
/** Checks that the given name is a valid identifier. */ static String checkValidIdentifier(String name) { Check.that( name.length() > 0 && Character.isJavaIdentifierStart(name.codePointAt(0)) && name.codePoints().skip(1).allMatch(Character::isJavaIdentifierPart), "'%0' is not a valid identifier", name); return name; }
private static String removeIllegalCharacters(String filename) { final char[] chars = filename.toCharArray(); for (int i = 0; i < chars.length; ++i) if (i == 0 ? !Character.isJavaIdentifierStart(chars[i]) : !Character.isJavaIdentifierPart(chars[i])) chars[i] = '_'; return new String(chars); }
// Here are the details of the matching. We track the current brace depth, and we artificially // consider that the whole file is at brace depth 1 inside a pseudo-class whose name is the // name of the package. When we see a class definition, we push the fully-qualified name of the // class on a stack so that we can associate abstract methods we find with the possibly-nested // class they belong to. A class definition must occur at brace depth one more than the class // containing it, which is equivalent to saying that the brace depth must be the same as the // class stack depth. This check excludes local class definitions within methods and // initializers. If we meet these constraints and we see the word "class" followed by an // identifier Foo, then we consider that we are entering the definition of class Foo. We determine // the fully-qualified name of Foo, which is container.Foo, where container is the current top of // the class stack (initially, the package name). We push this new fully-qualified name on the // class stack. We have not yet seen the left brace with the class definition so at this point the // class stack depth is one more than the brace depth. When we subsequently see a right brace that // takes us back to this situation then we know we have completed the definition of Foo and we can // pop it from the class stack. // // We check that the token after "class" is indeed an identifier to avoid confusion // with Foo.class. Even though the tokenizer does not distinguish between identifiers and // keywords, it is enough to exclude the single word "instanceof" because that is the only word // that can legally appear after Foo.class (though in a legal program the resultant expression // will always be true). // // Again, we are at the top level of a class when the brace depth is equal to the class stack // depth. If we then see the word "abstract" then that is the start either of an abstract class // definition or of an abstract method. We record that we have seen "abstract" and we cancel // that indication as soon as we see a left brace, to exclude the abstract class case, and also // the case of interfaces or @interfaces redundantly declared abstract. Now, when // we see an identifier that is preceded by an uncanceled "abstract" and followed by a left paren // then we have found an abstract method of the class on the top of the class stack. We record it // in the list of abstract methods of the class on the top of the class stack. We don't bother // checking that the method has no parameters, because an @AutoCursor class will cause a compiler // error if there are abstract methods with parameters, since the @AutoCursor processor doesn't // know how to implement them in the concrete subclass it generates. ImmutableListMultimap<String, String> abstractMethods( JavaTokenizer tokenizer, String packageName) { ImmutableListMultimap.Builder<String, String> abstractMethods = ImmutableListMultimap.builder(); Deque<String> classStack = new ArrayDeque<String>(); classStack.addLast(packageName); int braceDepth = 1; boolean sawAbstract = false; String className = null; for (String previousToken = "", token = tokenizer.nextToken(); token != null; previousToken = token, token = tokenizer.nextToken()) { boolean topLevel = (braceDepth == classStack.size()); if (className != null) { if (Character.isJavaIdentifierStart(className.charAt(0)) && !className.equals("instanceof")) { String container = classStack.getLast(); // container might be empty in the case of a packageless class classStack.add(container.isEmpty() ? className : container + "." + className); } className = null; } if (token.equals("{")) { braceDepth++; sawAbstract = false; } else if (token.equals("}")) { braceDepth--; if (topLevel) { classStack.removeLast(); } } else if (topLevel) { if (token.equals("class") || token.equals("interface")) { className = tokenizer.nextToken(); } else if (token.equals("abstract")) { sawAbstract = true; } else if (token.equals("(")) { if (sawAbstract && Character.isJavaIdentifierStart(previousToken.charAt(0))) { abstractMethods.put(classStack.getLast(), previousToken); } sawAbstract = false; } } } return abstractMethods.build(); }
/** * Checks if the given position is start one for HTML tag. * * @param javadocText text of javadoc comments. * @param pos position to check. * @return <code>true</code> some HTML tag starts from given position. */ private static boolean isTag(String[] javadocText, Point pos) { final int column = pos.getColumnNo() + 1; final String text = javadocText[pos.getLineNo()]; // Character.isJavidentifier... may not be a valid HTML // identifier but is valid for generics return column < text.length() && (Character.isJavaIdentifierStart(text.charAt(column)) || text.charAt(column) == '/') || column >= text.length(); }
private static boolean isFunctionName(String tok) { final char begin = tok.charAt(0); final boolean isIdentifier = Character.isJavaIdentifierStart(begin) || '"' == begin; return isIdentifier && !LOGICAL.contains(tok) && !END_CLAUSES.contains(tok) && !QUANTIFIERS.contains(tok) && !DML.contains(tok) && !MISC.contains(tok); }
/** Returns trus if str represents a valid Java identifier. */ public static boolean isJavaIdentifier(String str) { if (str.length() == 0 || !Character.isJavaIdentifierStart(str.charAt(0))) { return false; } for (int i = 1; i < str.length(); i++) { if (!Character.isJavaIdentifierPart(str.charAt(i))) { return false; } } return true; }
/** * Add a single object into the script context. * * @param key the name in the context this object is to stored under. * @param bean the object to be stored in the script context. */ public void addBean(String key, Object bean) { boolean isValid = key.length() > 0 && Character.isJavaIdentifierStart(key.charAt(0)); for (int i = 1; isValid && i < key.length(); i++) { isValid = Character.isJavaIdentifierPart(key.charAt(i)); } if (isValid) { beans.put(key, bean); } }
private boolean identifierComplete(String[] parts, boolean[] dots, int index) { /* * The identifier is complete if: * - It's the last token or * - The next token does not start as a valid Java identifier or * - A dot is to be insert after identifier. */ return index == parts.length - 1 || !Character.isJavaIdentifierStart(firstChar(parts[index + 1])) || insertDot(parts, dots, index); }
private boolean insertDot(String[] parts, boolean[] dots, int index) { /* * A dot is to be inserted if: * - It's not the last token and * - Indication to actually try to insert it and * - The next token start as a valid Java identifier. */ return index < parts.length - 1 && dots[index] && Character.isJavaIdentifierStart(firstChar(parts[index + 1])); }
public boolean hasToken(int position) { if (!Character.isJavaIdentifierStart(myBuffer.charAt(position))) return false; final int start = position; for (position++; position < myEndOffset; position++) { final char c = myBuffer.charAt(position); if (!isIdentifierPart(c)) break; } IElementType tokenType = CustomHighlighterTokenType.IDENTIFIER; myTokenInfo.updateData(start, position, tokenType); return true; }
/** * Determines whether a string is a valid Java identifier. * * <p>A valid Java identifier may not start with a number, but may contain any combination of * letters, digits, underscores, or dollar signs. * * <p>See the <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.8"> * Java Language Specification</a> * * @param identifier The identifier to test for validity. * @return Whether the identifier was valid. */ public static boolean isValidIdentifier(String identifier) { if (identifier.isEmpty() || !Character.isJavaIdentifierStart(identifier.charAt(0))) { return false; } for (int i = 1; i < identifier.length(); i++) { if (!Character.isJavaIdentifierPart(identifier.charAt(i))) { return false; } } return true; }