From 184c578f630de5b9ac6001fbc5610981025cdabe Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 28 Nov 2013 18:32:27 +0400 Subject: [PATCH 1/4] fixed PY-11500 False positive in method may be static inspection for decorated methods --- .../PyMethodMayBeStaticInspection.java | 4 +++- .../PyMethodMayBeStaticInspection/decorated.py | 15 +++++++++++++++ .../PyMethodMayBeStaticInspectionTest.java | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 python/testData/inspections/PyMethodMayBeStaticInspection/decorated.py diff --git a/python/src/com/jetbrains/python/inspections/PyMethodMayBeStaticInspection.java b/python/src/com/jetbrains/python/inspections/PyMethodMayBeStaticInspection.java index 2bc6424a5b6c..71f2772dae08 100644 --- a/python/src/com/jetbrains/python/inspections/PyMethodMayBeStaticInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyMethodMayBeStaticInspection.java @@ -70,7 +70,9 @@ public class PyMethodMayBeStaticInspection extends PyInspection { if (!supers.isEmpty()) return; final Collection overrides = PyOverridingMethodsSearch.search(node, true).findAll(); if (!overrides.isEmpty()) return; - if (PyUtil.isDecoratedAsAbstract(node) || node.getModifier() != null) return; + final PyDecoratorList decoratorList = node.getDecoratorList(); + if (decoratorList != null) return; + if (node.getModifier() != null) return; final Property property = containingClass.findPropertyByCallable(node); if (property != null) return; diff --git a/python/testData/inspections/PyMethodMayBeStaticInspection/decorated.py b/python/testData/inspections/PyMethodMayBeStaticInspection/decorated.py new file mode 100644 index 000000000000..c16da1e807cc --- /dev/null +++ b/python/testData/inspections/PyMethodMayBeStaticInspection/decorated.py @@ -0,0 +1,15 @@ +def bar(f): + def wrapper(self, *args, **kwargs): + print('running {cls}.{method}'.format(cls=type(self).__name__, + method=f.__name__)) + return f(self, *args, **kwargs) + return wrapper + + +class C(object): + @bar + def foo(self): # False positive: self is used by @bar + return 'foo' + + +C().foo() \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/inspections/PyMethodMayBeStaticInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyMethodMayBeStaticInspectionTest.java index ea40830d38dc..885f0430ae73 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyMethodMayBeStaticInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyMethodMayBeStaticInspectionTest.java @@ -15,6 +15,7 @@ */ package com.jetbrains.python.inspections; +import com.intellij.testFramework.TestDataPath; import com.jetbrains.python.PythonTestUtil; import com.jetbrains.python.fixtures.PyTestCase; @@ -23,6 +24,7 @@ import java.util.Arrays; /** * User: ktisha */ +@TestDataPath("$CONTENT_ROOT/../testData/inspections/PyMethodMayBeStaticInspection/") public class PyMethodMayBeStaticInspectionTest extends PyTestCase { public void testTruePositive() { @@ -65,6 +67,10 @@ public class PyMethodMayBeStaticInspectionTest extends PyTestCase { doTest(); } + public void testDecorated() { + doTest(); + } + public void testOverwrittenMethod() { doTest(); } From 43b08e428274d547caee092551d6628289941c2b Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 2 Dec 2013 15:10:55 +0100 Subject: [PATCH 2/4] fix GenerateGetterSetterTest formatting expectations --- .../intellij/codeInsight/GenerateGetterSetterTest.groovy | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/GenerateGetterSetterTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/GenerateGetterSetterTest.groovy index bc735f932d7a..07fee955af1f 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/GenerateGetterSetterTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/GenerateGetterSetterTest.groovy @@ -42,7 +42,8 @@ class Foo { public YesNoRAMField getIsStateForceMailField() { return isStateForceMailField; - }} + } +} ''' } @@ -61,7 +62,8 @@ class Foo { public boolean isStateForceMailField() { return isStateForceMailField; - }} + } +} ''' } From 3e946cce6e9614c223d1809c801d39840e78046c Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Mon, 2 Dec 2013 16:44:11 +0400 Subject: [PATCH 3/4] IDEA-117389 Groovy: Introduce constant from string part --- .../constant/GrIntroduceConstantHandler.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/constant/GrIntroduceConstantHandler.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/constant/GrIntroduceConstantHandler.java index 9d03af8b5a78..95db618dbd6a 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/constant/GrIntroduceConstantHandler.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/constant/GrIntroduceConstantHandler.java @@ -15,6 +15,7 @@ */ package org.jetbrains.plugins.groovy.refactoring.introduce.constant; +import com.intellij.openapi.util.Ref; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.HelpID; @@ -93,9 +94,14 @@ public class GrIntroduceConstantHandler extends GrIntroduceFieldHandlerBase getIntroducer(@NotNull GrIntroduceContext context, - OccurrencesChooser.ReplaceChoice choice) { - return new GrInplaceConstantIntroducer(context, choice); + protected GrAbstractInplaceIntroducer getIntroducer(@NotNull GrIntroduceContext context, @NotNull OccurrencesChooser.ReplaceChoice choice) { + final Ref contextRef = Ref.create(context); + + if (context.getStringPart() != null) { + extractStringPart(contextRef); + } + + return new GrInplaceConstantIntroducer(contextRef.get(), choice); } private static class ConstantChecker extends GroovyRecursiveElementVisitor { From 44b70b3f2bf1f0eeeefc228186b1ad31c14fdb5c Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Mon, 2 Dec 2013 16:52:43 +0400 Subject: [PATCH 4/4] Groovy: introduce parameter: invoke in-place mode only when extracting from expression --- .../parameter/GrIntroduceParameterHandler.java | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/parameter/GrIntroduceParameterHandler.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/parameter/GrIntroduceParameterHandler.java index 8392482441b3..4b81a28734a4 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/parameter/GrIntroduceParameterHandler.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/parameter/GrIntroduceParameterHandler.java @@ -220,9 +220,6 @@ public class GrIntroduceParameterHandler implements RefactoringActionHandler, Me for (PsiElement element : context.getOccurrences()) { occurrences.add(createRange(document, element)); } - RangeMarker expressionRangeMarker = createRange(document, context.getExpression()); - RangeMarker stringPartRangeMarker = createRange(document, context.getStringPart()); - RangeMarker varRangeMarker = createRange(document, context.getVar()); GrExpressionWrapper expr = new GrExpressionWrapper(GroovyIntroduceParameterUtil.findExpr(settings)); @@ -249,8 +246,7 @@ public class GrIntroduceParameterHandler implements RefactoringActionHandler, Me GrVariable parameter = pointer != null ? pointer.getElement() : null; if (parameter != null) { - GrInplaceIntroducer introducer = getIntroducer(parameter, context, settings, occurrences, varRangeMarker, expressionRangeMarker, stringPartRangeMarker, - expr); + GrInplaceIntroducer introducer = getIntroducer(parameter, context, settings, occurrences, expr); PsiDocumentManager.getInstance(info.getProject()).doPostponedOperationsAndUnblockDocument(context.getEditor().getDocument()); introducer.performInplaceRefactoring(introducer.suggestNames(context)); } @@ -263,11 +259,7 @@ public class GrIntroduceParameterHandler implements RefactoringActionHandler, Me GrIntroduceContext context, GrIntroduceParameterSettings settings, List occurrences, - RangeMarker varRangeMarker, - RangeMarker expressionRangeMarker, - RangeMarker stringPartRangeMarker, GrExpressionWrapper expr) { - //return new GrInplaceVariableIntroducer(parameter, context.getEditor(), context.getProject(), REFACTORING_NAME, occurrences, parameter); return new GrInplaceParameterIntroducer(parameter, context.getEditor(), context.getProject(), REFACTORING_NAME, occurrences, context.getPlace(), settings, expr); } @@ -298,11 +290,7 @@ public class GrIntroduceParameterHandler implements RefactoringActionHandler, Me } private static boolean isInplace(IntroduceParameterInfo info, Editor editor) { - GrExpression expr = GroovyIntroduceParameterUtil.findExpr(info); - GrVariable var = GroovyIntroduceParameterUtil.findVar(info); - StringPartInfo stringPart = info.getStringPartInfo(); - - return (expr != null || var != null || stringPart != null) && GrIntroduceHandlerBase.isInplace(editor, info.getContext()); + return GroovyIntroduceParameterUtil.findExpr(info) != null && GrIntroduceHandlerBase.isInplace(editor, info.getContext()); }