001: package org.acm.seguin.pmd.rules;
002:
003: import java.text.MessageFormat;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.Set;
007: import net.sourceforge.jrefactory.ast.ASTClassOrInterfaceType;
008: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
009: import net.sourceforge.jrefactory.ast.ASTImportDeclaration;
010: import net.sourceforge.jrefactory.ast.ASTName;
011:
012: import org.acm.seguin.pmd.AbstractRule;
013: import org.acm.seguin.pmd.RuleContext;
014:
015: /**
016: * Description of the Class
017: *
018: * @author Chris Seguin
019: */
020: public class UnusedImportsRule extends AbstractRule {
021:
022: private Set imports = new HashSet();
023:
024: /**
025: * Description of the Method
026: *
027: * @param node Description of Parameter
028: * @param data Description of Parameter
029: * @return Description of the Returned Value
030: */
031: public Object visit(ASTCompilationUnit node, Object data) {
032: imports.clear();
033:
034: super .visit(node, data);
035:
036: RuleContext ctx = (RuleContext) data;
037: for (Iterator i = imports.iterator(); i.hasNext();) {
038: ImportWrapper wrapper = (ImportWrapper) i.next();
039: String msg = MessageFormat.format(getMessage(),
040: new Object[] { wrapper.getName() });
041: ctx.getReport().addRuleViolation(
042: createRuleViolation(ctx, wrapper.getLine(), msg));
043: }
044: return data;
045: }
046:
047: /**
048: * Description of the Method
049: *
050: * @param node Description of Parameter
051: * @param data Description of Parameter
052: * @return Description of the Returned Value
053: */
054: public Object visit(ASTImportDeclaration node, Object data) {
055: if (!node.isImportOnDemand()) {
056: ASTName importedType = (ASTName) node.jjtGetFirstChild();
057: String className;
058: if (importedType.getImage().indexOf('.') != -1) {
059: int lastDot = importedType.getImage().lastIndexOf('.') + 1;
060: className = importedType.getImage().substring(lastDot);
061: } else {
062: className = importedType.getImage();
063: }
064: ImportWrapper wrapper = new ImportWrapper(className, node
065: .getBeginLine());
066: imports.add(wrapper);
067: }
068:
069: return data;
070: }
071:
072: /**
073: * Description of the Method
074: *
075: * @param node Description of Parameter
076: * @param data Description of Parameter
077: * @return Description of the Returned Value
078: */
079: public Object visit(ASTName node, Object data) {
080: String name;
081: if (node.getImage().indexOf('.') == -1) {
082: name = node.getImage();
083: } else {
084: name = node.getImage().substring(0,
085: node.getImage().indexOf('.'));
086: }
087: ImportWrapper candidate = new ImportWrapper(name, -1);
088: if (imports.contains(candidate)) {
089: imports.remove(candidate);
090: }
091: return data;
092: }
093:
094: /**
095: * Description of the Method
096: *
097: * @param node Description of Parameter
098: * @param data Description of Parameter
099: * @return Description of the Returned Value
100: */
101: public Object visit(ASTClassOrInterfaceType node, Object data) {
102: String name;
103: if (node.getImage().indexOf('.') == -1) {
104: name = node.getImage();
105: } else {
106: name = node.getImage().substring(0,
107: node.getImage().indexOf('.'));
108: }
109: ImportWrapper candidate = new ImportWrapper(name, -1);
110: if (imports.contains(candidate)) {
111: imports.remove(candidate);
112: }
113: return data;
114: }
115:
116: }
|