mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
better quickfix
This commit is contained in:
@@ -1227,7 +1227,7 @@ unused.catch.parameter.ignore.catch.option=Ignore for 'catch' blocks containing
|
||||
unused.catch.parameter.ignore.empty.option=Ignore unused 'catch' parameters in tests
|
||||
add.serialversionuidfield.quickfix=Add 'serialVersionUID' field
|
||||
delete.import.quickfix=Delete unnecessary import
|
||||
encapsulate.variable.quickfix=Encapsulate variable
|
||||
encapsulate.variable.quickfix=Encapsulate field ''{0}''
|
||||
extract.method.quickfix=Extract method
|
||||
inline.call.quickfix=Inline call
|
||||
inline.variable.quickfix=Inline variable
|
||||
|
||||
+4
-3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2009 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2011 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.
|
||||
@@ -42,7 +42,8 @@ public class PackageVisibleFieldInspection extends BaseInspection {
|
||||
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new EncapsulateVariableFix();
|
||||
final PsiField field = (PsiField) infos[0];
|
||||
return new EncapsulateVariableFix(field.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,7 +69,7 @@ public class PackageVisibleFieldInspection extends BaseInspection {
|
||||
field.hasModifierProperty(PsiModifier.FINAL)) {
|
||||
return;
|
||||
}
|
||||
registerFieldError(field);
|
||||
registerFieldError(field, field);
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
-3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2011 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.
|
||||
@@ -26,25 +26,31 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ProtectedFieldInspection extends BaseInspection {
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message("protected.field.display.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"protected.field.problem.descriptor");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new EncapsulateVariableFix();
|
||||
final PsiField field = (PsiField) infos[0];
|
||||
return new EncapsulateVariableFix(field.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseInspectionVisitor buildVisitor() {
|
||||
return new ProtectedFieldVisitor();
|
||||
}
|
||||
@@ -59,7 +65,7 @@ public class ProtectedFieldInspection extends BaseInspection {
|
||||
field.hasModifierProperty(PsiModifier.FINAL)) {
|
||||
return;
|
||||
}
|
||||
registerFieldError(field);
|
||||
registerFieldError(field, field);
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
-3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2011 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.
|
||||
@@ -33,17 +33,20 @@ public class PublicFieldInspection extends BaseInspection {
|
||||
@SuppressWarnings({"PublicField"})
|
||||
public boolean ignoreEnums = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message("public.field.display.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"public.field.problem.descriptor");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public JComponent createOptionsPanel() {
|
||||
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message(
|
||||
@@ -51,14 +54,18 @@ public class PublicFieldInspection extends BaseInspection {
|
||||
"ignoreEnums");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new EncapsulateVariableFix();
|
||||
final PsiField field = (PsiField) infos[0];
|
||||
return new EncapsulateVariableFix(field.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseInspectionVisitor buildVisitor() {
|
||||
return new PublicFieldVisitor();
|
||||
}
|
||||
@@ -87,7 +94,7 @@ public class PublicFieldInspection extends BaseInspection {
|
||||
}
|
||||
}
|
||||
}
|
||||
registerFieldError(field);
|
||||
registerFieldError(field, field);
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-88
@@ -15,18 +15,14 @@
|
||||
*/
|
||||
package com.siyeh.ig.encapsulation;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PropertyUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.fixes.EncapsulateVariableFix;
|
||||
import com.siyeh.ig.psiutils.MethodUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -74,88 +70,7 @@ public class UseOfAnotherObjectsPrivateFieldInspection
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
final PsiField field = (PsiField) infos[0];
|
||||
final String propertyName = field.getName();
|
||||
final PsiReferenceExpression referenceExpression =
|
||||
(PsiReferenceExpression) infos[1];
|
||||
final boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
|
||||
final PsiClass containingClass = field.getContainingClass();
|
||||
if (PsiUtil.isAccessedForReading(referenceExpression)) {
|
||||
if (PsiUtil.isAccessedForWriting(referenceExpression)) {
|
||||
return null;
|
||||
}
|
||||
final PsiMethod getter =
|
||||
PropertyUtil.findPropertyGetter(containingClass,
|
||||
propertyName, isStatic, true);
|
||||
if (getter == null) {
|
||||
return null;
|
||||
}
|
||||
return new UseOfAnotherObjectsPrivateFieldFix(getter);
|
||||
} else if (PsiUtil.isAccessedForWriting(referenceExpression)) {
|
||||
final PsiMethod setter =
|
||||
PropertyUtil.findPropertySetter(containingClass,
|
||||
propertyName, isStatic, true);
|
||||
if (setter == null) {
|
||||
return null;
|
||||
}
|
||||
return new UseOfAnotherObjectsPrivateFieldFix(setter);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class UseOfAnotherObjectsPrivateFieldFix
|
||||
extends InspectionGadgetsFix {
|
||||
|
||||
private final PsiMethod method;
|
||||
|
||||
public UseOfAnotherObjectsPrivateFieldFix(PsiMethod method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Replace with call to '" + method.getName() + "'";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor)
|
||||
throws IncorrectOperationException {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
final PsiElement parent = element.getParent();
|
||||
if (!(parent instanceof PsiReferenceExpression)) {
|
||||
return;
|
||||
}
|
||||
final PsiReferenceExpression referenceExpression =
|
||||
(PsiReferenceExpression) parent;
|
||||
final StringBuilder methodCallText = new StringBuilder();
|
||||
final PsiExpression qualifierExpression =
|
||||
referenceExpression.getQualifierExpression();
|
||||
if (qualifierExpression != null) {
|
||||
methodCallText.append(qualifierExpression.getText());
|
||||
methodCallText.append('.');
|
||||
}
|
||||
methodCallText.append(method.getName());
|
||||
if (!PsiUtil.isOnAssignmentLeftHand(referenceExpression)) {
|
||||
methodCallText.append("()");
|
||||
replaceExpression(referenceExpression,
|
||||
methodCallText.toString());
|
||||
} else {
|
||||
final PsiAssignmentExpression assignmentExpression =
|
||||
PsiTreeUtil.getParentOfType(referenceExpression,
|
||||
PsiAssignmentExpression.class);
|
||||
if (assignmentExpression == null) {
|
||||
return;
|
||||
}
|
||||
methodCallText.append('(');
|
||||
final PsiExpression rhs = assignmentExpression.getRExpression();
|
||||
if (rhs != null) {
|
||||
methodCallText.append(rhs.getText());
|
||||
}
|
||||
methodCallText.append(')');
|
||||
replaceExpression(assignmentExpression,
|
||||
methodCallText.toString());
|
||||
}
|
||||
}
|
||||
return new EncapsulateVariableFix(field.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -205,7 +120,7 @@ public class UseOfAnotherObjectsPrivateFieldInspection
|
||||
if(fieldNameElement == null){
|
||||
return;
|
||||
}
|
||||
registerError(fieldNameElement, field, expression);
|
||||
registerError(fieldNameElement, field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2005 Dave Griffith
|
||||
* Copyright 2003-2011 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,6 +19,7 @@ import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiReferenceExpression;
|
||||
import com.intellij.refactoring.JavaRefactoringActionHandlerFactory;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
@@ -27,14 +28,37 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class EncapsulateVariableFix extends InspectionGadgetsFix {
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return InspectionGadgetsBundle.message("encapsulate.variable.quickfix");
|
||||
private final String fieldName;
|
||||
|
||||
public EncapsulateVariableFix(String fieldName) {
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return InspectionGadgetsBundle.message("encapsulate.variable.quickfix",
|
||||
fieldName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFix(Project project, ProblemDescriptor descriptor) {
|
||||
final PsiElement nameElement = descriptor.getPsiElement();
|
||||
final PsiField field = (PsiField) nameElement.getParent();
|
||||
final PsiElement parent = nameElement.getParent();
|
||||
final PsiField field;
|
||||
if (parent instanceof PsiField) {
|
||||
field = (PsiField) parent;
|
||||
} else if (parent instanceof PsiReferenceExpression) {
|
||||
final PsiReferenceExpression referenceExpression =
|
||||
(PsiReferenceExpression) parent;
|
||||
final PsiElement target = referenceExpression.resolve();
|
||||
if (!(target instanceof PsiField)) {
|
||||
return;
|
||||
}
|
||||
field = (PsiField) target;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
final JavaRefactoringActionHandlerFactory factory =
|
||||
JavaRefactoringActionHandlerFactory.getInstance();
|
||||
final RefactoringActionHandler renameHandler =
|
||||
|
||||
Reference in New Issue
Block a user