IDEA-112222 Validate @Contract annotation is related to the code

This commit is contained in:
peter
2014-06-01 17:20:57 +02:00
parent b09762d64c
commit 5d8ee45b19
21 changed files with 547 additions and 149 deletions
@@ -0,0 +1,12 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class Foo {
@Contract("!null,true->!null")
String delegationToInstance(@NotNull Foo f, boolean createIfNeeded) { return f.getString(createIfNeeded); }
@Contract("true->!null")
String getString(boolean createIfNeeded) { return createIfNeeded ? "" : null; }
}
@@ -0,0 +1,32 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
abstract class Foo {
abstract String getString();
@Contract("null -> null;!null -> !null")
public static String delegate(@Nullable String s) {
return s == null ? null : s.substring(1);
}
@Contract("null -> null;!null -> !null")
public static String callee(@Nullable Foo element) {
return element == null ? null : delegate(element.getString());
}
@Contract("!null -> !null")
@Nullable public static String delegate2(@Nullable String s) {
return s == null ? null : s.substring(1);
}
@Contract("!null -> !null")
@Nullable public static String callee2(@Nullable Object element) {
if (element instanceof Foo) return delegate2(((Foo)element).getString());
if (element != null) return element.toString();
return null;
}
}
@@ -0,0 +1,25 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
abstract class Foo {
public static final String CONSTANT = getSomeString();
static native String getSomeString();
@Contract("null -> false")
static boolean isConstant(@Nullable String s) {
return s == CONSTANT;
}
@Contract("null -> false")
static boolean isSomeString(@Nullable String s) {
return s == getSomeString();
}
@Contract("null,_ -> false")
static boolean isParameter(@Nullable String s, String param) {
return s == param;
}
}
@@ -0,0 +1,17 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class Foo {
@Contract("!null,true->!null")
String delegationToInstance(@NotNull Foo f, boolean createIfNeeded) {
return <warning descr="Contract clause '!null, true -> !null' is violated: exception might be thrown instead of returning !null">f.getString(createIfNeeded)</warning>;
}
@Contract("true->fail")
String getString(boolean fail) {
if (fail) throw new RuntimeException();
return "a";
}
}
@@ -0,0 +1,11 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class Foo {
@Contract("true->fail")
void <warning descr="Contract clause 'true -> fail' is violated: no exception is thrown">assertFalse</warning>(boolean fail) {
}
}
@@ -0,0 +1,11 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
class Foo {
@Nullable
@Contract ( "_ -> null")
String foo(String s) {
return <warning descr="Contract clause '_ -> null' is violated">"42"</warning>;
}
}
@@ -0,0 +1,16 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class Foo {
@Contract("null->false")
boolean plainDelegation(Object x) {
return <warning descr="Contract clause 'null -> false' is violated">bar(2, x)</warning>;
}
@Contract("_,null->true")
boolean bar(int i, @Nullable Object foo) {
return foo == null;
}
}
@@ -0,0 +1,13 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
class Foo {
@Contract("null,_->true")
boolean bar(@Nullable Object foo, int i) {
if (foo == null) {
<warning descr="Contract clause 'null, _ -> true' is violated: exception might be thrown instead of returning true">throw new RuntimeException();</warning>
}
return i == 2;
}
}
@@ -0,0 +1,10 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
class Foo {
@Contract("null->true")
boolean bar(@Nullable Object foo) {
return <warning descr="Contract clause 'null -> true' is violated">foo != null && foo.hashCode() == 3</warning>;
}
}
@@ -0,0 +1,10 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
class Foo {
@Contract("_,null->fail")
boolean bar(int i, @Nullable Object foo) {
return <warning descr="Contract clause '_, null -> fail' is violated">foo == null</warning>;
}
}
@@ -2,15 +2,16 @@ import org.jetbrains.annotations.Contract;
class Foo {
public void main(String[] args) {
public void main(String s) {
for (int i = 0; i < 10; i++) {
assertTrue("str", true);
assertTrue("str", s != null);
s.hashCode();
}
}
@Contract("_, false->fail")
void assertTrue(String msg, boolean value) {
if (!value) throw new RuntimeException();
}
}
@@ -2,6 +2,8 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.RuntimeException;
class Contracts {
public void simpleFail(@Nullable String message) {
@@ -11,12 +13,12 @@ class Contracts {
@Contract("_->fail")
private void notBlank(@Nullable Object message) {
throw new RuntimeException();
}
@Contract("_,_,_->fail")
private void notBlank(@Nullable Object o, String message, Object... args) {
throw new RuntimeException();
}
public void varargFail(@Nullable String message) {