Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8671795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:02:26+00:00 2026-06-12T19:02:26+00:00

I am trying to implement a class representing an XML tree as follows: public

  • 0

I am trying to implement a class representing an XML tree as follows:

    public class XML<T extends XML<T>> {

        private final List<MarkupLanguage> nodeList = new ArrayList<>();

        private final Map<String, String> attributeList = new HashMap<>();

        public T attr(final String key, final String value) {
            if (value != null) {
                this.attributeList.put(key, value);
            } 
            return (T) this;
        }

        public T appendTo(final T node) {
            node.add(this);
            return (T) this;
        }
        ...
    } 

My problem is typing of these clauses – I am getting unchecked cast for “return (T) this;”

and also when I try to use the XML class by itself:

    final XML url = new XML("url");
    new XML("loc")
        .add("http://goout.cz")
        .appendTo(url);

I am getting:

    Unchecked cast to call appendTo(T) as a member of raw type XML.

How can I improve my code to get better typing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:02:27+00:00Added an answer on June 12, 2026 at 7:02 pm

    What about the approach below (simple inheritance plus generic methods):

    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    
    public class XmlTest {
    
        @Test
        public void test() {
            XMLFirstSubclass url = new XMLFirstSubclass("url");
            XMLSecondSubclass xmlSecondSubclassInstance = new XMLSecondSubclass(
                    "loc").add("http://goout.cz").appendTo(url);
        }
    }
    
    abstract class XML {
    
        private final List<String> texts = new ArrayList<String>();
    
        private final List<XML> nodes = new ArrayList<XML>();
    
        private final String nodeName;
    
        protected XML(String nodeName) {
            this.nodeName = nodeName;
        }
    
        @SuppressWarnings("unchecked")
        public <R extends XML> R add(String text) {
            texts.add(text);
            return (R) this;
        }
    
        @SuppressWarnings("unchecked")
        public <R extends XML, T extends XML> R add(T node) {
            nodes.add(node);
            return (R) this;
        }
    
        @SuppressWarnings("unchecked")
        public <R extends XML, T extends XML> R appendTo(T node) {
            node.add(this);
            return (R) this;
        }
    }
    
    class XMLFirstSubclass extends XML {
        public XMLFirstSubclass(String nodeName) {
            super(nodeName);
        }
    }
    
    class XMLSecondSubclass extends XML {
        public XMLSecondSubclass(String nodeName) {
            super(nodeName);
        }
    }
    

    Note that the generic methods allow to get a node from one type T and return the instance’s type R, which can be different than the argument’s type. T can be different than R, but both inherit XML.

    Comments about the approach presented in the question

    The approach that you’re using until now can lead to strange situtations.
    Let’s illustrate this with an example.

    Below, we try to write the first class that specializes XML:

    public class XMLFirstSubclass extends XML<OtherXMLSubclass> { ... }
    

    If we’re writing the first XML subclass, the only possible values to OtherXMLSubclass is XMLFirstSubclass or not declaring the generic type at all.

    First option:

    public class XMLFirstSubclass extends XML<XMLFirstSubclass> { ... }
    

    Second:

    public class XMLFirstSubclass extends XML { ... }
    

    If you chose to use generics in your class design, the second option seems bad.
    Taking a closer look into the first option, it opens the possibility of getting subclasses like:

    class XMLSecondSubclass extends XML<XMLFirstSubclass> {
        ... 
    }
    

    Note that this compiles perfectly, but will cause class cast exceptions in XMLSecondSubclass method calls at runtime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a new class inherited from List<string> , to load
I am trying to implement a class Node representing a node in a directed
I'm trying to implement my custom authorize attribute like: public class MyCustomAuth : AuthorizeAttribute
I'm trying to implement my own List system in Java. the List class file
I'm trying to implement a class that stores a generic nullable type: public class
While trying to learn c++, I tried to implement class representing very basic trie.
I am trying to implement a List class using pointers and am trying to
I am trying to implement a DateTime class in C++: class DateTime { public:
Hi Im new to PHP and trying to implement a class. Inside the class
I am trying to implement a class for serialization (XML for now). The idea

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.