-
-
Notifications
You must be signed in to change notification settings - Fork 357
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: RoleHandlerHelper#getRoleHandlers(Class), forEachRoleHandler, getParentRoleHandler #1794
Changes from 2 commits
f2331dc
ed1ce09
fc5fddb
bed4ec7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,11 @@ | |
package spoon.reflect.meta.impl; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
|
||
import spoon.SpoonException; | ||
import spoon.reflect.declaration.CtElement; | ||
|
@@ -32,6 +36,8 @@ public class RoleHandlerHelper { | |
private RoleHandlerHelper() { | ||
} | ||
|
||
private static Map<Class<?>, List<RoleHandler>> roleHandlersByClass = new HashMap<>(); | ||
|
||
@SuppressWarnings("unchecked") | ||
private static final List<RoleHandler>[] roleHandlers = new List[CtRole.values().length]; | ||
static { | ||
|
@@ -56,6 +62,7 @@ public static RoleHandler getRoleHandler(Class<? extends CtElement> targetClass, | |
} | ||
return rh; | ||
} | ||
|
||
/** | ||
* @param targetClass the class of the to be manipulated node | ||
* @param role defines the to be manipulated attribute | ||
|
@@ -71,4 +78,51 @@ public static RoleHandler getOptionalRoleHandler(Class<? extends CtElement> targ | |
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @param targetClass a Close whose handlers we are looking for | ||
* @return all RoleHandlers available for the `targetClass` | ||
*/ | ||
public static List<RoleHandler> getRoleHandlers(Class<? extends CtElement> targetClass) { | ||
List<RoleHandler> handlers = roleHandlersByClass.get(targetClass); | ||
if (handlers == null) { | ||
List<RoleHandler> modifiableHandlers = new ArrayList<>(); | ||
for (CtRole role : CtRole.values()) { | ||
RoleHandler roleHandler = getOptionalRoleHandler(targetClass, role); | ||
if (roleHandler != null) { | ||
modifiableHandlers.add(roleHandler); | ||
} | ||
} | ||
handlers = Collections.unmodifiableList(modifiableHandlers); | ||
roleHandlersByClass.put(targetClass, handlers); | ||
} | ||
return handlers; | ||
} | ||
|
||
/** | ||
* @param consumer is called for each RoleHandler of SpoonModel | ||
*/ | ||
public static void forEachRoleHandler(Consumer<RoleHandler> consumer) { | ||
for (List<RoleHandler> list : roleHandlers) { | ||
for (RoleHandler roleHandler : list) { | ||
consumer.accept(roleHandler); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param element the {@link CtElement} whose {@link RoleHandler} in `element.getParent()` is needed. | ||
* @return {@link RoleHandler} of {@link CtRole} of parent's attribute, which contains `element` | ||
*/ | ||
public static RoleHandler getParentRoleHandler(CtElement element) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to getRoleHandler? add API documentation? thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
are you sure? I think that there should be something about
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
a role is always with respect to the parent, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in case of this method yes. |
||
if (element.isParentInitialized() == false) { | ||
return null; | ||
} | ||
CtElement parent = element.getParent(); | ||
CtRole roleInParent = element.getRoleInParent(); | ||
if (roleInParent == null) { | ||
return null; | ||
} | ||
return RoleHandlerHelper.getRoleHandler(parent.getClass(), roleInParent); | ||
} | ||
} |
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 don't understand your documentation here: what's a "Close"?
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 should sleep more ... at least sometime ... "Close" is wrong. Correct is "Class" ;-) I will fix it.