tkobayas commented on code in PR #5840:
URL: 
https://github.com/apache/incubator-kie-drools/pull/5840#discussion_r1560640736


##########
drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/operatorspec/CustomOperatorSpec.java:
##########
@@ -24,18 +24,41 @@
 import org.drools.model.functions.Operator;
 import org.drools.model.codegen.execmodel.generator.RuleContext;
 
+
+
 public class CustomOperatorSpec extends NativeOperatorSpec {
     public static final CustomOperatorSpec INSTANCE = new CustomOperatorSpec();
 
+    /*
+     Three different cases for generating code:
+       1) if EvaluatorDefinition evalDef contains the method getInstance and 
implements Operator.SingleValue use the getInstance(opName) and do not create a 
CustomOperatorWrapper
+       2) if EvaluatorDefinition evalDef contains the method getInstance use 
getInstance(opName) vs creating a new instance of the class
+       3) none of the above :   new CustomOperatorWrapper( .. new evalDef)
+    */
     @Override
     protected Operator addOperatorArgument( RuleContext context, 
MethodCallExpr methodCallExpr, String opName ) {
         EvaluatorDefinition evalDef = context.getEvaluatorDefinition( opName );
         if (evalDef == null) {
             throw new RuntimeException( "Unknown custom operator: " + opName );
         }
 
-        String arg = "new " + CustomOperatorWrapper.class.getCanonicalName() + 
"( new " + evalDef.getClass().getCanonicalName() + "().getEvaluator(" +
-                ValueType.class.getCanonicalName() + ".OBJECT_TYPE, \"" + 
opName + "\", false, null), \"" + opName + "\")";
+        String arg;
+        try {
+            evalDef.getClass().getMethod("getInstance",String.class);
+            boolean needWrapper = 
!Operator.SingleValue.class.isAssignableFrom(evalDef.getClass());
+            if (needWrapper) {
+                // case 2
+                arg = "new " + CustomOperatorWrapper.class.getCanonicalName() 
+ "( "+ evalDef.getClass().getCanonicalName() + ".getInstance(\"" + opName+ 
"\").getEvaluator(" +
+                        ValueType.class.getCanonicalName() + ".OBJECT_TYPE, 
\"" + opName + "\", false, null), \"" + opName + "\")";

Review Comment:
   From my point of view, do we need this `case 2`? I understand that there 
might be a case where a user creates a custom operator with `getInstance` and 
not implementing `Operator.SingleValue`. But I guess it's very rare... 
@JaredDavis22 Do you expect it will happen in your application?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to