[jira] [Reopened] (GROOVY-8880) Traits - static/instance init blocks

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Reopened] (GROOVY-8880) Traits - static/instance init blocks

JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/GROOVY-8880?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Anton Pryamostanov reopened GROOVY-8880:
----------------------------------------

Please see above comment.

> Traits - static/instance init blocks
> ------------------------------------
>
>                 Key: GROOVY-8880
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8880
>             Project: Groovy
>          Issue Type: Improvement
>          Components: Compiler, groovy-runtime
>    Affects Versions: 2.5.2
>            Reporter: Anton Pryamostanov
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 3.0.0-alpha-4, 2.5.5
>
>
> Let's consider the below examples of Traits with Init blocks:
>  *Instance init block:*
> {code}
> trait InstanceTrait {
>     {
>         System.out.println("Instance init")
>     }
> }
> class InstanceTraitClass implements InstanceTrait {
> }
> new InstanceTraitClass()
> {code}
> This gives the below compilation exception:
> {code:java}
> General error during class generation: NPE while processing script1542098577743.groovy
> groovy.lang.GroovyRuntimeException: NPE while processing script1542098577743.groovy
>     at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:2
>     at org.codehaus.groovy.control.CompilationUnit$17.call(CompilationUnit.java:8
>     at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:10
>     at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:6
>     at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:5
>     at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:5
>     at org.codehaus.groovy.control.CompilationUnit$compile$1.call(Unknown Sour
>     at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:
>     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:1
>     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:1
>     at groovy.inspect.swingui.AstNodeToScriptAdapter.compileToScript(AstNodeToScriptAdapter.groovy:
>     at groovy.inspect.swingui.AstNodeToScriptAdapter$compileToScript.call(Unknown Sour
>     at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:
>     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:1
>     at groovy.inspect.swingui.AstBrowser$_decompile_closure6.doCall(AstBrowser.groovy:3
>     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Meth
>     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
>     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
>     at java.lang.reflect.Method.invoke(Method.java:4
>     at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:
>     at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:3
>     at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:2
>     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:10
>     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:10
>     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:10
>     at groovy.lang.Closure.call(Closure.java:4
>     at groovy.lang.Closure.call(Closure.java:4
>     at groovy.lang.Closure.run(Closure.java:4
>     at java.lang.Thread.run(Thread.java:7
> Caused by: java.lang.NullPointerException
>     at org.codehaus.groovy.classgen.asm.CallSiteWriter.prepareCallSite(CallSiteWriter.java:2
>     at org.codehaus.groovy.classgen.asm.CallSiteWriter.prepareSiteAndReceiver(CallSiteWriter.java:2
>     at org.codehaus.groovy.classgen.asm.CallSiteWriter.prepareSiteAndReceiver(CallSiteWriter.java:2
>     at org.codehaus.groovy.classgen.asm.CallSiteWriter.makeCallSite(CallSiteWriter.java:2
>     at org.codehaus.groovy.classgen.asm.InvocationWriter.makeCachedCall(InvocationWriter.java:3
>     at org.codehaus.groovy.classgen.asm.InvocationWriter.makeCall(InvocationWriter.java:3
>     at org.codehaus.groovy.classgen.asm.InvocationWriter.makeCall(InvocationWriter.java:1
>     at org.codehaus.groovy.classgen.asm.InvocationWriter.makeInvokeMethodCall(InvocationWriter.java:
>     at org.codehaus.groovy.classgen.asm.InvocationWriter.writeInvokeMethod(InvocationWriter.java:4
>     at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethodCallExpression(AsmClassGenerator.java:7
>     at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:
>     at org.codehaus.groovy.classgen.asm.StatementWriter.writeExpressionStatement(StatementWriter.java:6
>     at org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.writeExpressionStatement(OptimizingStatementWriter.java:3
>     at org.codehaus.groovy.classgen.AsmClassGenerator.visitExpressionStatement(AsmClassGenerator.java:6
>     at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:
>     at org.ehaus.groovy.classgen.asm.StatementWriter.writeBlockStatement(StatementWriter.java:85)
>     at org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.writeBlockStatement(OptimizingStatementWriter.java:1
>     at org.codehaus.groovy.classgen.AsmClassGenerator.visitBlockStatement(AsmClassGenerator.java:5
>     at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:
>     at org.codehaus.groovy.classgen.asm.StatementWriter.writeBlockStatement(StatementWriter.java:
>     at org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.writeBlockStatement(OptimizingStatementWriter.java:1
>     at org.codehaus.groovy.classgen.AsmClassGenerator.visitBlockStatement(AsmClassGenerator.java:5
>     at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:
>     at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitObjectInitializerStatements(ClassCodeVisitorSupport.java:
>     at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:
>     at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:2
>     ... 28 m
> {code}
> *Static init block:*
> {code}
> trait StaticTrait {
>     static {
>         System.out.println("Static init")
>     }
> }
> class StaticTraitClass implements StaticTrait {
> }
> new StaticTraitClass()
> {code}
> This compiles but gives below runtime exception:
> {code:java}
> java.lang.ExceptionInInitializerError
> at ConsoleScript278.run(ConsoleScript278:12)
> Caused by: groovy.lang.MissingMethodException: No signature of method: static StaticTrait.<clinit>() is applicable for argument types: (java.lang.Class) values: [class StaticTraitClass]
> at StaticTrait$Trait$Helper.$static_methodMissing(ConsoleScript278)
> at StaticTraitClass.<clinit>(ConsoleScript278)
> ... 1 more
> {code}
> *Expected enhancement*:
> - If it is easy to implement - please add support for above init blocks into traits.
> Use case: adding initialization traits to classes.
> - If it is difficult to implement - please add user-friendly error message for both cases and take decision not to support init blocks in Groovy traits.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)