StaticAccess FindBugs detector

Introduction

StaticAccess detector is a FindBugs plugin, which enables you to verify that methods do not rely on static state, that is, that they don't read or write static variables. This can be useful for example when writing thread-safe or "pure" methods.

You may also read the blog entry about the detector.

Installation

First of all, you must download and install FindBugs (which is pretty straightforward). You can find the package and instructions on their webpage. By the way, installing and using FindBugs is worth it even if you don't use the StaticAccess detector ;).

After you download the staticaccess-detector.jar file, you may either place it in the $FINDBUGS_HOME/plugin directory (the plugin will be automatically picked by FindBugs then), or specify it on the commandline using the -pluginList option. For example, if you are using the textui:

java -jar $FINDBUGS_HOME/findbugs.jar -textui -auxclasspath staticaccess-detector-annotations.jar -pluginList staticaccess-detector.jar $CLASSES

You can of course also use the plugin using the gui, maven plugin etc; essentially, everywhere where FindBugs plugins are supported.

Downloads

The source code is available on github: https://github.com/adamw/staticaccess-detector/tree/master.

And the binaries in the maven repository: http://repository.mamut.net.pl/content/repositories/releases/pl/net/mamut/staticaccess-detector/0.1/.

Also, you can take a look at the javadocs.

Examples

import pl.net.mamut.staticaccess.StaticIndependent;

public class StaticIndependentExample {
    public static Integer globalInt = 10;
    public static final Integer CONSTANT = 12;

    // This method is annotated as static-independent, that is, cannot rely on static (global state)
    @StaticIndependent
    public void testStaticIndepdendent() {
        int local = globalInt;          // ERROR: cannot read static variables
        globalInt = 11;                 // ERROR: cannot write static variables
	
        int local2 = CONSTANT;          // OK: can read constants
    }
    
    // Unannotated methods aren't checked
    public void testStaticDependent() { 
    	int local = globalInt;          // OK: this method isn't static-independent
    }
}
import pl.net.mamut.staticaccess.StaticIndependent;
import pl.net.mamut.staticaccess.StaticDependent;

import edu.umd.cs.findbugs.annotations.DefaultAnnotationForMethods;

@DefaultAnnotationForMethods(StaticIndependent.class)
public class StaticIndependentByDefaultExample {
    public static Integer globalInt = 10;
    public static final Integer CONSTANT = 12;

    // This method is static-independent by default
    public void testStaticIndepdendent() {
        int local = globalInt;          // ERROR: cannot read static variables
    }
    
    // Overriding the default
    @StaticDependent
    public void testStaticDependent() { 
    	int local = globalInt;          // OK: this method isn't static-independent
    }
}

More info

In case of any questions, suggestions, remarks, etc., write me at adam (at) warski (dot) org.