Skip to content

Commit

Permalink
GROOVY-11352
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 23, 2024
1 parent 49da260 commit 2f03b94
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2318,6 +2318,31 @@ public void testAccessOuterClassMemberFromInnerClassConstructor3() {
runConformTest(sources, "ok");
}

@Test // GROOVY-11352
public void testAccessOuterClassMemberFromInnerClassConstructor4() {
//@formatter:off
String[] sources = {
"Script.groovy",
"class Super {\n" +
" protected final String s\n" +
" Super(String s) { this.s = s }\n" +
"}\n" +
"class Outer {\n" +
" static String initValue() { 'ok' }\n" +
" static class Inner extends Super {\n" +
" Inner() {\n" +
" super(initValue())\n" + // here
" }\n" +
" }\n" +
" String test() { new Inner().s }\n" +
"}\n" +
"print new Outer().test()\n",
};
//@formatter:on

runConformTest(sources, "ok");
}

@Test // GROOVY-9501
public void testAccessOuterClassMemberFromInnerClassMethod1() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,17 @@ public void visitMapEntryExpression(MapEntryExpression mee) {
@Override
public void visitMethodCallExpression(MethodCallExpression mce) {
if (inSpecialConstructorCall() && isThisObjectExpression(mce)) {
MethodNode methodTarget = mce.getMethodTarget();
if (methodTarget == null || !(methodTarget.isStatic() || classNode.getOuterClasses().contains(methodTarget.getDeclaringClass()))) {
if (!mce.isImplicitThis()) {
throw newVariableError(mce.getObjectExpression().getText(), mce.getObjectExpression());
} else {
boolean outerOrStaticMember = false;
if (mce.getMethodTarget() != null) {
outerOrStaticMember = mce.getMethodTarget().isStatic() || classNode.getOuterClasses().contains(mce.getMethodTarget().getDeclaringClass());
} else if (mce.isImplicitThis()) { // GROOVY-11352
outerOrStaticMember = classNode.getOuterClasses().stream().anyMatch(oc -> oc.hasPossibleStaticMethod(mce.getMethodAsString(), mce.getArguments()));
}
if (!outerOrStaticMember) {
if (mce.isImplicitThis()) {
throw newVariableError(mce.getMethodAsString(), mce.getMethod());
} else {
throw newVariableError(mce.getObjectExpression().getText(), mce.getObjectExpression());
}
}
nodes.push(mce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,12 +828,17 @@ public void visitMapEntryExpression(MapEntryExpression mee) {
@Override
public void visitMethodCallExpression(MethodCallExpression mce) {
if (inSpecialConstructorCall() && isThisObjectExpression(mce)) {
MethodNode methodTarget = mce.getMethodTarget();
if (methodTarget == null || !(methodTarget.isStatic() || classNode.getOuterClasses().contains(methodTarget.getDeclaringClass()))) {
if (!mce.isImplicitThis()) {
throw newVariableError(mce.getObjectExpression().getText(), mce.getObjectExpression());
} else {
boolean outerOrStaticMember = false;
if (mce.getMethodTarget() != null) {
outerOrStaticMember = mce.getMethodTarget().isStatic() || classNode.getOuterClasses().contains(mce.getMethodTarget().getDeclaringClass());
} else if (mce.isImplicitThis()) { // GROOVY-11352
outerOrStaticMember = classNode.getOuterClasses().stream().anyMatch(oc -> oc.hasPossibleStaticMethod(mce.getMethodAsString(), mce.getArguments()));
}
if (!outerOrStaticMember) {
if (mce.isImplicitThis()) {
throw newVariableError(mce.getMethodAsString(), mce.getMethod());
} else {
throw newVariableError(mce.getObjectExpression().getText(), mce.getObjectExpression());
}
}
nodes.push(mce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,12 +805,17 @@ public void visitMapEntryExpression(MapEntryExpression mee) {
@Override
public void visitMethodCallExpression(MethodCallExpression mce) {
if (inSpecialConstructorCall() && isThisObjectExpression(mce)) {
MethodNode methodTarget = mce.getMethodTarget();
if (methodTarget == null || !(methodTarget.isStatic() || classNode.getOuterClasses().contains(methodTarget.getDeclaringClass()))) {
if (!mce.isImplicitThis()) {
throw newVariableError(mce.getObjectExpression().getText(), mce.getObjectExpression());
} else {
boolean outerOrStaticMember = false;
if (mce.getMethodTarget() != null) {
outerOrStaticMember = mce.getMethodTarget().isStatic() || classNode.getOuterClasses().contains(mce.getMethodTarget().getDeclaringClass());
} else if (mce.isImplicitThis()) { // GROOVY-11352
outerOrStaticMember = classNode.getOuterClasses().stream().anyMatch(oc -> oc.hasPossibleStaticMethod(mce.getMethodAsString(), mce.getArguments()));
}
if (!outerOrStaticMember) {
if (mce.isImplicitThis()) {
throw newVariableError(mce.getMethodAsString(), mce.getMethod());
} else {
throw newVariableError(mce.getObjectExpression().getText(), mce.getObjectExpression());
}
}
nodes.push(mce);
Expand Down

0 comments on commit 2f03b94

Please sign in to comment.