KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005 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.ClassContext;
25 import java.util.*;
26 import org.apache.bcel.classfile.Field;
27
28 public class VolatileUsage extends BytecodeScanningDetector {
29       private BugReporter bugReporter;
30
31     public VolatileUsage(BugReporter bugReporter) {
32             this.bugReporter = bugReporter;
33     }
34         
35     @Override JavaDoc
36          public void visitClassContext(ClassContext classContext) {
37                 classContext.getJavaClass().accept(this);
38     }
39
40 static class FieldRecord {
41                 String JavaDoc className;
42                 String JavaDoc name;
43                 String JavaDoc signature;
44                 boolean isStatic;
45         }
46
47
48     Map<String JavaDoc,FieldRecord> fieldInfo = new HashMap<String JavaDoc,FieldRecord>();
49     Set<String JavaDoc> initializationWrites = new HashSet<String JavaDoc>();
50     Set<String JavaDoc> otherWrites = new HashSet<String JavaDoc>();
51     
52
53     @Override JavaDoc
54          public void visit(Field obj) {
55         super.visit(obj);
56         int flags = obj.getAccessFlags();
57         if ((flags & ACC_VOLATILE) == 0) return;
58         if (getFieldSig().charAt(0) == '[') {
59            FieldRecord f = new FieldRecord();
60             f.className = getDottedClassName();
61             f.name = getFieldName();
62             f.signature = getFieldSig();
63             f.isStatic = !((flags & ACC_STATIC) == 0);
64             fieldInfo.put(getDottedClassName() + "." + getFieldName(),
65                     f);
66         }
67     }
68
69     @Override JavaDoc
70     public void sawOpcode(int seen) {
71                 switch (seen) {
72                 case PUTSTATIC:
73             {
74                         String JavaDoc name = (getClassConstantOperand()
75                     + "." + getNameConstantOperand())
76                                 .replace('/', '.');
77             if (getMethodName().equals("<clinit>"))
78                 initializationWrites.add(name);
79             else otherWrites.add(name);
80             break;
81             }
82                 case PUTFIELD:
83                         {
84             String JavaDoc name = (getClassConstantOperand()
85                     + "." + getNameConstantOperand())
86                                 .replace('/', '.');
87             if (getMethodName().equals("<init>"))
88                 initializationWrites.add(name);
89             else otherWrites.add(name);
90             break;
91             }
92         }
93         }
94                 
95
96     @Override JavaDoc
97          public void report() {
98
99         for(Map.Entry<String JavaDoc, FieldRecord> r : fieldInfo.entrySet()) {
100            String JavaDoc name = r.getKey();
101            FieldRecord f = r.getValue();
102            int priority = LOW_PRIORITY;
103            if (initializationWrites.contains(name)
104             && !otherWrites.contains(name))
105              priority = NORMAL_PRIORITY;
106            bugReporter.reportBug(
107             new BugInstance(this, "VO_VOLATILE_REFERENCE_TO_ARRAY", priority)
108              .addClass(f.className)
109              .addField(f.className, f.name, f.signature, f.isStatic));
110         }
111         }
112 }
113
Popular Tags