KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.bcel.Repository;
23 import org.apache.bcel.classfile.Field;
24 import org.apache.bcel.classfile.JavaClass;
25
26 import edu.umd.cs.findbugs.DeepSubtypeAnalysis;
27 import edu.umd.cs.findbugs.BugInstance;
28 import edu.umd.cs.findbugs.BugReporter;
29 import edu.umd.cs.findbugs.Detector;
30 import edu.umd.cs.findbugs.ba.ClassContext;
31 import edu.umd.cs.findbugs.util.ClassName;
32 import edu.umd.cs.findbugs.visitclass.PreorderVisitor;
33
34 public class ComparatorIdiom extends PreorderVisitor implements Detector {
35
36     BugReporter bugReporter;
37
38     public ComparatorIdiom(BugReporter bugReporter) {
39         this.bugReporter = bugReporter;
40     }
41
42     public void visitClassContext(ClassContext classContext) {
43         classContext.getJavaClass().accept(this);
44     }
45
46
47     @Override JavaDoc
48     public void visit(JavaClass obj) {
49         try {
50             if (Repository.instanceOf(obj, "java.util.Comparator")
51                     && !ClassName.isAnonymous(getClassName())
52                     && !Repository.instanceOf(obj, "java.io.Serializable")) {
53                 int priority = NORMAL_PRIORITY;
54                 if (obj.isInterface() || obj.isAbstract()) {
55                     priority = LOW_PRIORITY;
56                 } else {
57                 double easilySerializable = 1.0;
58                 for(Field f : obj.getFields()) {
59                     try {
60                     String JavaDoc signature = f.getSignature();
61                     char firstChar = signature.charAt(0);
62                     if (firstChar == 'L' || firstChar == '[')
63                         easilySerializable *= DeepSubtypeAnalysis.isDeepSerializable(signature);
64                     } catch (ClassNotFoundException JavaDoc e) {
65                         easilySerializable = 0.0;
66                         break;
67                     }
68                 }
69                 
70                 if (easilySerializable < 0.9) priority = LOW_PRIORITY;
71                 int lastDollar = getClassName().lastIndexOf('$');
72                 if (lastDollar > 0 && Character.isDigit(getClassName().charAt(lastDollar+1)))
73                     priority = LOW_PRIORITY;
74                 }
75                 bugReporter
76                         .reportBug(new BugInstance(this,
77                                 "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE",
78                                 priority).addClass(this));
79
80             }
81         } catch (ClassNotFoundException JavaDoc e) {
82             // ignore it
83
}
84     }
85
86     public void report() {
87
88     }
89 }
90
Popular Tags