From b0b62211d68c1bb5c78335f5e44de5ad621ed4cf Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 6 Sep 2016 12:37:05 +0700 Subject: [PATCH] IDEA-160784 Migration to Stream API: replace with Stream.count() when possible (now i+=1 and i=i+1 also supported) --- .../StreamApiMigrationInspection.java | 35 +++++++++++++++---- .../streamApiMigration/beforeCountArray.java | 2 +- .../streamApiMigration/beforeCountOuter.java | 2 +- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/StreamApiMigrationInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/StreamApiMigrationInspection.java index f6c71d3c5d8a..c39096d48def 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/StreamApiMigrationInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/StreamApiMigrationInspection.java @@ -175,18 +175,39 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo }; } + private static boolean isLiteral(PsiElement element, Object value) { + return element instanceof PsiLiteralExpression && value.equals(((PsiLiteralExpression)element).getValue()); + } + private static PsiExpression extractIncrementedExpression(PsiStatement statement) { if(!(statement instanceof PsiExpressionStatement)) return null; PsiExpression expression = ((PsiExpressionStatement)statement).getExpression(); - PsiExpression operand; if(expression instanceof PsiPostfixExpression) { - if(!JavaTokenType.PLUSPLUS.equals(((PsiPostfixExpression)expression).getOperationTokenType())) return null; - operand = ((PsiPostfixExpression)expression).getOperand(); + if(JavaTokenType.PLUSPLUS.equals(((PsiPostfixExpression)expression).getOperationTokenType())) { + return ((PsiPostfixExpression)expression).getOperand(); + } } else if(expression instanceof PsiPrefixExpression) { - if(!JavaTokenType.PLUSPLUS.equals(((PsiPrefixExpression)expression).getOperationTokenType())) return null; - operand = ((PsiPrefixExpression)expression).getOperand(); - } else return null; // TODO: support i = i+1; - return operand; + if(JavaTokenType.PLUSPLUS.equals(((PsiPrefixExpression)expression).getOperationTokenType())) { + return ((PsiPrefixExpression)expression).getOperand(); + } + } else if(expression instanceof PsiAssignmentExpression) { + PsiAssignmentExpression assignment = (PsiAssignmentExpression)expression; + if(JavaTokenType.PLUSEQ.equals(assignment.getOperationTokenType())) { + if (isLiteral(assignment.getRExpression(), 1)) { + return assignment.getLExpression(); + } + } else if(JavaTokenType.EQ.equals(assignment.getOperationTokenType())) { + if (assignment.getRExpression() instanceof PsiBinaryExpression) { + PsiBinaryExpression binOp = (PsiBinaryExpression)assignment.getRExpression(); + if(JavaTokenType.PLUS.equals(binOp.getOperationTokenType()) && binOp.getROperand() != null && ( + isLiteral(binOp.getROperand(), 1) && binOp.getLOperand().getText().equals(assignment.getLExpression().getText()) || + isLiteral(binOp.getLOperand(), 1) && binOp.getROperand().getText().equals(assignment.getLExpression().getText()))) { + return assignment.getLExpression(); + } + } + } + } + return null; } @Nullable diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCountArray.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCountArray.java index 5d1ed4169250..15a2f4097260 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCountArray.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCountArray.java @@ -6,7 +6,7 @@ public class Main { for(String str : array) { String trimmed = str.trim(); if(trimmed.length() > 10) { - longStrings++; + longStrings = longStrings + 1; } } return longStrings; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCountOuter.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCountOuter.java index 4da30a7d6f1f..aaf4234b74e7 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCountOuter.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCountOuter.java @@ -9,7 +9,7 @@ public class Main { if(element != null) { for(String str : element) { if(str.startsWith("xyz")) { - count++; + ++count; } } }