groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-8880) Traits - static/instance init blocks
Date Thu, 15 Nov 2018 14:23:00 GMT

    [ https://issues.apache.org/jira/browse/GROOVY-8880?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16688131#comment-16688131
] 

ASF GitHub Bot commented on GROOVY-8880:
----------------------------------------

GitHub user paulk-asert opened a pull request:

    https://github.com/apache/groovy/pull/823

    GROOVY-8880: Traits - static/instance init blocks

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/paulk-asert/groovy groovy8880

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/groovy/pull/823.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #823
    
----
commit 9b1261be1d3fb71a3c260e5f7b5b302f3344a879
Author: Paul King <paulk@...>
Date:   2018-11-15T14:20:01Z

    GROOVY-8880: Traits - static/instance init blocks

----


> 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
>            Priority: Major
>
> 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)

Mime
View raw message