diff --git a/plugins/IntentionPowerPak/src/META-INF/plugin.xml b/plugins/IntentionPowerPak/src/META-INF/plugin.xml index 380808c9acdb..7ea25d31c697 100644 --- a/plugins/IntentionPowerPak/src/META-INF/plugin.xml +++ b/plugins/IntentionPowerPak/src/META-INF/plugin.xml @@ -367,6 +367,10 @@ com.siyeh.ipp.asserttoif.AssertToIfIntention intention.category.other + + com.siyeh.ipp.asserttoif.IfToAssertionIntention + intention.category.other + com.siyeh.ipp.enumswitch.CreateEnumSwitchBranchesIntention intention.category.other diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index e632bb3b6277..ecbf16303814 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -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 '||' diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/AssertToIfIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/AssertToIfIntention.java index 7354fa52a1c3..f393b5fd751c 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/AssertToIfIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/AssertToIfIntention.java @@ -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); diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/IfStatementPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/IfStatementPredicate.java new file mode 100644 index 000000000000..178b4f403924 --- /dev/null +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/IfStatementPredicate.java @@ -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; + } +} diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/IfToAssertionIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/IfToAssertionIntention.java new file mode 100644 index 000000000000..4e3e68528dbb --- /dev/null +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/IfToAssertionIntention.java @@ -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; + } +} diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/switchtoif/IfToSwitchPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/switchtoif/IfToSwitchPredicate.java index ea026df4f17d..54d148bbdbc4 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/switchtoif/IfToSwitchPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/switchtoif/IfToSwitchPredicate.java @@ -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; } diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/after.java.template index 5f8c67e08b8f..b34986a06ef9 100644 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/after.java.template +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/after.java.template @@ -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"); } } } \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/before.java.template index 043b141e9a1b..3e6f25242330 100644 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/before.java.template +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/before.java.template @@ -1,5 +1,5 @@ public class X { void f(Object foo) { - assert foo!=null: "Argument is null"; + assert foo != null: "Argument is null"; } } \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/description.html b/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/description.html index d26d7dc650fc..f14615dee136 100644 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/description.html +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/AssertToIfIntention/description.html @@ -1,7 +1,7 @@ -This intention converts a selected assert statement with an - if statement which throws an IllegalArgumentException if the asserted condition is false. +This intention replaces a selected assert statement with an + if statement which throws an AssertionError if the asserted condition is false. diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/IfToAssertionIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/IfToAssertionIntention/after.java.template new file mode 100644 index 000000000000..064643ed0caa --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/IfToAssertionIntention/after.java.template @@ -0,0 +1,5 @@ +public class X { + void f(Object foo) { + assert foo != null : "Argument is null"; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/IfToAssertionIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/IfToAssertionIntention/before.java.template new file mode 100644 index 000000000000..b6dad71f2e90 --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/IfToAssertionIntention/before.java.template @@ -0,0 +1,7 @@ +public class X { + void f(Object foo) { + if (foo==null) { + throw new AssertionError("Argument is null"); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/IfToAssertionIntention/description.html b/plugins/IntentionPowerPak/src/intentionDescriptions/IfToAssertionIntention/description.html new file mode 100644 index 000000000000..cdaf1a0c60b3 --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/IfToAssertionIntention/description.html @@ -0,0 +1,7 @@ + + +This intention replaces a selected if statement + which throws an AssertionError if the condition is false, with an assert statement. + + +