Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-5051

'this' uses in a superclass is treated as subclass, preventing acces to private members

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.8.2, 2.4.0-rc-1
    • None
    • groovy-runtime
    • None

    Description

      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:

      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()
      

      Exception thrown

      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)
      

      The only change it needs is to change the private to def / protected in the line marked with '// <<<'.

      Similar Java code behaves correctly:

      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();
      	}
      }
      

      When Sub is instantiated, the call to the private get() is done in the superclass, who can access it.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              wujek Wujek
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated: