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 2021/03/17 23:15:00 UTC
[jira] [Commented] (GROOVY-5051) 'this' uses in a superclass is
treated as subclass, preventing acces to private members
[ https://issues.apache.org/jira/browse/GROOVY-5051?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17303766#comment-17303766 ]
Eric Milles commented on GROOVY-5051:
-------------------------------------
See also GROOVY-2433
> 'this' uses in a superclass is treated as subclass, preventing acces to private members
> ---------------------------------------------------------------------------------------
>
> Key: GROOVY-5051
> URL: https://issues.apache.org/jira/browse/GROOVY-5051
> Project: Groovy
> Issue Type: Bug
> Components: groovy-runtime
> Affects Versions: 1.8.2, 2.4.0-rc-1
> Reporter: Wujek
> Priority: Major
>
> The original thread: http://groovy.329449.n5.nabble.com/Currying-an-instance-method-exception-with-modifier-private-td4849071.html
> The following code explains the issue:
> {code}
> class Base {
> private __value(arg) { println "$arg" } // <<< here
> def value = this.&__value.curry('value')
> def extend() {
> def tmp = this.&__value.curry('extend')
> tmp()
> }
> }
> class Sub extends Base {
> def test() {
> value()
> extend()
> }
> }
> new Sub().test()
> {code}
> Exception thrown
> {code}
> Sep 28, 2011 6:41:42 PM org.codehaus.groovy.runtime.StackTraceUtils sanitize
> WARNING: Sanitizing stacktrace:
> java.lang.IllegalArgumentException: Can't curry 1 arguments for a closure with 0 parameters.
> at org.codehaus.groovy.runtime.CurriedClosure.<init>(CurriedClosure.java:72)
> at org.codehaus.groovy.runtime.CurriedClosure.<init>(CurriedClosure.java:86)
> at groovy.lang.Closure.curry(Closure.java:527)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:226)
> at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:64)
> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
> at Base.<init>(ConsoleScript7:4)
> at Sub.<init>(ConsoleScript7)
> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
> at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
> at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
> at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
> at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
> at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:54)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:186)
> at ConsoleScript7.run(ConsoleScript7:19)
> at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
> at groovy.lang.GroovyShell.run(GroovyShell.java:517)
> at groovy.lang.GroovyShell.run(GroovyShell.java:172)
> at groovy.lang.GroovyShell$run.call(Unknown Source)
> at groovy.ui.Console$_runScriptImpl_closure16.doCall(Console.groovy:924)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
> at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
> at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
> at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:883)
> at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
> at groovy.ui.Console$_runScriptImpl_closure16.doCall(Console.groovy)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
> at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
> at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
> at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:883)
> at groovy.lang.Closure.call(Closure.java:410)
> at groovy.lang.Closure.call(Closure.java:404)
> at groovy.lang.Closure.run(Closure.java:488)
> at java.lang.Thread.run(Thread.java:662)
> java.lang.IllegalArgumentException: Can't curry 1 arguments for a closure with 0 parameters.
> at Base.<init>(ConsoleScript7:4)
> at Sub.<init>(ConsoleScript7)
> at ConsoleScript7.run(ConsoleScript7:19)
> {code}
> The only change it needs is to change the private to def / protected in the line marked with '// <<<'.
> Similar Java code behaves correctly:
> {code}
> class Base {
> private int get() {
> return 17;
> }
> int a = this.get(); // <<< here
> public void test() {
> System.out.println(a);
> }
> }
> public class Sub extends Base {
> public static void main(String[] args) {
> new Sub().test();
> }
> }
> {code}
> When Sub is instantiated, the call to the private get() is done in the superclass, who can access it.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)