Files
openide/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/data/Test01.java
T
Tagir Valeev c41245708f Bytecode analysis: purity inference improvement (also fixes IDEA-172989):
1. Lambda/method reference creation is pure
2. String concatenation is pure
3. Constructor which only modifies own fields (calls setters, etc.) is pure
4. Exception creation is pure
5. A few hardcoded native methods
2017-05-18 10:52:53 +07:00

158 lines
4.0 KiB
Java

/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection.bytecodeAnalysis.data;
import com.intellij.codeInspection.bytecodeAnalysis.ExpectContract;
import com.intellij.codeInspection.bytecodeAnalysis.ExpectNotNull;
import java.lang.reflect.Array;
/**
* @author lambdamix
*/
public class Test01 {
static void f(@ExpectNotNull Object o1, @ExpectNotNull Object o2) {
if (o1 == null) throw new NullPointerException();
else s(o2, o2);
}
static void g(@ExpectNotNull Object o, boolean b) {
if (b) f(o, o);
else s(o, o);
}
static void s(@ExpectNotNull Object o1, Object o2) {
t(o1);
v(o2);
}
static void t(@ExpectNotNull Object o) {
o.toString();
}
@ExpectContract(pure = true)
static void v(Object o) {
}
@ExpectContract("null->null")
static String toString1(Object o) {
return o == null ? null : o.toString();
}
@ExpectContract("null->!null")
static String toString2(Object o) {
return o == null ? "null" : o.toString();
}
@ExpectContract(pure = true)
@ExpectNotNull
static String constantString() {
return "s";
}
@ExpectContract(value = "!null->!null;null->null", pure = true)
static String idString(String s) {
return s;
}
@ExpectContract(pure = true)
@ExpectNotNull
public Test01 getThis() {
return this;
}
@ExpectContract(pure = true)
@ExpectNotNull
protected Test01 createRoot() {
return new Test01();
}
@ExpectContract(value = "!null->false;null->true", pure = true)
static boolean isNull(Object o) {
return o == null;
}
@ExpectContract(value = "!null->true;null->false", pure = true)
static boolean isNotNull(Object o) {
return !isNull(o);
}
interface MySupplier {
String get();
}
@ExpectNotNull
@ExpectContract(pure = true)
public static MySupplier lambda(@ExpectNotNull String s) {
return () -> s.trim();
}
@ExpectNotNull
@ExpectContract(pure = true)
public MySupplier lambdaNonStatic(@ExpectNotNull String s) {
return () -> getThis().hashCode() + s.trim();
}
@ExpectNotNull
public MySupplier lambdaBranching(@ExpectNotNull String s, String t, boolean b) {
if(b) {
System.out.println(s);
} else {
System.out.println(t);
}
return () -> s.trim();
}
@ExpectNotNull
@ExpectContract(pure = true)
public static MySupplier methodReference(@ExpectNotNull String s) {
return s::trim;
}
@ExpectContract(pure = true)
public static void assertNotNull(@ExpectNotNull Object obj, String message) {
if(obj == null) {
throw new IllegalArgumentException(message);
}
}
@ExpectNotNull
@ExpectContract(pure = true)
public static long[] copyOfRange(@ExpectNotNull long[] arr, int from, int to) {
int diff = to - from;
if (diff < 0) {
throw new IllegalArgumentException("Invalid arguments: " + from + '>' + to);
}
long[] copy = new long[diff];
System.arraycopy(arr, from, copy, 0, Math.min(arr.length - from, diff));
return copy;
}
@ExpectContract(pure = true)
public static <I, O> O[] copyOfRangeObject(@ExpectNotNull I[] arr, int from, int to, @ExpectNotNull Class<? extends O[]> newType) {
int diff = to - from;
if (diff < 0) {
throw new IllegalArgumentException("Invalid arguments: " + from + '>' + to);
}
@SuppressWarnings("unchecked")
O[] copy = (O[]) Array.newInstance(newType.getComponentType(), diff);
//noinspection SuspiciousSystemArraycopy
System.arraycopy(arr, from, copy, 0, Math.min(arr.length - from, diff));
return copy;
}
}