KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004-2006 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 java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import edu.umd.cs.findbugs.*;
28 import edu.umd.cs.findbugs.ba.XFactory;
29 import edu.umd.cs.findbugs.ba.XField;
30
31 import org.apache.bcel.classfile.*;
32 import org.jaxen.function.StringLengthFunction;
33
34 public class HugeSharedStringConstants extends BytecodeScanningDetector {
35
36     /**
37      *
38      */

39     private static final int SIZE_OF_HUGE_CONSTANT = 500;
40
41     String JavaDoc getStringKey(String JavaDoc s) {
42         return s.length() + ":" + s.hashCode();
43     }
44
45     HashMap JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>> map = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>>();
46
47     HashMap JavaDoc<String JavaDoc, XField> definition = new HashMap JavaDoc<String JavaDoc, XField>();
48
49     HashMap JavaDoc<String JavaDoc, Integer JavaDoc> stringSize = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
50
51     BugReporter bugReporter;
52
53     public HugeSharedStringConstants(BugReporter bugReporter) {
54         this.bugReporter = bugReporter;
55     }
56
57     @Override JavaDoc
58     public void visit(ConstantString s) {
59         String JavaDoc value = s.getBytes(getConstantPool());
60         if (value.length() < SIZE_OF_HUGE_CONSTANT)
61             return;
62         String JavaDoc key = getStringKey(value);
63         Set JavaDoc<String JavaDoc> set = map.get(key);
64         if (set == null) {
65             set = new HashSet JavaDoc<String JavaDoc>();
66             map.put(key, set);
67         }
68         set.add(getDottedClassName());
69     }
70
71     @Override JavaDoc
72     public void visit(ConstantValue s) {
73         if (!visitingField())
74             return;
75         int i = s.getConstantValueIndex();
76         Constant c = getConstantPool().getConstant(i);
77         if (c instanceof ConstantString) {
78             String JavaDoc value = ((ConstantString) c).getBytes(getConstantPool());
79             if (value.length() < SIZE_OF_HUGE_CONSTANT)
80                 return;
81             String JavaDoc key = getStringKey(value);
82             definition.put(key, XFactory.createXField(this));
83             stringSize.put(key, value.length());
84         }
85
86     }
87
88     @Override JavaDoc
89     public void report() {
90         for (Map.Entry JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>> e : map.entrySet()) {
91             Set JavaDoc<String JavaDoc> occursIn = e.getValue();
92             if (occursIn.size() == 1)
93                 continue;
94             XField field = definition.get(e.getKey());
95             if (field == null) continue;
96             Integer JavaDoc length = stringSize.get(e.getKey());
97             int overhead = length * (occursIn.size()-1);
98             if (overhead < 3*SIZE_OF_HUGE_CONSTANT) continue;
99             String JavaDoc className = field.getClassName();
100         
101             BugInstance bug = new BugInstance(this, "HSC_HUGE_SHARED_STRING_CONSTANT",
102                     overhead > 20*SIZE_OF_HUGE_CONSTANT ? HIGH_PRIORITY :
103                         ( overhead > 8*SIPUSH ? NORMAL_PRIORITY : LOW_PRIORITY))
104                         .addClass(className).addField(field).addInt(length).addInt(occursIn.size()-1);
105             for (String JavaDoc c : occursIn)
106                 if (!c.equals(className))
107                     bug.addClass(c);
108
109             bugReporter.reportBug(bug);
110
111         }
112
113     }
114
115 }
116
Popular Tags