There is a strange behavior in Digester that I cannot wrap my head around.
I have the following code that call the "Role" object's constructor whenever it encounter the "roles/role" node in the input xml:
AbstractRulesModule loader = (new AbstractRulesModule() {
protected void configure() {
forPattern("roles/role").createObject().ofType(Role.class)
.usingConstructor(String.class, String.class).then()
.callParam().fromAttribute("machine").ofIndex(0);
forPattern("roles/role").callParam().fromAttribute("name")
.ofIndex(1);
forPattern("roles/role").setNext("add");
}
});
Digester digester = DigesterLoader.newLoader(loader).newDigester();
List<Role> roles = new ArrayList<>();
digester.push(roles);
digester.parse(new File("c:/RoleMapping.xml"));
System.out.println(roles);
System.out.println(Role.count);
Every time the Role's constructor is called, Role.count is incremented. Strangely, after running the above code against the following xml, Role.count is 2 instead of 1. When I debug the code, it seems that Digester tried to create 2 extra object with "null" as the constructor parameters.
<roles>
<role name="m1" machine="mymachine" />
</roles>
This would lead to all sort of problem if I have code checking if the constructor's arguments are null.
The definition of my Role class is:
public class Role {
private String machine;
private String name;
static int count = 0;
public Role(String machine, String name) {
this.machine = machine;
this.name = name;
count++;
}
}