Git Product home page Git Product logo

mutabilitydetector4findbugs's Introduction

Latest Version Email Join the chat at https://gitter.im/MutabilityDetector/main Build Status

What is Mutability Detector?

Mutability Detector is designed to analyse Java classes and report on whether instances of a given class are immutable. It can be used:

  • In a unit test, with an assertion like assertImmutable(MyClass.class). Is your class actually immutable? What about after that change you just made?
  • As a FindBugs plugin. Those classes you annotated with @Immutable, are they actually?
  • At runtime. Does your API require being given immutable objects?
  • From the command line. Do you want to quickly run Mutability Detector over an entire code base?

Why Try To Detect Mutability?

Developing classes to be immutable has several benefits. An immutable object is one which cannot be changed once it is constructed. While writing concurrent programs, using immutable objects can greatly simplify complex systems, as sharing an object across threads is much safer. There are a few rules for what makes an object immutable, and it is easy to break the rules and render the object unsafe. This could lead to subtle, hard-to-detect bugs which could lower the integrity of the system. Using an automated tool to recognise mutability where it's not intended can reduce the complexity of writing immutable classes.

Mutability Detector analyses on the strict side, very few classes are found to be perfectly immutable, for instance, java.lang.String and java.lang.Integer are not immutable because of a non-final field, and primitive array field, respectively. Mutability Detector will not be released under a 1.0 version until these cases can be correctly analysed.

If this sounds like it may be interesting or useful to you, continue reading for more information on getting started. You may also want to take a look at the Mutability Detector Blog.

Getting Started

To use Mutability Detector directly, either from the command line, at runtime in your application, or as part of your unit tests, grab the jar available from Maven Central. Or you can declare it in your Maven-compatible build tool, with the following coordinates:

<dependency>
    <groupId>org.mutabilitydetector</groupId>
    <artifactId>MutabilityDetector</artifactId>
    <version>[latest version here]</version>
    <scope>test</scope>
</dependency>

Latest Version

Using Mutability Detector in Unit Testing

Just add MutabilityDetector to your unit testing classpath. Adding your first assertion is as simple as:

import static org.mutabilitydetector.unittesting.MutabilityAssert.assertImmutable;

@Test public void checkMyClassIsImmutable() {
    assertImmutable(MyClass.class); 
}

Though it is possible (or likely) that you will have to configure the assertion to deal with any false positives that arise. See the JavaDoc on MutabilityAssert for further information.

Using Mutability Detector from the Command Line

An example of how to run it is probably the most useful. If you want to analyse MyCodebase.jar use:

java -jar MutabilityDetector.jar -classpath path/to/MyCodebase.jar

Mutability Detector can handle jars as seen above, or directories of class files (thanks go to the authors of classpath-explorer). So if your codebase was in the filesystem as directories and .class files, and the directory MyCodebase was the root of that, you could run:

java -jar MutabilityDetector.jar -classpath path/to/MyCodebase

The output is a list of the analysed classes against the result of asking "Is immutable?", ie. IMMUTABLE, NOT_IMMUTABLE, EFFECTIVELY_IMMUTABLE.

Execute java -jar MutabilityDetector.jar --help for a complete listing of the command line options.

Using Mutability Detector within Your Application

It is possible to use Mutability Detector at runtime. For example, consider if you have a library which requires that objects passed to it are immutable. On receiving such an object, you can ask Mutability Detector if it is actually immutable.

Check out the code snippet in this example, which shows correct usage against trunk code.

FindBugs Plugin

To have Mutability Detector inspect your classes during a FindBugs analysis, grab the MutabilityDetector4FindBugs jar, and configure it to be picked up by FindBugs during a normal analysis, as described here.

Mutability Detector will perform it's analysis on any classes annotated with @Immutable.

MutabilityDetector4FindBugs is also available from Maven Central, with the following coordinates:

<dependency>
    <groupId>org.mutabilitydetector</groupId>
    <artifactId>MutabilityDetector4FindBugs</artifactId>
    <version>[latest version here]</version>
    <scope>test</scope>
</dependency>

MutabilityDetector4FindBugs Latest Version

mutabilitydetector4findbugs's People

Contributors

grundlefleck avatar neomatrix369 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

mutabilitydetector4findbugs's Issues

Upgrade FindBugs dependency to 2.0.1

Presently the MutabilityDetector uses 1.3.9, which release in 2009, instead of 2.0.1, which released in 2012. MutabilityDetector will require some source changes for this:

[ERROR] /home/gaul/work/MutabilityDetector4FindBugs/src/main/java/org/mutabilitydetector/findbugs/IncorrectlyAppliedImmutableAnnotationDetector.java:[53,16] error: name clash: visitAnnotation(String,Map<String,Object>,boolean) in IncorrectlyAppliedImmutableAnnotationDetector and visitAnnotation(String,Map<String,ElementValue>,boolean) in AnnotationVisitor have the same erasure, yet neither overrides the other
[ERROR]
[ERROR] /home/gaul/work/MutabilityDetector4FindBugs/src/main/java/org/mutabilitydetector/findbugs/IncorrectlyAppliedImmutableAnnotationDetector.java:[54,13] error: no suitable method found for visitAnnotation(String,Map<String,Object>,boolean)
[ERROR]
[ERROR] method AnnotationVisitor.visitAnnotation(Annotations) is not applicable
[ERROR] (actual and formal argument lists differ in length)
[ERROR] method AnnotationVisitor.visitAnnotation(String,Map<String,ElementValue>,boolean) is not applicable
[ERROR] (actual argument Map<String,Object> cannot be converted to Map<String,ElementValue> by method invocation conversion)
[ERROR] method PreorderVisitor.visitAnnotation(Annotations) is not applicable
[ERROR] (actual and formal argument lists differ in length)
[ERROR] /home/gaul/work/MutabilityDetector4FindBugs/src/main/java/org/mutabilitydetector/findbugs/IncorrectlyAppliedImmutableAnnotationDetector.java:[52,4] error: method does not override or implement a method from a supertype

java.util.zip.ZipException while analyzing a class file

When I try to analyze a class file with findbugs GUI tool and MutabilityDetector4FindBugs plugin, findbugs popups a dialog box which describes an exception occurred as follows:

  Exception analyzing Test using detector org.mutabilitydetector.findbugs.MutabilityDetector4FindBugs
    java.lang.RuntimeException: java.util.zip.ZipException: error in opening zip file
      At org.mutabilitydetector.repackaged.com.google.classpath.ClassPathFactory.createFromPaths(ClassPathFactory.java:50)
      At org.mutabilitydetector.repackaged.com.google.classpath.ClassPathFactory.createFromPath(ClassPathFactory.java:39)
      At org.mutabilitydetector.findbugs.FBClasspathConverter.createClassPathForCodeBases(FBClasspathConverter.java:18)
      At org.mutabilitydetector.findbugs.MutabilityDetector4FindBugs$AnalysisSessionHolder.makeFindBugsClasspathAvailable(MutabilityDetector4FindBugs.java:78)
      At org.mutabilitydetector.findbugs.MutabilityDetector4FindBugs$AnalysisSessionHolder.createNewAnalysisSession(MutabilityDetector4FindBugs.java:69)
      At org.mutabilitydetector.findbugs.MutabilityDetector4FindBugs$AnalysisSessionHolder.lazyGet(MutabilityDetector4FindBugs.java:58)
      At org.mutabilitydetector.findbugs.IncorrectlyAppliedImmutableAnnotationDetector.doMutabilityAnalysis(IncorrectlyAppliedImmutableAnnotationDetector.java:72)
      At org.mutabilitydetector.findbugs.IncorrectlyAppliedImmutableAnnotationDetector.visitClassContext(IncorrectlyAppliedImmutableAnnotationDetector.java:66)
      At org.mutabilitydetector.findbugs.MutabilityDetector4FindBugs.visitClassContext(MutabilityDetector4FindBugs.java:47)
      At edu.umd.cs.findbugs.DetectorToDetector2Adapter.visitClass(DetectorToDetector2Adapter.java:74)
      At edu.umd.cs.findbugs.FindBugs2.analyzeApplication(FindBugs2.java:1209)
      At edu.umd.cs.findbugs.FindBugs2.execute(FindBugs2.java:282)
      At edu.umd.cs.findbugs.gui2.BugLoader.doAnalysis(BugLoader.java:99)
      At edu.umd.cs.findbugs.gui2.AnalyzingDialog$AnalysisThread.run(AnalyzingDialog.java:260)

And also it produces log entries to console as follows:

5월 14, 2015 10:58:07 오전 org.mutabilitydetector.repackaged.com.google.classpath.JARClassPath loadEntries
경고: Failed to read Jar file /home/nori/mysrc/test/java/mutability/Test.class

Finally the result of the analyze does not include diagonastic reports about mutability.

It works well when analyzing a jar file.

Release 0.3 including MutabilityDetector 0.9

MutabilityDetector 0.9 improves String detection which I need to analyze my code base. I set MutabilityDetector to 0.9, installed 0.3-SNAPSHOT, and tested the correct analysis in FindBugs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.