mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-15 02:59:33 +07:00
Also: avoid manual mocking of Java 10 classes in StreamCollector10Inlining test, use mockJDK11 instead Also: rewrite SliceTestCase and its inheritors to LightJavaCodeInsightFixtureTestCase, as annotations.jar is not included into project created by DaemonAnalyzerTestCase Also: 'mutates' attribute of @Contract annotation is resolvable now, as we can use newer jetbrains-annotations library. Also: documentation tests now don't generate links to JetBrains annotations, which corresponds to the actual behavior in production GitOrigin-RevId: e460826893c1277cb2b78b18aae9d5aca97d8333
78 lines
2.3 KiB
Java
78 lines
2.3 KiB
Java
import org.jetbrains.annotations.Contract;
|
|
import org.jetbrains.annotations.Unmodifiable;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Collection;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
public class MutabilityBasics {
|
|
@Unmodifiable
|
|
static <T> List<T> emptyList() {
|
|
return Collections.emptyList();
|
|
}
|
|
|
|
@Contract(mutates = "param")
|
|
static <T extends Comparable<T>> void sort(List<T> collection) {
|
|
Collections.sort(collection);
|
|
}
|
|
|
|
@Contract(mutates = "param1")
|
|
static <T extends Comparable<T>> void addAll(Collection<T> collection, List<T> other) {
|
|
sort(<warning descr="Immutable object is passed where mutable is expected">other</warning>);
|
|
collection.addAll(other);
|
|
}
|
|
|
|
// Purity implies that no arguments should be changed
|
|
@Contract(pure = true)
|
|
static <T extends Comparable<T>> T min(List<T> list) {
|
|
sort(<warning descr="Immutable object is passed where mutable is expected">list</warning>);
|
|
return list.get(0);
|
|
}
|
|
|
|
interface Point {
|
|
int get();
|
|
|
|
@Contract(mutates = "this")
|
|
void set(int x);
|
|
|
|
@Contract(pure = true)
|
|
default void setZero() {
|
|
// cannot modify itself (call mutating method), because declared as pure
|
|
<warning descr="Immutable object is modified">set</warning>(0);
|
|
}
|
|
}
|
|
|
|
@Unmodifiable
|
|
static Point getZero() {
|
|
return new Point() {
|
|
@Override
|
|
public int get() {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public void set(int x) {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
};
|
|
}
|
|
|
|
// Differs from getZero as getZero() is considered as getter with predefined value
|
|
@Unmodifiable
|
|
static Point zero() {
|
|
return getZero();
|
|
}
|
|
|
|
@Unmodifiable List<String> list = Arrays.asList("foo", "bar", "baz");
|
|
|
|
void test() {
|
|
List<String> collection = emptyList();
|
|
sort(<warning descr="Immutable object is passed where mutable is expected">collection</warning>);
|
|
sort(<warning descr="Immutable object is passed where mutable is expected">MutabilityBasics.<String>emptyList()</warning>);
|
|
getZero().<warning descr="Immutable object is modified">set</warning>(1);
|
|
zero().<warning descr="Immutable object is modified">set</warning>(1);
|
|
sort(<warning descr="Immutable object is passed where mutable is expected">list</warning>);
|
|
}
|
|
}
|