mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEADEV-522
This commit is contained in:
@@ -12,6 +12,7 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.PsiBaseElementProcessor;
|
||||
import com.intellij.psi.search.PsiElementProcessor;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
@@ -19,6 +20,7 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class RedundantCastUtil {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.redundantCast.RedundantCastUtil");
|
||||
public static List<PsiTypeCastExpression> getRedundantCasts(PsiElement where) {
|
||||
final ArrayList<PsiTypeCastExpression> result = new ArrayList<PsiTypeCastExpression>();
|
||||
PsiElementProcessor<PsiTypeCastExpression> processor = new PsiBaseElementProcessor<PsiTypeCastExpression>() {
|
||||
@@ -47,10 +49,6 @@ public class RedundantCastUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public void visitReferenceExpression(PsiReferenceExpression expression) {
|
||||
visitElement(expression);
|
||||
}
|
||||
|
||||
public void visitConditionalExpression(PsiConditionalExpression expression) {
|
||||
// Do not go inside conditional expression because branches are required to be exactly the same type, not assignable.
|
||||
}
|
||||
@@ -296,14 +294,15 @@ public class RedundantCastUtil {
|
||||
if (fromType == null || toType == null) return;
|
||||
if (parent instanceof PsiReferenceExpression) {
|
||||
if (toType instanceof PsiClassType && fromType instanceof PsiPrimitiveType) return; //explicit boxing
|
||||
|
||||
//Check accessibility
|
||||
if (fromType instanceof PsiClassType) {
|
||||
PsiElement element = ((PsiReferenceExpression)parent).resolve();
|
||||
final PsiReferenceExpression refExpression = ((PsiReferenceExpression)parent);
|
||||
PsiElement element = refExpression.resolve();
|
||||
if (!(element instanceof PsiMember)) return;
|
||||
PsiClass accessClass = ((PsiClassType)fromType).resolve();
|
||||
if (accessClass == null) return;
|
||||
if (!parent.getManager().getResolveHelper().isAccessible((PsiMember)element, typeCast, accessClass)) return;
|
||||
if (!isCastRedundantInRefExpression(refExpression, typeCast.getOperand())) return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,6 +312,21 @@ public class RedundantCastUtil {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isCastRedundantInRefExpression (PsiReferenceExpression refExpression, PsiExpression castOperand) {
|
||||
PsiElement resolved = refExpression.resolve();
|
||||
final PsiReferenceExpression copy = (PsiReferenceExpression)refExpression.copy();
|
||||
try {
|
||||
copy.getQualifierExpression().replace(castOperand);
|
||||
if (copy.resolve() != resolved) return false;
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isTypeCastSemantical(PsiTypeCastExpression typeCast) {
|
||||
PsiExpression operand = typeCast.getOperand();
|
||||
if (operand != null) {
|
||||
|
||||
Reference in New Issue
Block a user