Search in sources :

Example 1 with ImportWrapper

use of net.sourceforge.pmd.lang.rule.ImportWrapper in project pmd by pmd.

the class UnusedImportsRule method visit.

@Override
public Object visit(ASTImportDeclaration node, Object data) {
    if (node.isImportOnDemand()) {
        ASTName importedType = (ASTName) node.jjtGetChild(0);
        imports.add(new ImportWrapper(importedType.getImage(), null, node, node.getType(), node.isStatic()));
    } else {
        if (!node.isImportOnDemand()) {
            ASTName importedType = (ASTName) node.jjtGetChild(0);
            String className;
            if (isQualifiedName(importedType)) {
                int lastDot = importedType.getImage().lastIndexOf('.') + 1;
                className = importedType.getImage().substring(lastDot);
            } else {
                className = importedType.getImage();
            }
            imports.add(new ImportWrapper(importedType.getImage(), className, node));
        }
    }
    return data;
}
Also used : ImportWrapper(net.sourceforge.pmd.lang.rule.ImportWrapper) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Example 2 with ImportWrapper

use of net.sourceforge.pmd.lang.rule.ImportWrapper in project pmd by pmd.

the class UnusedImportsRule method check.

protected void check(Node node) {
    if (imports.isEmpty()) {
        return;
    }
    ImportWrapper candidate = getImportWrapper(node);
    Iterator<ImportWrapper> it = imports.iterator();
    while (it.hasNext()) {
        ImportWrapper i = it.next();
        if (i.matches(candidate)) {
            it.remove();
            return;
        }
    }
    if (TypeNode.class.isAssignableFrom(node.getClass()) && ((TypeNode) node).getType() != null) {
        Class<?> c = ((TypeNode) node).getType();
        if (c.getPackage() != null) {
            candidate = new ImportWrapper(c.getPackage().getName(), null);
            if (imports.contains(candidate)) {
                imports.remove(candidate);
            }
        }
    }
}
Also used : ImportWrapper(net.sourceforge.pmd.lang.rule.ImportWrapper) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode)

Example 3 with ImportWrapper

use of net.sourceforge.pmd.lang.rule.ImportWrapper in project pmd by pmd.

the class UnusedImportsRule method visit.

@Override
public Object visit(ASTCompilationUnit node, Object data) {
    imports.clear();
    super.visit(node, data);
    visitComments(node);
    /*
         * special handling for Bug 2606609 : False "UnusedImports" positive in
         * package-info.java package annotations are processed before the import
         * clauses so they need to be examined again later on.
         */
    if (node.jjtGetNumChildren() > 0 && node.jjtGetChild(0) instanceof ASTPackageDeclaration) {
        visit((ASTPackageDeclaration) node.jjtGetChild(0), data);
    }
    for (ImportWrapper wrapper : imports) {
        addViolation(data, wrapper.getNode(), wrapper.getFullName());
    }
    return data;
}
Also used : ImportWrapper(net.sourceforge.pmd.lang.rule.ImportWrapper) ASTPackageDeclaration(net.sourceforge.pmd.lang.java.ast.ASTPackageDeclaration)

Example 4 with ImportWrapper

use of net.sourceforge.pmd.lang.rule.ImportWrapper in project pmd by pmd.

the class DuplicateImportsRule method visit.

public Object visit(ASTCompilationUnit node, Object data) {
    singleTypeImports = new HashSet<>();
    importOnDemandImports = new HashSet<>();
    super.visit(node, data);
    // import java.io.File;
    for (ImportWrapper thisImportOnDemand : importOnDemandImports) {
        for (ImportWrapper thisSingleTypeImport : singleTypeImports) {
            // java.io.File
            String singleTypeFullName = thisSingleTypeImport.getName();
            int lastDot = singleTypeFullName.lastIndexOf('.');
            // java.io
            String singleTypePkg = singleTypeFullName.substring(0, lastDot);
            // File
            String singleTypeName = singleTypeFullName.substring(lastDot + 1);
            if (thisImportOnDemand.getName().equals(singleTypePkg) && !isDisambiguationImport(node, singleTypePkg, singleTypeName)) {
                addViolation(data, thisSingleTypeImport.getNode(), singleTypeFullName);
            }
        }
    }
    singleTypeImports.clear();
    importOnDemandImports.clear();
    return data;
}
Also used : ImportWrapper(net.sourceforge.pmd.lang.rule.ImportWrapper)

Example 5 with ImportWrapper

use of net.sourceforge.pmd.lang.rule.ImportWrapper in project pmd by pmd.

the class DuplicateJspImportsRule method visit.

@Override
public Object visit(ASTJspDirectiveAttribute node, Object data) {
    if (!"import".equals(node.getName())) {
        return super.visit(node, data);
    }
    String values = node.getValue();
    StringTokenizer st = new StringTokenizer(values, ",");
    int count = st.countTokens();
    for (int ix = 0; ix < count; ix++) {
        String token = st.nextToken();
        ImportWrapper wrapper = new ImportWrapper(token, token, node);
        if (imports.contains(wrapper)) {
            addViolation(data, node, node.getImage());
        } else {
            imports.add(wrapper);
        }
    }
    return super.visit(node, data);
}
Also used : StringTokenizer(java.util.StringTokenizer) ImportWrapper(net.sourceforge.pmd.lang.rule.ImportWrapper)

Aggregations

ImportWrapper (net.sourceforge.pmd.lang.rule.ImportWrapper)6 StringTokenizer (java.util.StringTokenizer)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTPackageDeclaration (net.sourceforge.pmd.lang.java.ast.ASTPackageDeclaration)1 Comment (net.sourceforge.pmd.lang.java.ast.Comment)1 DummyJavaNode (net.sourceforge.pmd.lang.java.ast.DummyJavaNode)1 FormalComment (net.sourceforge.pmd.lang.java.ast.FormalComment)1 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)1