if (concatenation == null) return null;
final PsiType type = appendable.getType();
if (type == null) return null;
- final StringBuilder result =
- buildAppendExpression(concatenation, type.equalsToText("java.lang.Appendable"), new StringBuilder(appendable.getText()));
+ final boolean useStringValueOf = !type.equalsToText(CommonClassNames.JAVA_LANG_STRING_BUFFER) &&
+ !type.equalsToText(CommonClassNames.JAVA_LANG_STRING_BUILDER);
+ final StringBuilder result = buildAppendExpression(concatenation, useStringValueOf, new StringBuilder(appendable.getText()));
if (result == null) return null;
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(appendable.getProject());
return factory.createExpressionFromText(result.toString(), appendable);
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
+import com.intellij.psi.util.InheritanceUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
private final IElementType myTokenType;
private final PsiType myLhsType;
private final PsiAssignmentExpression myAssignmentExpression;
+ private volatile TypeInfo myTypeInfo;
public ChangeToAppendFix(@NotNull IElementType eqOpSign, @NotNull PsiType lType, @NotNull PsiAssignmentExpression assignmentExpression) {
myTokenType = eqOpSign;
public String getText() {
return QuickFixBundle.message("change.to.append.text",
ChangeToAppendUtil.buildAppendExpression(myAssignmentExpression.getRExpression(),
- myLhsType.equalsToText("java.lang.Appendable"),
- new StringBuilder(
- myAssignmentExpression.getLExpression().getText())));
+ getTypeInfo().myUseStringValueOf,
+ new StringBuilder(myAssignmentExpression.getLExpression().getText())));
}
@NotNull
return JavaTokenType.PLUSEQ == myTokenType &&
myAssignmentExpression.isValid() &&
PsiManager.getInstance(project).isInProject(myAssignmentExpression) &&
- (myLhsType.equalsToText(CommonClassNames.JAVA_LANG_STRING_BUILDER) ||
- myLhsType.equalsToText(CommonClassNames.JAVA_LANG_STRING_BUFFER) ||
- myLhsType.equalsToText("java.lang.Appendable"));
+ getTypeInfo().myAppendable;
}
@Override
if (appendExpression == null) return;
myAssignmentExpression.replace(appendExpression);
}
+
+ @NotNull
+ private TypeInfo getTypeInfo() {
+ if (myTypeInfo != null) return myTypeInfo;
+ myTypeInfo = calculateTypeInfo();
+ return myTypeInfo;
+ }
+
+ @NotNull
+ private TypeInfo calculateTypeInfo() {
+ if (myLhsType.equalsToText(CommonClassNames.JAVA_LANG_STRING_BUILDER) ||
+ myLhsType.equalsToText(CommonClassNames.JAVA_LANG_STRING_BUFFER)) {
+ return new TypeInfo(true, false);
+ }
+ if (InheritanceUtil.isInheritor(myLhsType, "java.lang.Appendable")) {
+ return new TypeInfo(true, true);
+ }
+ return new TypeInfo(false, false);
+ }
+
+ private static class TypeInfo {
+ private final boolean myAppendable;
+ private final boolean myUseStringValueOf;
+
+ TypeInfo(boolean appendable, boolean useStringValueOf) {
+ myAppendable = appendable;
+ myUseStringValueOf = useStringValueOf;
+ }
+ }
}