KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > FindFinalizeInvocations


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.detect;
21
22
23 import edu.umd.cs.findbugs.*;
24
25 import org.apache.bcel.classfile.*;
26
27 public class FindFinalizeInvocations extends BytecodeScanningDetector implements StatelessDetector {
28     private static final boolean DEBUG = SystemProperties.getBoolean("ffi.debug");
29
30     private BugReporter bugReporter;
31
32     public FindFinalizeInvocations(BugReporter bugReporter) {
33         this.bugReporter = bugReporter;
34     }
35     
36
37
38     boolean sawSuperFinalize;
39
40     @Override JavaDoc
41          public void visit(Method obj) {
42         if (DEBUG) System.out.println("FFI: visiting " + getFullyQualifiedMethodName());
43         if (getMethodName().equals("finalize")
44                 && getMethodSig().equals("()V")
45                 && (obj.getAccessFlags() & (ACC_PUBLIC)) != 0
46         )
47             bugReporter.reportBug(new BugInstance(this, "FI_PUBLIC_SHOULD_BE_PROTECTED", NORMAL_PRIORITY).addClassAndMethod(this));
48     }
49
50     @Override JavaDoc
51          public void visit(Code obj) {
52         sawSuperFinalize = false;
53         super.visit(obj);
54         if (!getMethodName().equals("finalize")
55                 || !getMethodSig().equals("()V"))
56             return;
57         String JavaDoc overridesFinalizeIn
58                 = Lookup.findSuperImplementor(getDottedClassName(),
59                         "finalize",
60                         "()V",
61                         bugReporter);
62         boolean superHasNoFinalizer = overridesFinalizeIn.equals("java.lang.Object");
63         // System.out.println("superclass: " + superclassName);
64
if (obj.getCode().length == 1) {
65             if (superHasNoFinalizer) {
66                 if (!getMethod().isFinal())
67                 bugReporter.reportBug(new BugInstance(this, "FI_EMPTY", NORMAL_PRIORITY).addClassAndMethod(this));
68             } else
69                 bugReporter.reportBug(new BugInstance(this, "FI_NULLIFY_SUPER", NORMAL_PRIORITY)
70                         .addClassAndMethod(this)
71                         .addClass(overridesFinalizeIn));
72         } else if (obj.getCode().length == 5 && sawSuperFinalize)
73             bugReporter.reportBug(new BugInstance(this, "FI_USELESS", NORMAL_PRIORITY).addClassAndMethod(this));
74         else if (!sawSuperFinalize && !superHasNoFinalizer)
75             bugReporter.reportBug(new BugInstance(this, "FI_MISSING_SUPER_CALL", NORMAL_PRIORITY).addClassAndMethod(this)
76                     .addClass(overridesFinalizeIn));
77     }
78
79     @Override JavaDoc
80          public void sawOpcode(int seen) {
81         if (seen == INVOKEVIRTUAL && getNameConstantOperand().equals("finalize"))
82             bugReporter.reportBug(new BugInstance(this, "FI_EXPLICIT_INVOCATION",
83                         getMethodName().equals("finalize") && getMethodSig().equals("()V") ? HIGH_PRIORITY : NORMAL_PRIORITY)
84                     .addClassAndMethod(this)
85                     .addCalledMethod(this).describe("METHOD_CALLED")
86                     .addSourceLine(this, getPC()));
87         if (seen == INVOKESPECIAL && getNameConstantOperand().equals("finalize"))
88             sawSuperFinalize = true;
89     }
90 }
91
Popular Tags