/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/EncapsulateVariableFix.java

https://bitbucket.org/nbargnesi/idea
Java | 82 lines | 61 code | 6 blank | 15 comment | 7 complexity | f499f872de1be0508a903d0d3fb477b0 MD5 | raw file
  1. /*
  2. * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.siyeh.ig.fixes;
  17. import com.intellij.codeInspection.ProblemDescriptor;
  18. import com.intellij.openapi.application.ApplicationManager;
  19. import com.intellij.openapi.project.Project;
  20. import com.intellij.psi.PsiElement;
  21. import com.intellij.psi.PsiField;
  22. import com.intellij.psi.PsiReferenceExpression;
  23. import com.intellij.refactoring.JavaRefactoringActionHandlerFactory;
  24. import com.intellij.refactoring.RefactoringActionHandler;
  25. import com.siyeh.InspectionGadgetsBundle;
  26. import com.siyeh.ig.InspectionGadgetsFix;
  27. import org.jetbrains.annotations.NotNull;
  28. public class EncapsulateVariableFix extends InspectionGadgetsFix {
  29. private final String fieldName;
  30. public EncapsulateVariableFix(String fieldName) {
  31. this.fieldName = fieldName;
  32. }
  33. @Override
  34. @NotNull
  35. public String getName() {
  36. return InspectionGadgetsBundle.message("encapsulate.variable.quickfix",
  37. fieldName);
  38. }
  39. @Override
  40. public void doFix(final Project project, ProblemDescriptor descriptor) {
  41. final PsiElement nameElement = descriptor.getPsiElement();
  42. final PsiElement parent = nameElement.getParent();
  43. final PsiField field;
  44. if (parent instanceof PsiField) {
  45. field = (PsiField)parent;
  46. }
  47. else if (parent instanceof PsiReferenceExpression) {
  48. final PsiReferenceExpression referenceExpression =
  49. (PsiReferenceExpression)parent;
  50. final PsiElement target = referenceExpression.resolve();
  51. if (!(target instanceof PsiField)) {
  52. return;
  53. }
  54. field = (PsiField)target;
  55. }
  56. else {
  57. return;
  58. }
  59. final JavaRefactoringActionHandlerFactory factory =
  60. JavaRefactoringActionHandlerFactory.getInstance();
  61. final RefactoringActionHandler renameHandler =
  62. factory.createEncapsulateFieldsHandler();
  63. final Runnable runnable = new Runnable() {
  64. @Override
  65. public void run() {
  66. renameHandler.invoke(project, new PsiElement[]{field}, null);
  67. }
  68. };
  69. if (ApplicationManager.getApplication().isUnitTestMode()) {
  70. runnable.run();
  71. }
  72. else {
  73. ApplicationManager.getApplication().invokeLater(runnable, project.getDisposed());
  74. }
  75. }
  76. }