highsource / jaxb2-basics

Useful plugins and tools for JAXB2.
BSD 2-Clause "Simplified" License
109 stars 54 forks source link

simplify plugin does not set replacement property parent #106

Open bertramn opened 5 years ago

bertramn commented 5 years ago

Problem:

When the simplify plugin replaces multiple choice properties, it does not set the parent of the replacement property which leads to failures in other plugins that may be executed after it. For instance any call to CPropertyInfo.displayName() on a replaced property will fail with a NPE.

This is caused by adding the replaced property to the CClassInfo.properties but not setting the class as the parent of the property.

SimplifyPlugin.java#L167 SimplifyPlugin.java#L192 SimplifyPlugin.java#L195 SimplifyPlugin.java#L243

Possible solution:

The method CPropertyInfo.setParent is package scoped. The only way to set the parent is to first add the replacement property to the class using the CClassInfo.addProperty() method, then remove it from the collection and then add it back at the correct index position.

CPropertyInfo newProperty = createElementPropertyInfo(oldProperty, elementDef, ... );
// attach/detach to set parent
classInfo.addProperty(newProperty);
classInfo.getProperties().remove(newProperty);
// then add to right place and remove old one
classInfo.getProperties().add(index, newProperty);
classInfo.getProperties().remove(oldProperty);

This will require modification of the class properties outside the visitation loop or else the code that modifies the properties list will fail with ConcurrentModificationException. We would need to collect enough information in the visitor to be able to apply the modifications after finalising the visitation.


SimplifyVisitor visitor = new SimplifyVisitor();

for (CPropertyInfo property : classInfo.getProperties()) {
  property.accept(visitor);
}

for(Modification mod : visitor.getModifications()) {
  mod.apply(classInfo);
}

A bit hacky but works reliably.