-
-
Notifications
You must be signed in to change notification settings - Fork 351
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
feature: CtRole#getSuperRole(), CtRole#getSubRole() #1725
Conversation
|
||
static { | ||
for (CtRole role : values()) { | ||
role.subRoles = EMPTY; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of doing this static loop, can't you make the assignment in a constructor of CtRole?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to do that, but java compiler has problem with that.
private static final CtRole[] EMPTY = new CtRole[0];
private CtRole superRole = EMPTY; //<-- compilation error.
I wonder why too..,. but it is so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some hints about why it does not work: https://stackoverflow.com/questions/29174345/illegal-reference-to-static-field-from-initializer
However it will work if you do not use a constant but directly private CtRole[] subRoles = new Ctrole[0];
.
You are right, this approach is nicer and we do not have to care about memory for these few cases. I have added also test. It is ready for merge from my point of view. |
Could you please merge it? Or is there any problem? ;-) |
I have one problem here :-) The problem is to have a mutable enum, which can be considered as a serious design issue. Do you need to have variable super relationships? or would it be possible to hard code the required super relationships in an immutable enum eg:
|
I think it is not possible to make it completely immutable, because we cannot refer enum items in their constructor, which are not yet created. So if you want any changes please try to implement them. I have no idea how to do it better. |
Personally I don't have a problem with the fact that it makes the enum potentially mutable: it's encapsulated inside the enum and everything's private. However, you're usage here is very limited: it's only relationship between TypeMembers and few associated roles. Unless you already expect that there will be other relationship, else why not hard coded this data directly in |
08fe0d9
to
8e45ba6
Compare
I changed |
8e45ba6
to
7f46193
Compare
@monperrus , is it enough immutable? Or too much ugly? ;-) I had to fix also ModelRoleHandler, which generated depending on order of roles in CtRole definition. Now it is independent on that order and it is sorted by name. |
It looks good!
I'd propose to remove the constructor with boolean, TYPE_MEMBER(true), the list of subroles, and to compute subroles on the fly in a stateless manner. |
I do not like such "slowing down loops" if they are not necessary for simpler maintenance. I guess this solution is acceptable now. |
Thanks a lot Pavel.
This is where we disagree. I tend to think that statelessness is essentially good for reliability (less bugs) and maintenance (easier to debug and understanding). I'm OK to trade statelessness for performance once there is a clear use case with a target metric (eg < 1 sec). |
I need that in new TemplateMatcher to distinguish between derived attributes and real attributes.
E.g.
CtRole.CONSTRUCTOR.getSuperRole() == CtRole.TYPE_MEMBER