Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Not A Problem
-
1.8.0
-
None
Description
I was going to extend my UUID class to let me convert instances to a byte array – the Groovy way, using metaprogramming.
However, while this
UUID.metaClass.asType = { Class clazz -> UUID u = delegate clazz == byte[] ? ({ LessSuckyByteArrayOutputStream bOut = new LessSuckyByteArrayOutputStream(16) new DataOutputStream(bOut).with { writeLong(u.time) writeLong(u.clockSeqAndNode) } bOut.buf })() : UUID.metaClass.asType(clazz) }
works well for
new UUID() as byte[]
writing
new UUID() as String
now prints
groovy.lang.ExpandoMetaClass@181b3d4[class com.eaio.uuid.UUID]
Using delegate.asType(clazz) results in a StackOverflowError.
I also tried other things but none of them worked either.
How can I extend Object#asType(Class) for my own types while delegating to the original code otherwise?