比较'对象'中的值时可能出现的错误字段

时间:2017-03-06 19:48:29

标签: drools jboss-tools

我已经能够在6.0.3和6.2.0中重新创建此错误。我不确定解释它的最佳方式,所以我也会添加可以重现它的代码。

我已经将代码减少到导致错误的原因。在我的原始代码中,我使用了一些类似的其他pojos。它不是粘贴所有这些类,而是使用一些具有相同效果的常见java类。您可以使用任何类来获得相同的错误

EventObject - 可以是存储' Object'的任何类。并为该字段设置了一个getter和setter

Random - 可以是任何没有带有单个String参数的构造函数的类

整数 - 可以是任何类

当规则比较字段中包含的值且该字段属于'对象' class,有时规则会尝试创建一个对象的新实例,它试图将值与之比较,并将该值的String表示传递给构造函数。

import java.util.EventObject;
import java.util.Random;

/**
 * The offending line is the comparison of the EventObjects Source (an Integer 
 * in this case) with the Random. Occasionally when trying to compare the two,
 * the rules processor tries to find a constructor for Random that has a string.
 * If it finds one, it attempts to pass String.valueOf(Integer) to the
 * constructor. If it doesn't find it, a reflections error is thrown:
 * java.lang.NoSuchMethodError: java.util.Random.<init>(Ljava/lang/String;)V
 *
 * The actual classes for the three types don't matter as long as EventObject is
 * any class that contains an Object field that can be set and retrieved, and
 * Random is any class that does not contain a Constructor with a single
 * String argument
 */

rule "Insert objects"
    dialect "java"
    agenda-group "aHiddenAgenda"
    when
        not Integer()
    then
        /* add more EventObjects to increase the failure rate. 10 almost always
         * works, 1000 almost never works
         */
        for(int i = 0; i < 175; i++) {
            insert(new EventObject(new Integer(i)));
        }
        for(int i = 0; i < 10; i++) {
            insert(new Random());
        }
end

rule "Find a Random where no EventObject contains it"
    dialect "java"
    agenda-group "aHiddenAgenda"
    when
        $toCompare : Random()
        not EventObject( $toCompare == source )
    then
end

1 个答案:

答案 0 :(得分:0)

好的,您已经遇到了6.2.0及更早版本中的错误。

它已经在6.3.0和6.4.0中消失了,我已经尝试过,可能是后来的版本。