api for GrIfStatement

This commit is contained in:
Maxim Medvedev
2011-04-19 14:54:17 +04:00
parent 9d08dc45fc
commit a1f887806e
14 changed files with 65 additions and 71 deletions
@@ -167,12 +167,10 @@ class RecursionUtils {
private static boolean ifStatementMayReturnBeforeRecursing(
GrIfStatement ifStatement, GrMethod method) {
GrCondition condition = ifStatement.getCondition();
if (!(condition instanceof GrExpression)) {
return false;
}
final GrExpression test = (GrExpression) condition;
if (expressionDefinitelyRecurses(test, method)) {
GrExpression condition = ifStatement.getCondition();
if (condition == null) return false;
if (expressionDefinitelyRecurses(condition, method)) {
return false;
}
final GrStatement thenBranch = ifStatement.getThenBranch();
@@ -476,9 +474,8 @@ class RecursionUtils {
}
}
private static boolean switchStatementDefinitelyRecurses(
GrSwitchStatement switchStatement, GrMethod method) {
final GrExpression switchExpression = (GrExpression) switchStatement.getCondition();
private static boolean switchStatementDefinitelyRecurses(GrSwitchStatement switchStatement, GrMethod method) {
final GrExpression switchExpression = switchStatement.getCondition();
return expressionDefinitelyRecurses(switchExpression, method);
}
@@ -511,11 +508,10 @@ class RecursionUtils {
private static boolean ifStatementDefinitelyRecurses(
GrIfStatement ifStatement, GrMethod method) {
final GrCondition condition = ifStatement.getCondition();
if (!(condition instanceof GrExpression)) {
return false;
}
if (expressionDefinitelyRecurses((GrExpression) condition, method)) {
final GrExpression condition = ifStatement.getCondition();
if (condition == null) return false;
if (expressionDefinitelyRecurses(condition, method)) {
return true;
}
final GrStatement thenBranch = ifStatement.getThenBranch();
@@ -527,9 +523,9 @@ class RecursionUtils {
statementDefinitelyRecurses(elseBranch, method);
}
private static boolean forStatementDefinitelyRecurses(
GrForStatement forStatement, GrMethod method) {
private static boolean forStatementDefinitelyRecurses(GrForStatement forStatement, GrMethod method) {
final GrForClause clause = forStatement.getClause();
if (clause == null) return false;
final GrVariable var = clause.getDeclaredVariable();
if (var != null) {
final GrExpression initializer = var.getInitializerGroovy();
@@ -21,7 +21,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.codeInspection.BaseInspection;
import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor;
import org.jetbrains.plugins.groovy.intentions.utils.BoolUtils;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
@@ -53,11 +52,11 @@ public class GroovyNegatedIfInspection extends BaseInspection {
public void visitIfStatement(GrIfStatement grIfStatement) {
super.visitIfStatement(grIfStatement);
final GrCondition condition = grIfStatement.getCondition();
if (!(condition instanceof GrExpression)) {
final GrExpression condition = grIfStatement.getCondition();
if (condition == null) {
return;
}
if (!BoolUtils.isNegation((GrExpression) condition)) {
if (!BoolUtils.isNegation(condition)) {
return;
}
if (grIfStatement.getElseBranch() == null || grIfStatement.getThenBranch() == null) {
@@ -24,7 +24,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.codeInspection.BaseInspection;
import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor;
import org.jetbrains.plugins.groovy.codeInspection.GroovyFix;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
@@ -72,7 +71,7 @@ public class GroovyConstantIfStatementInspection extends BaseInspection {
assert ifStatement != null;
final GrStatement thenBranch = ifStatement.getThenBranch();
final GrStatement elseBranch = ifStatement.getElseBranch();
final GrExpression condition = (GrExpression) ifStatement.getCondition();
final GrExpression condition = ifStatement.getCondition();
// todo still needs some handling for conflicting declarations
if (isFalse(condition)) {
if (elseBranch != null) {
@@ -91,16 +90,15 @@ public class GroovyConstantIfStatementInspection extends BaseInspection {
public void visitIfStatement(GrIfStatement statement) {
super.visitIfStatement(statement);
final GrCondition condition = statement.getCondition();
if (!(condition instanceof GrExpression)) {
final GrExpression condition = statement.getCondition();
if (condition == null) {
return;
}
final GrStatement thenBranch = statement.getThenBranch();
if (thenBranch == null) {
return;
}
final GrExpression conditionExpression = (GrExpression) condition;
if (isTrue(conditionExpression) || isFalse(conditionExpression)) {
if (isTrue(condition) || isFalse(condition)) {
registerStatementError(statement);
}
}
@@ -27,7 +27,6 @@ import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor;
import org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils;
import org.jetbrains.plugins.groovy.codeInspection.utils.EquivalenceChecker;
import org.jetbrains.plugins.groovy.codeInspection.utils.SideEffectChecker;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSynchronizedStatement;
@@ -77,11 +76,11 @@ public class GroovyDoubleCheckedLockingInspection extends BaseInspection {
public void visitIfStatement(@NotNull GrIfStatement statement) {
super.visitIfStatement(statement);
final GrCondition outerCondition = statement.getCondition();
if (!(outerCondition instanceof GrExpression)) {
final GrExpression outerCondition = statement.getCondition();
if (outerCondition == null) {
return;
}
if (SideEffectChecker.mayHaveSideEffects((GrExpression) outerCondition)) {
if (SideEffectChecker.mayHaveSideEffects(outerCondition)) {
return;
}
GrStatement thenBranch = statement.getThenBranch();
@@ -106,12 +105,11 @@ public class GroovyDoubleCheckedLockingInspection extends BaseInspection {
return;
}
final GrIfStatement innerIf = (GrIfStatement) statements[0];
final GrCondition innerCondition = innerIf.getCondition();
if (!(innerCondition instanceof GrExpression)) {
final GrExpression innerCondition = innerIf.getCondition();
if (innerCondition == null) {
return;
}
if (!EquivalenceChecker.expressionsAreEquivalent((GrExpression) innerCondition,
(GrExpression) outerCondition)) {
if (!EquivalenceChecker.expressionsAreEquivalent(innerCondition, outerCondition)) {
return;
}
if (ignoreOnVolatileVariables &&
@@ -377,8 +377,8 @@ public class EquivalenceChecker {
private static boolean ifStatementsAreEquivalent(@NotNull GrIfStatement statement1,
@NotNull GrIfStatement statement2) {
final GrExpression condition1 = (GrExpression) statement1.getCondition();
final GrExpression condition2 = (GrExpression) statement2.getCondition();
final GrExpression condition1 = statement1.getCondition();
final GrExpression condition2 = statement2.getCondition();
final GrStatement thenBranch1 = statement1.getThenBranch();
final GrStatement thenBranch2 = statement2.getThenBranch();
final GrStatement elseBranch1 = statement1.getElseBranch();
@@ -42,12 +42,11 @@ public class MergeIfAndIntention extends Intention {
throws IncorrectOperationException {
final GrIfStatement parentStatement =
(GrIfStatement) element;
assert parentStatement != null;
final GrStatement parentThenBranch = (GrStatement) parentStatement.getThenBranch();
final GrStatement parentThenBranch = parentStatement.getThenBranch();
final GrIfStatement childStatement =
(GrIfStatement) ConditionalUtils.stripBraces(parentThenBranch);
final GrExpression childCondition = (GrExpression) childStatement.getCondition();
final GrExpression childCondition = childStatement.getCondition();
final String childConditionText;
if (ParenthesesUtils.getPrecendence(childCondition)
> ParenthesesUtils.AND_PRECEDENCE) {
@@ -56,7 +55,7 @@ public class MergeIfAndIntention extends Intention {
childConditionText = childCondition.getText();
}
final GrExpression parentCondition = (GrExpression) parentStatement.getCondition();
final GrExpression parentCondition = parentStatement.getCondition();
final String parentConditionText;
if (ParenthesesUtils.getPrecendence(parentCondition)
> ParenthesesUtils.AND_PRECEDENCE) {
@@ -65,7 +64,7 @@ public class MergeIfAndIntention extends Intention {
parentConditionText = parentCondition.getText();
}
final GrStatement childThenBranch = (GrStatement) childStatement.getThenBranch();
final GrStatement childThenBranch = childStatement.getThenBranch();
@NonNls final String statement =
"if(" + parentConditionText + "&&" + childConditionText + ')' +
childThenBranch.getText();
@@ -36,7 +36,6 @@ public class SplitElseIfIntention extends Intention {
public void processIntention(@NotNull PsiElement element, Project project, Editor editor)
throws IncorrectOperationException {
final GrIfStatement parentStatement = (GrIfStatement) element;
assert parentStatement != null;
final GrStatement elseBranch = parentStatement.getElseBranch();
IntentionUtils.replaceStatement("if(" + parentStatement.getCondition().getText()+ ")"+ parentStatement.getThenBranch().getText() +
"else{\n" + elseBranch.getText() +"\n}", parentStatement);
@@ -15,15 +15,14 @@
*/
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers.GrFixer;
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
/**
* User: Dmitry.Krasilschikov
@@ -36,7 +35,7 @@ public class GrIfConditionFixer implements GrFixer {
final GrIfStatement ifStatement = (GrIfStatement) psiElement;
final PsiElement rParen = ifStatement.getRParenth();
final PsiElement lParen = ifStatement.getLParenth();
final GrCondition condition = ifStatement.getCondition();
final GrExpression condition = ifStatement.getCondition();
if (condition == null) {
if (lParen == null || rParen == null) {
@@ -22,7 +22,6 @@ import com.intellij.util.IncorrectOperationException;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers.GrFixer;
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
/**
@@ -16,35 +16,36 @@
package org.jetbrains.plugins.groovy.lang.psi.api.statements;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition;
import org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.annotations.Nullable;
import com.intellij.util.IncorrectOperationException;
import com.intellij.psi.PsiJavaToken;
import com.intellij.psi.PsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
/**
* @autor: ilyas
*/
public interface GrIfStatement extends GrStatement, GrControlStatement {
public GrCondition getCondition();
public GrStatement getThenBranch();
public GrStatement getElseBranch();
public GrStatement replaceThenBranch(GrStatement newBranch) throws IncorrectOperationException;
public GrStatement replaceElseBranch(GrStatement newBranch) throws IncorrectOperationException;
@Nullable
GrExpression getCondition();
@Nullable
public PsiElement getElseKeyword();
GrStatement getThenBranch();
@Nullable
public PsiElement getRParenth();
GrStatement getElseBranch();
GrStatement replaceThenBranch(GrStatement newBranch) throws IncorrectOperationException;
GrStatement replaceElseBranch(GrStatement newBranch) throws IncorrectOperationException;
@Nullable
public PsiElement getLParenth();
PsiElement getElseKeyword();
@Nullable
PsiElement getRParenth();
@Nullable
PsiElement getLParenth();
}
@@ -20,12 +20,14 @@ import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl;
/**
@@ -44,17 +46,19 @@ public class GrIfStatementImpl extends GroovyPsiElementImpl implements GrIfState
return "IF statement";
}
public GrCondition getCondition() {
@Nullable
public GrExpression getCondition() {
PsiElement lParenth = getLParenth();
if (lParenth == null) return null;
PsiElement afterLParen = lParenth.getNextSibling();
if (afterLParen instanceof GrCondition) return ((GrCondition) afterLParen);
if (afterLParen instanceof GrExpression) return (GrExpression)afterLParen;
return null;
}
@Nullable
public GrStatement getThenBranch() {
GrStatement[] statements = findChildrenByClass(GrStatement.class);
@@ -64,6 +68,7 @@ public class GrIfStatementImpl extends GroovyPsiElementImpl implements GrIfState
return null;
}
@Nullable
public GrStatement getElseBranch() {
GrStatement[] statements = findChildrenByClass(GrStatement.class);
if (statements.length == 3 && (statements[2] instanceof GrStatement)) {
@@ -74,11 +79,12 @@ public class GrIfStatementImpl extends GroovyPsiElementImpl implements GrIfState
}
public GrStatement replaceThenBranch(GrStatement newBranch) throws IncorrectOperationException {
if (getThenBranch() == null ||
final GrStatement thenBranch = getThenBranch();
if (thenBranch == null ||
newBranch == null) {
throw new IncorrectOperationException();
}
ASTNode oldBodyNode = getThenBranch().getNode();
ASTNode oldBodyNode = thenBranch.getNode();
if (oldBodyNode.getTreePrev() != null &&
GroovyTokenTypes.mNLS.equals(oldBodyNode.getTreePrev().getElementType())) {
ASTNode whiteNode = GroovyPsiElementFactory.getInstance(getProject()).createWhiteSpace().getNode();
@@ -93,11 +99,11 @@ public class GrIfStatementImpl extends GroovyPsiElementImpl implements GrIfState
}
public GrStatement replaceElseBranch(GrStatement newBranch) throws IncorrectOperationException {
if (getElseBranch() == null ||
newBranch == null) {
final GrStatement elseBranch = getElseBranch();
if (elseBranch == null || newBranch == null) {
throw new IncorrectOperationException();
}
ASTNode oldBodyNode = getElseBranch().getNode();
ASTNode oldBodyNode = elseBranch.getNode();
if (oldBodyNode.getTreePrev() != null &&
GroovyTokenTypes.mNLS.equals(oldBodyNode.getTreePrev().getElementType())) {
ASTNode whiteNode = GroovyPsiElementFactory.getInstance(getProject()).createWhiteSpace().getNode();
@@ -29,7 +29,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpres
public class IfElseExprSurrounder extends GroovyConditionSurrounder {
protected TextRange surroundExpression(GrExpression expression) {
GrIfStatement ifStatement = (GrIfStatement) GroovyPsiElementFactory.getInstance(expression.getProject()).createTopElementFromText("if(a){4\n} else{\n}");
replaceToOldExpression((GrExpression) ifStatement.getCondition(), expression);
replaceToOldExpression(ifStatement.getCondition(), expression);
ifStatement = expression.replaceWithStatement(ifStatement);
GrStatement psiElement = ifStatement.getThenBranch();
@@ -29,7 +29,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpres
public class IfExprSurrounder extends GroovyConditionSurrounder {
protected TextRange surroundExpression(GrExpression expression) {
GrIfStatement ifStatement = (GrIfStatement) GroovyPsiElementFactory.getInstance(expression.getProject()).createTopElementFromText("if(a){4\n}");
replaceToOldExpression((GrExpression)ifStatement.getCondition(), expression);
replaceToOldExpression(ifStatement.getCondition(), expression);
ifStatement = expression.replaceWithStatement(ifStatement);
GrStatement thenBranch = ifStatement.getThenBranch();
@@ -173,7 +173,7 @@ public class CodeBlockGenerator extends Generator {
@Override
public void visitIfStatement(GrIfStatement ifStatement) {
final GrCondition condition = ifStatement.getCondition();
final GrExpression condition = ifStatement.getCondition();
final GrStatement thenBranch = ifStatement.getThenBranch();
final GrStatement elseBranch = ifStatement.getElseBranch();