Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add direct enum ref in JCodemodel #68

Closed
guiguilechat opened this issue Apr 8, 2019 · 6 comments
Closed

add direct enum ref in JCodemodel #68

guiguilechat opened this issue Apr 8, 2019 · 6 comments
Assignees

Comments

@guiguilechat
Copy link

guiguilechat commented Apr 8, 2019

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
Copy link
Owner

phax commented Apr 8, 2019

Will review it after my vacation :) looks good

@guiguilechat
Copy link
Author

Enjoy the vacation :)

@guiguilechat
Copy link
Author

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
Copy link
Owner

phax commented Apr 15, 2019

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.

@phax phax self-assigned this Apr 15, 2019
@stale
Copy link

stale bot commented Jul 14, 2019

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.

@stale stale bot added the wontfix label Jul 14, 2019
@phax phax removed the wontfix label Jul 15, 2019
@phax
Copy link
Owner

phax commented Jul 15, 2019

I assume it is okay. Part of v3.2.4

@phax phax closed this as completed Jul 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants