Java Code Examples for com.github.javaparser.ast.ImportDeclaration#getName()

The following examples show how to use com.github.javaparser.ast.ImportDeclaration#getName() . 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: ReferenceToConstantsAnalyzer.java    From deadcode4j with Apache License 2.0 6 votes vote down vote up
private static Function<? super ImportDeclaration, ? extends String> toImportedType() {
    return new Function<ImportDeclaration, String>() {
        @Nullable
        @Override
        public String apply(@Nullable ImportDeclaration input) {
            if (input == null) {
                return null;
            }
            NameExpr name = input.getName();
            if (input.isStatic() && !input.isAsterisk()) {
                name = QualifiedNameExpr.class.cast(name).getQualifier();
            }
            return name.toString();
        }
    };
}
 
Example 2
Source File: ASTHelper.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Gets the imports.
 *
 * @param cu the cu
 * @return the imports
 */
static Set<String> getImports(CompilationUnit cu) {
    Set<String> importSet = null;
    List<ImportDeclaration> imports = cu.getImports();
    if (CollectionUtil.isNotEmpty(imports)) {
        importSet = new HashSet<String>(imports.size());
        for (ImportDeclaration im : imports) {
            String imStr = (im.isStatic() ? "static " : "") + im.getName() + (im.isAsterisk() ? ".*" : "");
            importSet.add(imStr);
        }
    } else {
        importSet = new HashSet<String>(0);
    }
    return importSet;
}
 
Example 3
Source File: Sources.java    From sundrio with Apache License 2.0 5 votes vote down vote up
public String apply(Node node) {
    if (node instanceof NamedNode) {
        String name = ((NamedNode)node).getName();
        Node current = node;
        while (!(current instanceof CompilationUnit)) {
            current = current.getParentNode();
        }

        CompilationUnit compilationUnit = (CompilationUnit) current;

        for (ImportDeclaration importDecl : compilationUnit.getImports()) {
            NameExpr importExpr = importDecl.getName();
            if (importExpr instanceof QualifiedNameExpr) {
                QualifiedNameExpr qualifiedNameExpr = (QualifiedNameExpr) importExpr;
                String className = qualifiedNameExpr.getName();
                if (name.equals(className)) {
                    return qualifiedNameExpr.getQualifier().toString();
                }
            } else if (importDecl.getName().getName().endsWith(SEPARATOR + name)) {
                String importName = importDecl.getName().getName();
                return  importName.substring(0, importName.length() - name.length() -1);
            }
        }

       try {
           Class.forName(JAVA_LANG + "." + name);
           return JAVA_LANG;
       } catch (ClassNotFoundException ex) {
           return compilationUnit.getPackage().getPackageName();
       }
    }
    return null;
}
 
Example 4
Source File: Sources.java    From sundrio with Apache License 2.0 5 votes vote down vote up
public Set<ClassRef> apply(Node node) {
    Set<ClassRef> imports = new LinkedHashSet<ClassRef>();

    if (node instanceof NamedNode) {
        String name = ((NamedNode)node).getName();
        Node current = node;
        while (!(current instanceof CompilationUnit)) {
            current = current.getParentNode();
        }

        CompilationUnit compilationUnit = (CompilationUnit) current;

        for (ImportDeclaration importDecl : compilationUnit.getImports()) {
            String className = null;
            String packageName = null;

            NameExpr importExpr = importDecl.getName();
            if (importExpr instanceof QualifiedNameExpr) {
                QualifiedNameExpr qualifiedNameExpr = (QualifiedNameExpr) importExpr;
                className = qualifiedNameExpr.getName();
                packageName = qualifiedNameExpr.getQualifier().toString();
            } else if (importDecl.getName().getName().endsWith(SEPARATOR + name)) {
                String importName = importDecl.getName().getName();
                packageName = importName.substring(0, importName.length() - name.length() -1);
            }
            if (className != null && !className.isEmpty()) {
                imports.add(new ClassRefBuilder().withNewDefinition().withName(className).withPackageName(packageName).and().build());
            }
        }
    }
    return imports;
}