mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
StreamApiMigrationInspection: utility methods moved to appropriate classes; LambdaUtil contracts added
This commit is contained in:
+4
-4
@@ -104,18 +104,18 @@ class ReplaceWithFindFirstFix extends MigrateToStreamFix {
|
||||
return "(" + castType.getText() + ")" + stream + ".orElse(null)";
|
||||
}
|
||||
}
|
||||
if(StreamApiMigrationInspection.isLiteral(falseExpression, Boolean.FALSE) && PsiType.BOOLEAN.equals(trueExpression.getType())) {
|
||||
return stream + ".filter(" + StreamApiMigrationInspection.createLambda(var, trueExpression) + ").isPresent()";
|
||||
if(ExpressionUtils.isLiteral(falseExpression, Boolean.FALSE) && PsiType.BOOLEAN.equals(trueExpression.getType())) {
|
||||
return stream + ".filter(" + LambdaUtil.createLambda(var, trueExpression) + ").isPresent()";
|
||||
}
|
||||
if(trueExpression instanceof PsiConditionalExpression) {
|
||||
PsiConditionalExpression condition = (PsiConditionalExpression)trueExpression;
|
||||
if(EquivalenceChecker.getCanonicalPsiEquivalence().expressionsAreEquivalent(falseExpression, condition.getElseExpression())) {
|
||||
return generateOptionalUnwrap(
|
||||
stream + ".filter(" + StreamApiMigrationInspection.createLambda(var, condition.getCondition()) + ")", tb,
|
||||
stream + ".filter(" + LambdaUtil.createLambda(var, condition.getCondition()) + ")", tb,
|
||||
condition.getThenExpression(), falseExpression);
|
||||
}
|
||||
}
|
||||
stream += ".map(" + StreamApiMigrationInspection.createLambda(var, trueExpression) + ")";
|
||||
stream += ".map(" + LambdaUtil.createLambda(var, trueExpression) + ")";
|
||||
}
|
||||
stream += ".orElse(" + falseExpression.getText() + ")";
|
||||
return stream;
|
||||
|
||||
+6
-6
@@ -56,10 +56,10 @@ class ReplaceWithMatchFix extends MigrateToStreamFix {
|
||||
if(tb.getSingleStatement() instanceof PsiReturnStatement) {
|
||||
PsiReturnStatement returnStatement = (PsiReturnStatement)tb.getSingleStatement();
|
||||
PsiExpression value = returnStatement.getReturnValue();
|
||||
if (StreamApiMigrationInspection.isLiteral(value, Boolean.TRUE) || StreamApiMigrationInspection.isLiteral(value, Boolean.FALSE)) {
|
||||
if (ExpressionUtils.isLiteral(value, Boolean.TRUE) || ExpressionUtils.isLiteral(value, Boolean.FALSE)) {
|
||||
boolean foundResult = (boolean)((PsiLiteralExpression)value).getValue();
|
||||
PsiReturnStatement nextReturnStatement = StreamApiMigrationInspection.getNextReturnStatement(foreachStatement);
|
||||
if (nextReturnStatement != null && StreamApiMigrationInspection.isLiteral(nextReturnStatement.getReturnValue(), !foundResult)) {
|
||||
if (nextReturnStatement != null && ExpressionUtils.isLiteral(nextReturnStatement.getReturnValue(), !foundResult)) {
|
||||
String methodName = foundResult ? "anyMatch" : "noneMatch";
|
||||
String streamText = generateStream(iteratedValue, operations).toString();
|
||||
streamText = addTerminalOperation(streamText, methodName, foreachStatement, tb);
|
||||
@@ -95,11 +95,11 @@ class ReplaceWithMatchFix extends MigrateToStreamFix {
|
||||
PsiExpression initializer = var.getInitializer();
|
||||
if(initializer != null && StreamApiMigrationInspection.isDeclarationJustBefore(var, foreachStatement)) {
|
||||
String replacement;
|
||||
if(StreamApiMigrationInspection.isLiteral(initializer, Boolean.FALSE) &&
|
||||
StreamApiMigrationInspection.isLiteral(rValue, Boolean.TRUE)) {
|
||||
if(ExpressionUtils.isLiteral(initializer, Boolean.FALSE) &&
|
||||
ExpressionUtils.isLiteral(rValue, Boolean.TRUE)) {
|
||||
replacement = streamText;
|
||||
} else if(StreamApiMigrationInspection.isLiteral(initializer, Boolean.TRUE) &&
|
||||
StreamApiMigrationInspection.isLiteral(rValue, Boolean.FALSE)) {
|
||||
} else if(ExpressionUtils.isLiteral(initializer, Boolean.TRUE) &&
|
||||
ExpressionUtils.isLiteral(rValue, Boolean.FALSE)) {
|
||||
replacement = "!"+streamText;
|
||||
} else {
|
||||
replacement = streamText + "?" + rValue.getText() + ":" + initializer.getText();
|
||||
|
||||
+7
-16
@@ -124,11 +124,6 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
return null;
|
||||
}
|
||||
|
||||
@Contract("null, _ -> false")
|
||||
static boolean isLiteral(PsiElement element, Object value) {
|
||||
return element instanceof PsiLiteralExpression && value.equals(((PsiLiteralExpression)element).getValue());
|
||||
}
|
||||
|
||||
@Contract("null, null -> true; null, !null -> false")
|
||||
private static boolean sameReference(PsiExpression expr1, PsiExpression expr2) {
|
||||
if(expr1 == null && expr2 == null) return true;
|
||||
@@ -195,7 +190,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
}
|
||||
} else if(expression instanceof PsiAssignmentExpression) {
|
||||
PsiAssignmentExpression assignment = (PsiAssignmentExpression)expression;
|
||||
if(isLiteral(extractAddend(assignment), 1)) {
|
||||
if(ExpressionUtils.isLiteral(extractAddend(assignment), 1)) {
|
||||
return assignment.getLExpression();
|
||||
}
|
||||
}
|
||||
@@ -375,10 +370,6 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
return mapperCall instanceof PsiReferenceExpression && ((PsiReferenceExpression)mapperCall).resolve() == variable;
|
||||
}
|
||||
|
||||
static String createLambda(PsiVariable variable, PsiExpression expression) {
|
||||
return variable.getName() + " -> " + expression.getText();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiClassType createDefaultConsumerType(Project project, PsiVariable variable) {
|
||||
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
|
||||
@@ -550,9 +541,9 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
PsiReturnStatement returnStatement = (PsiReturnStatement)tb.getSingleStatement();
|
||||
PsiExpression value = returnStatement.getReturnValue();
|
||||
PsiReturnStatement nextReturnStatement = getNextReturnStatement(statement);
|
||||
if(nextReturnStatement != null && (isLiteral(value, Boolean.TRUE) || isLiteral(value, Boolean.FALSE))) {
|
||||
if(nextReturnStatement != null && (ExpressionUtils.isLiteral(value, Boolean.TRUE) || ExpressionUtils.isLiteral(value, Boolean.FALSE))) {
|
||||
boolean foundResult = (boolean)((PsiLiteralExpression)value).getValue();
|
||||
if(isLiteral(nextReturnStatement.getReturnValue(), !foundResult)) {
|
||||
if(ExpressionUtils.isLiteral(nextReturnStatement.getReturnValue(), !foundResult)) {
|
||||
String methodName;
|
||||
if (foundResult) {
|
||||
methodName = "anyMatch";
|
||||
@@ -652,7 +643,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
public String createReplacement(PsiElementFactory factory) {
|
||||
PsiExpression expression =
|
||||
myNegated ? factory.createExpressionFromText(BoolUtils.getNegatedExpressionText(myExpression), myExpression) : myExpression;
|
||||
return ".filter(" + createLambda(myVariable, expression) + ")";
|
||||
return ".filter(" + LambdaUtil.createLambda(myVariable, expression) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -694,7 +685,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
} else if(myVariable.getType() instanceof PsiPrimitiveType) {
|
||||
operationName = "mapToObj";
|
||||
}
|
||||
return "."+operationName+"(" + createLambda(myVariable, myExpression) + ")";
|
||||
return "." + operationName + "(" + LambdaUtil.createLambda(myVariable, myExpression) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -706,7 +697,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
@Override
|
||||
public String createReplacement(PsiElementFactory factory) {
|
||||
PsiExpression replacement = factory.createExpressionFromText(myExpression.getText() + ".stream()", myExpression);
|
||||
return ".flatMap(" + createLambda(myVariable, replacement) + ")";
|
||||
return ".flatMap(" + LambdaUtil.createLambda(myVariable, replacement) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -732,7 +723,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
}
|
||||
}
|
||||
}
|
||||
return "."+operation+"(" + createLambda(myVariable, replacement) + ")";
|
||||
return "." + operation + "(" + LambdaUtil.createLambda(myVariable, replacement) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ import java.util.*;
|
||||
*/
|
||||
public class LambdaUtil {
|
||||
public static final RecursionGuard ourParameterGuard = RecursionManager.createGuard("lambdaParameterGuard");
|
||||
public static ThreadLocal<Map<PsiElement, PsiType>> ourFunctionTypes = new ThreadLocal<Map<PsiElement, PsiType>>();
|
||||
public static final ThreadLocal<Map<PsiElement, PsiType>> ourFunctionTypes = new ThreadLocal<Map<PsiElement, PsiType>>();
|
||||
private static final Logger LOG = Logger.getInstance("#" + LambdaUtil.class.getName());
|
||||
|
||||
@Nullable
|
||||
@@ -128,6 +128,7 @@ public class LambdaUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Contract("null -> false")
|
||||
public static boolean isValidLambdaContext(@Nullable PsiElement context) {
|
||||
return context instanceof PsiTypeCastExpression ||
|
||||
context instanceof PsiAssignmentExpression ||
|
||||
@@ -149,6 +150,7 @@ public class LambdaUtil {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Contract("null -> null")
|
||||
@Nullable
|
||||
public static MethodSignature getFunction(PsiClass psiClass) {
|
||||
if (psiClass == null) return null;
|
||||
@@ -218,6 +220,7 @@ public class LambdaUtil {
|
||||
return signature.getMethod().getContainingClass() != methodSignature.getMethod().getContainingClass();
|
||||
}
|
||||
|
||||
@Contract("null -> null")
|
||||
@Nullable
|
||||
public static List<HierarchicalMethodSignature> findFunctionCandidates(final PsiClass psiClass) {
|
||||
if (psiClass != null && psiClass.isInterface() && !psiClass.isAnnotationType()) {
|
||||
@@ -504,8 +507,11 @@ public class LambdaUtil {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Contract(value = "null -> false", pure = true)
|
||||
public static boolean notInferredType(PsiType typeByExpression) {
|
||||
return typeByExpression instanceof PsiMethodReferenceType || typeByExpression instanceof PsiLambdaExpressionType || typeByExpression instanceof PsiLambdaParameterType;
|
||||
return typeByExpression instanceof PsiMethodReferenceType ||
|
||||
typeByExpression instanceof PsiLambdaExpressionType ||
|
||||
typeByExpression instanceof PsiLambdaParameterType;
|
||||
}
|
||||
|
||||
public static boolean isLambdaReturnExpression(PsiElement element) {
|
||||
@@ -581,6 +587,7 @@ public class LambdaUtil {
|
||||
}
|
||||
|
||||
//JLS 14.8 Expression Statements
|
||||
@Contract("null -> false")
|
||||
public static boolean isExpressionStatementExpression(PsiElement body) {
|
||||
return body instanceof PsiAssignmentExpression ||
|
||||
body instanceof PsiPrefixExpression &&
|
||||
@@ -852,6 +859,17 @@ public class LambdaUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate lambda text for single argument expression lambda
|
||||
*
|
||||
* @param variable lambda sole argument
|
||||
* @param expression lambda body (expression)
|
||||
* @return lambda text
|
||||
*/
|
||||
public static String createLambda(@NotNull PsiVariable variable, @NotNull PsiExpression expression) {
|
||||
return variable.getName() + " -> " + expression.getText();
|
||||
}
|
||||
|
||||
public static class TypeParamsChecker extends PsiTypeVisitor<Boolean> {
|
||||
private PsiMethod myMethod;
|
||||
private final PsiClass myClass;
|
||||
|
||||
+5
@@ -718,4 +718,9 @@ public class ExpressionUtils {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Contract("null, _ -> false")
|
||||
public static boolean isLiteral(PsiElement element, Object value) {
|
||||
return element instanceof PsiLiteralExpression && value.equals(((PsiLiteralExpression)element).getValue());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user