Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly analyse array field mutation #19

Open
Grundlefleck opened this issue Dec 5, 2012 · 0 comments
Open

Correctly analyse array field mutation #19

Grundlefleck opened this issue Dec 5, 2012 · 0 comments

Comments

@Grundlefleck
Copy link
Contributor

Original author: Grundlefleck@gmail.com (November 15, 2011 22:34:46)

Currently the following class will be called mutable:

public final class ImmutableButHasUnmodifiedArrayAsField {
private final int[] unmodifiedArray;

public ImmutableButHasUnmodifiedArrayAsField() {
    this.unmodifiedArray = new int[] { 3, 14 };
}

public int getWhole() {
    return unmodifiedArray[0];
}

public int getFraction() {
    return unmodifiedArray[1];
}

}

However, this class is immutable (effectively immutable if the field is not marked final). It is ensuring exclusive access to the mutable components (including the elements of the array, which would not be true if replacing int[] with a mutable type, e.g. List<>[]). An update to the analysis should allow such a class under the following rules:

  1. the array is not passed into constructor as a parameter
  2. the array does not escape (but consider if the "best guess" analysis of the escaped this reference checker will be good enough, if not, probably should abort).
  3. the type of array is immutable
  4. the references in the array are never changed, e.g. myArray[1] = 42

(Depending on the success of this attempt, the same rules could extend to any mutable type used as a field, except replace #4 with 'a mutating method is never called')

Original issue: http://code.google.com/p/mutability-detector/issues/detail?id=18

@ghost ghost assigned Grundlefleck Dec 5, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant