Search code examples
groovymetaprogrammingmixins

Mixin empty interface


Is there any way to mixin an empty interface to a groovy object of a wrapped primitive type? What I tried is:

interface B{}
a = new Boolean(false)
b = a.metaClass.mixin(B.class)
assert b instanceof B 

The error is "groovy.lang.GroovyRuntimeException: No default constructor for class B! Can't be mixed in."


Solution

  • This isn't a direct answer to your question, but if you're just looking to mark particular Boolean instances, could you use a marker property instead of an interface? For example:

    Boolean.metaClass.marked = false // establish the marker property
    
    mb = new Boolean(false)
    mb.marked = true
    
    ub = new Boolean(false)
    
    assert mb.marked
    assert ub.marked == false