Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.0-beta-5
-
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
Issue Links
- is related to
-
GROOVY-499 constructor can't invoke overloaded constructor
- Closed