@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; } }
@Nullable public static PsiClass findActivityClass(@NotNull Module module) { return JavaPsiFacade.getInstance(module.getProject()) .findClass( AndroidUtils.ACTIVITY_BASE_CLASS_NAME, module.getModuleWithDependenciesAndLibrariesScope(false)); }
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); }
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; } }
@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) { 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); } }
@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); } } }