@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; } }
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 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); }