IDEA-51686: Overloadded access to arrays

This commit is contained in:
Maxim Medvedev
2010-01-28 16:48:08 +03:00
parent 365ed16a15
commit 00051ab08f
3 changed files with 41 additions and 16 deletions
@@ -17,9 +17,9 @@
package org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.path;
import com.intellij.lang.ASTNode;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.PsiArrayType;
import com.intellij.psi.PsiType;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
@@ -27,9 +27,9 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty;
import org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.GrExpressionImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
import org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType;
/**
* @author ilyas
@@ -64,13 +64,26 @@ public class GrIndexPropertyImpl extends GrExpressionImpl implements GrIndexProp
PsiType thisType = selected.getType();
if (thisType != null) {
if (thisType instanceof PsiArrayType) {
PsiType componentType = ((PsiArrayType)thisType).getComponentType();
return TypesUtil.boxPrimitiveType(componentType, getManager(), getResolveScope());
}
GrArgumentList argList = getArgumentList();
if (argList != null) {
GrExpression[] arguments = argList.getExpressionArguments();
PsiType[] argTypes = new PsiType[arguments.length];
for (int i = 0; i < arguments.length; i++) {
PsiType argType = arguments[i].getType();
if (argType == null) argType = TypesUtil.getJavaLangObject(argList);
argTypes[i] = argType;
}
final PsiType overloadedOperatorType = TypesUtil.getOverloadedOperatorType(thisType, "getAt", this, argTypes);
if (overloadedOperatorType!=null) {
return overloadedOperatorType;
}
if (thisType instanceof PsiArrayType) {
PsiType componentType = ((PsiArrayType)thisType).getComponentType();
return TypesUtil.boxPrimitiveType(componentType, getManager(), getResolveScope());
}
if (thisType instanceof GrTupleType) {
PsiType[] types = ((GrTupleType)thisType).getParameters();
return types.length == 1 ? types[0] : null;
@@ -84,15 +97,6 @@ public class GrIndexPropertyImpl extends GrExpressionImpl implements GrIndexProp
if (InheritanceUtil.isInheritor(thisType, CommonClassNames.JAVA_UTIL_MAP)) {
return PsiUtil.substituteTypeParameter(thisType, CommonClassNames.JAVA_UTIL_MAP, 1, true);
}
GrExpression[] arguments = argList.getExpressionArguments();
PsiType[] argTypes = new PsiType[arguments.length];
for (int i = 0; i < arguments.length; i++) {
PsiType argType = arguments[i].getType();
if (argType == null) argType = TypesUtil.getJavaLangObject(argList);
argTypes[i] = argType;
}
return TypesUtil.getOverloadedOperatorType(thisType, "getAt", this, argTypes);
}
}
return null;
@@ -203,4 +203,6 @@ public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase {
public void testStringAndGStringUpperBound() throws Exception {doTest();}
public void testWithMethod() throws Exception {doTest();}
public void testArrayLikeAccess() throws Exception {doTest();}
}
@@ -0,0 +1,19 @@
def foo = [1, 2, 5]
def bar = [00, 11, 22, 33, 44, 55, 66, 77, 88]
// highlights right side as 'Can not assign Integer to Collection'
Collection<Integer> baz = bar[foo]
assert baz == [11, 22, 55]
// highlights right side as 'Can not assign Integer to Collection'
Collection<Integer> qux = bar[[1, 2, 5]]
assert qux == [11, 22, 55]
// accepted as correct
Collection<Integer> quux = [bar[1], bar[5], bar[2]]
assert quux == [11, 22, 55]
// highlights right side as 'Can not assign Integer to Collection'
Collection<Integer> quuux = bar[2..5]
assert quuux == [22, 33, 44, 55]