3

i defined a new class called BigDecimalWithAttrDisplay with the following implementation:

class BigDecimalWithAttrDisplay extends BigDecimal{
    String display;
    BigDecimalWithAttrDisplay(String val){super(val)}
    public String toString(){
        "BigDecimalWithAttrDisplay{val=${super.toString()}, display='$display'}";
    }
}

when trying to run code that use this class i get:

java.lang.IncompatibleClassChangeError: the number of constructors during runtime and compile time for java.math.BigDecimal do not match. Expected 17 but got 18
at groovy.lang.MetaClassImpl.selectConstructorAndTransformArguments(MetaClassImpl.java:1381)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.selectConstructorAndTransformArguments(ScriptBytecodeAdapter.java:234)
at com.e4x.auto.services.checkout.testapi.model.response.BigDecimalWithAttrDisplay.<init>(BigDecimalWithAttrDisplay.groovy:31)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:71)
at org.codehaus.groovy.runtime.callsite.ConstructorSite.callConstructor(ConstructorSite.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:194)

what is the problem and how can i fix it?

Yosefki
  • 383
  • 7
  • 22

3 Answers3

6

Try cleaning your created classes (do a fresh rebuild of your project). You might have artifacts from previous compilations.

Another possibility would be that you use Java 7 and Groovy code compiled with a version lower than 7. Then you might also encounter problems. (See for example: Geb - IncompatibleClassChangeError)

Community
  • 1
  • 1
MicSim
  • 26,265
  • 16
  • 90
  • 133
  • actually in my environment (using intelliJ) i don't have this problem. it's only happen on my integration servers so the always clean (every compilation start from scratch), and i use java 6 – Yosefki Jan 23 '13 at 11:25
  • I have this same issue were it works fine locally but not in the dev servers. – alltej Oct 07 '20 at 18:41
1

Couldn't reproduce your bug in 2.0.5, but if the problem is the lack of constructors, Groovy has an annotation called @InheritConstructors which might help.

Also take a look at @Delegate. It might be cooler than inheritance.

Will
  • 14,348
  • 1
  • 42
  • 44
  • this @Delegate is very nice.. but for some reason it doesn't work at all, is it supported when using java and groovy joint compilation? – Yosefki Jan 23 '13 at 15:27
  • java: someClass.java:91: cannot find symbol symbol : method intValue() location: class com....BigDecimalWithAttrDisplay (i'm using IntelliJ 12) – Yosefki Jan 23 '13 at 15:34
  • I had some problems with @Delegate, but those were 1.7.5 times. Do you think you can upgrade to groovy 2? – Will Jan 23 '13 at 18:19
  • sadly no. we are very much afraid of comparability issues and i need to learn more about the risks from moving to groovy 2. maybe its intelliJ or the join-compiler fault because when using pure groovy i don't get this error. this is a cool feature anyway :) – Yosefki Jan 23 '13 at 21:18
  • And what about a mixin/category? Or are you guys on typed groovy only? – Will Jan 24 '13 at 12:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23307/discussion-between-user1090419-and-will-p) – Yosefki Jan 24 '13 at 13:18
0

Add a call to BigDecimal constructor by adding super(val) in your constructor.