Problem
When using Root as a class name for the root element in a model definition, the JDOM writer generator would produce code that failed to compile due to a variable naming conflict.
The generated updateRoot() method would declare both:
- A parameter
Root root (from uncapitalizing the class name "Root")
- A local variable
Element root (for the XML element being created)
This resulted in a compilation error:
variable root is already defined in method updateRoot(org.example.Root,java.lang.String,Counter,Document)
Solution
Renamed the local Element variable from root to rootElement throughout the JDOMWriterGenerator.updateClass() method. This makes the generated code use distinct names:
root - the model object parameter
rootElement - the XML Element being created
Before (would not compile):
protected void updateRoot(Root root, String xmlTag, Counter counter, Document document) {
if (root != null) {
Element root = factory.element(xmlTag, "..."); // ❌ Conflict!
root.getName(); // Which 'root' is this?
}
}
After (compiles successfully):
protected void updateRoot(Root root, String xmlTag, Counter counter, Document document) {
if (root != null) {
Element rootElement = factory.element(xmlTag, "..."); // ✅ Clear distinction
updateElement(innerCount, rootElement, "name").setText(root.getName()); // ✅ No ambiguity
}
}
Testing
- Added
root-classname.mdo test model with Root as the class name
- Added
RootClassnameJDOMGeneratorTest to verify generated code compiles successfully
- All existing JDOM plugin tests continue to pass
- Verified the fix works with both namespace and non-namespace configurations
Fixes the issue where models with a root class named "Root" would generate non-compilable JDOM writer code.
Original prompt
This section details on the original issue you should resolve
<issue_title>Root can not be used as class name for jdom-writer</issue_title>
<issue_description>To reproduce:
<classes>
<class rootElement="true">
<name>Root</name>
<version>1.0.0+</version>
...
</class>
<classes>
Local field as root is used in code and conflict with generated code.
</issue_description>
Comments on the Issue (you are @copilot in this section)
Fixes #404
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.