Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2012 Dave Brosius
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 package com.mebigfatguy.fbcontrib.detect;
 import java.util.Map;
 import java.util.Set;
 import  org.apache.bcel.classfile.Code;
 import  org.apache.bcel.classfile.JavaClass;
 import  org.apache.bcel.classfile.Method;
 import  edu.umd.cs.findbugs.BugInstance;
 import  edu.umd.cs.findbugs.BugReporter;
 import  edu.umd.cs.findbugs.BytecodeScanningDetector;

looks for methods that implement awt or swing listeners and perform time consuming operations. Doing these operations in the gui thread will cause the interface to appear sluggish and non-responsive to the user. It is better to use a separate thread to do the time consuming work so that the user has a better experience.
 public class SluggishGui extends BytecodeScanningDetector
 	private static final Set<StringexpensiveCalls = new HashSet<String>();
 	static {
 	private BugReporter bugReporter;
 	private Set<JavaClass> guiInterfaces;
 	private Map<Code,Method> listenerCode;
 	private String methodName;
 	private String methodSig;
 	private boolean isListenerMethod = false;
 	private boolean methodReported = false;

constructs a SG detector given the reporter to report bugs on

bugReporter the sync of bug reports
 	public SluggishGui(BugReporter bugReporter) {
 		this. = bugReporter;

overrides the visitor to reset look for gui interfaces

classContext the context object for the currently parsed class
	public void visitClassContext(ClassContext classContext) {
		try {
			 = new HashSet<JavaClass>();
			JavaClass cls = classContext.getJavaClass();
			JavaClass[] infs = cls.getAllInterfaces();
			for (JavaClass inf : infs) {
				String name = inf.getClassName();
				if ((name.startsWith("java.awt.")
				||  name.startsWith("javax.swing."))
				&&  name.endsWith("Listener")) {
			if (.size() > 0) {
				 = new LinkedHashMap<Code,Method>();
catch (ClassNotFoundException cnfe) {
finally {
			 = null;
			 = null;

overrides the visitor to visit all of the collected listener methods

obj the context object of the currently parsed class
	public void visitAfter(JavaClass obj) {
		for (Code l : .keySet()) {
			 = false;
overrides the visitor collect method info

obj the context object of the currently parsed method
	public void visitMethod(Method obj) {
		 = obj.getName();
		 = obj.getSignature();

overrides the visitor to segregate method into two, those that implement listeners, and those that don't. The ones that don't are processed first.

obj the context object of the currently parsed code block
	public void visitCode(Code obj) {
		for (JavaClass inf : ) {
			Method[] methods = inf.getMethods();
			for (Method m : methods) {
				if (m.getName().equals()) {
					if (m.getSignature().equals()) {
		 = false;

overrides the visitor to look for the execution of expensive calls

seen the currently parsed opcode
	public void sawOpcode(int seen) {
		if ((seen == INVOKEINTERFACE)
		||  (seen == INVOKEVIRTUAL)
		||  (seen == INVOKESPECIAL)
		||  (seen == INVOKESTATIC)) {
			String methodInfo = clsName + ":" + mName;
			String thisMethodInfo = (clsName.equals(getClassName())) ? (mName + ":" + ) : "0";
			if (.contains(methodInfo) || .contains(thisMethodInfo)) {
					.reportBug(new BugInstance(this"SG_SLUGGISH_GUI", NORMAL_PRIORITY)
												.addMethod(this.getClassContext().getJavaClass(), .get(this.getCode())));
else {
New to GrepCode? Check out our FAQ X