Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.5.15, 4.0.0-beta-1, 3.0.9
-
Linux (Ubuntu 20.04)
Java 11
Gradle 7.1.1
Description
I'm unable to compile my OSS project under 3.0.8, it is throwing this error:
startup failed:
modules/db/src/main/groovy/com/intellisrc/db/Model.groovy:76: [Static type checking] - Cannot call T#getMap(T) with arguments [com.intellisrc.db.Model <T extends com.intellisrc.db.Table>]
@ line 76, column 16.
return table.getMap(this)
^modules/db/src/main/groovy/com/intellisrc/db/Table.groovy:261: [Static type checking] - Cannot call T#get(int) with arguments [int]
@ line 261, column 42.
type[origName] = refType.table.get(it.value as int)^
2 errors
(clicking on the links above will take you to the source code)
I prepared a (mutilated) simplified project to replicate the issue:
https://gitlab.com/intellisrc/groovy-compile-error
Here is the relevant code:
Model.groovy
// code placeholder @CompileStatic abstract class Model<T extends Table> implements Instanciable<T> { T getTable() { return getParametrizedInstance() } Map<String, Object> toMap() { return table.getMap(this) } }
Table.groovy
@CompileStatic class Table<T extends Model> implements Instanciable<T> { Map<String, Object> getMap(T type) { return [:] } T setMap(Map<String, Object> map) { T type = parametrizedInstance map.each { String origName = it.key Field field = null // some field if(field) { switch (field.type) { case Model: Constructor<?> c = field.type.getConstructor() Model refType = (c.newInstance() as Model) type[origName] = refType.table.get(it.value as int) break } } } return type } T get(int id) { T type = setMap([:]) return type } }
Instanciable.groovy
@CompileStatic trait Instanciable<T> { T getParametrizedInstance(int index = 0) { T obj = null try { ParameterizedType parameterizedType = this.class.genericSuperclass as ParameterizedType Class type = (Class) parameterizedType.actualTypeArguments[index] obj = (T) type.getDeclaredConstructor().newInstance() } catch(Exception ignore) {} return obj } }
NOTES:
- There are no issues when compiled in a lower version : 2.5.6 to 2.5.14, 3.0.0 to 3.0.7.
- Using `@CompileStatic` in all classes
Perhaps there is an error in my code which other versions didn't catch? However the code runs as expected when compiled.