hitty5
hitty5

Reputation: 1673

cast object to specific class by string

how can i dynamically cast oject to a specific class which is given as a string. e.g.

def a = (ClassA) testService.getObject(xmlString)

i would like to do something like this

(grailsApplication.getClassForName(classString)) testService.getObject(xmlString)

but the groovy compiler does not like this way of cast operation.

Upvotes: 1

Views: 1879

Answers (1)

denis.solonenko
denis.solonenko

Reputation: 11775

you could do

Class myClass = grailsApplication.getClassForName(classString)
myClass.cast testService.getObject(xmlString)

but I'm not sure how groovy this way is - it's really just Java way..

Upvotes: 3

Related Questions