KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import edu.umd.cs.findbugs.*;
24 import java.util.*;
25 import org.apache.bcel.classfile.*;
26
27 public class FindUnsyncGet extends BytecodeScanningDetector {
28     String JavaDoc prevClassName = " none ";
29     private BugReporter bugReporter;
30     static final int doNotConsider = ACC_PRIVATE | ACC_STATIC | ACC_NATIVE;
31
32     // Maps of property names to get and set methods
33
private HashMap<String JavaDoc, MethodAnnotation> getMethods = new HashMap<String JavaDoc, MethodAnnotation>();
34     private HashMap<String JavaDoc, MethodAnnotation> setMethods = new HashMap<String JavaDoc, MethodAnnotation>();
35
36     public FindUnsyncGet(BugReporter bugReporter) {
37         this.bugReporter = bugReporter;
38     }
39
40     @Override JavaDoc
41          public void report() {
42         // Find the set of properties for which we have both
43
// unsynchronized get and synchronized set methods
44
Set<String JavaDoc> commonProperties = new HashSet<String JavaDoc>(getMethods.keySet());
45         commonProperties.retainAll(setMethods.keySet());
46
47         // Report method pairs
48
for (String JavaDoc propName : commonProperties) {
49             MethodAnnotation getMethod = getMethods.get(propName);
50             MethodAnnotation setMethod = setMethods.get(propName);
51
52             bugReporter.reportBug(new BugInstance(this, "UG_SYNC_SET_UNSYNC_GET", NORMAL_PRIORITY)
53                     .addClass(prevClassName)
54                     .addMethod(getMethod)
55                     .addMethod(setMethod));
56         }
57         getMethods.clear();
58         setMethods.clear();
59     }
60
61     @Override JavaDoc
62          public void visit(JavaClass obj) {
63         report();
64         prevClassName = getDottedClassName();
65     }
66
67     @Override JavaDoc
68          public void visit(Method obj) {
69         int flags = obj.getAccessFlags();
70         if ((flags & doNotConsider) != 0) return;
71         String JavaDoc name = obj.getName();
72         boolean isSynchronized = (flags & ACC_SYNCHRONIZED) != 0;
73         /*
74         String sig = obj.getSignature();
75         char firstArg = sig.charAt(1);
76         char returnValue = sig.charAt(1 + sig.indexOf(')'));
77         boolean firstArgIsRef = (firstArg == 'L') || (firstArg == '[');
78         boolean returnValueIsRef = (returnValue == 'L') || (returnValue == '[');
79
80         System.out.println(className + "." + name
81                 + " " + firstArgIsRef
82                 + " " + returnValueIsRef
83                 + " " + isSynchronized
84                 + " " + isNative
85                 );
86         */

87         if (name.startsWith("get")
88                 && !isSynchronized
89         // && returnValueIsRef
90
) {
91             getMethods.put(name.substring(3), MethodAnnotation.fromVisitedMethod(this));
92         } else if (name.startsWith("set")
93                 && isSynchronized
94         // && firstArgIsRef
95
) {
96             setMethods.put(name.substring(3), MethodAnnotation.fromVisitedMethod(this));
97         }
98     }
99 }
100
Popular Tags