IDEA-13197 ("Convert to MessageFormat.format" / "Convert to String.format" should not run inside annotation parameter)

for "Replace + with StringBuffer" and "Replace + with String.format()"
This commit is contained in:
Bas Leijdekkers
2011-05-13 16:37:42 +02:00
parent 763c3f2504
commit 5128d8c736
5 changed files with 49 additions and 13 deletions
@@ -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
@@ -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 {
}
}
}
}
}
@@ -31,7 +31,7 @@ public class ReplaceConcatenationWithFormatStringIntention
@Override
@NotNull
protected PsiElementPredicate getElementPredicate() {
return new SimpleStringConcatenationPredicate();
return new SimpleStringConcatenationPredicate(true);
}
@Override
@@ -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
@@ -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;
}
}