| 1 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 prevClassName = " none "; 29 private BugReporter bugReporter; 30 static final int doNotConsider = ACC_PRIVATE | ACC_STATIC | ACC_NATIVE; 31 32 private HashMap<String , MethodAnnotation> getMethods = new HashMap<String , MethodAnnotation>(); 34 private HashMap<String , MethodAnnotation> setMethods = new HashMap<String , MethodAnnotation>(); 35 36 public FindUnsyncGet(BugReporter bugReporter) { 37 this.bugReporter = bugReporter; 38 } 39 40 @Override  41 public void report() { 42 Set<String > commonProperties = new HashSet<String >(getMethods.keySet()); 45 commonProperties.retainAll(setMethods.keySet()); 46 47 for (String 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  62 public void visit(JavaClass obj) { 63 report(); 64 prevClassName = getDottedClassName(); 65 } 66 67 @Override  68 public void visit(Method obj) { 69 int flags = obj.getAccessFlags(); 70 if ((flags & doNotConsider) != 0) return; 71 String name = obj.getName(); 72 boolean isSynchronized = (flags & ACC_SYNCHRONIZED) != 0; 73 87 if (name.startsWith("get") 88 && !isSynchronized 89 ) { 91 getMethods.put(name.substring(3), MethodAnnotation.fromVisitedMethod(this)); 92 } else if (name.startsWith("set") 93 && isSynchronized 94 ) { 96 setMethods.put(name.substring(3), MethodAnnotation.fromVisitedMethod(this)); 97 } 98 } 99 } 100 | Popular Tags |