mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Added new generic introduce variable fix for IDEA-89625 (Create a new inspection to report on method calls inside loop conditions.)
This commit is contained in:
@@ -1359,6 +1359,7 @@ c.style.array.declaration.replace.quickfix=Replace with Java-style array declara
|
||||
chained.method.call.ignore.option=Ignore chained method calls in field initializers
|
||||
chained.method.call.ignore.this.super.option=Ignore chained method calls in 'this()' and 'super()' calls
|
||||
introduce.variable.quickfix=Introduce variable
|
||||
introduce.variable.may.change.semantics.quickfix=Introduce variable (may change semantics)
|
||||
flip.comparison.quickfix=Flip comparison
|
||||
control.flow.statement.without.braces.add.quickfix=Add braces
|
||||
extends.object.remove.quickfix=Remove redundant 'extends Object'
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.siyeh.ig.fixes;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.ide.DataManager;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.AsyncResult;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiMethodCallExpression;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringActionHandlerFactory;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author Bas Leijdekkers
|
||||
*/
|
||||
public class IntroduceVariableFix extends InspectionGadgetsFix {
|
||||
|
||||
private final boolean myMayChangeSemantics;
|
||||
|
||||
public IntroduceVariableFix(boolean mayChangeSemantics) {
|
||||
myMayChangeSemantics = mayChangeSemantics;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
if (myMayChangeSemantics) {
|
||||
return InspectionGadgetsBundle.message("introduce.variable.may.change.semantics.quickfix");
|
||||
} else {
|
||||
return InspectionGadgetsBundle.message("introduce.variable.quickfix");
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PsiExpression getExpressionToExtract(PsiElement element) {
|
||||
return PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFix(final Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
|
||||
final PsiExpression expression = getExpressionToExtract(descriptor.getPsiElement());
|
||||
if (expression == null) {
|
||||
return;
|
||||
}
|
||||
final RefactoringActionHandler handler = JavaRefactoringActionHandlerFactory.getInstance().createIntroduceVariableHandler();
|
||||
final AsyncResult<DataContext> dataContextContainer = DataManager.getInstance().getDataContextFromFocus();
|
||||
dataContextContainer.doWhenDone(new AsyncResult.Handler<DataContext>() {
|
||||
@Override
|
||||
public void run(DataContext dataContext) {
|
||||
handler.invoke(project, new PsiElement[]{expression}, dataContext);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+23
-11
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,28 +19,42 @@ import com.intellij.psi.*;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.fixes.IntroduceVariableFix;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class MethodCallInLoopConditionInspection extends BaseInspection {
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"method.call.in.loop.condition.display.name");
|
||||
return InspectionGadgetsBundle.message("method.call.in.loop.condition.display.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"method.call.in.loop.condition.problem.descriptor");
|
||||
return InspectionGadgetsBundle.message("method.call.in.loop.condition.problem.descriptor");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new IntroduceVariableFix(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseInspectionVisitor buildVisitor() {
|
||||
return new MethodCallInLoopConditionVisitor();
|
||||
}
|
||||
|
||||
private static class MethodCallInLoopConditionVisitor
|
||||
extends BaseInspectionVisitor {
|
||||
private static class MethodCallInLoopConditionVisitor extends BaseInspectionVisitor {
|
||||
|
||||
@Override
|
||||
public void visitForStatement(@NotNull PsiForStatement statement) {
|
||||
@@ -63,8 +77,7 @@ public class MethodCallInLoopConditionInspection extends BaseInspection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitDoWhileStatement(
|
||||
@NotNull PsiDoWhileStatement statement) {
|
||||
public void visitDoWhileStatement(@NotNull PsiDoWhileStatement statement) {
|
||||
super.visitDoWhileStatement(statement);
|
||||
final PsiExpression condition = statement.getCondition();
|
||||
if (condition == null) {
|
||||
@@ -74,8 +87,7 @@ public class MethodCallInLoopConditionInspection extends BaseInspection {
|
||||
}
|
||||
|
||||
private void checkForMethodCalls(PsiExpression condition) {
|
||||
final PsiElementVisitor visitor =
|
||||
new JavaRecursiveElementVisitor() {
|
||||
final PsiElementVisitor visitor = new JavaRecursiveElementVisitor() {
|
||||
|
||||
@Override
|
||||
public void visitMethodCallExpression(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,23 +15,18 @@
|
||||
*/
|
||||
package com.siyeh.ig.style;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
|
||||
import com.intellij.ide.DataManager;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringActionHandlerFactory;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.fixes.IntroduceVariableFix;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.ParenthesesUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
@@ -46,27 +41,20 @@ public class ChainedMethodCallInspection extends BaseInspection {
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"chained.method.call.display.name");
|
||||
return InspectionGadgetsBundle.message("chained.method.call.display.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
protected String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"chained.method.call.problem.descriptor");
|
||||
return InspectionGadgetsBundle.message("chained.method.call.problem.descriptor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
final MultipleCheckboxOptionsPanel panel =
|
||||
new MultipleCheckboxOptionsPanel(this);
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message(
|
||||
"chained.method.call.ignore.option"),
|
||||
"m_ignoreFieldInitializations");
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message(
|
||||
"chained.method.call.ignore.this.super.option"),
|
||||
"m_ignoreThisSuperCalls");
|
||||
final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message("chained.method.call.ignore.option"), "m_ignoreFieldInitializations");
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message("chained.method.call.ignore.this.super.option"), "m_ignoreThisSuperCalls");
|
||||
return panel;
|
||||
}
|
||||
|
||||
@@ -82,55 +70,26 @@ public class ChainedMethodCallInspection extends BaseInspection {
|
||||
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new ChainedMethodCallFix();
|
||||
}
|
||||
|
||||
private static class ChainedMethodCallFix extends InspectionGadgetsFix {
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"introduce.variable.quickfix");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFix(final Project project, ProblemDescriptor descriptor) {
|
||||
final JavaRefactoringActionHandlerFactory factory =
|
||||
JavaRefactoringActionHandlerFactory.getInstance();
|
||||
final RefactoringActionHandler introduceHandler =
|
||||
factory.createIntroduceVariableHandler();
|
||||
final PsiElement methodNameElement = descriptor.getPsiElement();
|
||||
final PsiReferenceExpression methodCallExpression =
|
||||
(PsiReferenceExpression)methodNameElement.getParent();
|
||||
assert methodCallExpression != null;
|
||||
final PsiExpression qualifier =
|
||||
methodCallExpression.getQualifierExpression();
|
||||
final DataManager dataManager = DataManager.getInstance();
|
||||
final DataContext dataContext = dataManager.getDataContext();
|
||||
final Runnable runnable = new Runnable() {
|
||||
public void run() {
|
||||
introduceHandler.invoke(project,
|
||||
new PsiElement[]{qualifier}, dataContext);
|
||||
return new IntroduceVariableFix(false) {
|
||||
@Nullable
|
||||
@Override
|
||||
public PsiExpression getExpressionToExtract(PsiElement element) {
|
||||
final PsiElement parent = element.getParent();
|
||||
if (!(parent instanceof PsiReferenceExpression)) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
runnable.run();
|
||||
final PsiReferenceExpression methodExpression = (PsiReferenceExpression)parent;
|
||||
return methodExpression.getQualifierExpression();
|
||||
}
|
||||
else {
|
||||
ApplicationManager.getApplication().invokeLater(runnable,
|
||||
project.getDisposed());
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private class ChainedMethodCallVisitor extends BaseInspectionVisitor {
|
||||
|
||||
@Override
|
||||
public void visitMethodCallExpression(
|
||||
@NotNull PsiMethodCallExpression expression) {
|
||||
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
|
||||
super.visitMethodCallExpression(expression);
|
||||
final PsiReferenceExpression reference =
|
||||
expression.getMethodExpression();
|
||||
final PsiReferenceExpression reference = expression.getMethodExpression();
|
||||
final PsiExpression qualifier = reference.getQualifierExpression();
|
||||
if (qualifier == null) {
|
||||
return;
|
||||
@@ -139,16 +98,13 @@ public class ChainedMethodCallInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
if (m_ignoreFieldInitializations) {
|
||||
final PsiElement field =
|
||||
PsiTreeUtil.getParentOfType(expression, PsiField.class);
|
||||
final PsiElement field = PsiTreeUtil.getParentOfType(expression, PsiField.class);
|
||||
if (field != null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (m_ignoreThisSuperCalls) {
|
||||
final PsiExpressionList expressionList =
|
||||
PsiTreeUtil.getParentOfType(expression,
|
||||
PsiExpressionList.class);
|
||||
final PsiExpressionList expressionList = PsiTreeUtil.getParentOfType(expression, PsiExpressionList.class);
|
||||
if (expressionList != null) {
|
||||
final PsiElement parent = expressionList.getParent();
|
||||
if (ExpressionUtils.isConstructorInvocation(parent)) {
|
||||
@@ -161,8 +117,7 @@ public class ChainedMethodCallInspection extends BaseInspection {
|
||||
|
||||
private boolean isCallExpression(PsiExpression expression) {
|
||||
expression = ParenthesesUtils.stripParentheses(expression);
|
||||
return expression instanceof PsiMethodCallExpression ||
|
||||
expression instanceof PsiNewExpression;
|
||||
return expression instanceof PsiMethodCallExpression || expression instanceof PsiNewExpression;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,24 +15,18 @@
|
||||
*/
|
||||
package com.siyeh.ig.style;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
|
||||
import com.intellij.ide.DataManager;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringActionHandlerFactory;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.fixes.IntroduceVariableFix;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.*;
|
||||
|
||||
public class NestedMethodCallInspection extends BaseInspection {
|
||||
|
||||
@@ -44,22 +38,18 @@ public class NestedMethodCallInspection extends BaseInspection {
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"nested.method.call.display.name");
|
||||
return InspectionGadgetsBundle.message("nested.method.call.display.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
protected String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"nested.method.call.problem.descriptor");
|
||||
return InspectionGadgetsBundle.message("nested.method.call.problem.descriptor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
return new SingleCheckboxOptionsPanel(
|
||||
InspectionGadgetsBundle.message(
|
||||
"nested.method.call.ignore.option"),
|
||||
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("nested.method.call.ignore.option"),
|
||||
this, "m_ignoreFieldInitializations");
|
||||
}
|
||||
|
||||
@@ -70,7 +60,7 @@ public class NestedMethodCallInspection extends BaseInspection {
|
||||
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new NestedMethodCallFix();
|
||||
return new IntroduceVariableFix(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -78,55 +68,13 @@ public class NestedMethodCallInspection extends BaseInspection {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static class NestedMethodCallFix extends InspectionGadgetsFix {
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"introduce.variable.quickfix");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFix(final Project project, ProblemDescriptor descriptor) {
|
||||
final JavaRefactoringActionHandlerFactory factory =
|
||||
JavaRefactoringActionHandlerFactory.getInstance();
|
||||
final RefactoringActionHandler introduceHandler =
|
||||
factory.createIntroduceVariableHandler();
|
||||
final PsiElement methodNameElement = descriptor.getPsiElement();
|
||||
final PsiElement methodExpression = methodNameElement.getParent();
|
||||
if (methodExpression == null) {
|
||||
return;
|
||||
}
|
||||
final PsiElement methodCallExpression =
|
||||
methodExpression.getParent();
|
||||
final DataManager dataManager = DataManager.getInstance();
|
||||
final DataContext dataContext = dataManager.getDataContext();
|
||||
final Runnable runnable = new Runnable() {
|
||||
public void run() {
|
||||
introduceHandler.invoke(project,
|
||||
new PsiElement[]{methodCallExpression}, dataContext);
|
||||
}
|
||||
};
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
runnable.run();
|
||||
}
|
||||
else {
|
||||
ApplicationManager.getApplication().invokeLater(runnable,
|
||||
project.getDisposed());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class NestedMethodCallVisitor extends BaseInspectionVisitor {
|
||||
|
||||
@Override
|
||||
public void visitMethodCallExpression(
|
||||
@NotNull PsiMethodCallExpression expression) {
|
||||
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
|
||||
super.visitMethodCallExpression(expression);
|
||||
PsiExpression outerExpression = expression;
|
||||
while (outerExpression != null &&
|
||||
outerExpression.getParent() instanceof PsiExpression) {
|
||||
while (outerExpression != null && outerExpression.getParent() instanceof PsiExpression) {
|
||||
outerExpression = (PsiExpression)outerExpression.getParent();
|
||||
}
|
||||
if (outerExpression == null) {
|
||||
@@ -146,8 +94,7 @@ public class NestedMethodCallInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
if (m_ignoreFieldInitializations) {
|
||||
final PsiElement field =
|
||||
PsiTreeUtil.getParentOfType(expression, PsiField.class);
|
||||
final PsiElement field = PsiTreeUtil.getParentOfType(expression, PsiField.class);
|
||||
if (field != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user