You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2019/11/25 22:37:00 UTC
[jira] [Commented] (GROOVY-8762) Invalid this reference in nested
class
[ https://issues.apache.org/jira/browse/GROOVY-8762?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16981960#comment-16981960 ]
Eric Milles commented on GROOVY-8762:
-------------------------------------
Because the inner class Bar extends Closure, WriterController thinks the property expression "this.y" occurs within a closure and so writes out a Groovy dynamic property access sequence.
{code:java}
public boolean isInClosure() {
return classNode.getOuterClass() != null
&& classNode.getSuperClass() == ClassHelper.CLOSURE_TYPE;
}
public boolean isInClosureConstructor() {
return constructorNode != null
&& classNode.getOuterClass() != null
&& classNode.getSuperClass() == ClassHelper.CLOSURE_TYPE;
}
{code}
> Invalid this reference in nested class
> --------------------------------------
>
> Key: GROOVY-8762
> URL: https://issues.apache.org/jira/browse/GROOVY-8762
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 2.4.15, 2.5.1
> Reporter: paolo di tommaso
> Priority: Major
>
> The following snippet throws an exception when trying to initialise the `y` attribute
> {code:java}
> class Foo {
> static class Bar extends Closure {
> private int x
> private int y
> Bar(int a) {
> super(null, null);
> x = a
> this.y = a
> }
>
> public int getMaximumNumberOfParameters() {
> throw new UnsupportedOperationException()
> }
> public Class[] getParameterTypes() {
> throw new UnsupportedOperationException()
> }
> public Object call(final Object... args) {
> throw new UnsupportedOperationException()
> }
> public Object call(final Object arguments) {
> throw new UnsupportedOperationException()
> }
> public Object call() {
> throw new UnsupportedOperationException()
> }
> }
> def doSomething() {
> new Bar(1)
> }
> }
> assert new Foo().doSomething()
> {code}
> Reported error:
> {code}
> java.lang.NullPointerException
> at Foo$Bar.<init>(ConsoleScript8:12)
> at Foo.doSomething(ConsoleScript8:37)
> at Foo$doSomething.call(Unknown Source)
> at ConsoleScript8.run(ConsoleScript8:42)
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)