diff --git a/java/typeMigration/src/com/intellij/refactoring/typeMigration/inspections/MigrateAssertToMatcherAssertInspection.java b/java/typeMigration/src/com/intellij/refactoring/typeMigration/inspections/MigrateAssertToMatcherAssertInspection.java index d3d79e82219b..5e64bdc7c303 100644 --- a/java/typeMigration/src/com/intellij/refactoring/typeMigration/inspections/MigrateAssertToMatcherAssertInspection.java +++ b/java/typeMigration/src/com/intellij/refactoring/typeMigration/inspections/MigrateAssertToMatcherAssertInspection.java @@ -25,7 +25,6 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.psi.*; -import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PsiTreeUtil; @@ -51,11 +50,11 @@ public class MigrateAssertToMatcherAssertInspection extends LocalInspectionTool private final static Map> ASSERT_METHODS = new HashMap>(); static { - ASSERT_METHODS.put("assertArrayEquals", Pair.create("$a$, $b$", "$a$, org.hamcrest.CoreMatchers.is($b$)")); - ASSERT_METHODS.put("assertEquals", Pair.create("$a$, $b$", "$a$, org.hamcrest.CoreMatchers.is($b$)")); - ASSERT_METHODS.put("assertNotEquals", Pair.create("$a$, $b$", "$a$, org.hamcrest.CoreMatchers.not(org.hamcrest.CoreMatchers.is($b$))")); - ASSERT_METHODS.put("assertSame", Pair.create("$a$, $b$", "$a$, org.hamcrest.CoreMatchersSame.sameInstance($b$)")); - ASSERT_METHODS.put("assertNotSame", Pair.create("$a$, $b$", "$a$, org.hamcrest.CoreMatchers.not(org.hamcrest.CoreMatchersSame.sameInstance($b$))")); + ASSERT_METHODS.put("assertArrayEquals", Pair.create("$expected$, $actual$", "$actual$, org.hamcrest.CoreMatchers.is($expected$)")); + ASSERT_METHODS.put("assertEquals", Pair.create("$expected$, $actual$", "$actual$, org.hamcrest.CoreMatchers.is($expected$)")); + ASSERT_METHODS.put("assertNotEquals", Pair.create("$expected$, $actual$", "$actual$, org.hamcrest.CoreMatchers.not(org.hamcrest.CoreMatchers.is($expected$))")); + ASSERT_METHODS.put("assertSame", Pair.create("$expected$, $actual$", "$actual$, org.hamcrest.CoreMatchersSame.sameInstance($expected$)")); + ASSERT_METHODS.put("assertNotSame", Pair.create("$expected$, $actual$", "$actual$, org.hamcrest.CoreMatchers.not(org.hamcrest.CoreMatchersSame.sameInstance($expected$))")); ASSERT_METHODS.put("assertNotNull", Pair.create("$obj$", "$obj$, org.hamcrest.CoreMatchers.notNullValue()")); ASSERT_METHODS.put("assertNull", Pair.create("$obj$", "$obj$, org.hamcrest.CoreMatchers.nullValue()")); ASSERT_METHODS.put("assertTrue", Pair.create("$cond$", "$cond$, org.hamcrest.CoreMatchers.is(true)")); @@ -73,6 +72,9 @@ public class MigrateAssertToMatcherAssertInspection extends LocalInspectionTool @NotNull @Override public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { + if (JavaPsiFacade.getInstance(holder.getProject()).findClass("org.hamcrest.CoreMatchers", holder.getFile().getResolveScope()) == null) { + return PsiElementVisitor.EMPTY_VISITOR; + } return new JavaElementVisitor() { @Override public void visitMethodCallExpression(PsiMethodCallExpression expression) { @@ -88,7 +90,7 @@ public class MigrateAssertToMatcherAssertInspection extends LocalInspectionTool return; } holder - .registerProblem(expression, "Assert expression #ref can be replaced with 'assertThat' call #loc", new MyQuickFix()); + .registerProblem(expression.getMethodExpression(), "Assert expression #ref can be replaced with 'assertThat' call #loc", new MyQuickFix()); } }; } @@ -110,7 +112,9 @@ public class MigrateAssertToMatcherAssertInspection extends LocalInspectionTool @Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { - final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)descriptor.getPsiElement(); + final PsiElement element = descriptor.getPsiElement(); + if (element == null || !element.isValid() || !(element.getParent() instanceof PsiMethodCallExpression)) return; + final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)element.getParent(); final PsiMethod method = methodCall.resolveMethod(); if (method == null) { return; @@ -171,7 +175,11 @@ public class MigrateAssertToMatcherAssertInspection extends LocalInspectionTool } final boolean hasMessage = hasMessage(method); final String searchTemplate = "'Assert*." + method.getName() + "(" + (hasMessage ? "$msg$, " : "") + templatePair.getFirst() + ")"; - final String replaceTemplate = "$Assert$.assertThat(" + (hasMessage ? "$msg$, " : "") + templatePair.getSecond() + ")"; + final PsiClass containingClass = method.getContainingClass(); + LOG.assertTrue(containingClass != null); + final String qualifier = containingClass.getQualifiedName(); + LOG.assertTrue(qualifier != null); + final String replaceTemplate = qualifier + ".assertThat(" + (hasMessage ? "$msg$, " : "") + templatePair.getSecond() + ")"; return Pair.create(searchTemplate, replaceTemplate); } diff --git a/java/typeMigration/testData/inspections/migrateAssertsToAssertThat/all_after.java b/java/typeMigration/testData/inspections/migrateAssertsToAssertThat/all_after.java index ffcc6da6815b..85d04e7b152c 100644 --- a/java/typeMigration/testData/inspections/migrateAssertsToAssertThat/all_after.java +++ b/java/typeMigration/testData/inspections/migrateAssertsToAssertThat/all_after.java @@ -29,13 +29,13 @@ public class TestCase { void m3(Collection c, Object o) { Assert.assertThat(o, anyOf(c)); - Assert.assertThat(c, is(o)); - Assert.assertThat("msg", c, is(o)); + Assert.assertThat(o, is(c)); + Assert.assertThat("msg", o, is(c)); Assert.assertThat(c, notNullValue()); Assert.assertThat(c, nullValue()); } void m(int[] a, int[] b) { - Assert.assertThat(a, is(b)); + Assert.assertThat(b, is(a)); } } \ No newline at end of file