I'm trying to remove the square brackets that appear in toString off all collections in Grails. These square brackets are added in the toString of the java.util.AbstractCollection class. So the goal is to replace this toString method and I'm trying to use the ExpandoMetaClass.
I tried to use this code in the Bootstrap file:
def init = { servletContext ->
ExpandoMetaClass.enableGlobally()
AbstractCollection.metaClass.toString << {-> "Test result" }
}
But the error occurs:
Caused by: groovy.lang.GroovyRuntimeException: Cannot add new method [toString] for arguments [[]]. It already exists!
I also tried with the = operator instead of <<. The error does not occur but it has no effect.
Does anybody know how to solve this?
Update:
I realized that the problem is not in the metaclass assignment but probably something that grails does in the entity lists.
Example:
class SampleObject {
static hasMany = [sampleList: SampleList]
}
AbstractCollection.metaClass.toString = {
'Groovy'
}
println(sampleObject.sampleList.toString()) // print [item1, item2, ...]
println([].toString()) // prints Groovy
I also tested these:
AbstractPersistentCollection.metaClass.toString = {
'Groovy1'
}
Collection.metaClass.toString = {
'Groovy2'
}
PersistentSet.metaClass.toString = {
'Groovy3'
}