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

Can't have private constructors again

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Not A Problem
    • 1.0-beta-5
    • 1.0-JSR-4
    • None
    • None
    • Mac OSX 10.3.2, java 1.4.2

    Description

      To implement the singleton pattern, I like to make the constructor private so that the only way to access the singleton object is through a getInstance() method. Trying to do this in groovy fails.

      Here is the simple code:

      class SingletonBug {
      private static SingletonBug instance = null

      private SingletonBug() {
      }

      static SingletonBug getInstance()

      { if(instance == null) instance = new SingletonBug() return instance }

      static void main(args) {
      i = SingletonBug.getInstance()
      println "got it: {$i}"
      }
      }

      Running this through groovy fails with this stack trace:

      Caught: groovy.lang.GroovyRuntimeException: could not access constructor: private SingletonBug() with arguments: [] reason: java.lang.IllegalAccessException: Class groovy.lang.MetaClass can not access a member of class SingletonBug with modifiers "private"
      groovy.lang.GroovyRuntimeException: could not access constructor: private SingletonBug() with arguments: [] reason: java.lang.IllegalAccessException: Class groovy.lang.MetaClass can not access a member of class SingletonBug with modifiers "private"
      at groovy.lang.MetaClass.doConstructorInvoke(MetaClass.java:1119)
      at groovy.lang.MetaClass.invokeConstructor(MetaClass.java:407)
      at org.codehaus.groovy.runtime.Invoker.invokeConstructorOf(Invoker.java:159)
      at org.codehaus.groovy.runtime.InvokerHelper.invokeNoArgumentsConstructorOf(InvokerHelper.java:137)
      at SingletonBug.getInstance(SingletonBug.groovy:9)
      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:324)
      at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:63)
      at groovy.lang.MetaClass.doMethodInvoke(MetaClass.java:996)
      at groovy.lang.MetaClass.invokeStaticMethod(MetaClass.java:365)
      at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:124)
      at org.codehaus.groovy.runtime.InvokerHelper.invokeNoArgumentsMethod(InvokerHelper.java:102)
      at SingletonBug.main(SingletonBug.groovy:14)
      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:324)
      at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:63)
      at groovy.lang.MetaClass.doMethodInvoke(MetaClass.java:996)
      at groovy.lang.MetaClass.invokeStaticMethod(MetaClass.java:365)
      at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:124)
      at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:106)
      at groovy.lang.GroovyShell.run(GroovyShell.java:222)
      at groovy.lang.GroovyShell.main(GroovyShell.java:92)
      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:324)
      at org.codehaus.classworlds.Launcher.launchStandard(Launcher.java:410)
      at org.codehaus.classworlds.Launcher.launch(Launcher.java:344)
      at org.codehaus.classworlds.Launcher.main(Launcher.java:474)
      Caused by: java.lang.IllegalAccessException: Class groovy.lang.MetaClass can not access a member of class SingletonBug with modifiers "private"
      at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:268)
      at groovy.lang.MetaClass.doConstructorInvoke(MetaClass.java:1086)
      ... 32 more

      Attachments

        Activity

          People

            phkim Kim, Pilho
            hubbitus Pavel Alexeev
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: