View Javadoc
1 /* 2 * User: frank 3 * Date: Jun 21, 2002 4 * Time: 11:26:34 AM 5 */ 6 package net.sourceforge.pmd.rules; 7 8 import net.sourceforge.pmd.AbstractRule; 9 import net.sourceforge.pmd.RuleContext; 10 import net.sourceforge.pmd.ast.ASTMethodDeclarator; 11 import net.sourceforge.pmd.ast.ASTUnmodifiedClassDeclaration; 12 import net.sourceforge.pmd.symboltable.VariableNameDeclaration; 13 14 import java.text.MessageFormat; 15 import java.util.ArrayList; 16 import java.util.Arrays; 17 import java.util.Iterator; 18 19 public class BeanMembersShouldSerializeRule extends AbstractRule { 20 21 public Object visit(ASTUnmodifiedClassDeclaration node, Object data) { 22 ArrayList methList = new ArrayList(); 23 node.findChildrenOfType(ASTMethodDeclarator.class, methList); 24 25 ArrayList getSetMethList = new ArrayList(); 26 for (int i = 0; i < methList.size(); i++){ 27 ASTMethodDeclarator meth = (ASTMethodDeclarator)methList.get(i); 28 String methName = meth.getImage(); 29 if (methName.startsWith("get") || methName.startsWith("set")){ 30 getSetMethList.add(meth); 31 } 32 } 33 String[] methNameArray = new String[getSetMethList.size()]; 34 for (int i = 0; i < getSetMethList.size(); i++){ 35 ASTMethodDeclarator meth = (ASTMethodDeclarator)getSetMethList.get(i); 36 String methName = meth.getImage(); 37 methNameArray[i] = methName; 38 } 39 40 Arrays.sort(methNameArray); 41 42 for (Iterator i = node.getScope().getVariableDeclarations(true).keySet().iterator();i.hasNext();) { 43 VariableNameDeclaration decl = (VariableNameDeclaration)i.next(); 44 if (decl.getAccessNodeParent().isTransient()){ 45 //System.out.println("It's Transient!"); 46 continue; 47 } 48 if (decl.getAccessNodeParent().isStatic()){ 49 //System.out.println("It's Static!"); 50 continue; 51 } 52 String varName = decl.getImage(); 53 String firstChar = varName.substring(0,1); 54 //System.out.println("firstChar = " + firstChar); 55 varName = firstChar.toUpperCase() + varName.substring(1,varName.length()); 56 //System.out.println("varName = " + varName); 57 boolean hasGetMethod =false; 58 if (Arrays.binarySearch(methNameArray,"get" + varName) >= 0 ){ 59 hasGetMethod = true; 60 } 61 boolean hasSetMethod = false; 62 if (Arrays.binarySearch(methNameArray,"set" + varName) >= 0 ){ 63 hasSetMethod = true; 64 } 65 if (!hasGetMethod || !hasSetMethod) { 66 //System.out.println("decl.getImage = "+decl.getImage()); 67 RuleContext ctx = (RuleContext)data; 68 ctx.getReport().addRuleViolation(createRuleViolation(ctx, decl.getLine(), MessageFormat.format(getMessage(), new Object[] {decl.getImage()}))); 69 } 70 /* 71 if (decl.getAccessNodeParent().isPrivate() && !decl.getImage().equals("serialVersionUID") && !decl.getImage().equals("serialPersistentFields")) { 72 73 RuleContext ctx = (RuleContext)data; 74 ctx.getReport().addRuleViolation(createRuleViolation(ctx, decl.getLine(), MessageFormat.format(getMessage(), new Object[] {decl.getImage()}))); 75 }*/ 76 77 } 78 return super.visit(node, data); 79 } 80 81 82 }

This page was automatically generated by Maven