IDEA-126173 Specify exception type thrown by @NotNull annotations

This commit is contained in:
peter
2014-07-18 09:14:07 +02:00
parent f4d3703103
commit a7ec8b7660
9 changed files with 118 additions and 50 deletions
@@ -0,0 +1,5 @@
import org.jetbrains.annotations.NotNull;
public class CustomExceptionType {
public void foo(Object obj, @NotNull(exception = NullPointerException.class) Object obj2) { }
}
@@ -0,0 +1,8 @@
import org.jetbrains.annotations.*;
class Test {
public void foo(@NotNull(exception = NullPointerException.class) String a) { }
public void foo2(@NotNull(exception = <warning descr="Custom exception class should have a constructor with a single message parameter of String type">CustomException.class</warning>) String a) { }
}
class CustomException extends Exception {}
@@ -30,6 +30,7 @@ public class NullableStuffInspectionTest extends LightCodeInsightFixtureTestCase
public void testProblems2() throws Exception{ doTest(); }
public void testNullableFieldNotnullParam() throws Exception{ doTest(); }
public void testNotNullFieldNullableParam() throws Exception{ doTest(); }
public void testNotNullCustomException() throws Exception{ doTest(); }
public void testGetterSetterProblems() throws Exception{ doTest(); }
public void testOverriddenMethods() throws Exception{
@@ -67,48 +67,48 @@ public class NotNullVerifyingInstrumenterTest extends UsefulTestCase {
}
public void testSimpleReturn() throws Exception {
Class testClass = prepareTest();
Class<?> testClass = prepareTest();
Object instance = testClass.newInstance();
Method method = testClass.getMethod("test");
verifyCallThrowsException("@NotNull method SimpleReturn.test must not return null", instance, method);
}
public void testSimpleReturnWithMessage() throws Exception {
Class testClass = prepareTest();
Class<?> testClass = prepareTest();
Object instance = testClass.newInstance();
Method method = testClass.getMethod("test");
verifyCallThrowsException("This method cannot return null", instance, method);
}
public void testMultipleReturns() throws Exception {
Class testClass = prepareTest();
Class<?> testClass = prepareTest();
Object instance = testClass.newInstance();
Method method = testClass.getMethod("test", int.class);
verifyCallThrowsException("@NotNull method MultipleReturns.test must not return null", instance, method, 1);
}
public void testSimpleParam() throws Exception {
Class testClass = prepareTest();
Class<?> testClass = prepareTest();
Object instance = testClass.newInstance();
Method method = testClass.getMethod("test", Object.class);
verifyCallThrowsException("Argument 0 for @NotNull parameter of SimpleParam.test must not be null", instance, method, (Object)null);
}
public void testSimpleParamWithMessage() throws Exception {
Class testClass = prepareTest();
Class<?> testClass = prepareTest();
Object instance = testClass.newInstance();
Method method = testClass.getMethod("test", Object.class);
verifyCallThrowsException("SimpleParamWithMessage.test(o) cant be null", instance, method, (Object)null);
}
public void testConstructorParam() throws Exception {
Class testClass = prepareTest();
Class<?> testClass = prepareTest();
Constructor method = testClass.getConstructor(Object.class);
verifyCallThrowsException("Argument 0 for @NotNull parameter of ConstructorParam.<init> must not be null", null, method, (Object)null);
}
public void testConstructorParamWithMessage() throws Exception {
Class testClass = prepareTest();
Class<?> testClass = prepareTest();
Constructor method = testClass.getConstructor(Object.class);
verifyCallThrowsException("ConstructorParam.ConstructorParam.o cant be null", null, method, (Object)null);
}
@@ -132,6 +132,19 @@ public class NotNullVerifyingInstrumenterTest extends UsefulTestCase {
assertNotNull(field);
}
public void testCustomExceptionType() throws Exception {
Class<?> testClass = prepareTest();
try {
testClass.getMethod("foo", Object.class, Object.class).invoke(testClass.newInstance(), null, null);
fail();
}
catch (InvocationTargetException e) {
//noinspection ThrowableResultOfMethodCallIgnored
assertInstanceOf(e.getCause(), NullPointerException.class);
assertEquals("Argument 1 for @NotNull parameter of CustomExceptionType.foo must not be null", e.getCause().getMessage());
}
}
public void testEnumConstructorSecondParam() throws Exception {
Class testClass = prepareTest();
Object field = testClass.getField("Value");