private static String createInitializerReplacementText( PsiType varType, PsiExpression initializer) { final PsiType initializerType = initializer.getType(); final PsiClassType rawType = initializerType instanceof PsiClassType ? ((PsiClassType) initializerType).rawType() : null; final PsiClassType rawVarType = varType instanceof PsiClassType ? ((PsiClassType) varType).rawType() : null; if (rawType != null && rawVarType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_ARRAY_LIST) && rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_LIST)) { return "toList()"; } else if (rawType != null && rawVarType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_HASH_SET) && rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_SET)) { return "toSet()"; } else if (rawType != null) { return "toCollection(" + rawType.getClassName() + "::new)"; } else { return "toCollection(() -> " + initializer.getText() + ")"; } }
private static boolean isCollectCall(PsiStatement body, final PsiParameter parameter) { PsiIfStatement ifStatement = extractIfStatement(body); final PsiMethodCallExpression methodCallExpression = extractAddCall(body, ifStatement); if (methodCallExpression != null) { final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifierExpression = methodExpression.getQualifierExpression(); PsiClass qualifierClass = null; if (qualifierExpression instanceof PsiReferenceExpression) { if (ReferencesSearch.search(parameter, new LocalSearchScope(qualifierExpression)) .findFirst() != null) { return false; } final PsiElement resolve = ((PsiReferenceExpression) qualifierExpression).resolve(); if (resolve instanceof PsiVariable) { if (ReferencesSearch.search( resolve, new LocalSearchScope(methodCallExpression.getArgumentList())) .findFirst() != null) { return false; } } qualifierClass = PsiUtil.resolveClassInType(qualifierExpression.getType()); } else if (qualifierExpression == null) { final PsiClass enclosingClass = PsiTreeUtil.getParentOfType(body, PsiClass.class); if (PsiUtil.getEnclosingStaticElement(body, enclosingClass) == null) { qualifierClass = enclosingClass; } } if (qualifierClass != null && InheritanceUtil.isInheritor( qualifierClass, false, CommonClassNames.JAVA_UTIL_COLLECTION)) { while (ifStatement != null && PsiTreeUtil.isAncestor(body, ifStatement, false)) { final PsiExpression condition = ifStatement.getCondition(); if (condition != null && isConditionDependsOnUpdatedCollections(condition, qualifierExpression)) return false; ifStatement = PsiTreeUtil.getParentOfType(ifStatement, PsiIfStatement.class); } final PsiElement resolve = methodExpression.resolve(); if (resolve instanceof PsiMethod && "add".equals(((PsiMethod) resolve).getName()) && ((PsiMethod) resolve).getParameterList().getParametersCount() == 1) { final PsiExpression[] args = methodCallExpression.getArgumentList().getExpressions(); if (args.length == 1) { if (args[0] instanceof PsiCallExpression) { final PsiMethod method = ((PsiCallExpression) args[0]).resolveMethod(); return method != null && !method.hasTypeParameters() && !isThrowsCompatible(method); } return true; } } } } return false; }
private static String getIteratedValueText(PsiExpression iteratedValue) { return iteratedValue instanceof PsiCallExpression || iteratedValue instanceof PsiReferenceExpression || iteratedValue instanceof PsiQualifiedExpression || iteratedValue instanceof PsiParenthesizedExpression ? iteratedValue.getText() : "(" + iteratedValue.getText() + ")"; }
private static String compoundLambdaOrMethodReference( PsiParameter parameter, PsiExpression expression, String samQualifiedName, PsiType[] samParamTypes) { String result = ""; final Project project = parameter.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiClass functionClass = psiFacade.findClass(samQualifiedName, GlobalSearchScope.allScope(project)); for (int i = 0; i < samParamTypes.length; i++) { if (samParamTypes[i] instanceof PsiPrimitiveType) { samParamTypes[i] = ((PsiPrimitiveType) samParamTypes[i]).getBoxedType(expression); } } final PsiClassType functionalInterfaceType = functionClass != null ? psiFacade.getElementFactory().createType(functionClass, samParamTypes) : null; final PsiParameter[] parameters = {parameter}; final String methodReferenceText = LambdaCanBeMethodReferenceInspection.convertToMethodReference( expression, parameters, functionalInterfaceType, null); if (methodReferenceText != null) { LOG.assertTrue(functionalInterfaceType != null); result += "(" + functionalInterfaceType.getCanonicalText() + ")" + methodReferenceText; } else { result += parameter.getName() + " -> " + expression.getText(); } return result; }
private static String createMapperFunctionalExpressionText( PsiParameter parameter, PsiExpression expression) { String iteration = ""; if (!isIdentityMapping(parameter, expression)) { iteration += ".map("; iteration += compoundLambdaOrMethodReference( parameter, expression, "java.util.function.Function", new PsiType[] {parameter.getType(), expression.getType()}); iteration += ")"; } return iteration; }
private static boolean isConditionDependsOnUpdatedCollections( PsiExpression condition, PsiExpression qualifierExpression) { final PsiElement collection = qualifierExpression instanceof PsiReferenceExpression ? ((PsiReferenceExpression) qualifierExpression).resolve() : null; if (collection != null) { return ReferencesSearch.search(collection, new LocalSearchScope(condition)).findFirst() != null; } final boolean[] dependsOnCollection = {false}; condition.accept( new JavaRecursiveElementWalkingVisitor() { @Override public void visitMethodCallExpression(PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiExpression callQualifier = expression.getMethodExpression().getQualifierExpression(); if (callQualifier == null || callQualifier instanceof PsiThisExpression && ((PsiThisExpression) callQualifier).getQualifier() == null || callQualifier instanceof PsiSuperExpression && ((PsiSuperExpression) callQualifier).getQualifier() == null) { dependsOnCollection[0] = true; } } @Override public void visitThisExpression(PsiThisExpression expression) { super.visitThisExpression(expression); if (expression.getQualifier() == null && expression.getParent() instanceof PsiExpressionList) { dependsOnCollection[0] = true; } } @Override public void visitClass(PsiClass aClass) {} @Override public void visitLambdaExpression(PsiLambdaExpression expression) {} }); return dependsOnCollection[0]; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final PsiForeachStatement foreachStatement = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiForeachStatement.class); if (foreachStatement != null) { if (!FileModificationService.getInstance().preparePsiElementForWrite(foreachStatement)) return; final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); PsiStatement body = foreachStatement.getBody(); final PsiExpression iteratedValue = foreachStatement.getIteratedValue(); if (body != null && iteratedValue != null) { final PsiParameter parameter = foreachStatement.getIterationParameter(); final PsiIfStatement ifStatement = extractIfStatement(body); final PsiMethodCallExpression methodCallExpression = extractAddCall(body, ifStatement); if (methodCallExpression == null) return; if (isAddAllCall(foreachStatement, body)) { restoreComments(foreachStatement, body); final PsiExpression qualifierExpression = methodCallExpression.getMethodExpression().getQualifierExpression(); final String qualifierText = qualifierExpression != null ? qualifierExpression.getText() : ""; final String callText = StringUtil.getQualifiedName( qualifierText, "addAll(" + getIteratedValueText(iteratedValue) + ");"); PsiElement result = foreachStatement.replace( elementFactory.createStatementFromText(callText, foreachStatement)); reformatWhenNeeded(project, result); return; } final StringBuilder builder = new StringBuilder(getIteratedValueText(iteratedValue) + ".stream()"); builder.append(createFiltersChainText(body, parameter, ifStatement)); builder.append( createMapperFunctionalExpressionText( parameter, methodCallExpression.getArgumentList().getExpressions()[0])); builder.append(".collect(java.util.stream.Collectors."); PsiElement result = null; try { final PsiExpression qualifierExpression = methodCallExpression.getMethodExpression().getQualifierExpression(); if (qualifierExpression instanceof PsiReferenceExpression) { final PsiElement resolve = ((PsiReferenceExpression) qualifierExpression).resolve(); if (resolve instanceof PsiVariable) { if (resolve instanceof PsiLocalVariable && foreachStatement.equals( PsiTreeUtil.skipSiblingsForward( resolve.getParent(), PsiWhiteSpace.class))) { final PsiExpression initializer = ((PsiVariable) resolve).getInitializer(); if (initializer instanceof PsiNewExpression) { final PsiExpressionList argumentList = ((PsiNewExpression) initializer).getArgumentList(); if (argumentList != null && argumentList.getExpressions().length == 0) { restoreComments(foreachStatement, body); final String callText = builder.toString() + createInitializerReplacementText( ((PsiVariable) resolve).getType(), initializer) + ")"; result = initializer.replace( elementFactory.createExpressionFromText(callText, null)); simplifyRedundantCast(result); foreachStatement.delete(); return; } } } } } restoreComments(foreachStatement, body); final String qualifierText = qualifierExpression != null ? qualifierExpression.getText() : ""; final String callText = StringUtil.getQualifiedName( qualifierText, "addAll(" + builder.toString() + "toList()));"); result = foreachStatement.replace( elementFactory.createStatementFromText(callText, foreachStatement)); simplifyRedundantCast(result); } finally { reformatWhenNeeded(project, result); } } } }