Groovy abstract class constructors and inheritance

I have an abstract Java class that has a constructor, and I am extending it from the groovy class. (the idea is to keep the java class as a contract inside the application and load the external groovy classes that implement certain constructors and methods)

How can I force groovy to implement an abstract superclass constructor? Can groovy enforce the constructor of an abstract parent class?

The fact is that the Eclipse groovy IDE does not force me to implement the constructor of the parent class in a subclass, so I would create it groovy automatically, and therefore this was the reason for not forcing it. However, at runtime, when trying to get a constructor using a java reflex, it fails if I do not define a parent constructor in the subclass.

(I have 0 experience in Groovy)

+4
source share
1 answer

It looks like an uncontrolled situation in the compiler. When decompiling, the extension class gets an empty constructor. Tests should cover you, as this situation does not work at runtime.

I do not know how to use this class; I tried the ways I know:

abstract class AbstractClass { String string Integer integer AbstractClass(String string, Integer integer) { this.string = string this.integer = integer } } class ImplClass extends AbstractClass { } // every constructor fails abs1 = new ImplClass('a', 1) abs2 = [string: 'b', integer: 2] as ImplClass abs3 = new ImplClass(abs: 'c', a: 3) abs4 = ImplClass [string:'d', integer:4] 

None of them worked at runtime, but compiled perfectly ;-). The situation is more related to a compilation error and a runtime error. Maybe filling in JIRA?

On the other hand, if you need to inherit constructors, you can go to @groovy.transfom.InheritConstructors in an expandable class. This way you will have constructors without having to explicitly call super() .

+5
source

Source: https://habr.com/ru/post/1432786/


All Articles