From 00051ab08fda00b95b45495d40ae8adbdce94c0d Mon Sep 17 00:00:00 2001 From: Maxim Medvedev Date: Thu, 28 Jan 2010 16:48:08 +0300 Subject: [PATCH] IDEA-51686: Overloadded access to arrays --- .../expressions/path/GrIndexPropertyImpl.java | 36 ++++++++++--------- .../groovy/lang/GroovyHighlightingTest.java | 2 ++ .../highlighting/ArrayLikeAccess.groovy | 19 ++++++++++ 3 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 plugins/groovy/testdata/highlighting/ArrayLikeAccess.groovy diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrIndexPropertyImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrIndexPropertyImpl.java index a49e24029cd0..3e8f0cfc9272 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrIndexPropertyImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrIndexPropertyImpl.java @@ -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; diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java index d6a15df3f223..4a4ced60ae66 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java @@ -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();} } \ No newline at end of file diff --git a/plugins/groovy/testdata/highlighting/ArrayLikeAccess.groovy b/plugins/groovy/testdata/highlighting/ArrayLikeAccess.groovy new file mode 100644 index 000000000000..949799b7d93d --- /dev/null +++ b/plugins/groovy/testdata/highlighting/ArrayLikeAccess.groovy @@ -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 baz = bar[foo] +assert baz == [11, 22, 55] + +// highlights right side as 'Can not assign Integer to Collection' +Collection qux = bar[[1, 2, 5]] +assert qux == [11, 22, 55] + +// accepted as correct +Collection quux = [bar[1], bar[5], bar[2]] +assert quux == [11, 22, 55] + +// highlights right side as 'Can not assign Integer to Collection' + +Collection quuux = bar[2..5] +assert quuux == [22, 33, 44, 55]