01: /**
02: * <copyright>
03: * Copyright 1997-2002 BBNT Solutions, LLC
04: * under sponsorship of the Defense Advanced Research Projects Agency (DARPA).
05: *
06: * This program is free software; you can redistribute it and/or modify
07: * it under the terms of the Cougaar Open Source License as published by
08: * DARPA on the Cougaar Open Source Website (www.cougaar.org).
09: *
10: * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
11: * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
12: * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
14: * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
15: * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
16: * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
17: * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18: * PERFORMANCE OF THE COUGAAR SOFTWARE.
19: * </copyright>
20: *
21: * Created on Dec 13, 2002
22: */package org.acm.seguin.pmd.rules.design;
23:
24: import org.acm.seguin.pmd.AbstractRule;
25: import org.acm.seguin.pmd.RuleContext;
26: import net.sourceforge.jrefactory.ast.ASTAssignmentOperator;
27: import net.sourceforge.jrefactory.ast.ASTNullLiteral;
28: import net.sourceforge.jrefactory.ast.ASTStatementExpression;
29: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
30: import net.sourceforge.jrefactory.ast.SimpleNode;
31:
32: import net.sourceforge.jrefactory.ast.ASTLiteral;
33:
34: /**
35: * @author dpeugh
36: *
37: * This checks for excessive Null Assignments.
38: *
39: * For instance:
40: *
41: * public void foo() {
42: * Object x = null; // OK
43: * // Some stuff
44: * x = new Object(); // Also OK
45: * // Some more stuff
46: * x = null; // BAD
47: * }
48: */
49:
50: public class NullAssignmentRule extends AbstractRule {
51:
52: public Object visit(ASTStatementExpression expr, Object data) {
53: if (expr.jjtGetNumChildren() <= 2) {
54: return expr.childrenAccept(this , data);
55: }
56:
57: if (expr.jjtGetChild(1) instanceof ASTAssignmentOperator) {
58: SimpleNode curr = (SimpleNode) expr.jjtGetChild(2);
59:
60: for (int i = 0; i < 19; i++) {
61: if (curr.jjtGetNumChildren() == 0) {
62: return data;
63: }
64: curr = (SimpleNode) curr.jjtGetFirstChild();
65: if (curr == null) {
66: return data;
67: }
68: if (curr instanceof ASTNullLiteral) {
69: RuleContext ctx = (RuleContext) data;
70: ctx.getReport().addRuleViolation(
71: createRuleViolation(ctx, expr
72: .getBeginLine()));
73: }
74: }
75:
76: return data;
77: } else {
78: return expr.childrenAccept(this, data);
79: }
80: }
81: }
|