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