KickJava   Java API By Example, From Geeks To Geeks.

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


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 import edu.umd.cs.findbugs.ba.Hierarchy;
25 import org.apache.bcel.classfile.*;
26
27 public class StartInConstructor extends BytecodeScanningDetector implements StatelessDetector {
28     private BugReporter bugReporter;
29
30     public StartInConstructor(BugReporter bugReporter) {
31         this.bugReporter = bugReporter;
32     }
33
34
35
36     boolean isFinal;
37
38     @Override JavaDoc
39          public void visit(JavaClass obj) {
40         isFinal = (obj.getAccessFlags() & ACC_FINAL) != 0
41                 || (obj.getAccessFlags() & ACC_PUBLIC) == 0;
42     }
43
44     @Override JavaDoc
45          public void visit(Code obj) {
46         if (getMethodName().equals("<init>")) super.visit(obj);
47     }
48
49     @Override JavaDoc
50          public void sawOpcode(int seen) {
51         if (!isFinal && seen == INVOKEVIRTUAL && getNameConstantOperand().equals("start")
52                 && getSigConstantOperand().equals("()V")) {
53             try {
54                 if (Hierarchy.isSubtype(getDottedClassConstantOperand(), "java.lang.Thread")) {
55                     bugReporter.reportBug(new BugInstance(this, "SC_START_IN_CTOR", NORMAL_PRIORITY)
56                             .addClassAndMethod(this)
57                             .addCalledMethod(this)
58                             .addSourceLine(this));
59                 }
60             } catch (ClassNotFoundException JavaDoc e) {
61                 bugReporter.reportMissingClass(e);
62             }
63         }
64     }
65 }
66
Popular Tags