Need to Extend or Override parameters

32 views
Skip to first unread message

mcost...@np6.com

unread,
Aug 14, 2017, 9:51:38 AM8/14/17
to jcommander
JCommander is great! However, I am stuck with something. 

Background: I've got configs all over the place in this project. I've been able to remove a lot of redundant code by making Config classes that are just a set of parameters for Jcommander to populate. All good. But now I am bumping into the situation where these classes need extension or overriding. Example: I have a common config for X which many modules use, but one module needs a parameter of X as REQUIRED. 

My options are:
- pretend this parameter means something else and make a new class for it (not nice)
- extend a core class with with the different flavours of the parameters (every param has the same meaning this way, but now there are many files, and much separation for a set of parameters that should be together)
- have a full set of correctly grouped parameters in one class, and have the ability to override them for exceptional circumstances.

Questions: is the third option at all possible? what do you recommend for dealing with this problem?

Please and thank you

Cédric Beust ♔

unread,
Aug 14, 2017, 9:59:00 AM8/14/17
to jcommander
So you need to be able to extend a config class while modifying the annotations or attributes of its fields/methods...  As you already know, this is currently not supported in Java.

TestNG supports the concept of "Annotation transformers", where users can supply a listener that is handed all the annotations that TestNG found and which  can then be altered before TestNG sees them. It's a pretty straightforward mechanism that's been quite popular in TestNG.

I could implement something similar for JCommander but to be honest, this seems a bit overkill (especially since it's the first time I've heard someone need this for JCommander).

For now, I'd recommend some code duplication, with different config classes where the fields/methods have the attributes you need, and then have your classes extend the correct super class.



-- 
Cédric


--
You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mcost...@np6.com

unread,
Aug 14, 2017, 10:15:59 AM8/14/17
to jcommander, ced...@beust.com
Wonderful answer. Thanks! Doing now.

-- 
Cédric


To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages