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

Runtime IncompatibleClassChangeError using with() on a JavaBean if @CompileStatic is used

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 2.3.3, 2.4.0-beta-1
    • 2.3.4
    • Static compilation
    • None

    Description

      Consider the following Java class:

      package i;
      
      public class MyJavaBean {
      	private String foo;
      	private String bar;
      	
      	public String getFoo() {
      		return foo;
      	}
      	
      	public void setFoo(String foo) {
      		this.foo = foo;
      	}
      	
      	public String getBar() {
      		return bar;
      	}
      	
      	public void setBar(String bar) {
      		this.bar = bar;
      	}
      }
      

      and the following Groovy class:

      package i
      
      import groovy.transform.CompileStatic;
      
      @CompileStatic
      class I {
        void doSomething() {
      	  MyJavaBean bean = new MyJavaBean()
      	  bean.with {
      		  foo = 'foo'
      		  bar = 'bar'
      	  }
      	  println "$bean.foo and $bean.bar"
        }
        
        static void main(String[] args) {
      	  new I().doSomething()
        }
      }
      

      If you run it as a Groovy script, you get:

      Caught: java.lang.IncompatibleClassChangeError: Class i.MyJavaBean does not implement the requested interface groovy.lang.GroovyObject
      java.lang.IncompatibleClassChangeError: Class i.MyJavaBean does not implement the requested interface groovy.lang.GroovyObject
      	at i.I$_doSomething_closure1.doCall(I.groovy:10)
      	at i.I$_doSomething_closure1.call(I.groovy)
      	at i.I.doSomething(I.groovy:9)
      	at i.I.main(I.groovy:17)
      

      If you remove the @CompileStatic annotation from I class, all works fine.

      On another project (the real one were I caught this), I get the same exception but with a different stack trace/error message:

      java.lang.IncompatibleClassChangeError
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setGroovyObjectProperty(ScriptBytecodeAdapter.java:528)
      	at com.example.MyClass$_myMethod_closure4.doCall(MyClass.groovy:194)
      

      The location is always an assignment within a with() closure. Again, I can workaround this by removing static compilation from that method.

      Attachments

        Issue Links

          Activity

            People

              melix Cédric Champeau
              mauromol Mauro Molinari
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: