コード例 #1
0
  public void applyFix(
      @NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PyFunction function =
        PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PyFunction.class);
    if (function == null) return;
    final PyClass cls = function.getContainingClass();
    assert cls != null;
    final String functionName = function.getName();
    final String complementaryName = PyNames.NEW.equals(functionName) ? PyNames.INIT : PyNames.NEW;
    final TypeEvalContext context =
        TypeEvalContext.userInitiated(project, descriptor.getEndElement().getContainingFile());
    final PyFunction complementaryMethod =
        myOverridenMethod
            ? (PyFunction) PySuperMethodsSearch.search(function, context).findFirst()
            : cls.findMethodByName(complementaryName, true);

    assert complementaryMethod != null;
    final PyMethodDescriptor methodDescriptor =
        new PyMethodDescriptor(function) {
          @Override
          public List<PyParameterInfo> getParameters() {
            final List<PyParameterInfo> parameterInfos = super.getParameters();
            final int paramLength = function.getParameterList().getParameters().length;
            final int complementaryParamLength =
                complementaryMethod.getParameterList().getParameters().length;
            if (complementaryParamLength > paramLength)
              parameterInfos.add(new PyParameterInfo(-1, "**kwargs", "", false));
            return parameterInfos;
          }
        };
    final PyChangeSignatureDialog dialog = new PyChangeSignatureDialog(project, methodDescriptor);
    dialog.show();
  }
コード例 #2
0
    public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
      if (!CodeInsightUtilBase.preparePsiElementForWrite(descriptor.getPsiElement())) return;
      final PsiMethod psiMethod =
          PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiMethod.class);
      if (psiMethod != null) {
        final ArrayList<PsiElement> psiParameters = new ArrayList<PsiElement>();
        final RefElement refMethod = myManager != null ? myManager.getReference(psiMethod) : null;
        if (refMethod != null) {
          for (final RefParameter refParameter : getUnusedParameters((RefMethod) refMethod)) {
            psiParameters.add(refParameter.getElement());
          }
        } else {
          final PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
          for (PsiParameter parameter : parameters) {
            if (Comparing.strEqual(parameter.getName(), myHint)) {
              psiParameters.add(parameter);
              break;
            }
          }
        }
        final PsiModificationTracker tracker = psiMethod.getManager().getModificationTracker();
        final long startModificationCount = tracker.getModificationCount();

        removeUnusedParameterViaChangeSignature(psiMethod, psiParameters);
        if (refMethod != null && startModificationCount != tracker.getModificationCount()) {
          myProcessor.ignoreElement(refMethod);
        }
      }
    }
コード例 #3
0
 @Override
 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
   if (!FileModificationService.getInstance()
       .preparePsiElementForWrite(descriptor.getPsiElement())) return;
   PsiElement castTypeElement = descriptor.getPsiElement();
   PsiTypeCastExpression cast =
       castTypeElement == null ? null : (PsiTypeCastExpression) castTypeElement.getParent();
   if (cast != null) {
     RedundantCastUtil.removeCast(cast);
   }
 }
 @Override
 protected void doFix(Project project, ProblemDescriptor descriptor) {
   final TextRange textRange = descriptor.getTextRangeInElement();
   final PsiElement element = descriptor.getPsiElement();
   if (!(element instanceof PsiFile)) {
     return;
   }
   final PsiFile file = (PsiFile) element;
   final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
   if (document == null) {
     return;
   }
   document.replaceString(
       textRange.getStartOffset(), textRange.getEndOffset(), String.valueOf(c));
 }
コード例 #5
0
 @Override
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiIdentifier name = (PsiIdentifier) descriptor.getPsiElement();
   final PsiReferenceExpression expression = (PsiReferenceExpression) name.getParent();
   if (expression == null) {
     return;
   }
   final PsiMethodCallExpression call = (PsiMethodCallExpression) expression.getParent();
   final String methodName = expression.getReferenceName();
   if (call == null) {
     return;
   }
   final PsiMethod method = call.resolveMethod();
   if (method == null) {
     return;
   }
   final PsiClass containingClass = method.getContainingClass();
   final PsiExpressionList argumentList = call.getArgumentList();
   if (containingClass == null) {
     return;
   }
   final String containingClassName = containingClass.getQualifiedName();
   final String argText = argumentList.getText();
   PsiReplacementUtil.replaceExpressionAndShorten(
       call, containingClassName + '.' + methodName + argText);
 }
コード例 #6
0
 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
   final PsiDocTag myTag =
       PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiDocTag.class);
   if (myTag == null) return;
   if (!CodeInsightUtilBase.preparePsiElementForWrite(myTag)) return;
   myTag.delete();
 }
コード例 #7
0
 @Override
 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
   final PsiElement element = descriptor.getPsiElement();
   if (!(element instanceof PsiReferenceExpression)) {
     return;
   }
   final PsiElement parent = PsiUtil.skipParenthesizedExprUp(element.getParent());
   if (parent instanceof PsiVariable) {
     element.delete();
   }
   if (!(parent instanceof PsiAssignmentExpression)) {
     return;
   }
   final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) parent;
   final PsiExpression lhs = assignmentExpression.getLExpression();
   final PsiExpression rhs = assignmentExpression.getRExpression();
   if (PsiTreeUtil.isAncestor(lhs, element, false)) {
     if (rhs != null) {
       assignmentExpression.replace(rhs);
     } else {
       assignmentExpression.delete();
     }
   } else {
     final PsiElement grandParent = assignmentExpression.getParent();
     if (grandParent instanceof PsiExpressionStatement) {
       grandParent.delete();
     } else {
       assignmentExpression.replace(element);
     }
   }
 }
コード例 #8
0
    @Override
    public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
      final PsiElement element = descriptor.getPsiElement();
      final PsiModifierListOwner psiElement =
          PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class);
      if (psiElement != null) {
        RefJavaElement refElement =
            (RefJavaElement) (myManager != null ? myManager.getReference(psiElement) : null);
        try {
          if (psiElement instanceof PsiVariable) {
            ((PsiVariable) psiElement).normalizeDeclaration();
          }
          final PsiModifierList modifierList = psiElement.getModifierList();
          LOG.assertTrue(modifierList != null);
          modifierList.setModifierProperty(PsiModifier.FINAL, true);
          modifierList.setModifierProperty(PsiModifier.VOLATILE, false);
        } catch (IncorrectOperationException e) {
          LOG.error(e);
        }

        if (refElement != null) {
          RefJavaUtil.getInstance().setIsFinal(refElement, true);
        }
      }
    }
 @Override
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final JSExpression expression = (JSExpression) descriptor.getPsiElement();
   final String newExpression = calculateReplacementExpression(expression);
   replaceExpression(expression, newExpression);
 }
 @Override
 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
   PsiMethodCallExpression expression =
       PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiMethodCallExpression.class);
   LOG.assertTrue(expression != null);
   myHandler.getTemplate().convertToStream(expression, null, false);
 }
コード例 #11
0
 @Override
 public void doFix(Project project, ProblemDescriptor descriptor)
   throws IncorrectOperationException {
   final PsiElement forElement = descriptor.getPsiElement();
   final PsiElement parent = forElement.getParent();
   if (!(parent instanceof PsiForStatement)) {
     return;
   }
   final PsiForStatement forStatement = (PsiForStatement)parent;
   final String newExpression;
   if (isArrayLoopStatement(forStatement)) {
     newExpression = createArrayIterationText(forStatement);
   }
   else if (isCollectionLoopStatement(forStatement, ignoreUntypedCollections)) {
     newExpression = createCollectionIterationText(forStatement);
   }
   else if (isIndexedListLoopStatement(forStatement, ignoreUntypedCollections)) {
     newExpression = createListIterationText(forStatement);
   }
   else {
     return;
   }
   if (newExpression == null) {
     return;
   }
   replaceStatementAndShortenClassNames(forStatement, newExpression);
 }
コード例 #12
0
    public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
      final PsiElement element = descriptor.getPsiElement();
      final PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
      LOG.assertTrue(method != null);
      PsiParameter parameter = PsiTreeUtil.getParentOfType(element, PsiParameter.class, false);
      if (parameter == null) {
        final PsiParameter[] parameters = method.getParameterList().getParameters();
        for (PsiParameter psiParameter : parameters) {
          if (Comparing.strEqual(psiParameter.getName(), myParameterName)) {
            parameter = psiParameter;
            break;
          }
        }
      }
      if (parameter == null) return;
      if (!CommonRefactoringUtil.checkReadOnlyStatus(project, parameter)) return;

      final PsiExpression defToInline;
      try {
        defToInline =
            JavaPsiFacade.getInstance(project)
                .getElementFactory()
                .createExpressionFromText(myValue, parameter);
      } catch (IncorrectOperationException e) {
        return;
      }
      final PsiParameter parameterToInline = parameter;
      inlineSameParameterValue(method, parameterToInline, defToInline);
    }
コード例 #13
0
 @Override
 protected void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement element = descriptor.getPsiElement();
   final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
   final PsiExpression newExpression =
       factory.createExpressionFromText("new java.lang.NullPointerException()", element);
   element.replace(newExpression);
 }
    @Override
    protected void doFix(Project project, ProblemDescriptor descriptor)
        throws IncorrectOperationException {
      final PsiElement element = descriptor.getPsiElement();

      PsiExpression exp = expression.getElement();
      if (exp == null) return;
      element.replace(exp);
    }
 @Override
 public void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement identifier = descriptor.getPsiElement();
   final GrIfStatement statement = (GrIfStatement) identifier.getParent();
   assert statement != null;
   final GrStatement thenBranch = statement.getThenBranch();
   replaceStatement(statement, thenBranch);
 }
コード例 #16
0
 @Override
 public void applyFix(
     @NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
   final PsiFile file = descriptor.getPsiElement().getContainingFile();
   final Backgroundable backgroundable = getFixTask(file);
   ProgressManager.getInstance()
       .runProcessWithProgressAsynchronously(
           backgroundable, new BackgroundableProcessIndicator(backgroundable));
 }
 @Override
 protected void doFix(Project project, ProblemDescriptor descriptor) {
   final PsiElement element = descriptor.getPsiElement();
   if (element instanceof PsiLambdaExpression) {
     final PsiElement body = ((PsiLambdaExpression) element).getBody();
     if (body instanceof PsiExpression) {
       RefactoringUtil.expandExpressionLambdaToCodeBlock(body);
     }
   }
 }
 @Override
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement element = descriptor.getPsiElement();
   if (constructor) {
     makeConstructorPackageLocal(project, element);
   } else {
     makeMemberPackageLocal(element);
   }
 }
コード例 #19
0
 @Override
 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
   if (!FileModificationService.getInstance()
       .preparePsiElementForWrite(descriptor.getPsiElement())) return;
   final PsiElement psiElement = descriptor.getPsiElement();
   if (psiElement instanceof PsiInstanceOfExpression) {
     try {
       final PsiExpression compareToNull =
           JavaPsiFacade.getInstance(psiElement.getProject())
               .getElementFactory()
               .createExpressionFromText(
                   ((PsiInstanceOfExpression) psiElement).getOperand().getText() + " != null",
                   psiElement.getParent());
       psiElement.replace(compareToNull);
     } catch (IncorrectOperationException e) {
       LOG.error(e);
     }
   }
 }
コード例 #20
0
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiNewExpression expression = (PsiNewExpression) descriptor.getPsiElement();
   final PsiExpressionList argList = expression.getArgumentList();
   assert argList != null;
   final PsiExpression[] args = argList.getExpressions();
   final PsiExpression arg = args[0];
   final String text = arg.getText();
   replaceExpression(expression, className + ".valueOf(" + text + ')');
 }
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement breakKeywordElement = descriptor.getPsiElement();
   final PsiBreakStatement breakStatement = (PsiBreakStatement) breakKeywordElement.getParent();
   final PsiIdentifier identifier = breakStatement.getLabelIdentifier();
   if (identifier == null) {
     return;
   }
   identifier.delete();
 }
 @Override
 protected void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement element = descriptor.getPsiElement();
   if (!(element instanceof PsiClassObjectAccessExpression)) {
     return;
   }
   final PsiClassObjectAccessExpression classObjectAccessExpression =
       (PsiClassObjectAccessExpression) element;
   replaceExpression(classObjectAccessExpression, newClassName + ".class");
 }
 @Override
 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
   final PsiTypeElement typeElement =
       PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiTypeElement.class);
   if (!FileModificationService.getInstance().preparePsiElementForWrite(typeElement)) return;
   final PsiMethodReferenceExpression expression =
       PsiTreeUtil.getParentOfType(typeElement, PsiMethodReferenceExpression.class);
   if (expression != null) {
     expression.replace(createMethodReference(expression, typeElement));
   }
 }
コード例 #24
0
    public void visitElement(PsiElement psiElement) {
      super.visitElement(psiElement);

      if (myProblems != null) {
        final TextRange textRange = psiElement.getTextRange();
        for (ProblemDescriptor problem : myProblems) {
          if (textRange.contains(problem.getPsiElement().getTextRange())) {
            return;
          }
        }
      }

      if (psiElement instanceof XPathExpression) {
        checkExpression(((XPathExpression) psiElement));
      } else if (psiElement instanceof XPathNodeTest) {
        checkNodeTest(((XPathNodeTest) psiElement));
      } else if (psiElement instanceof XPathPredicate) {
        checkPredicate((XPathPredicate) psiElement);
      }
    }
コード例 #25
0
 @Override
 protected void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement element = descriptor.getPsiElement();
   final String text = element.getText();
   final int number = Integer.parseInt(text, 8);
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiElementFactory factory = psiFacade.getElementFactory();
   final PsiExpression decimalNumber =
       factory.createExpressionFromText(Integer.toString(number), element);
   element.replace(decimalNumber);
 }
コード例 #26
0
  private static Map<ProblemDescriptor, HighlightDisplayLevel> runInspectionTool(
      final PsiFile file,
      final LocalInspectionTool inspectionTool,
      final HighlightDisplayLevel level) {
    Map<ProblemDescriptor, HighlightDisplayLevel> problemsMap =
        new LinkedHashMap<ProblemDescriptor, HighlightDisplayLevel>();
    for (ProblemDescriptor descriptor : runInspectionOnFile(file, inspectionTool)) {
      final ProblemHighlightType highlightType = descriptor.getHighlightType();

      final HighlightDisplayLevel highlightDisplayLevel;
      if (highlightType == ProblemHighlightType.WEAK_WARNING) {
        highlightDisplayLevel = HighlightDisplayLevel.WEAK_WARNING;
      } else if (highlightType == ProblemHighlightType.INFORMATION) {
        highlightDisplayLevel = HighlightDisplayLevel.DO_NOT_SHOW;
      } else {
        highlightDisplayLevel = level;
      }
      problemsMap.put(descriptor, highlightDisplayLevel);
    }
    return problemsMap;
  }
コード例 #27
0
    @Override
    public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
      if (!FileModificationService.getInstance()
          .preparePsiElementForWrite(descriptor.getPsiElement())) return;
      final PsiModifierListOwner element =
          PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiModifierListOwner.class);
      if (element != null) {
        RefElement refElement = null;
        if (myManager != null) {
          refElement = myManager.getReference(element);
        }
        try {
          if (element instanceof PsiVariable) {
            ((PsiVariable) element).normalizeDeclaration();
          }

          PsiModifierList list = element.getModifierList();

          LOG.assertTrue(list != null);

          if (element instanceof PsiMethod) {
            PsiMethod psiMethod = (PsiMethod) element;
            PsiClass containingClass = psiMethod.getContainingClass();
            if (containingClass != null
                && containingClass.getParent() instanceof PsiFile
                && myHint == PsiModifier.PRIVATE
                && list.hasModifierProperty(PsiModifier.FINAL)) {
              list.setModifierProperty(PsiModifier.FINAL, false);
            }
          }

          list.setModifierProperty(myHint, true);
          if (refElement instanceof RefJavaElement) {
            RefJavaUtil.getInstance().setAccessModifier((RefJavaElement) refElement, myHint);
          }
        } catch (IncorrectOperationException e) {
          LOG.error(e);
        }
      }
    }
 @Override
 protected void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement element = descriptor.getPsiElement();
   final PsiAnnotation annotation = PsiTreeUtil.getParentOfType(element, PsiAnnotation.class);
   if (annotation == null) {
     return;
   }
   final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
   final String annotationText = buildAnnotationText(annotation);
   final PsiAnnotation newAnnotation = factory.createAnnotationFromText(annotationText, element);
   annotation.replace(newAnnotation);
 }
コード例 #29
0
 @Override
 protected void doFix(Project project, ProblemDescriptor descriptor) {
   final PsiElement element = descriptor.getPsiElement();
   if (!(element instanceof PsiTypeElement)) {
     return;
   }
   final PsiTypeElement typeElement = (PsiTypeElement) element;
   final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
   final PsiClassType type =
       factory.createTypeByFQClassName(CommonClassNames.JAVA_LANG_RUNTIME_EXCEPTION);
   final PsiTypeElement newTypeElement = factory.createTypeElement(type);
   typeElement.replace(newTypeElement);
 }
コード例 #30
0
 @Override
 protected void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiMethodCallExpression methodCallExpression =
       (PsiMethodCallExpression) descriptor.getPsiElement().getParent().getParent();
   final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
   final PsiExpression qualifier = methodExpression.getQualifierExpression();
   if (qualifier == null) {
     PsiReplacementUtil.replaceExpression(methodCallExpression, "this");
   } else {
     methodCallExpression.replace(qualifier);
   }
 }