Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Oct 5, 2025

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.

Copilot AI and others added 2 commits October 5, 2025 17:48
…e to rootElement

Co-authored-by: slachiewicz <6705942+slachiewicz@users.noreply.github.com>
Co-authored-by: slachiewicz <6705942+slachiewicz@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Root can not be used as class name for jdom-writer Fix Root class name conflict in JDOM writer generator Oct 5, 2025
Copilot finished work on behalf of slachiewicz October 5, 2025 17:53
@Copilot Copilot AI requested a review from slachiewicz October 5, 2025 17:53
@slachiewicz slachiewicz marked this pull request as ready for review October 5, 2025 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Root can not be used as class name for jdom-writer
2 participants