phax / jcodemodel

A heavily extended fork of the com.sun.codemodel (from 2013/09)
Other
93 stars 34 forks source link

add direct enum ref in JCodemodel #68

Closed guiguilechat closed 4 years ago

guiguilechat commented 5 years ago

sorry if this is already present, did not find it.

I already have this code in a common class :

class A {
A(MyEnum param){}
}

I want to use JCodeModel to create the class B:

class B extends A{
B(){
super(MyEnum.enum1);
}
}

I create the definedclass bclass, create the constructor, but when I want to set super(enum1) I need to call JExpr.enumConstantRef(cm.ref(MyEnum.class), enum1.name())

I think it would be easier to add an enumRef in JCodeModel that allow to refer to an enum constant more easily. eg

/** 
* refer to clazz.ref
*/
    public <T extends Enum<T>> JEnumConstantRef enumRef(Class<T> clazz, T ref) {
        return JExpr.enumConstantRef(ref(clazz), ref.name());
    }

(did not test this code, maybe the idea is bad - it compiles but :P )

phax commented 5 years ago

Will review it after my vacation :) looks good

guiguilechat commented 5 years ago

Enjoy the vacation :)

guiguilechat commented 5 years ago

maybe instead of enumRef, overload the lit() method ? At first I searched in the lit because I thought it would be there. Not sure though, it's not a litteral value but a litteral reference.

phax commented 5 years ago

I now added a method enumConstantRef to AbstractJClass so that you can basically code like this: cm.ref (MyEnum.class).enumConstantRef ("ENUM1") - I don't want to bloat the JCodeModel class with these utility methods - there would be hundreds of them. Please tell if that already helps.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

phax commented 4 years ago

I assume it is okay. Part of v3.2.4