01: package net.sourceforge.pmd.rules;
02:
03: import net.sourceforge.pmd.AbstractRule;
04: import net.sourceforge.pmd.ast.ASTCompilationUnit;
05: import net.sourceforge.pmd.ast.ASTName;
06: import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
07: import net.sourceforge.pmd.symboltable.MethodScope;
08:
09: import java.util.HashSet;
10: import java.util.Set;
11:
12: public class AvoidCallingFinalize extends AbstractRule {
13:
14: private Set<MethodScope> checked = new HashSet<MethodScope>();
15:
16: public Object visit(ASTCompilationUnit acu, Object ctx) {
17: checked.clear();
18: return super .visit(acu, ctx);
19: }
20:
21: public Object visit(ASTName name, Object ctx) {
22: if (name.getImage() == null
23: || !name.getImage().endsWith("finalize")) {
24: return ctx;
25: }
26: MethodScope meth = name.getScope().getEnclosingMethodScope();
27: if (meth.getName().equals("finalize")) {
28: return ctx;
29: }
30: if (checked.contains(meth)) {
31: return ctx;
32: }
33: checked.add(meth);
34: addViolation(ctx, name);
35: return ctx;
36: }
37:
38: public Object visit(ASTPrimaryPrefix pp, Object ctx) {
39: if (pp.getImage() == null
40: || !pp.getImage().endsWith("finalize")) {
41: return super .visit(pp, ctx);
42: }
43: MethodScope meth = pp.getScope().getEnclosingMethodScope();
44: if (meth.getName().equals("finalize")) {
45: return super.visit(pp, ctx);
46: }
47: if (checked.contains(meth)) {
48: return super.visit(pp, ctx);
49: }
50: checked.add(meth);
51: addViolation(ctx, pp);
52: return super.visit(pp, ctx);
53: }
54: }
|