« Return to Thread: Groovy runs code in static initializers during compile

Groovy runs code in static initializers during compile

by Aaron Digulla :: Rate this Message:

Reply to Author | View in Thread

I'm a big fan of static initializers (aka singletons). In Groovy, I  
when an exception can be thrown in a static initializer, I see this  
when the plugin compiles the code:

java.lang.ExceptionInInitializerError
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:169)
        at ch.globus.mda.MdaDatabase$_createK001_closure1.class$(MdaDatabase.groovy)
        at  
ch.globus.mda.MdaDatabase$_createK001_closure1.doCall(MdaDatabase.groovy:172)
        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.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
        at  
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
        at  
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:97)
        at ch.globus.mda.MdaDatabase$_createK001_closure1.doCall(MdaDatabase.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.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
        at  
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
        at groovy.lang.Closure.call(Closure.java:188)
        at groovy.lang.Closure.call(Closure.java:183)
        at groovy.util.BuilderSupport.doInvokeMethod(BuilderSupport.java:179)
        at groovy.util.BuilderSupport.invokeMethod(BuilderSupport.java:95)
        at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:136)
        at  
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
        at  
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
        at ch.globus.mda.MdaDatabase.createK001(MdaDatabase.groovy:22)
        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.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
        at  
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
        at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:664)
        at org.codehaus.groovy.runtime.Invoker.invokeStaticMethod(Invoker.java:158)
        at  
org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:126)
        at  
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeStaticMethodN(ScriptBytecodeAdapter.java:228)
        at  
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeStaticMethod0(ScriptBytecodeAdapter.java:235)
        at ch.globus.mda.MdaDatabase.<clinit>(MdaDatabase.groovy:15)
        at sun.misc.Unsafe.ensureClassInitialized(Native Method)
        at  
sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:25)
        at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:122)
        at java.lang.reflect.Field.acquireFieldAccessor(Field.java:918)
        at java.lang.reflect.Field.getFieldAccessor(Field.java:899)
        at java.lang.reflect.Field.get(Field.java:358)
        at  
org.codehaus.groovy.control.ResolveVisitor.getTimeStamp(ResolveVisitor.java:275)
        at  
org.codehaus.groovy.control.ResolveVisitor.isSourceNewer(ResolveVisitor.java:298)
        at  
org.codehaus.groovy.control.ResolveVisitor.resolveToScript(ResolveVisitor.java:327)
        at  
org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.java:248)
        at  
org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:775)
        at  
org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:128)
        at  
org.codehaus.groovy.control.CompilationUnit$5.call(CompilationUnit.java:595)
        at  
org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:833)
        at  
org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:480)
        at  
org.codehaus.groovy.eclipse.core.compiler.GroovyCompiler.compile(GroovyCompiler.java:158)
        at  
org.codehaus.groovy.eclipse.core.compiler.GroovyCompiler.compile(GroovyCompiler.java:89)
        at  
org.codehaus.groovy.eclipse.core.model.GroovyProject.compileGroovyFile(GroovyProject.java:496)
        at  
org.codehaus.groovy.eclipse.core.GroovyCore.sourceChanged(GroovyCore.java:106)
        at  
org.codehaus.groovy.eclipse.editor.GroovyReconcilingStrategy.reconcile(GroovyReconcilingStrategy.java:78)
        at  
org.eclipse.jface.text.reconciler.MonoReconciler.process(MonoReconciler.java:71)
        at  
org.eclipse.jface.text.reconciler.AbstractReconciler$BackgroundThread.run(AbstractReconciler.java:204)
Caused by: java.lang.NullPointerException
        at  
net.sf.ehcache.CacheManager.replaceCacheWithDecoratedCache(CacheManager.java:838)
        at ch.globus.basics.cache.GroovyCache.init(GroovyCache.java:44)
        at ch.globus.basics.cache.GroovyCache.<init>(GroovyCache.java:32)
        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.runtime.MetaClassHelper.doConstructorInvoke(MetaClassHelper.java:562)
        at groovy.lang.MetaClassImpl.doConstructorInvoke(MetaClassImpl.java:1756)
        at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:758)
        at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:688)
        at org.codehaus.groovy.runtime.Invoker.invokeConstructorOf(Invoker.java:163)
        at  
org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:140)
        at  
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeNewN(ScriptBytecodeAdapter.java:243)
        at ch.globus.mda.MdaMwst.<clinit>(MdaMwst.groovy:24)
        ... 63 more

This tells me that it's unsafe to use singletons in Groovy if they are  
"complex" (for example, when they depend on runtime configuration). Is  
this a bug in the compiler or a general limitation of Groovy?

Regards,

--
Aaron "Optimizer" Digulla a.k.a. Philmann Dark
"It's not the universe that's limited, it's our imagination.
Follow me and I'll show you something beyond the limits."
http://www.pdark.de/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

 « Return to Thread: Groovy runs code in static initializers during compile