diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java index 77918af1f81..c349daa3c95 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java @@ -255,8 +255,8 @@ public class JetPsiFactory { } @NotNull - public static JetBinaryExpression createBinaryExpression(Project project, @NotNull JetExpression lhs, @NotNull String op, @NotNull JetExpression rhs) { - return createBinaryExpression(project, lhs.getText(), op, rhs.getText()); + public static JetBinaryExpression createBinaryExpression(Project project, @Nullable JetExpression lhs, @NotNull String op, @Nullable JetExpression rhs) { + return createBinaryExpression(project, lhs != null ? lhs.getText() : "", op, rhs != null ? rhs.getText() : ""); } public static JetTypeCodeFragment createTypeCodeFragment(Project project, String text, PsiElement context) { @@ -267,29 +267,19 @@ public class JetPsiFactory { return new JetExpressionCodeFragmentImpl(project, "fragment.kt", text, context); } - @NotNull - public static JetIsExpression createIsExpression(Project project, @NotNull String lhs, @NotNull String rhs, boolean negated) { - return (JetIsExpression) createExpression(project, lhs + " " + (negated ? "!is" : "is") + " " + rhs); - } - - @NotNull - public static JetIsExpression createIsExpression(Project project, @NotNull JetExpression lhs, @NotNull JetTypeReference rhs, boolean negated) { - return createIsExpression(project, lhs.getText(), rhs.getText(), negated); - } - @NotNull public static JetReturnExpression createReturn(Project project, @NotNull String text) { return (JetReturnExpression) createExpression(project, "return " + text); } @NotNull - public static JetReturnExpression createReturn(Project project, @NotNull JetExpression expression) { - return createReturn(project, expression.getText()); + public static JetReturnExpression createReturn(Project project, @Nullable JetExpression expression) { + return createReturn(project, expression != null ? expression.getText() : ""); } @NotNull public static JetIfExpression createIf(Project project, - @NotNull JetExpression condition, @NotNull JetExpression thenExpr, @Nullable JetExpression elseExpr) { + @Nullable JetExpression condition, @Nullable JetExpression thenExpr, @Nullable JetExpression elseExpr) { return (JetIfExpression) createExpression(project, JetPsiUnparsingUtils.toIf(condition, thenExpr, elseExpr)); } @@ -325,8 +315,8 @@ public class JetPsiFactory { } @NotNull - public IfChainBuilder elseBranch(@NotNull JetExpression expression) { - return elseBranch(expression.getText()); + public IfChainBuilder elseBranch(@Nullable JetExpression expression) { + return elseBranch(expression != null ? expression.getText() : ""); } @NotNull @@ -373,8 +363,8 @@ public class JetPsiFactory { } @NotNull - public WhenBuilder condition(@NotNull JetExpression expression) { - return condition(expression.getText()); + public WhenBuilder condition(@Nullable JetExpression expression) { + return condition(expression != null ? expression.getText() : ""); } @NotNull @@ -383,8 +373,8 @@ public class JetPsiFactory { } @NotNull - public WhenBuilder pattern(@NotNull JetTypeReference typeReference, boolean negated) { - return pattern(typeReference.getText(), negated); + public WhenBuilder pattern(@Nullable JetTypeReference typeReference, boolean negated) { + return pattern(typeReference != null ? typeReference.getText() : "", negated); } @NotNull @@ -393,8 +383,8 @@ public class JetPsiFactory { } @NotNull - public WhenBuilder range(@NotNull JetExpression argument, boolean negated) { - return range(argument.getText(), negated); + public WhenBuilder range(@Nullable JetExpression argument, boolean negated) { + return range(argument != null ? argument.getText() : "", negated); } @NotNull @@ -409,8 +399,8 @@ public class JetPsiFactory { } @NotNull - public WhenBuilder branchExpression(@NotNull JetExpression expression) { - return branchExpression(expression.getText()); + public WhenBuilder branchExpression(@Nullable JetExpression expression) { + return branchExpression(expression != null ? expression.getText() : ""); } @NotNull @@ -424,8 +414,8 @@ public class JetPsiFactory { } @NotNull - public WhenBuilder entry(@NotNull JetWhenEntry whenEntry) { - return entry(whenEntry.getText()); + public WhenBuilder entry(@Nullable JetWhenEntry whenEntry) { + return entry(whenEntry != null ? whenEntry.getText() : ""); } @NotNull @@ -434,8 +424,8 @@ public class JetPsiFactory { } @NotNull - public WhenBuilder elseEntry(@NotNull JetExpression expression) { - return elseEntry(expression.getText()); + public WhenBuilder elseEntry(@Nullable JetExpression expression) { + return elseEntry(expression != null ? expression.getText() : ""); } @NotNull @@ -447,20 +437,4 @@ public class JetPsiFactory { return (JetWhenExpression) createExpression(project, sb.toString()); } } - - @NotNull - public static JetParenthesizedExpression createParenthesizedExpression(Project project, @NotNull String text) { - return (JetParenthesizedExpression) createExpression(project, "(" + text + ")"); - } - - @NotNull - public static JetParenthesizedExpression createParenthesizedExpression(Project project, @NotNull JetExpression expression) { - return createParenthesizedExpression(project, expression.getText()); - } - - @NotNull - public static JetParenthesizedExpression createParenthesizedExpressionIfNeeded(Project project, @NotNull JetExpression expression) { - return (expression instanceof JetParenthesizedExpression) ? - (JetParenthesizedExpression) expression : createParenthesizedExpression(project, expression); - } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUnparsingUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUnparsingUtils.java index 7995a3fbab2..ed3b8a7adfe 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUnparsingUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUnparsingUtils.java @@ -24,8 +24,12 @@ public class JetPsiUnparsingUtils { } @NotNull - public static String toIf(@NotNull JetExpression condition, @NotNull JetExpression thenExpression, @Nullable JetExpression elseExpression) { - return toIf(condition.getText(), thenExpression.getText(), elseExpression != null ? elseExpression.getText() : null); + public static String toIf(@Nullable JetExpression condition, @Nullable JetExpression thenExpression, @Nullable JetExpression elseExpression) { + return toIf( + condition != null ? condition.getText() : "", + thenExpression != null ? thenExpression.getText() : "", + elseExpression != null ? elseExpression.getText() : null + ); } @NotNull @@ -34,8 +38,8 @@ public class JetPsiUnparsingUtils { } @NotNull - public static String toBinaryExpression(@NotNull JetExpression left, @NotNull String op, @NotNull JetElement right) { - return toBinaryExpression(left.getText(), op, right.getText()); + public static String toBinaryExpression(@Nullable JetExpression left, @NotNull String op, @Nullable JetElement right) { + return toBinaryExpression(left != null ? left.getText() : "", op, right != null ? right.getText() : ""); } @NotNull @@ -44,7 +48,9 @@ public class JetPsiUnparsingUtils { } @NotNull - public static String parenthesizeIfNeeded(@NotNull JetExpression expression) { + public static String parenthesizeIfNeeded(@Nullable JetExpression expression) { + if (expression == null) return ""; + String text = expression.getText(); return (expression instanceof JetParenthesizedExpression || expression instanceof JetConstantExpression || diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/IfWhenUtils.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/IfWhenUtils.java index 020a7076f0b..5d7fabec852 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/IfWhenUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/IfWhenUtils.java @@ -1,10 +1,12 @@ package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lexer.JetTokens; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils.*; @@ -18,7 +20,7 @@ public class IfWhenUtils { } public static boolean checkIfToWhen(@NotNull JetIfExpression ifExpression) { - return ifExpression.getCondition() != null && ifExpression.getThen() != null && ifExpression.getElse() != null; + return ifExpression.getThen() != null && ifExpression.getElse() != null; } public static boolean checkWhenToIf(@NotNull JetWhenExpression whenExpression) { @@ -29,7 +31,9 @@ public class IfWhenUtils { assert expression != null : TRANSFORM_WITHOUT_CHECK; } - private static List splitExpressionToOrBranches(JetExpression expression) { + private static List splitExpressionToOrBranches(@Nullable JetExpression expression) { + if (expression == null) return Collections.emptyList(); + final List branches = new ArrayList(); expression.accept( @@ -80,14 +84,19 @@ public class IfWhenUtils { JetExpression thenBranch = currIfExpression.getThen(); JetExpression elseBranch = currIfExpression.getElse(); - assertNotNull(condition); assertNotNull(thenBranch); assertNotNull(elseBranch); List orBranches = splitExpressionToOrBranches(condition); - for (JetExpression orBranch : orBranches) { - builder.condition(orBranch); + + if (orBranches.isEmpty()) { + builder.condition(""); + } else { + for (JetExpression orBranch : orBranches) { + builder.condition(orBranch); + } } + //noinspection ConstantConditions builder.branchExpression(thenBranch); @@ -135,19 +144,12 @@ public class IfWhenUtils { List entries = whenExpression.getEntries(); for (JetWhenEntry entry : entries) { JetExpression branch = entry.getExpression(); - assertNotNull(branch); if (entry.isElse()) { - //noinspection ConstantConditions builder.elseBranch(branch); } else { String branchConditionText = combineWhenConditions(entry.getConditions(), whenExpression.getSubjectExpression()); - - JetExpression branchExpression = entry.getExpression(); - assertNotNull(branchExpression); - - //noinspection ConstantConditions - builder.ifBranch(branchConditionText, branchExpression.getText()); + builder.ifBranch(branchConditionText, branch != null ? branch.getText() : ""); } } diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/WhenUtils.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/WhenUtils.java index 1b0bb1e06db..fbe4d9236f2 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/WhenUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/WhenUtils.java @@ -120,10 +120,8 @@ public class WhenUtils { for (JetWhenEntry entry : whenExpression.getEntries()) { JetExpression branchExpression = entry.getExpression(); - assertNotNull(branchExpression); if (entry.isElse()) { - //noinspection ConstantConditions builder.elseEntry(branchExpression); continue; } @@ -135,30 +133,21 @@ public class WhenUtils { if (conditionExpression instanceof JetIsExpression) { JetIsExpression isExpression = (JetIsExpression) conditionExpression; - - JetTypeReference typeReference = isExpression.getTypeRef(); - assertNotNull(typeReference); - - //noinspection ConstantConditions - builder.pattern(typeReference, isExpression.isNegated()); + builder.pattern(isExpression.getTypeRef(), isExpression.isNegated()); } else if (conditionExpression instanceof JetBinaryExpression) { JetBinaryExpression binaryExpression = (JetBinaryExpression) conditionExpression; JetExpression rhs = binaryExpression.getRight(); - assertNotNull(rhs); IElementType op = binaryExpression.getOperationToken(); if (op == JetTokens.IN_KEYWORD) { - //noinspection ConstantConditions builder.range(rhs, false); } else if (op == JetTokens.NOT_IN) { - //noinspection ConstantConditions builder.range(rhs, true); } else if (op == JetTokens.EQEQ) { - //noinspection ConstantConditions builder.condition(rhs); } else assert false : TRANSFORM_WITHOUT_CHECK; @@ -166,7 +155,6 @@ public class WhenUtils { else assert false : TRANSFORM_WITHOUT_CHECK; } - //noinspection ConstantConditions builder.branchExpression(branchExpression); } @@ -176,37 +164,22 @@ public class WhenUtils { static String whenConditionToExpressionText(@NotNull JetWhenCondition condition, JetExpression subject) { if (condition instanceof JetWhenConditionIsPattern) { JetWhenConditionIsPattern patternCondition = (JetWhenConditionIsPattern) condition; - - JetTypeReference typeReference = patternCondition.getTypeRef(); - assertNotNull(typeReference); - - assertNotNull(subject); - - //noinspection ConstantConditions - return toBinaryExpression(subject, (patternCondition.isNegated() ? "!is" : "is"), typeReference); + return toBinaryExpression(subject, (patternCondition.isNegated() ? "!is" : "is"), patternCondition.getTypeRef()); } if (condition instanceof JetWhenConditionInRange) { JetWhenConditionInRange rangeCondition = (JetWhenConditionInRange) condition; - - JetExpression rangeExpression = rangeCondition.getRangeExpression(); - assertNotNull(rangeExpression); - - assertNotNull(subject); - - //noinspection ConstantConditions - return toBinaryExpression(subject, rangeCondition.getOperationReference().getText(), rangeExpression); + return toBinaryExpression(subject, rangeCondition.getOperationReference().getText(), rangeCondition.getRangeExpression()); } assert condition instanceof JetWhenConditionWithExpression : TRANSFORM_WITHOUT_CHECK; JetExpression conditionExpression = ((JetWhenConditionWithExpression) condition).getExpression(); - assertNotNull(conditionExpression); - //noinspection ConstantConditions - return subject != null ? - toBinaryExpression(parenthesizeIfNeeded(subject), "==", parenthesizeIfNeeded(conditionExpression)) - : conditionExpression.getText(); + if (subject != null) { + return toBinaryExpression(parenthesizeIfNeeded(subject), "==", parenthesizeIfNeeded(conditionExpression)); + } + return conditionExpression != null ? conditionExpression.getText() : ""; } public static void eliminateWhenSubject(@NotNull JetWhenExpression whenExpression) { @@ -217,10 +190,8 @@ public class WhenUtils { for (JetWhenEntry entry : whenExpression.getEntries()) { JetExpression branchExpression = entry.getExpression(); - assertNotNull(branchExpression); if (entry.isElse()) { - //noinspection ConstantConditions builder.elseEntry(branchExpression); continue; @@ -230,7 +201,6 @@ public class WhenUtils { builder.condition(whenConditionToExpressionText(condition, subject)); } - //noinspection ConstantConditions builder.branchExpression(branchExpression); }