1 package test.net.sourceforge.pmd.rules.finalize;
2
3 import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
4 import test.net.sourceforge.pmd.testframework.TestDescriptor;
5 import net.sourceforge.pmd.Rule;
6 import net.sourceforge.pmd.PMD;
7 import net.sourceforge.pmd.rules.XPathRule;
8
9 public class FinalizeDoesNotCallSuperFinalizeRuleTest extends SimpleAggregatorTst {
10
11 private Rule rule;
12
13 public void setUp() {
14 rule = new XPathRule();
15 rule.addProperty("xpath", "//MethodDeclaration[MethodDeclarator[@Image='finalize'][not(FormalParameters/*)]]/Block/BlockStatement[last()][not(Statement/StatementExpression/PrimaryExpression/PrimaryPrefix[@Image='finalize'])]");
16 }
17
18 public void testAll() {
19 runTests(new TestDescriptor[] {
20 new TestDescriptor(TEST1, "bad", 1, rule),
21 new TestDescriptor(TEST2, "ok", 0, rule),
22 });
23 }
24
25 private static final String TEST1 =
26 "public class Foo {" + PMD.EOL +
27 " public void finalize() {" + PMD.EOL +
28 " super.finalize();" + PMD.EOL +
29 " int x = 2;" + PMD.EOL +
30 " }" + PMD.EOL +
31 "}";
32
33 private static final String TEST2 =
34 "public class Foo {" + PMD.EOL +
35 " public void finalize() {" + PMD.EOL +
36 " int x = 2;" + PMD.EOL +
37 " super.finalize();" + PMD.EOL +
38 " }" + PMD.EOL +
39 "}";
40
41 /*
42 TODO
43 This should handle a call to super.finalize() within a finally block, e.g.
44
45 protected void finalize()
46 {
47 try { // do something
48 }
49 finally
50 {
51 super.finalize(); // this is OK and should not be flagged
52 }
53 }
54 */
55 }
This page was automatically generated by Maven