You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.4 KiB
58 lines
1.4 KiB
4 years ago
|
/*
|
||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
|
*
|
||
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
|
*/
|
||
|
package codetoanalyze.java.immutability;
|
||
|
|
||
|
import com.moblica.common.xmob.utils.Immutable;
|
||
|
|
||
|
class ArrayTest {
|
||
|
@Immutable final int[] testArray = new int[] {0, 1, 2, 4};
|
||
|
@Immutable static String[] suitArray = {"spades", "hearts", "diamonds", "clubs"};
|
||
|
int[] mutableArray = new int[] {0};
|
||
|
|
||
|
void array_mod_bad() {
|
||
|
testArray[3] = 3; // modifications to an immutable array are not ok
|
||
|
}
|
||
|
|
||
|
int[] get_testArray() {
|
||
|
return testArray;
|
||
|
}
|
||
|
|
||
|
void mutate_array_via_getter_bad() {
|
||
|
int[] array = get_testArray();
|
||
|
array[2] = 7; // ERROR!
|
||
|
}
|
||
|
|
||
|
void mutate_array_via_aliasing_bad() {
|
||
|
int[] array = get_testArray();
|
||
|
int[] otherArray = array;
|
||
|
otherArray[2] = 7; // ERROR!
|
||
|
}
|
||
|
|
||
|
void mutate_array_via_callee_bad() {
|
||
|
int[] array = get_testArray();
|
||
|
int[] otherArray = array;
|
||
|
mutate_param_ok(array); // ERROR!
|
||
|
}
|
||
|
|
||
|
void mutate_param_ok(int[] array) {
|
||
|
array[2] = 7;
|
||
|
}
|
||
|
|
||
|
void mutate_static_array_bad() {
|
||
|
suitArray[0] = "pades"; // ERROR!
|
||
|
}
|
||
|
|
||
|
void mutable_array_mod_ok() {
|
||
|
mutableArray[0] = 3; // modifications to mutable arrays are ok
|
||
|
}
|
||
|
|
||
|
void mixed_mod_bad() {
|
||
|
mutableArray[0] = 3; // modifications to mutable arrays are ok
|
||
|
mutate_array_via_callee_bad(); // // modifications to immutables are not
|
||
|
}
|
||
|
}
|