-
-
Notifications
You must be signed in to change notification settings - Fork 428
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
[automation] Ensure unique module IDs for overloaded @RuleAction
methods
#4441
Changes from 2 commits
1f0f54f
33c9a96
cb77453
ca6bdfb
33ced98
41e60af
86df5e0
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 |
---|---|---|
|
@@ -57,7 +57,8 @@ | |
* Helper methods for {@link AnnotatedActions} {@link ModuleTypeProvider} | ||
* | ||
* @author Stefan Triller - Initial contribution | ||
* @author Florian Hotze - Added configuration description parameters for thing modules | ||
* @author Florian Hotze - Added configuration description parameters for thing modules, Added method signature hash to | ||
* module ID in case of method overloads | ||
* @author Laurent Garnier - Converted into a an OSGi component | ||
*/ | ||
@NonNullByDefault | ||
|
@@ -96,7 +97,7 @@ public Collection<ModuleInformation> parseAnnotations(String name, Object action | |
List<Output> outputs = getOutputsFromAction(method); | ||
|
||
RuleAction ruleAction = method.getAnnotation(RuleAction.class); | ||
String uid = name + "." + method.getName(); | ||
String uid = getModuleIdFromMethod(name, clazz, method); | ||
Set<String> tags = new HashSet<>(Arrays.asList(ruleAction.tags())); | ||
|
||
ModuleInformation mi = new ModuleInformation(uid, actionProvider, method); | ||
|
@@ -113,6 +114,21 @@ public Collection<ModuleInformation> parseAnnotations(String name, Object action | |
return moduleInformation; | ||
} | ||
|
||
public String getModuleIdFromMethod(String actionScope, Class<?> clazz, Method method) { | ||
boolean hasOverloads = Arrays.stream(clazz.getDeclaredMethods()) | ||
.filter(m -> m.isAnnotationPresent(RuleAction.class)).map(Method::getName) | ||
.filter(name -> name.equals(method.getName())).count() > 1; | ||
String uid = actionScope + "." + method.getName(); | ||
if (hasOverloads) { | ||
logger.debug("@RuleAction method {}::{} has overloads. Appending signature hash to UID.", | ||
clazz.getSimpleName(), method.getName()); | ||
String signature = Arrays.stream(method.getParameterTypes()).map(Class::getName) | ||
.collect(Collectors.joining(",")); | ||
uid += "#" + signature.hashCode(); | ||
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.
BTW: Why don't we do that for each method? That would make the need to check if there are overloads unnecessary. 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. Good question. It is not really required, but thinking about it, it will be beneficial if one adds an overload for a method in the future as the id of the existing method won't change. |
||
} | ||
return uid; | ||
} | ||
|
||
private List<Input> getInputsFromAction(Method method) { | ||
List<Input> inputs = new ArrayList<>(); | ||
|
||
|
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.
For performance reasons I would suggest to split this:
ThingActions
will never change) should be done when theThingActions
are added (and stored in something that allows to remove them when theThingActions
is removed)This can be refactored later if you find it too complicated now.