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

This page was automatically generated by Maven