Skip to content

Commit

Permalink
Fix for issue #378: resolve variables to outer class(es)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 10, 2017
1 parent 0dde05c commit 70aee2b
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>() { int compareTo(String a, String b) {} }";
int start = contents.lastIndexOf("Comparable");
int end = start + "Comparable".length();
assertType(contents, start, end, "java.lang.Comparable<java.lang.String>");
}

@Test
public void testAnonInner3() {
String contents = "def foo = new Comparable<String>() { 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<String>() { 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<String>() { int compareTo(String a, String b) {} }\n" +
"foo = new Comparable<String>() { 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 =
Expand Down Expand Up @@ -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<String>() { int compareTo(String a, String b) {} }";
int start = contents.lastIndexOf("Comparable");
int end = start + "Comparable".length();
assertType(contents, start, end, "java.lang.Comparable<java.lang.String>");
}

@Test
public void testAnonInner3() {
String contents = "def foo = new Comparable<String>() { 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<String>() { 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<String>() { int compareTo(String a, String b) {} }\n" +
"foo = new Comparable<String>() { 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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 70aee2b

Please sign in to comment.