Visible Array Field

Background

The content of arrays in Java is always mutable, even when the reference to the array itself is declared final. Although this is documented behaviour, it is sometimes counter intuitive.
This codecheck generates warnings/errors when an array field is visible or directly accessible via an accessor method.

Since 0.7

Examples

Bad code:

Good code:



public static final int[] maxDimensions = {10, 10, 10};
public static final List<Integer> maxDimensions;
static {
    Integer[] dimensions = {10, 10, 10};
    maxDimensions = Collections.unmodifiableList(
            Arrays.asList(dimensions));
}


private static final int[] sizes = {10, 10, 10};
public static int[] getSizes() {
    return sizes;
}
private static final int[] sizes = {10, 10, 10};
public static int[] getSizes() {
    int[] result = new int[sizes.length];
    System.arraycopy(sizes, 0, result, 0, sizes.length);
    return result;
}


Requires bindings

This code check requires Eclipse to resolve bindings. Enabling this code check increases build time.

@SuppressWarnings

Warnings generated by this codecheck can be suppressed by adding the @SuppressWarnings("visible-array") annotation.

Available quick fixes

No quick fixes are yet available.