IDEA-62171 (Intention on assert) adds "Replace If with Assert Statement" intention

This commit is contained in:
Bas Leijdekkers
2010-12-02 15:05:16 +01:00
parent 2ad71fb92a
commit bbf281878a
12 changed files with 206 additions and 14 deletions
@@ -367,6 +367,10 @@
<className>com.siyeh.ipp.asserttoif.AssertToIfIntention</className>
<categoryKey>intention.category.other</categoryKey>
</intentionAction>
<intentionAction>
<className>com.siyeh.ipp.asserttoif.IfToAssertionIntention</className>
<categoryKey>intention.category.other</categoryKey>
</intentionAction>
<intentionAction>
<className>com.siyeh.ipp.enumswitch.CreateEnumSwitchBranchesIntention</className>
<categoryKey>intention.category.other</categoryKey>
@@ -134,6 +134,8 @@ replace.postfix.expression.with.assignment.intention.family.name=Replace Postfix
replace.assignment.with.postfix.expression.intention.family.name=Replace Assignment with Postfix Expression
convert.j.unit3.test.case.to.j.unit4.intention.family.name=Convert JUnit3 Test Case to JUnit4
convert.j.unit3.test.case.to.j.unit4.intention.name=Convert to JUnit4 Test Case
if.to.assertion.intention.family.name=Replace If with Assert Statement
if.to.assertion.intention.name=Replace if with assert statement
#hand made
demorgans.intention.name1=Replace '\\&\\&' with '||'
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2005 Dave Griffith
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,28 +27,28 @@ import org.jetbrains.annotations.NotNull;
public class AssertToIfIntention extends Intention {
@Override
@NotNull
protected PsiElementPredicate getElementPredicate() {
return new AssertStatementPredicate();
}
public void processIntention(PsiElement element)
@Override
public void processIntention(@NotNull PsiElement element)
throws IncorrectOperationException {
final PsiAssertStatement assertStatement = (PsiAssertStatement)element;
assert assertStatement != null;
final PsiExpression condition = assertStatement.getAssertCondition();
final PsiExpression description =
assertStatement.getAssertDescription();
final String negatedConditionString =
BoolUtils.getNegatedExpressionText(condition);
@NonNls final String newStatement;
if (description == null) {
newStatement = "if(" + negatedConditionString +
"){ throw new IllegalArgumentException();}";
"){ throw new java.lang.AssertionError();}";
} else {
newStatement = "if(" + negatedConditionString +
"){ throw new IllegalArgumentException(" +
"){ throw new java.lang.AssertionError(" +
description.getText() + ");}";
}
replaceStatement(newStatement, assertStatement);
@@ -0,0 +1,78 @@
/*
* Copyright 2010 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ipp.asserttoif;
import com.intellij.psi.*;
import com.siyeh.ipp.base.PsiElementPredicate;
class IfStatementPredicate implements PsiElementPredicate {
public boolean satisfiedBy(PsiElement element) {
if(!(element instanceof PsiJavaToken)){
return false;
}
final PsiJavaToken token = (PsiJavaToken)element;
if(token.getTokenType() != JavaTokenType.IF_KEYWORD){
return false;
}
final PsiElement parent = element.getParent();
if(!(parent instanceof PsiIfStatement)){
return false;
}
final PsiIfStatement statement = (PsiIfStatement) parent;
final PsiStatement elseBranch = statement.getElseBranch();
if (elseBranch != null) {
return false;
}
final PsiStatement thenBranch = statement.getThenBranch();
return isThrowNewAssertionError(thenBranch);
}
private static boolean isThrowNewAssertionError(PsiElement element) {
if (element instanceof PsiThrowStatement) {
final PsiThrowStatement throwStatement =
(PsiThrowStatement) element;
final PsiExpression exception = throwStatement.getException();
if (!(exception instanceof PsiNewExpression)) {
return false;
}
final PsiNewExpression newExpression = (PsiNewExpression) exception;
final PsiJavaCodeReferenceElement classReference =
newExpression.getClassReference();
if (classReference == null) {
return false;
}
final PsiElement target = classReference.resolve();
if (!(target instanceof PsiClass)) {
return false;
}
final PsiClass aClass = (PsiClass) target;
final String qualifiedName = aClass.getQualifiedName();
return "java.lang.AssertionError".equals(qualifiedName);
} else if (element instanceof PsiBlockStatement) {
final PsiBlockStatement blockStatement =
(PsiBlockStatement) element;
final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
final PsiStatement[] statements = codeBlock.getStatements();
if (statements.length != 1) {
return false;
}
final PsiStatement statement = statements[0];
return isThrowNewAssertionError(statement);
}
return false;
}
}
@@ -0,0 +1,88 @@
/*
* Copyright 2010 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.siyeh.ipp.asserttoif;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.BoolUtils;
import org.jetbrains.annotations.NotNull;
public class IfToAssertionIntention extends Intention {
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new IfStatementPredicate();
}
@Override
protected void processIntention(@NotNull PsiElement element)
throws IncorrectOperationException {
final PsiElement parent = element.getParent();
if (!(parent instanceof PsiIfStatement)) {
return;
}
final PsiIfStatement ifStatement = (PsiIfStatement) parent;
final PsiExpression condition = ifStatement.getCondition();
final String negatedExpressionText =
BoolUtils.getNegatedExpressionText(condition);
final StringBuilder newStatementText = new StringBuilder("assert ");
newStatementText.append(negatedExpressionText);
final PsiStatement thenBranch = ifStatement.getThenBranch();
final String message = getMessage(thenBranch);
if (message != null) {
newStatementText.append(':');
newStatementText.append(message);
}
newStatementText.append(';');
replaceStatement(newStatementText.toString(), ifStatement);
}
private static String getMessage(PsiElement element) {
if (element instanceof PsiBlockStatement) {
final PsiBlockStatement blockStatement = (PsiBlockStatement) element;
final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
final PsiStatement[] statements = codeBlock.getStatements();
if (statements.length != 1) {
return null;
}
final PsiStatement statement = statements[0];
return getMessage(statement);
} else if (element instanceof PsiThrowStatement) {
final PsiThrowStatement throwStatement = (PsiThrowStatement) element;
final PsiExpression exception = throwStatement.getException();
if (!(exception instanceof PsiNewExpression)) {
return null;
}
final PsiNewExpression newExpression = (PsiNewExpression) exception;
final PsiExpressionList argumentList =
newExpression.getArgumentList();
if (argumentList == null) {
return null;
}
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length != 1) {
return null;
}
final PsiExpression argument = arguments[0];
return argument.getText();
}
return null;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2005 Dave Griffith
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,10 +32,11 @@ class IfToSwitchPredicate implements PsiElementPredicate{
if(token.getTokenType() != JavaTokenType.IF_KEYWORD){
return false;
}
if(!(element.getParent() instanceof PsiIfStatement)){
final PsiElement parent = element.getParent();
if(!(parent instanceof PsiIfStatement)){
return false;
}
final PsiIfStatement statement = (PsiIfStatement) element.getParent();
final PsiIfStatement statement = (PsiIfStatement) parent;
if(ErrorUtil.containsError(statement)){
return false;
}
@@ -1,7 +1,7 @@
public class X {
void f(Object foo) {
if(foo==null){
throw new IllegalArgumentException("Argument is null");
if (foo==null) {
throw new AssertionError("Argument is null");
}
}
}
@@ -1,5 +1,5 @@
public class X {
void f(Object foo) {
<spot>assert foo!=null: "Argument is null";</spot>
<spot>assert foo != null: "Argument is null";</spot>
}
}
@@ -1,7 +1,7 @@
<html>
<body>
<font face="verdana" size="-1">This intention converts a selected <b>assert</b> statement with an
<b>if</b> statement which throws an IllegalArgumentException if the asserted condition is false.
<font face="verdana" size="-1">This intention replaces a selected <b>assert</b> statement with an
<b>if</b> statement which throws an AssertionError if the asserted condition is false.
</font>
</body>
</html>
@@ -0,0 +1,5 @@
public class X {
void f(Object foo) {
assert foo != null : "Argument is null";
}
}
@@ -0,0 +1,7 @@
public class X {
void f(Object foo) {
<spot>if</spot> (foo==null) {
throw new AssertionError("Argument is null");
}
}
}
@@ -0,0 +1,7 @@
<html>
<body>
<font face="verdana" size="-1">This intention replaces a selected <b>if</b> statement
which throws an AssertionError if the condition is false, with an <b>assert</b> statement.
</font>
</body>
</html>