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