static fields not checked?

7 views
Skip to first unread message

jasonbra...@gmail.com

unread,
Apr 20, 2018, 1:54:23 PM4/20/18
to mutability-detector
Hi, all. This project is pretty cool, but I was pretty surprised to notice that it doesn't appear to check static fields. Is this correct? For example, this passes:

private static final class TestClass {
public static final int[] ofCourseMutableArray = new int[] {5, 10};
}

public static void main(String[] args) {
assertImmutable(TestClass.class);
}

Is there a way to check against this, too? Or is this an area open for enhancement?

Thanks!

Graham Allan

unread,
Apr 24, 2018, 3:53:02 PM4/24/18
to mutability-detector
Hi Jason,

It's known that it doesn't check static fields. That's a bit of a puzzle for me, because the possibility for false positives seems quite high, and implementing an accurate analysis would be quite difficult, I think.

For example, you could argue that your TestClass is immutable, because of any methods that you call on it, you'll always get the same answer, and it can't be subclassed. The question then becomes, is it valid to treat e.g. instanceOfTestClass.ofCourseMutableArray like an non-static field? IIRC static fields can still be accessed like that in Java, so maybe static fields should not be treated specially?

So, the summary is:
 - No Mutability Detector deliberately doesn't consider static fields
 - It could though! Are you interesting in submitting a Pull Request, or even an Issue to discuss further?

All the best!
Graham
Reply all
Reply to author
Forward
0 new messages