inheritance - Is there a way to inherit interface method annotation in Groovy? (possible glitch in @Delegate) -
my example done koshuke args4j's @option annotation same apply other method annotation. clarify, annotation may used either field or setter.
it works follows:
class anoptionexample { @option(name = '-text') string text } and test case:
def 'recognises option simple object'() { given: def options = new anoptionexample() def parser = new cmdlineparser(options) when: parser.parseargument('-text=whatever') then: options.text == 'whatever' } now assuming annotate on interface level , re-use @option definitions have kind of multi-inheritance allow use of different interfaces different option sets there go (simplified example):
interface credentials { @option(name = '-username') void setusername(string username) @option(name = '-password') void setpassword(string username) string getusername() string getpassword() } class credentialsimpl implements credentials { string username string password } test case shows failure:
def 'does not recognise option interface'() { given: def options = new credentialsimpl() def parser = new cmdlineparser(options) when: parser.parseargument('-username=john', '-password=qwerty123') then: def ex = thrown(cmdlineexception) ex.message == '"-username=john" not valid option' } well, method annotations of interface not inherited. fair enough - that's java way, how groovy? hope given following:
class someoptionswithdelegate { @delegate(methodannotations = true) final credentials credentials = new credentialsimpl() @option(name = '-url') string url } and test case shows strange:
def 'does recognise option interface via @delegate'() { given: def options = new someoptionswithdelegate() def parser = new cmdlineparser(options) when: parser.parseargument('-username=john', '-password=qwerty123', '-url=http://auth.plop.com') then: with(options) { username == 'john' password == 'qwerty123' url == 'http://auth.plop.com' } } surprisingly @delegate(methodannotations = true) works despite fact object instance delegation done has no annotations - credentials interface has them. , there no method annotation inheritance... oh wait, that's point. seems glitch. why @delegate able pick-up annotations stated on interface level if not inherited credentialsimpl?
on other hand have such behaviour feature wouldn't need use delegation here, instead go like:
@inheritinterfacemethodannotations class someoptionsviainterface implements credentials { string username string password @option(name = '-url') string url } clearly relevant test case above example have failed same way 2nd example. question: there imaginary annotation made - @inheritinterfacemethodannotations available? possible implement considering @delegate glitch. seems useful feature, perhaps has done it. if not, advice how implement on own welcome.
you should traits groovy way of handling situations this. it's sort of extending class, can many traits want. there rules method overlap, , ways determine trait's method call. annotations should work well.
trait credentials { string username @option(name = '-username') void setusername(string ausername){ username = ausername} @option(name = '-password') abstract void setpassword(string username) string getusername() {return username} abstract string getpassword() } class credentialsimpl implements credentials { string password @option(name = '-password') void setpassword(string password){ this.password = password} string getpassword() { password } } they can added objects @ runtime well
def 'does recognise option interface via @delegate'() { given: def parser = new cmdlineparser(new object() credentials ) when: parser.parseargument('-username=john', '-password=qwerty123', '-url=http://auth.plop.com') then: with(options) { username == 'john' password == 'qwerty123' url == 'http://auth.plop.com' } }
Comments
Post a Comment