KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004 Dave Brosius <dbrosius@users.sourceforge.net>
4  * Copyright (C) 2004 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
24 import edu.umd.cs.findbugs.*;
25 import java.util.*;
26 import org.apache.bcel.Repository;
27 import org.apache.bcel.classfile.*;
28
29
30 public class InstantiateStaticClass extends BytecodeScanningDetector {
31     private BugReporter bugReporter;
32
33     Map<String JavaDoc,Boolean JavaDoc> isStaticClass = new HashMap<String JavaDoc,Boolean JavaDoc>();
34     
35     public InstantiateStaticClass(BugReporter bugReporter) {
36         this.bugReporter = bugReporter;
37     }
38     
39     @Override JavaDoc
40          public void sawOpcode(int seen) {
41         try {
42             if ((seen == INVOKESPECIAL)
43             && getNameConstantOperand().equals("<init>")
44             && getSigConstantOperand().equals("()V")) {
45                 String JavaDoc clsName = getClassConstantOperand();
46                 if (clsName.equals("java/lang/Object"))
47                     return;
48             
49                 //ignore superclass synthesized ctor calls
50
if (getMethodName().equals("<init>") && (getPC() == 1))
51                     return;
52                 
53                 //ignore the typesafe enumerated constant pattern
54
if (getMethodName().equals("<clinit>") && (getClassName().equals(clsName)))
55                     return;
56                 
57                 Boolean JavaDoc b = isStaticClass.get(clsName);
58                 if (b == null) {
59                     b = Boolean.valueOf(isStaticOnlyClass(clsName));
60                     isStaticClass.put(clsName, b);
61                     }
62                 if (b)
63                 
64                 bugReporter.reportBug(new BugInstance(this, "ISC_INSTANTIATE_STATIC_CLASS", LOW_PRIORITY)
65                         .addClassAndMethod(this)
66                         .addSourceLine(this));
67             }
68         } catch (ClassNotFoundException JavaDoc cnfe) {
69             bugReporter.reportMissingClass(cnfe);
70         }
71     }
72
73    private boolean isStaticOnlyClass(String JavaDoc clsName) throws ClassNotFoundException JavaDoc {
74                 clsName = clsName.replace('/', '.');
75                 JavaClass cls = Repository.lookupClass(clsName);
76                 if (cls.getInterfaceNames().length > 0)
77                     return false;
78                 String JavaDoc superClassName = cls.getSuperclassName();
79                 if (!superClassName.equals("java.lang.Object"))
80                     return false;
81                 
82                 Method[] methods = cls.getMethods();
83                 int staticCount = 0;
84        for (Method m : methods) {
85            if (m.isStatic()) {
86                staticCount++;
87                continue;
88            }
89
90            if (m.getName().equals("<init>")) {
91                if (!m.getSignature().equals("()V"))
92                    return false;
93
94                Code c = m.getCode();
95
96                if (c.getCode().length > 5)
97                    return false;
98            } else {
99                return false;
100            }
101        }
102                 
103                 Field[] fields = cls.getFields();
104        for (Field f : fields) {
105            if (f.isStatic()) {
106                staticCount++;
107                continue;
108            }
109
110            if (!f.isPrivate())
111                return false;
112        }
113
114                 if (staticCount == 0) return false;
115                 return true;
116                 }
117
118 }
119
Popular Tags