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

Traits - static/instance init blocks

    XMLWordPrintableJSON

Details

    Description

      Let's consider the below examples of Traits with Init blocks:
      Instance init block:

      trait InstanceTrait {
      
          {
              System.out.println("Instance init")
          }
      
      }
      
      class InstanceTraitClass implements InstanceTrait {
      }
      
      new InstanceTraitClass()
      

      This gives the below compilation exception:

      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
      

      Static init block:

      trait StaticTrait {
      
          static {
              System.out.println("Static init")
          }
      
      }
      
      class StaticTraitClass implements StaticTrait {
      }
      
      new StaticTraitClass()
      

      This compiles but gives below runtime exception:

      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
      

      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.

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              anton.pryamostanov Anton Pryamostanov
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: