コード例 #1
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);
    }
 @Nullable
 public static PsiClass findActivityClass(@NotNull Module module) {
   return JavaPsiFacade.getInstance(module.getProject())
       .findClass(
           AndroidUtils.ACTIVITY_BASE_CLASS_NAME,
           module.getModuleWithDependenciesAndLibrariesScope(false));
 }
コード例 #3
0
  @Nullable
  private LocalQuickFix[] createNPEFixes(
      PsiExpression qualifier, PsiExpression expression, boolean onTheFly) {
    if (qualifier == null || expression == null) return null;
    if (qualifier instanceof PsiMethodCallExpression) return null;
    if (qualifier instanceof PsiLiteralExpression
        && ((PsiLiteralExpression) qualifier).getValue() == null) return null;

    try {
      final List<LocalQuickFix> fixes = new SmartList<LocalQuickFix>();

      if (PsiUtil.getLanguageLevel(qualifier).isAtLeast(LanguageLevel.JDK_1_4)) {
        final Project project = qualifier.getProject();
        final PsiElementFactory elementFactory =
            JavaPsiFacade.getInstance(project).getElementFactory();
        final PsiBinaryExpression binary =
            (PsiBinaryExpression) elementFactory.createExpressionFromText("a != null", null);
        binary.getLOperand().replace(qualifier);
        fixes.add(new AddAssertStatementFix(binary));
      }

      addSurroundWithIfFix(qualifier, fixes, onTheFly);

      if (ReplaceWithTernaryOperatorFix.isAvailable(qualifier, expression)) {
        fixes.add(new ReplaceWithTernaryOperatorFix(qualifier));
      }
      return fixes.toArray(new LocalQuickFix[fixes.size()]);
    } catch (IncorrectOperationException e) {
      LOG.error(e);
      return null;
    }
  }
 public static PsiDirectory[] getIntentionDescriptionsDirs(Module module) {
   final PsiPackage aPackage =
       JavaPsiFacade.getInstance(module.getProject()).findPackage(INSPECTION_DESCRIPTIONS);
   if (aPackage != null) {
     return aPackage.getDirectories(GlobalSearchScope.moduleWithDependenciesScope(module));
   } else {
     return PsiDirectory.EMPTY_ARRAY;
   }
 }
 private static PsiMethodReferenceExpression createMethodReference(
     PsiMethodReferenceExpression expression, PsiTypeElement typeElement) {
   final PsiType type = typeElement.getType();
   final PsiElementFactory elementFactory =
       JavaPsiFacade.getElementFactory(expression.getProject());
   final PsiMethodReferenceExpression copy = (PsiMethodReferenceExpression) expression.copy();
   copy.getQualifierType()
       .replace(elementFactory.createTypeElement(((PsiClassType) type).rawType()));
   return copy;
 }
  @Override
  public ProblemDescriptor[] checkClass(
      @NotNull PsiClass aClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
    final Project project = aClass.getProject();
    final PsiIdentifier nameIdentifier = aClass.getNameIdentifier();
    final Module module = ModuleUtil.findModuleForPsiElement(aClass);

    if (nameIdentifier == null || module == null || !PsiUtil.isInstanciatable(aClass)) return null;

    final PsiClass base =
        JavaPsiFacade.getInstance(project)
            .findClass(INTENTION, GlobalSearchScope.allScope(project));

    if (base == null || !aClass.isInheritor(base, true)) return null;

    String descriptionDir = getDescriptionDirName(aClass);
    if (StringUtil.isEmptyOrSpaces(descriptionDir)) {
      return null;
    }

    for (PsiDirectory description : getIntentionDescriptionsDirs(module)) {
      PsiDirectory dir = description.findSubdirectory(descriptionDir);
      if (dir == null) continue;
      final PsiFile descr = dir.findFile("description.html");
      if (descr != null) {
        if (!hasBeforeAndAfterTemplate(dir.getVirtualFile())) {
          PsiElement problem = aClass.getNameIdentifier();
          ProblemDescriptor problemDescriptor =
              manager.createProblemDescriptor(
                  problem == null ? nameIdentifier : problem,
                  "Intention must have 'before.*.template' and 'after.*.template' beside 'description.html'",
                  isOnTheFly,
                  ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
          return new ProblemDescriptor[] {problemDescriptor};
        }

        return null;
      }
    }

    final PsiElement problem = aClass.getNameIdentifier();
    final ProblemDescriptor problemDescriptor =
        manager.createProblemDescriptor(
            problem == null ? nameIdentifier : problem,
            "Intention does not have a description",
            isOnTheFly,
            new LocalQuickFix[] {new CreateHtmlDescriptionFix(descriptionDir, module, true)},
            ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
    return new ProblemDescriptor[] {problemDescriptor};
  }
 @Override
 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
   PsiElement element = descriptor.getStartElement();
   if (!(element instanceof PsiLoopStatement)) return;
   PsiLoopStatement loop = (PsiLoopStatement) element;
   IteratorDeclaration declaration;
   declaration = IteratorDeclaration.fromLoop(loop);
   if (declaration == null) return;
   PsiStatement body = loop.getBody();
   if (!(body instanceof PsiBlockStatement)) return;
   PsiStatement[] statements = ((PsiBlockStatement) body).getCodeBlock().getStatements();
   PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
   String replacement = null;
   CommentTracker ct = new CommentTracker();
   if (statements.length == 2 && statements[1] instanceof PsiIfStatement) {
     PsiVariable variable = declaration.getNextElementVariable(statements[0]);
     if (variable == null) return;
     PsiExpression condition = ((PsiIfStatement) statements[1]).getCondition();
     if (condition == null) return;
     replacement = generateRemoveIf(declaration, ct, condition, variable.getName());
   } else if (statements.length == 1 && statements[0] instanceof PsiIfStatement) {
     PsiExpression condition = ((PsiIfStatement) statements[0]).getCondition();
     if (condition == null) return;
     PsiElement ref = declaration.findOnlyIteratorRef(condition);
     if (ref != null) {
       PsiElement call = ref.getParent().getParent();
       if (!declaration.isIteratorMethodCall(call, "next")) return;
       PsiType type = ((PsiExpression) call).getType();
       JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
       SuggestedNameInfo info =
           javaCodeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, type);
       if (info.names.length == 0) {
         info =
             javaCodeStyleManager.suggestVariableName(
                 VariableKind.PARAMETER, "value", null, type);
       }
       String paramName =
           javaCodeStyleManager.suggestUniqueVariableName(info, condition, true).names[0];
       ct.replace(call, factory.createIdentifier(paramName));
       replacement = generateRemoveIf(declaration, ct, condition, paramName);
     }
   }
   if (replacement == null) return;
   ct.delete(declaration.getIterator());
   PsiElement result = ct.replaceAndRestoreComments(loop, replacement);
   LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result);
   CodeStyleManager.getInstance(project).reformat(result);
 }
 @Override
 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
   final PsiReferenceParameterList typeArgumentList =
       (PsiReferenceParameterList) descriptor.getPsiElement();
   if (!FileModificationService.getInstance().preparePsiElementForWrite(typeArgumentList))
     return;
   try {
     final PsiMethodCallExpression expr =
         (PsiMethodCallExpression)
             JavaPsiFacade.getInstance(project)
                 .getElementFactory()
                 .createExpressionFromText("foo()", null);
     typeArgumentList.replace(expr.getTypeArgumentList());
   } catch (IncorrectOperationException e) {
     LOG.error(e);
   }
 }
コード例 #9
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);
     }
   }
 }