Question

I've added a compareTo method to the byte[] class using a Groovy Extension Module. However, I'd also like to add the Comparable interface to the same class, but couldn't figure out how to do it.

I'm guessing that it might be possible to achieve using AST transformations. Or perhaps there is a simpler way? Perhaps by manipulating the expando meta class?

Was it helpful?

Solution

You can not do this, via AST transform. AST transform is only applicable to Groovy classes when byte[] is JDK one.

Metaclass allows you to add your own methods and properties to class, but can't change java inner instanceof behaviour. So, you can't add interface to JDK core class, to use in Java libraries.

Maybe, you should consider to use wrapper class, implementing Comparable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top