KickJava   Java API By Example, From Geeks To Geeks.

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


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

20
21 package edu.umd.cs.findbugs.detect;
22
23 import org.apache.bcel.Repository;
24 import org.apache.bcel.classfile.Code;
25 import org.apache.bcel.classfile.JavaClass;
26 import org.apache.bcel.classfile.Method;
27
28 import edu.umd.cs.findbugs.BugInstance;
29 import edu.umd.cs.findbugs.BugReporter;
30 import edu.umd.cs.findbugs.BytecodeScanningDetector;
31 import edu.umd.cs.findbugs.ba.ClassContext;
32
33 public class BadAppletConstructor extends BytecodeScanningDetector {
34     private BugReporter bugReporter;
35     private final JavaClass appletClass;
36     private boolean inConstructor;
37
38     public BadAppletConstructor(BugReporter bugReporter) {
39         this.bugReporter = bugReporter;
40         JavaClass appletClass = null;
41         try {
42             appletClass = Repository.lookupClass("java.applet.Applet");
43         } catch (ClassNotFoundException JavaDoc cnfe) {
44             bugReporter.reportMissingClass(cnfe);
45         }
46         this.appletClass = appletClass;
47     }
48     
49
50
51     @Override JavaDoc
52          public void visitClassContext(ClassContext classContext) {
53         if (appletClass == null)
54             return;
55             
56         JavaClass cls = classContext.getJavaClass();
57         try {
58             if (cls.instanceOf(appletClass))
59                 cls.accept(this);
60         } catch (ClassNotFoundException JavaDoc cnfe) {
61             bugReporter.reportMissingClass(cnfe);
62         }
63     }
64     
65     @Override JavaDoc
66          public void visit(Method obj) {
67         inConstructor = obj.getName().equals("<init>");
68     }
69     
70     @Override JavaDoc
71          public void visit(Code obj) {
72         if (inConstructor)
73             super.visit(obj);
74     }
75     
76     @Override JavaDoc
77          public void sawOpcode(int seen) {
78         if (seen == INVOKEVIRTUAL) {
79             String JavaDoc method = getNameConstantOperand();
80             String JavaDoc signature = getSigConstantOperand();
81             if (((method.equals("getDocumentBase") || method.equals("getCodeBase")) && signature.equals("()Ljava/net/URL;"))
82             || (method.equals("getAppletContext") && signature.equals("()Ljava/applet/AppletContext;"))
83             || (method.equals("getParameter") && signature.equals("(Ljava/lang/String;)Ljava/lang/String;")))
84                 bugReporter.reportBug(new BugInstance(this, "BAC_BAD_APPLET_CONSTRUCTOR", NORMAL_PRIORITY)
85                     .addClassAndMethod(this)
86                     .addSourceLine(this));
87         }
88     }
89 }
90
91 // vim:ts=4
92
Popular Tags