View Javadoc
1 /*** 2 * <copyright> 3 * Copyright 1997-2002 BBNT Solutions, LLC 4 * under sponsorship of the Defense Advanced Research Projects Agency (DARPA). 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the Cougaar Open Source License as published by 8 * DARPA on the Cougaar Open Source Website (www.cougaar.org). 9 * 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 */ 23 package net.sourceforge.pmd.rules.design; 24 25 import net.sourceforge.pmd.AbstractRule; 26 import net.sourceforge.pmd.RuleContext; 27 import net.sourceforge.pmd.ast.ASTAssignmentOperator; 28 import net.sourceforge.pmd.ast.ASTNullLiteral; 29 import net.sourceforge.pmd.ast.ASTStatementExpression; 30 import net.sourceforge.pmd.ast.SimpleNode; 31 32 /*** 33 * @author dpeugh 34 * 35 * This checks for excessive Null Assignments. 36 * 37 * For instance: 38 * 39 * public void foo() { 40 * Object x = null; // OK 41 * // Some stuff 42 * x = new Object(); // Also OK 43 * // Some more stuff 44 * x = null; // BAD 45 * } 46 */ 47 48 public class NullAssignmentRule extends AbstractRule { 49 50 public Object visit(ASTStatementExpression expr, Object data) { 51 if (expr.jjtGetNumChildren() <= 2) { 52 return expr.childrenAccept(this, data); 53 } 54 55 if (expr.jjtGetChild(1) instanceof ASTAssignmentOperator) { 56 SimpleNode curr = (SimpleNode) expr.jjtGetChild(2); 57 for (int i = 0; i < 6; i++) { 58 if (curr.jjtGetNumChildren() != 0) { 59 curr = (SimpleNode) curr.jjtGetChild(0); 60 } 61 } 62 63 if (curr instanceof ASTNullLiteral) { 64 RuleContext ctx = (RuleContext) data; 65 ctx.getReport().addRuleViolation(createRuleViolation(ctx, expr.getBeginLine())); 66 } 67 68 return data; 69 } else { 70 return expr.childrenAccept(this, data); 71 } 72 } 73 }

This page was automatically generated by Maven