IDEA-192155 Contract checker: highlight the relevant part of contract

This commit is contained in:
Tagir Valeev
2018-05-17 15:34:26 +07:00
parent 7b113f992d
commit 3a8057e248
7 changed files with 239 additions and 52 deletions
@@ -1,7 +1,7 @@
import org.jetbrains.annotations.Contract;
class Zoo {
@Contract(<warning descr="Contract return value 'null': not applicable for constructor">"null->null"</warning> )
@Contract("null-><warning descr="Contract return value 'null': not applicable for constructor">null</warning>" )
Zoo(Object o) {}
@Contract("_->fail" )
@@ -2,13 +2,13 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
class Foo {
@Contract(<warning descr="Contract clause 'null -> null' is unreachable: previous contracts cover all possible cases">"_ -> !null; null -> null"</warning>)
@Contract("_ -> !null; <warning descr="Contract clause 'null -> null' is unreachable: previous contracts cover all possible cases">null -> null</warning>")
public native String nonTrivialAfterTrivial(String x);
@Contract(<warning descr="Contract clause '!null -> null' is never satisfied as its conditions are covered by previous contracts">"!null -> !null; !null -> null"</warning>)
@Contract("!null -> !null; <warning descr="Contract clause '!null -> null' is never satisfied as its conditions are covered by previous contracts">!null -> null</warning>")
public native String repeating(String x);
@Contract(<warning descr="Contract clause 'true, _, _ -> fail' is never satisfied as its conditions are covered by previous contracts">"true, false, _ -> !null; true, true, _ -> null; true, _, _ -> fail"</warning>)
@Contract("true, false, _ -> !null; true, true, _\u0020-> null; <warning descr="Contract clause 'true, _, _ -> fail' is never satisfied as its conditions are covered by previous contracts">true, _, _ -> fail</warning>")
public native String booleanProblem(boolean x, boolean y, String z);
@Contract("true, false, _ -> !null; true, true, _ -> null; false, _, _ -> fail")
@@ -17,6 +17,11 @@ class Foo {
@Contract("true, false, _ -> !null; false, _, _ -> fail; true, true, _ -> null")
public native String booleanOk2(boolean x, boolean y, String z);
@Contract(<warning descr="Contract clause 'null, null, _, null, !null -> fail' is never satisfied as its conditions are covered by previous contracts">"null, null, null, null, null -> null; null, null, !null, null, _ -> null; null, null, null, null, !null -> !null; null, null, _, null, !null -> fail"</warning>)
static final String MY_LOVELY_CONTRACT = "null, null, !null, null, _ -> null; ";
@Contract("null, null, null, null, null -> null; "+
MY_LOVELY_CONTRACT+
"null, null, null, null, !null -> !null; "+
("<warning descr="Contract clause 'null, null, _, null, !null -> fail' is never satisfied as its conditions are covered by previous contracts">null, null, _, null, !null -> fail</warning>"))
public native String test(String a, String b, String c, String d, String e);
}
@@ -2,56 +2,56 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
class Foo {
@Contract(<warning descr="A contract clause must be in form arg1, ..., argN -> return-value">"a"</warning>)
@Contract("<warning descr="A contract clause must be in form arg1, ..., argN -> return-value">a</warning>")
void malformedContract() {}
@Contract(<warning descr="Method takes 2 parameters, while contract clause 'null -> _' expects 1">"null -> _"</warning>)
@Contract("<warning descr="Method takes 2 parameters, while contract clause 'null -> _' expects 1">null -> _</warning>")
void wrongParameterCount(Object a, boolean b) {}
@Contract(pure=true)
void voidPureMethod() {}
@Contract(<warning descr="Contract return value 'null': not applicable for primitive return type 'void'">"->null"</warning>)
@Contract("-><warning descr="Contract return value 'null': not applicable for primitive return type 'void'">null</warning>")
public native void throwMe();
@Contract(<warning descr="Contract return value 'null': not applicable for primitive return type 'boolean'">"->null"</warning>)
@Contract("-><warning descr="Contract return value 'null': not applicable for primitive return type 'boolean'">null</warning>")
public native boolean wrongReturnType();
@Contract(<warning descr="Contract return value 'true': method return type must be 'boolean'">"->true"</warning>)
@Contract("-><warning descr="Contract return value 'true': method return type must be 'boolean'">true</warning>")
public native String wrongReturnType2();
@Contract(<warning descr="Contract return value 'param1': not applicable for method which has 0 parameters">"->param1"</warning>)
@Contract("-><warning descr="Contract return value 'param1': not applicable for method which has 0 parameters">param1</warning>")
public native String absentParameter();
@Contract(<warning descr="Contract return value 'param2': not applicable for method which has 1 parameter">"_->param2"</warning>)
@Contract("_-><warning descr="Contract return value 'param2': not applicable for method which has 1 parameter">param2</warning>")
public native String absentParameter2(String x);
@Contract(<warning descr="Contract return value 'param1': return type 'String' must be assignable from parameter type 'CharSequence'">"_->param1"</warning>)
@Contract("_-><warning descr="Contract return value 'param1': return type 'String' must be assignable from parameter type 'CharSequence'">param1</warning>")
public native String wrongParameterType(CharSequence x);
@Contract("_->param1")
public native Object okParameterType(Integer x);
@Contract(<warning descr="Contract return value 'new': not applicable for primitive return type 'boolean'">"->new"</warning>)
@Contract("-><warning descr="Contract return value 'new': not applicable for primitive return type 'boolean'">new</warning>")
public native boolean wrongReturnTypeNew();
@Contract(<warning descr="Contract return value 'this': not applicable for primitive return type 'boolean'">"->this"</warning>)
@Contract("-><warning descr="Contract return value 'this': not applicable for primitive return type 'boolean'">this</warning>")
public native boolean wrongReturnTypeThis();
@Contract(<warning descr="Contract return value 'this': method return type should be compatible with method containing class">"->this"</warning>)
@Contract("-><warning descr="Contract return value 'this': method return type should be compatible with method containing class">this</warning>")
public native String wrongReturnTypeThis2();
public native Foo okReturnTypeThis();
@Contract(<warning descr="Contract return value 'this': not applicable for static method">"->this"</warning>)
@Contract("-><warning descr="Contract return value 'this': not applicable for static method">this</warning>")
public native static Foo staticThis();
@Contract(<warning descr="Return value should be one of: null, !null, true, false, this, new, paramN, fail, _. Found: foo">"->foo"</warning>)
@Contract("-><warning descr="Return value should be one of: null, !null, true, false, this, new, paramN, fail, _. Found: foo">foo</warning>")
public native void invalidReturn();
@Contract(<warning descr="Contract clause 'true -> fail': parameter #1 has 'String' type (expected boolean)">"true -> fail"</warning>)
@Contract("<warning descr="Contract clause 'true -> fail': parameter #1 has 'String' type (expected boolean)">true</warning> -> fail")
public native void invalidType(String s);
@Contract(<warning descr="Contract clause 'null -> fail': parameter #1 has primitive type 'int'">"null -> fail"</warning>)
@Contract("<warning descr="Contract clause 'null -> fail': parameter #1 has primitive type 'int'">null</warning> -> fail")
public native void invalidType(int s);
}