diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index 167383937143..2025851ab4b5 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -47,8 +47,9 @@ remove.unnecessary.parentheses.intention.name=Remove unnecessary parentheses remove.unnecessary.parentheses.intention.family.name=Remove Unnecessary Parentheses merge.if.and.intention.name=Merge nested 'if's merge.if.and.intention.family.name=Merge Nested Ifs to ANDed Condition -replace.concatenation.with.string.buffer.intention.name=Replace '+' with '.append()' -replace.concatenation.with.string.buffer.intention.family.name=Replace + with StringBuffer.append() +replace.concatenation.with.string.buffer.intention.name=Replace '+' with 'StringBuffer.append()' +replace.concatenation.with.string.builder.intention.name=Replace '+' with 'StringBuilder.append()' +replace.concatenation.with.string.buffer.intention.family.name=Replace + with StringBuilder.append() remove.conditional.intention.name=Simplify '?:' remove.conditional.intention.family.name=Remove Pointless Conditional move.comment.to.separate.line.intention.name=Move comment to separate line @@ -121,8 +122,8 @@ wrap.vararg.arguments.with.explicit.array.intention.name=Wrap vararg arguments w wrap.vararg.arguments.with.explicit.array.intention.family.name=Wrap Vararg Arguments with Explicit Array Creation extract.while.loop.condition.to.if.statement.intention.name=Extract condition to internal 'if' statement extract.while.loop.condition.to.if.statement.intention.family.name=Extract While Loop Condition to Internal If Statement -replace.concatenation.with.format.string.intention.name=Replace '+' with 'String.format()' call -replace.concatenation.with.format.string.intention.family.name=Replace String Concatenation with String.format() Call +replace.concatenation.with.format.string.intention.name=Replace '+' with 'String.format()' +replace.concatenation.with.format.string.intention.family.name=Replace String Concatenation with String.format() copy.concatenated.string.to.clipboard.intention.name=Copy String concatenation text to the clipboard copy.concatenated.string.to.clipboard.intention.family.name=Copy String Concatenation Text to the Clipboard flip.expression.intention.family.name=Flip Binary Expression diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/CopyConcatenatedStringToClipboardIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/CopyConcatenatedStringToClipboardIntention.java index a79dccd80c04..730cd0649435 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/CopyConcatenatedStringToClipboardIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/CopyConcatenatedStringToClipboardIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2010 Bas Leijdekkers + * Copyright 2008-2011 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,7 +31,7 @@ public class CopyConcatenatedStringToClipboardIntention extends Intention { @Override @NotNull protected PsiElementPredicate getElementPredicate() { - return new SimpleStringConcatenationPredicate(); + return new SimpleStringConcatenationPredicate(false); } @Override @@ -85,4 +85,4 @@ public class CopyConcatenatedStringToClipboardIntention extends Intention { } } } -} \ No newline at end of file +} diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithFormatStringIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithFormatStringIntention.java index 865f3f5db8ba..ff4318730190 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithFormatStringIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithFormatStringIntention.java @@ -31,7 +31,7 @@ public class ReplaceConcatenationWithFormatStringIntention @Override @NotNull protected PsiElementPredicate getElementPredicate() { - return new SimpleStringConcatenationPredicate(); + return new SimpleStringConcatenationPredicate(true); } @Override diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithStringBufferIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithStringBufferIntention.java index 3ad5fa884003..8e16a1c2cad4 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithStringBufferIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/ReplaceConcatenationWithStringBufferIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2011 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. @@ -18,19 +18,32 @@ package com.siyeh.ipp.concatenation; import com.intellij.psi.*; import com.intellij.psi.util.PsiUtil; import com.intellij.util.IncorrectOperationException; +import com.siyeh.IntentionPowerPackBundle; import com.siyeh.ipp.base.Intention; +import com.siyeh.ipp.base.MutablyNamedIntention; import com.siyeh.ipp.base.PsiElementPredicate; import com.siyeh.ipp.psiutils.ConcatenationUtils; import com.siyeh.ipp.psiutils.ParenthesesUtils; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; -public class ReplaceConcatenationWithStringBufferIntention extends Intention { +public class ReplaceConcatenationWithStringBufferIntention extends MutablyNamedIntention { + + @Override + protected String getTextForElement(PsiElement element) { + if (PsiUtil.isLanguageLevel5OrHigher(element)) { + return IntentionPowerPackBundle.message( + "replace.concatenation.with.string.builder.intention.name"); + } else { + return IntentionPowerPackBundle.message( + "replace.concatenation.with.string.buffer.intention.name"); + } + } @Override @NotNull public PsiElementPredicate getElementPredicate() { - return new SimpleStringConcatenationPredicate(); + return new SimpleStringConcatenationPredicate(true); } @Override diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/SimpleStringConcatenationPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/SimpleStringConcatenationPredicate.java index 3549a2807ea6..12b00a3feba2 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/SimpleStringConcatenationPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/concatenation/SimpleStringConcatenationPredicate.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2011 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. @@ -15,7 +15,8 @@ */ package com.siyeh.ipp.concatenation; -import com.intellij.psi.PsiElement; +import com.intellij.psi.*; +import com.intellij.psi.util.PsiTreeUtil; import com.siyeh.ipp.base.PsiElementPredicate; import com.siyeh.ipp.psiutils.ConcatenationUtils; import com.siyeh.ipp.psiutils.ErrorUtil; @@ -23,10 +24,31 @@ import com.siyeh.ipp.psiutils.ErrorUtil; class SimpleStringConcatenationPredicate implements PsiElementPredicate{ + private final boolean excludeConcatentationsInsideAnnotations; + + public SimpleStringConcatenationPredicate(boolean excludeConcatentationsInsideAnnotations) { + this.excludeConcatentationsInsideAnnotations = excludeConcatentationsInsideAnnotations; + } + public boolean satisfiedBy(PsiElement element){ if(!ConcatenationUtils.isConcatenation(element)){ return false; } + if (excludeConcatentationsInsideAnnotations && isInsideAnnotation(element)) { + return false; + } return !ErrorUtil.containsError(element); } + + private static boolean isInsideAnnotation(PsiElement element) { + for (int i = 0; i < 20 && element instanceof PsiBinaryExpression; i++) { + // optimization: don't check deep string concatenation more than 20 levels up. + element = element.getParent(); + if (element instanceof PsiNameValuePair || + element instanceof PsiArrayInitializerMemberValue) { + return true; + } + } + return false; + } }