Java Code Examples for javax.lang.model.SourceVersion#isIdentifier()

The following examples show how to use javax.lang.model.SourceVersion#isIdentifier() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaCompiler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/** Resolve an identifier.
 * @param name      The identifier to resolve
 */
public Symbol resolveIdent(String name) {
    if (name.equals(""))
        return syms.errSymbol;
    JavaFileObject prev = log.useSource(null);
    try {
        JCExpression tree = null;
        for (String s : name.split("\\.", -1)) {
            if (!SourceVersion.isIdentifier(s)) // TODO: check for keywords
                return syms.errSymbol;
            tree = (tree == null) ? make.Ident(names.fromString(s))
                                  : make.Select(tree, names.fromString(s));
        }
        JCCompilationUnit toplevel =
            make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil());
        toplevel.packge = syms.unnamedPackage;
        return attr.attribIdent(tree, toplevel);
    } finally {
        log.useSource(prev);
    }
}
 
Example 2
Source File: JavaCompiler.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/** Resolve an identifier.
 * @param name      The identifier to resolve
 */
public Symbol resolveIdent(String name) {
    if (name.equals(""))
        return syms.errSymbol;
    JavaFileObject prev = log.useSource(null);
    try {
        JCExpression tree = null;
        for (String s : name.split("\\.", -1)) {
            if (!SourceVersion.isIdentifier(s)) // TODO: check for keywords
                return syms.errSymbol;
            tree = (tree == null) ? make.Ident(names.fromString(s))
                                  : make.Select(tree, names.fromString(s));
        }
        JCCompilationUnit toplevel =
            make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil());
        toplevel.packge = syms.unnamedPackage;
        return attr.attribIdent(tree, toplevel);
    } finally {
        log.useSource(prev);
    }
}
 
Example 3
Source File: ClassReader.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void fillIn(PackageSymbol p,
                    Location location,
                    Iterable<JavaFileObject> files)
{
    currentLoc = location;
    for (JavaFileObject fo : files) {
        switch (fo.getKind()) {
        case CLASS:
        case SOURCE: {
            // TODO pass binaryName to includeClassFile
            String binaryName = fileManager.inferBinaryName(currentLoc, fo);
            String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1);
            if (SourceVersion.isIdentifier(simpleName) ||
                simpleName.equals("package-info"))
                includeClassFile(p, fo);
            break;
        }
        default:
            extraFileActions(p, fo);
        }
    }
}
 
Example 4
Source File: JavacProcessingEnvironment.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean isValidOptionName(String optionName) {
    for(String s : optionName.split("\\.", -1)) {
        if (!SourceVersion.isIdentifier(s))
            return false;
    }
    return true;
}
 
Example 5
Source File: JavacFileManager.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isValidName(String name) {
    // Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ),
    // but the set of keywords depends on the source level, and we don't want
    // impls of JavaFileManager to have to be dependent on the source level.
    // Therefore we simply check that the argument is a sequence of identifiers
    // separated by ".".
    for (String s : name.split("\\.", -1)) {
        if (!SourceVersion.isIdentifier(s))
            return false;
    }
    return true;
}
 
Example 6
Source File: JavacProcessingEnvironment.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return true if the argument string is a valid import-style
 * string specifying claimed annotations; return false otherwise.
 */
public static boolean isValidImportString(String s) {
    if (s.equals("*"))
        return true;

    boolean valid = true;
    String t = s;
    int index = t.indexOf('*');

    if (index != -1) {
        // '*' must be last character...
        if (index == t.length() -1) {
            // ... any and preceding character must be '.'
            if ( index-1 >= 0 ) {
                valid = t.charAt(index-1) == '.';
                // Strip off ".*$" for identifier checks
                t = t.substring(0, t.length()-2);
            }
        } else
            return false;
    }

    // Verify string is off the form (javaId \.)+ or javaId
    if (valid) {
        String[] javaIds = t.split("\\.", t.length()+2);
        for(String javaId: javaIds)
            valid &= SourceVersion.isIdentifier(javaId);
    }
    return valid;
}
 
Example 7
Source File: JavacFileManager.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static boolean isValidName(String name) {
    // Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ),
    // but the set of keywords depends on the source level, and we don't want
    // impls of JavaFileManager to have to be dependent on the source level.
    // Therefore we simply check that the argument is a sequence of identifiers
    // separated by ".".
    for (String s : name.split("\\.", -1)) {
        if (!SourceVersion.isIdentifier(s))
            return false;
    }
    return true;
}
 
Example 8
Source File: JavacFileManager.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private static boolean isValidName(String name) {
    // Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ),
    // but the set of keywords depends on the source level, and we don't want
    // impls of JavaFileManager to have to be dependent on the source level.
    // Therefore we simply check that the argument is a sequence of identifiers
    // separated by ".".
    for (String s : name.split("\\.", -1)) {
        if (!SourceVersion.isIdentifier(s))
            return false;
    }
    return true;
}
 
Example 9
Source File: JavacProcessingEnvironment.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static boolean isValidOptionName(String optionName) {
    for(String s : optionName.split("\\.", -1)) {
        if (!SourceVersion.isIdentifier(s))
            return false;
    }
    return true;
}
 
Example 10
Source File: BaseUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Test whether a given string is a valid Java identifier.
* @param id string which should be checked
* @return <code>true</code> if a valid identifier
* @see SourceVersion#isIdentifier
* @see SourceVersion#isKeyword
*/
public static boolean isJavaIdentifier(String id) {
    if (id == null) {
        return false;
    }
    return SourceVersion.isIdentifier(id) && !SourceVersion.isKeyword(id);
}
 
Example 11
Source File: JavacFileManager.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Insert all files in subdirectory subdirectory of directory directory
 * which match fileKinds into resultList
 */
private void listDirectory(File directory,
                           RelativeDirectory subdirectory,
                           Set<JavaFileObject.Kind> fileKinds,
                           boolean recurse,
                           ListBuffer<JavaFileObject> resultList) {
    File d = subdirectory.getFile(directory);
    if (!caseMapCheck(d, subdirectory))
        return;

    File[] files = d.listFiles();
    if (files == null)
        return;

    if (sortFiles != null)
        Arrays.sort(files, sortFiles);

    for (File f: files) {
        String fname = f.getName();
        if (f.isDirectory()) {
            if (recurse && SourceVersion.isIdentifier(fname)) {
                listDirectory(directory,
                              new RelativeDirectory(subdirectory, fname),
                              fileKinds,
                              recurse,
                              resultList);
            }
        } else {
            if (isValidFile(fname, fileKinds)) {
                JavaFileObject fe =
                    new RegularFileObject(this, fname, new File(d, fname));
                resultList.append(fe);
            }
        }
    }
}
 
Example 12
Source File: ClassFinder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("fallthrough")
private void fillIn(PackageSymbol p,
                    Location location,
                    Iterable<JavaFileObject> files)
{
    currentLoc = location;
    for (JavaFileObject fo : files) {
        switch (fo.getKind()) {
        case OTHER:
            if (!isSigFile(location, fo)) {
                extraFileActions(p, fo);
                break;
            }
            //intentional fall-through:
        case CLASS:
        case SOURCE: {
            // TODO pass binaryName to includeClassFile
            String binaryName = fileManager.inferBinaryName(currentLoc, fo);
            String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1);
            if (SourceVersion.isIdentifier(simpleName) ||
                simpleName.equals("package-info"))
                includeClassFile(p, fo);
            break;
        }
        default:
            extraFileActions(p, fo);
            break;
        }
    }
}
 
Example 13
Source File: JavacProcessingEnvironment.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isValidOptionName(String optionName) {
    for(String s : optionName.split("\\.", -1)) {
        if (!SourceVersion.isIdentifier(s))
            return false;
    }
    return true;
}
 
Example 14
Source File: JavacProcessingEnvironment.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static boolean isValidOptionName(String optionName) {
    for(String s : optionName.split("\\.", -1)) {
        if (!SourceVersion.isIdentifier(s))
            return false;
    }
    return true;
}
 
Example 15
Source File: JavacFileManager.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean isValid(Path fileName) {
    if (fileName == null) {
        return true;
    } else {
        String name = fileName.toString();
        if (name.endsWith("/")) {
            name = name.substring(0, name.length() - 1);
        }
        return SourceVersion.isIdentifier(name);
    }
}
 
Example 16
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**Find span of the name in the DocTree's reference tree (see {@link #getReferenceName(com.sun.source.util.DocTreePath)}
 * identifier in the source. Returns starting and ending offset of the name in
 * the source code that was parsed (ie. {@link CompilationInfo.getText()}, which
 * may differ from the positions in the source document if it has been already
 * altered.
 * 
 * @param ref reference for which the identifier should be found
 * @return the span of the name, or null if cannot be found
 * @since 0.124
 */
public int[] findNameSpan(DocCommentTree docTree, ReferenceTree ref) {
    Name name = ((DCReference) ref).memberName;
    if (name == null || !SourceVersion.isIdentifier(name)) {
        //names like "<error>", etc.
        return null;
    }
    
    int pos = (int) info.getDocTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), docTree, ref);
    
    if (pos < 0)
        return null;
    
    TokenSequence<JavaTokenId> tokenSequence = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());
    
    tokenSequence.move(pos);
    
    if (!tokenSequence.moveNext() || tokenSequence.token().id() != JavaTokenId.JAVADOC_COMMENT) return null;
    
    TokenSequence<JavadocTokenId> jdocTS = tokenSequence.embedded(JavadocTokenId.language());
    
    jdocTS.move(pos);
    
    boolean wasNext;
    
    while ((wasNext = jdocTS.moveNext()) && jdocTS.token().id() != JavadocTokenId.HASH)
        ;
    
    if (wasNext && jdocTS.moveNext()) {
        if (jdocTS.token().id() == JavadocTokenId.IDENT &&
            name.contentEquals(jdocTS.token().text())) {
            return new int[] {
                jdocTS.offset(),
                jdocTS.offset() + jdocTS.token().length()
            };
        }
    }
    
    return null;
}
 
Example 17
Source File: JavacFileManager.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isValidName(String name) {
    // Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ),
    // but the set of keywords depends on the source level, and we don't want
    // impls of JavaFileManager to have to be dependent on the source level.
    // Therefore we simply check that the argument is a sequence of identifiers
    // separated by ".".
    for (String s : name.split("\\.", -1)) {
        if (!SourceVersion.isIdentifier(s))
            return false;
    }
    return true;
}
 
Example 18
Source File: FileObjects.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isJavaPath(
        @NonNull final String path,
        final char separator) {
    for (String name : path.split(Pattern.quote(Character.toString(separator)))) {
        if (!SourceVersion.isIdentifier(name)) {
            return false;
        }
    }
    return true;
}
 
Example 19
Source File: JavacFileManager.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Insert all files in subdirectory subdirectory of this archive
 * which match fileKinds into resultList
 */
@Override
public void list(File userPath,
                 RelativeDirectory subdirectory,
                 Set<JavaFileObject.Kind> fileKinds,
                 boolean recurse,
                 ListBuffer<JavaFileObject> resultList) throws IOException {
    File resolvedSubdirectory = packages.get(subdirectory);

    if (resolvedSubdirectory == null)
        return;

    File[] files;
    try {
        files=resolvedSubdirectory.listFiles();
        if(sortFiles!=null) Arrays.sort(files,sortFiles);
    } catch (Exception ignore) {
        return;
    }

    for (File f : files) {
        String fname = f.getPath();
        if (fname.endsWith("/"))
            fname = fname.substring(0, fname.length() - 1);
        if (f.isDirectory()) {
            if (recurse && SourceVersion.isIdentifier(fname)) {
                list(userPath,
                        new RelativeDirectory(subdirectory, fname),
                        fileKinds,
                        recurse,
                        resultList);
            }
        } else {
            if (isValidFile(fname, fileKinds)) {
                RelativeFile file = new RelativeFile(subdirectory, fname);
                JavaFileObject fe = PathFileObject.forDirectoryPath(JavacFileManager.this,
                        f, archivePath, file);
                resultList.append(fe);
            }
        }
    }

}
 
Example 20
Source File: DefaultDictionaryValidator.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
boolean isProhibited(String name) {
    return prohibitedNames.contains(name) || SourceVersion.isKeyword(name) || !SourceVersion.isIdentifier(name);
}