From 650fb6da9e4fc3b8da4651808a2c01940f0f23c0 Mon Sep 17 00:00:00 2001 From: "Maxim.Medvedev" Date: Sun, 6 May 2012 17:23:45 +0400 Subject: [PATCH] highlight incorrect throw statements --- .../GroovyAssignabilityCheckInspection.java | 11 +++++++ .../groovy/lang/GroovyHighlightingTest.groovy | 32 +++++++++++++------ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/assignment/GroovyAssignabilityCheckInspection.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/assignment/GroovyAssignabilityCheckInspection.java index d12ed7428ad5..6551ffc30a0c 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/assignment/GroovyAssignabilityCheckInspection.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/assignment/GroovyAssignabilityCheckInspection.java @@ -46,6 +46,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArg import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock; import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock; import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.*; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty; @@ -349,6 +350,16 @@ public class GroovyAssignabilityCheckInspection extends BaseInspection { } } + @Override + public void visitThrowStatement(GrThrowStatement throwStatement) { + super.visitThrowStatement(throwStatement); + + final GrExpression exception = throwStatement.getException(); + if (exception != null) { + checkAssignability(PsiType.getJavaLangThrowable(throwStatement.getManager(), throwStatement.getResolveScope()), exception, exception); + } + } + private boolean checkLiteralConstructorApplicability(GroovyResolveResult result, GrListOrMap listOrMap, boolean checkUnknownArgs) { final PsiElement element = result.getElement(); LOG.assertTrue(element instanceof PsiMethod && ((PsiMethod)element).isConstructor()); diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy index 40b5b5007a4b..2e24f7a14738 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy @@ -867,7 +867,7 @@ C< } public void testRawClosureReturnType() { - myFixture.configureByText('_.groovy', '''\ + testHighlighting('''\ class A { A(T t) {this.t = t} @@ -880,17 +880,17 @@ class A { def a = new A(new Date()) Date d = a.cl() -''') - testHighlighting(GroovyUncheckedAssignmentOfMemberOfRawTypeInspection) +''', GroovyUncheckedAssignmentOfMemberOfRawTypeInspection) } - private void testHighlighting(Class... inspections) { + private void testHighlighting(String text, Class... inspections) { + myFixture.configureByText('_.groovy', text) myFixture.enableInspections(inspections) myFixture.testHighlighting(true, false, true) } void testMethodRefs1() { - myFixture.configureByText('_.groovy', '''\ + testHighlighting('''\ class A { int foo(){2} @@ -903,12 +903,11 @@ int i = foo() int i2 = foo(2) Date d = foo(2) Date d2 = foo() -''') - testHighlighting(GroovyAssignabilityCheckInspection) +''', GroovyAssignabilityCheckInspection) } void testMethodRefs2() { - myFixture.configureByText('_.groovy', '''\ + testHighlighting('''\ class Bar { def foo(int i, String s2) {s2} def foo(int i, int i2) {i2} @@ -920,8 +919,21 @@ String s = cl("2") int s2 = cl("2") int i = cl(3) String i2 = cl(3) -''') - testHighlighting(GroovyAssignabilityCheckInspection) +''', GroovyAssignabilityCheckInspection) } + void testThrowObject() { + testHighlighting('''\ +def foo() { + throw new RuntimeException() +} +def bar () { + throw new Object() +} + +def test() { + throw new Throwable() +} +''', GroovyAssignabilityCheckInspection) + } } \ No newline at end of file