diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java index a74140aaa4..066e6062d8 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java @@ -861,6 +861,111 @@ public void testInnerClass6() { assertType(contents, start, end, "Outer"); } + @Test + public void testInnerClass7() { + String contents = "class A {\n" + + " protected def f\n" + + " protected def m() { }\n" + + "}\n" + + "class AA extends A {\n" + + " class AAA {\n" + + " def something() {\n" + + " f \n" + + " m()\n" + + " }\n" + + " }\n" + + "}"; + int offset = contents.lastIndexOf('f'); + assertDeclaringType(contents, offset, offset + 1, "A"); + offset = contents.lastIndexOf('m'); + assertDeclaringType(contents, offset, offset + 1, "A"); + } + + @Test + public void testInnerClass8() { + String contents = "class A {\n" + + " protected def f\n" + + " protected def m() { }\n" + + "}\n" + + "class AA extends A {\n" + + " static class AAA {\n" + + " def something() {\n" + + " f \n" + + " m()\n" + + " }\n" + + " }\n" + + "}"; + int offset = contents.lastIndexOf('f'); + assertUnknownConfidence(contents, offset, offset + 1, "A", false); + offset = contents.lastIndexOf('m'); + assertUnknownConfidence(contents, offset, offset + 1, "A", false); + } + + @Test + public void testAnonInner1() { + String contents = "def foo = new Runnable() { void run() {} }"; + int start = contents.lastIndexOf("Runnable"); + int end = start + "Runnable".length(); + assertType(contents, start, end, "java.lang.Runnable"); + } + + @Test + public void testAnonInner2() { + String contents = "def foo = new Comparable() { int compareTo(String a, String b) {} }"; + int start = contents.lastIndexOf("Comparable"); + int end = start + "Comparable".length(); + assertType(contents, start, end, "java.lang.Comparable"); + } + + @Test + public void testAnonInner3() { + String contents = "def foo = new Comparable() { int compareTo(String a, String b) { compareTo()} }"; + int start = contents.lastIndexOf("compareTo"); + int end = start + "compareTo".length(); + assertDeclaringType(contents, start, end, "Search$1"); + } + + @Test + public void testAnonInner4() { + String contents = "def foo = new Comparable() { int compareTo(String a, String b) {} }\n" + + "foo.compareTo"; + int start = contents.lastIndexOf("compareTo"); + int end = start + "compareTo".length(); + assertDeclaringType(contents, start, end, "Search$1"); + } + + @Test + public void testAnonInner5() { + String contents = "def foo = new Comparable() { int compareTo(String a, String b) {} }\n" + + "foo = new Comparable() { int compareTo(String a, String b) {} }\n" + + "foo.compareTo"; + int start = contents.lastIndexOf("compareTo"); + int end = start + "compareTo".length(); + assertDeclaringType(contents, start, end, "Search$2"); + } + + @Test // https://github.com/groovy/groovy-eclipse/issues/378 + public void testAnonInner6() { + String contents = "class A {\n" + + " protected def f\n" + + " protected def m() { }\n" + + "}\n" + + "class AA extends A {\n" + + " void init() {\n" + + " def whatever = new Object() {\n" + + " def something() {\n" + + " f \n" + + " m()\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + int offset = contents.lastIndexOf('f'); + assertDeclaringType(contents, offset, offset + 1, "A"); + offset = contents.lastIndexOf('m'); + assertDeclaringType(contents, offset, offset + 1, "A"); + } + @Test public void testConstantFromSuper() { String contents = @@ -1639,49 +1744,6 @@ public void testDeclarationAtBeginningOfMethod() { assertType(contents, start, end, "java.lang.String"); } - @Test - public void testAnonInner1() { - String contents = "def foo = new Runnable() { void run() {} }"; - int start = contents.lastIndexOf("Runnable"); - int end = start + "Runnable".length(); - assertType(contents, start, end, "java.lang.Runnable"); - } - - @Test - public void testAnonInner2() { - String contents = "def foo = new Comparable() { int compareTo(String a, String b) {} }"; - int start = contents.lastIndexOf("Comparable"); - int end = start + "Comparable".length(); - assertType(contents, start, end, "java.lang.Comparable"); - } - - @Test - public void testAnonInner3() { - String contents = "def foo = new Comparable() { int compareTo(String a, String b) { compareTo()} }"; - int start = contents.lastIndexOf("compareTo"); - int end = start + "compareTo".length(); - assertDeclaringType(contents, start, end, "Search$1"); - } - - @Test - public void testAnonInner4() { - String contents = "def foo = new Comparable() { int compareTo(String a, String b) {} }\n" + - "foo.compareTo"; - int start = contents.lastIndexOf("compareTo"); - int end = start + "compareTo".length(); - assertDeclaringType(contents, start, end, "Search$1"); - } - - @Test - public void testAnonInner5() { - String contents = "def foo = new Comparable() { int compareTo(String a, String b) {} }\n" + - "foo = new Comparable() { int compareTo(String a, String b) {} }\n" + - "foo.compareTo"; - int start = contents.lastIndexOf("compareTo"); - int end = start + "compareTo".length(); - assertDeclaringType(contents, start, end, "Search$2"); - } - @Test // GRECLIPSE-1638 public void testInstanceOf1() { String contents = diff --git a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java index 39cb8cc62d..8a5e01b04e 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java @@ -559,6 +559,13 @@ protected ASTNode findDeclaration(String name, ClassNode declaringType, boolean return accessor; } + if (declaringType instanceof InnerClassNode && (declaringType.getModifiers() & ClassNode.ACC_STATIC) == 0) { + ASTNode declaration = findDeclaration(name, ((InnerClassNode) declaringType).getOuterClass(), isLhsExpression, isStaticExpression, directFieldAccess, methodCallArgumentTypes); + if (declaration != null) { + return declaration; + } + } + if (methodCallArgumentTypes == null) { // reference may be in method pointer or static import; look for method as last resort return findMethodDeclaration(name, declaringType, null);