j-easy / easy-rules

The simple, stupid rules engine for Java
https://github.com/j-easy/easy-rules/wiki
MIT License
4.9k stars 1.06k forks source link

Rules won't work with private method declaration #213

Closed swayamraina closed 4 years ago

swayamraina commented 5 years ago

Hi,

While testing out the code, I found that none of the annotations work if one declares the method as private and the following error message is returned at runtime.

Exception in thread "main" java.lang.IllegalArgumentException: Rule 'XXX' must have at least one public method annotated with 'org.jeasy.rules.annotation.Action'

I think method access should not be a constraint for the rule engine as marking the method as private and annotating it means that the dev wants to restrict application level access to the method and only wants the rule-engine to use it.

swayamraina commented 5 years ago

@benas Is this project under active development/maintenance? What do you think of this issue?

fmbenhassine commented 5 years ago

I'm in PTO this week with limited access to internet. I will get back to you asap.

zhhaojie commented 5 years ago

Hi,

While testing out the code, I found that none of the annotations work if one declares the method as private and the following error message is returned at runtime.

Exception in thread "main" java.lang.IllegalArgumentException: Rule 'XXX' must have at least one public method annotated with 'org.jeasy.rules.annotation.Action'

I think method access should not be a constraint for the rule engine as marking the method as private and annotating it means that the dev wants to restrict application level access to the method and only wants the rule-engine to use it.

Hi, the answer can be found of the class RuleDefinitionValidator#getMethods which can only get the public methods of your rule class ( including inherited methods ). But why NOT rule#getClass()#getDeclaredMethods(excluding inherited methods) ? because the Rule is an interface. evaluate and execute are both public method.

Also, take a look at BasicRule. we can extend BasicRule and override the evaluate and execute method. if the is one or more private action methods of BasicRule, how do we organize our Orders ? we should start from Order(1) or Order(2)?

At last , checkout RuleDefinitionValidator#checkActionMethods & isActionMethodWellDefined

@benas is that TRUE?

zhhaojie commented 5 years ago

@swayamraina

this issue may also help . https://github.com/j-easy/easy-rules/issues/66

swayamraina commented 5 years ago

@benas did you get the time to look at this?

fmbenhassine commented 4 years ago

@swayamraina The reason for that is the following: When you create a POJO with annotations to define a rule, Easy Rules will create a dynamic proxy that implements the Rule interface, in which the evaluate/execute methods are public. So any implementation of the interface must adhere to that contract (it is not possible to restrict visibility in the implementing class).

@zhhaojie What you said is correct. Thank you very much for the follow up on this issue!