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.ast.ASTClassOrInterfaceDeclaration;
8 import net.sourceforge.pmd.ast.ASTMethodDeclarator;
9 import net.sourceforge.pmd.ast.ASTPrimitiveType;
10 import net.sourceforge.pmd.ast.ASTResultType;
11 import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18
19 public class BeanMembersShouldSerializeRule extends AbstractRule {
20
21 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
22 if (node.isInterface()) {
23 return data;
24 }
25
26
27 List methList = node.findChildrenOfType(ASTMethodDeclarator.class);
28
29 List getSetMethList = new ArrayList();
30 for (Iterator i = methList.iterator(); i.hasNext();) {
31 ASTMethodDeclarator meth = (ASTMethodDeclarator) i.next();
32 if (isBeanAccessor(meth)) {
33 getSetMethList.add(meth);
34 }
35 }
36 String[] methNameArray = new String[getSetMethList.size()];
37 for (int i = 0; i < getSetMethList.size(); i++) {
38 methNameArray[i] = ((ASTMethodDeclarator) getSetMethList.get(i)).getImage();
39 }
40
41 Arrays.sort(methNameArray);
42
43 Map vars = node.getScope().getVariableDeclarations();
44 for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
45 VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
46 if (((List) vars.get(decl)).isEmpty() || decl.getAccessNodeParent().isTransient() || decl.getAccessNodeParent().isStatic()) {
47 continue;
48 }
49 String varName = decl.getImage();
50 varName = varName.substring(0, 1).toUpperCase() + varName.substring(1, varName.length());
51 boolean hasGetMethod = Arrays.binarySearch(methNameArray, "get" + varName) >= 0 || Arrays.binarySearch(methNameArray, "is" + varName) >= 0;
52 boolean hasSetMethod = Arrays.binarySearch(methNameArray, "set" + varName) >= 0;
53 if (!hasGetMethod || !hasSetMethod) {
54 addViolation(data, decl.getNode(), decl.getImage());
55 }
56 }
57 return super.visit(node, data);
58 }
59
60 private boolean isBeanAccessor(ASTMethodDeclarator meth) {
61 if (meth.getImage().startsWith("get") || meth.getImage().startsWith("set")) {
62 return true;
63 }
64 if (meth.getImage().startsWith("is")) {
65 ASTResultType ret = (ASTResultType) meth.jjtGetParent().jjtGetChild(0);
66 List primitives = ret.findChildrenOfType(ASTPrimitiveType.class);
67 if (!primitives.isEmpty() && ((ASTPrimitiveType) primitives.get(0)).isBoolean()) {
68 return true;
69 }
70 }
71 return false;
72 }
73 }