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

Problem With CompileStatic And Class.newInstance()

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.3.0-beta-1, 2.2.3
    • Compiler
    • None

    Description

      While upgrading Grails to be compatible with Groovy 2.3 and JDK8 I came across a number of places where we invoke .newInstance() on a Class and that code no longer compiles with the current 2.3 code.

      newinstance $ groovy -version
      Groovy Version: 2.1.9 JVM: 1.8.0 Vendor: Oracle Corporation OS: Mac OS X
      newinstance $ 
      newinstance $ cat Demo.groovy 
      @groovy.transform.CompileStatic
      class Demo {
          def createBuffer(Class<StringBuffer> c, String initialValue) {
              c.newInstance(initialValue)
          }
      }
      newinstance $ 
      newinstance $ groovyc Demo.groovy 
      newinstance $ 
      
      newinstance $ groovy -version
      Groovy Version: 2.3.0-SNAPSHOT JVM: 1.8.0 Vendor: Oracle Corporation OS: Mac OS X
      newinstance $ 
      newinstance $ cat Demo.groovy 
      @groovy.transform.CompileStatic
      class Demo {
          def createBuffer(Class<StringBuffer> c, String initialValue) {
              c.newInstance(initialValue)
          }
      }
      newinstance $ 
      newinstance $ groovyc Demo.groovy 
      >>> a serious error occurred: BUG! exception in phase 'instruction selection' in source unit 'Demo.groovy' Declaring class for method call to 'java.lang.Object newInstance(java.lang.Class, java.lang.Object[])' declared in java.lang.Class was not matched with found receiver java.lang.StringBuffer. This should not have happened!
      >>> stacktrace:
      BUG! exception in phase 'instruction selection' in source unit 'Demo.groovy' Declaring class for method call to 'java.lang.Object newInstance(java.lang.Class, java.lang.Object[])' declared in java.lang.Class was not matched with found receiver java.lang.StringBuffer. This should not have happened!
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.extractPlaceHolders(StaticTypeCheckingVisitor.java:3878)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.resolvePlaceHoldersFromDeclaration(StaticTypeCheckingVisitor.java:3797)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.resolvePlaceHoldersFromDeclaration(StaticTypeCheckingVisitor.java:3795)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.inferReturnTypeGenerics(StaticTypeCheckingVisitor.java:3615)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.inferReturnTypeGenerics(StaticTypeCheckingVisitor.java:3610)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitMethodCallExpression(StaticTypeCheckingVisitor.java:2501)
      	at org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitMethodCallExpression(StaticCompilationVisitor.java:245)
      

      Attachments

        Activity

          People

            melix Cédric Champeau
            brownj Jeff Brown
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: