java: no annotations needed in type cast

This commit is contained in:
Roman Shevchenko
2014-03-06 10:55:44 +01:00
parent 1687a22e89
commit 4969888e1f
4 changed files with 12 additions and 11 deletions
@@ -81,14 +81,14 @@ public class AddTypeCastFix extends LocalQuickFixAndIntentionActionOnPsiElement
originalExpression.replace(typeCast);
}
static PsiExpression createCastExpression(PsiExpression originalExpression, Project project, PsiType type) {
static PsiExpression createCastExpression(PsiExpression original, Project project, PsiType type) {
// remove nested casts
PsiElement expression = PsiUtil.deparenthesizeExpression(originalExpression);
PsiElement expression = PsiUtil.deparenthesizeExpression(original);
if (expression == null) return null;
PsiElementFactory factory = JavaPsiFacade.getInstance(originalExpression.getProject()).getElementFactory();
PsiTypeCastExpression typeCast = (PsiTypeCastExpression)factory.createExpressionFromText("(Type)value", null);
assertNotNull(typeCast.getCastType()).replace(factory.createTypeElement(type));
PsiElementFactory factory = JavaPsiFacade.getInstance(original.getProject()).getElementFactory();
String text = "(" + type.getCanonicalText(false) + ")value";
PsiTypeCastExpression typeCast = (PsiTypeCastExpression)factory.createExpressionFromText(text, original);
typeCast = (PsiTypeCastExpression)CodeStyleManager.getInstance(project).reformat(typeCast);
if (expression instanceof PsiConditionalExpression) {
@@ -124,5 +124,4 @@ public class AddTypeCastFix extends LocalQuickFixAndIntentionActionOnPsiElement
public boolean startInWriteAction() {
return true;
}
}
@@ -7,6 +7,6 @@ import static java.lang.annotation.ElementType.*;
class C {
{
Object o = null;
@TA int @TA [] a = (@TA int @TA[]) o;
@TA int @TA [] a = (int[]) o;
}
}
@@ -1,5 +1,6 @@
// "Cast to 'java.lang.Integer'" "true"
// "Cast to 'java.util.List<java.lang.Integer>'" "true"
import java.lang.annotation.*;
import java.util.*;
import static java.lang.annotation.ElementType.*;
@Target({TYPE_USE}) @interface TA { }
@@ -7,6 +8,6 @@ import static java.lang.annotation.ElementType.*;
class C {
{
Object o = null;
@TA <caret>Integer i = (@TA Integer) o;
@TA List<@TA Integer> i = (List<Integer>) o;
}
}
@@ -1,5 +1,6 @@
// "Cast to 'java.lang.Integer'" "true"
// "Cast to 'java.util.List<java.lang.Integer>'" "true"
import java.lang.annotation.*;
import java.util.*;
import static java.lang.annotation.ElementType.*;
@Target({TYPE_USE}) @interface TA { }
@@ -7,6 +8,6 @@ import static java.lang.annotation.ElementType.*;
class C {
{
Object o = null;
@TA <caret>Integer i = o;
@TA <caret>List<@TA Integer> i = o;
}
}