diff --git a/java/java-impl/src/com/intellij/psi/impl/source/PsiParameterImpl.java b/java/java-impl/src/com/intellij/psi/impl/source/PsiParameterImpl.java index 595131f23757..f9f1b3dcc076 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/PsiParameterImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/PsiParameterImpl.java @@ -179,7 +179,7 @@ public class PsiParameterImpl extends JavaStubPsiElement imple public PsiElement getDeclarationScope() { final PsiElement parent = getParent(); if (parent == null) return this; - if (parent instanceof PsiParameterList){ + if (parent instanceof PsiParameterList) { return parent.getParent(); } if (parent instanceof PsiForeachStatement) { @@ -191,16 +191,17 @@ public class PsiParameterImpl extends JavaStubPsiElement imple PsiElement[] children = parent.getChildren(); //noinspection ConstantConditions if (children != null) { - for(int i = 0; i < children.length; i++){ - if (children[i].equals(this)){ - while(!(children[i] instanceof PsiCodeBlock)){ + for (int i = 0; i < children.length; i++) { + if (children[i].equals(this)) { + while (!(children[i] instanceof PsiCodeBlock)) { + //noinspection AssignmentToForLoopParameter i++; } return children[i]; } } } - LOG.error("codeblock not found among parameter' "+this+" parents children: "+ Arrays.asList(children)); + LOG.error("Code block not found among parameter' " + this + " parents children: " + Arrays.asList(children)); return null; } @@ -211,7 +212,7 @@ public class PsiParameterImpl extends JavaStubPsiElement imple } myCachedType = null; - return SourceTreeToPsiMap.psiElementToTree(getTypeElement()).findChildByType(JavaTokenType.ELLIPSIS) != null; + return SourceTreeToPsiMap.psiToTreeNotNull(getTypeElement()).findChildByType(JavaTokenType.ELLIPSIS) != null; } @NotNull diff --git a/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiReferenceExpressionImpl.java b/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiReferenceExpressionImpl.java index 444bd687db57..d85d4a99a724 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiReferenceExpressionImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiReferenceExpressionImpl.java @@ -49,12 +49,8 @@ import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.PsiUtil; import com.intellij.psi.util.TypeConversionUtil; import com.intellij.refactoring.util.RefactoringUtil; -import com.intellij.util.ArrayUtil; -import com.intellij.util.CharTable; -import com.intellij.util.Function; -import com.intellij.util.IncorrectOperationException; +import com.intellij.util.*; import gnu.trove.THashSet; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -277,20 +273,18 @@ public class PsiReferenceExpressionImpl extends ExpressionPsiElement implements return element.getText(); } - @NonNls private static final String LENGTH = "length"; + private final Function ourTypeEvaluator = new TypeEvaluator(); - private final TypeEvaluator ourTypeEvaluator = new TypeEvaluator(); - - private static class TypeEvaluator implements Function { + private static class TypeEvaluator implements NullableFunction { public PsiType fun(final PsiReferenceExpressionImpl expr) { JavaResolveResult result = expr.advancedResolve(false); PsiElement resolve = result.getElement(); if (resolve == null) { ASTNode refName = expr.findChildByRole(ChildRole.REFERENCE_NAME); - if (refName != null && refName.getText().equals(LENGTH)) { + if (refName != null && "length".equals(refName.getText())) { ASTNode qualifier = expr.findChildByRole(ChildRole.QUALIFIER); if (qualifier != null && ElementType.EXPRESSION_BIT_SET.contains(qualifier.getElementType())) { - PsiType type = ((PsiExpression)SourceTreeToPsiMap.treeElementToPsi(qualifier)).getType(); + PsiType type = SourceTreeToPsiMap.treeToPsiNotNull(qualifier).getType(); if (type instanceof PsiArrayType) { return PsiType.INT; } @@ -300,30 +294,32 @@ public class PsiReferenceExpressionImpl extends ExpressionPsiElement implements } PsiTypeParameterListOwner owner = null; - PsiType ret; + PsiType ret = null; if (resolve instanceof PsiVariable) { PsiType type = ((PsiVariable)resolve).getType(); ret = type instanceof PsiEllipsisType ? ((PsiEllipsisType)type).toArrayType() : type; - if (resolve instanceof PsiField && !((PsiField)resolve).hasModifierProperty(PsiModifier.STATIC)) owner = ((PsiField)resolve).getContainingClass(); + if (resolve instanceof PsiField && !((PsiField)resolve).hasModifierProperty(PsiModifier.STATIC)) { + owner = ((PsiField)resolve).getContainingClass(); + } } else if (resolve instanceof PsiMethod) { PsiMethod method = (PsiMethod)resolve; ret = method.getReturnType(); owner = method; } - else { - return null; - } if (ret == null) return null; + final LanguageLevel languageLevel = PsiUtil.getLanguageLevel(expr); if (ret instanceof PsiClassType) { ret = ((PsiClassType)ret).setLanguageLevel(languageLevel); } - if (languageLevel.compareTo(LanguageLevel.JDK_1_5) >= 0) { - if (owner != null && PsiUtil.isRawSubstitutor(owner, result.getSubstitutor())) return TypeConversionUtil.erasure(ret); - PsiType substitutedType = result.getSubstitutor().substitute(ret); - return PsiImplUtil.normalizeWildcardTypeByPosition(substitutedType, expr); + if (languageLevel.isAtLeast(LanguageLevel.JDK_1_5)) { + final PsiSubstitutor substitutor = result.getSubstitutor(); + if (owner == null || !PsiUtil.isRawSubstitutor(owner, substitutor)) { + PsiType substitutedType = substitutor.substitute(ret); + return PsiImplUtil.normalizeWildcardTypeByPosition(substitutedType, expr); + } } return TypeConversionUtil.erasure(ret); diff --git a/java/openapi/src/com/intellij/psi/PsiExpression.java b/java/openapi/src/com/intellij/psi/PsiExpression.java index 8ec1cbe6537f..958604512d75 100644 --- a/java/openapi/src/com/intellij/psi/PsiExpression.java +++ b/java/openapi/src/com/intellij/psi/PsiExpression.java @@ -16,6 +16,7 @@ package com.intellij.psi; import com.intellij.util.Function; +import com.intellij.util.NullableFunction; import org.jetbrains.annotations.Nullable; /** @@ -26,7 +27,8 @@ public interface PsiExpression extends PsiAnnotationMemberValue { * The empty array of PSI expressions which can be reused to avoid unnecessary allocations. */ PsiExpression[] EMPTY_ARRAY = new PsiExpression[0]; - Function EXPRESSION_TO_TYPE = new Function() { + + Function EXPRESSION_TO_TYPE = new NullableFunction() { public PsiType fun(final PsiExpression expression) { return expression.getType(); } diff --git a/java/openapi/src/com/intellij/psi/util/PsiUtil.java b/java/openapi/src/com/intellij/psi/util/PsiUtil.java index 97d14f4c5fd9..7987c44956ad 100644 --- a/java/openapi/src/com/intellij/psi/util/PsiUtil.java +++ b/java/openapi/src/com/intellij/psi/util/PsiUtil.java @@ -423,8 +423,10 @@ public final class PsiUtil extends PsiUtilBase { public static boolean isApplicable(PsiMethod method, PsiSubstitutor substitutorForMethod, PsiExpressionList argList) { return getApplicabilityLevel(method, substitutorForMethod, argList) != ApplicabilityLevel.NOT_APPLICABLE; } + public static boolean isApplicable(PsiMethod method, PsiSubstitutor substitutorForMethod, PsiExpression[] argList) { - return getApplicabilityLevel(method, substitutorForMethod, ContainerUtil.map2Array(argList, PsiType.class, PsiExpression.EXPRESSION_TO_TYPE),getLanguageLevel(method)) != ApplicabilityLevel.NOT_APPLICABLE; + final PsiType[] types = ContainerUtil.map2Array(argList, PsiType.class, PsiExpression.EXPRESSION_TO_TYPE); + return getApplicabilityLevel(method, substitutorForMethod, types, getLanguageLevel(method)) != ApplicabilityLevel.NOT_APPLICABLE; } public static int getApplicabilityLevel(PsiMethod method, PsiSubstitutor substitutorForMethod, PsiExpressionList argList) {