Skip to content

Commit

Permalink
GROOVY-9510, GROOVY-10192 (pt.4)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Aug 15, 2023
1 parent 8346dad commit c3c2fd9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,46 @@ public void testClosure9() {
" getDelegate()\n" +
" thisObject\n" +
" getThisObject()\n" +
" this\n" + // lastIndexOf
" }\n" +
" }\n" +
"}";
//@formatter:on
assertType(contents, "this", "java.lang.Class<Bar>");
assertType(contents, "owner", "java.lang.Class<Bar>");
assertType(contents, "getOwner()", "java.lang.Class<Bar>");
assertType(contents, "delegate", "java.lang.Class<Bar>");
assertType(contents, "getDelegate()", "java.lang.Class<Bar>");
assertType(contents, "thisObject", "java.lang.Class<Bar>");
assertType(contents, "getThisObject()", "java.lang.Class<Bar>");
}

@Test // closure in static scope wrt owner
public void testClosure9a() {
createUnit("Foo",
"@interface Foo {\n" +
" Class<? extends Closure> value()\n" +
"}\n");

//@formatter:off
String contents =
"@Foo({\n" +
" baz\n" +
" owner\n" +
" getOwner()\n" +
" delegate\n" +
" getDelegate()\n" +
" thisObject\n" +
" getThisObject()\n" +
" this\n" + // lastIndexOf
"})\n" +
"class Bar {\n" +
" int baz\n" +
"}";
//@formatter:on
int offset = contents.indexOf("baz");
assertUnknownConfidence(contents, offset, offset + 3);
assertType(contents, "this", "java.lang.Class<Bar>");
assertType(contents, "owner", "java.lang.Class<Bar>");
assertType(contents, "getOwner()", "java.lang.Class<Bar>");
assertType(contents, "delegate", "java.lang.Class<Bar>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public void visitJDT(final IType type, final ITypeRequestor requestor) {
return;
}

scopes.add(new VariableScope(scopes.getLast(), node, false));
scopes.add(new VariableScope(scopes.getLast(), node, true));
ASTNode enclosingDeclaration0 = enclosingDeclarationNode;
IJavaElement enclosingElement0 = enclosingElement;
enclosingDeclarationNode = node;
Expand Down Expand Up @@ -316,6 +316,8 @@ public void visitJDT(final IType type, final ITypeRequestor requestor) {
visitClassReference(face);
}

scopes.removeLast();
scopes.add(new VariableScope(scopes.getLast(), node, false));
try {
List<IMember> members = membersOf(type, node.isScript());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,22 @@ public VariableScope(VariableScope parent, ASTNode enclosingNode, boolean isStat
this.scopeNode = enclosingNode;
this.shared = (parent != null ? parent.shared : new SharedState());
this.enclosingCallStackDepth = this.shared.enclosingCallStack.size();
this.isStaticScope = (isStatic || (!(enclosingNode instanceof ClassNode) && parent != null && parent.isStaticScope)) &&
(getEnclosingClosureScope() == null); // if in a closure, items may be found on delegate or owner
this.isStaticScope = (isStatic || (parent != null && parent.isStaticScope && !(enclosingNode instanceof ClassNode)))
&& (getEnclosingClosureScope() == null); // if in a closure, items may be found on delegate or owner

// determine if scope belongs to script body
if (enclosingNode instanceof ClassNode || enclosingNode instanceof FieldNode) {
this.shared.isRunMethod = false;
} else if (enclosingNode instanceof MethodNode) {
this.shared.isRunMethod = ((MethodNode) enclosingNode).isScriptBody();
}
// TODO: When scope is popped, should the flag be restored to its previous value?
// TODO: Should the flag be restored to its previous value when scope is popped?

// initialize type of "this" (and by extension "super"; see lookupName("super"))
if (enclosingNode instanceof ClassNode) {
ClassNode type = (ClassNode) enclosingNode;
addVariable("this", newClassClassNode(type), type);
} else if (!isStatic && (parent != null && parent.scopeNode instanceof ClassNode)) {
} else if (!isStaticScope && !(enclosingNode instanceof ClosureExpression) && (parent != null && parent.scopeNode instanceof ClassNode)) {
ClassNode type = (ClassNode) parent.scopeNode;
addVariable("this", type, type); // switch from Class<T> to T
}
Expand Down Expand Up @@ -412,7 +412,7 @@ public boolean isOwnerStatic() {
break;
}
}
return false;
return CLASS_CLASS_NODE.equals(getOwner());
}

public boolean isFieldAccessDirect() {
Expand Down

0 comments on commit c3c2fd9

Please sign in to comment.