Note that there are some explanatory texts on larger screens.

plurals
  1. POJava/Bukkit - Deserializing a list of Subclasses without a Serializable Superclass
    primarykey
    data
    text
    <p>I've asked this on the Bukkit forums but the support received from there doesn't seem to cut it. What I'm trying to do is serialize a list of subclass objects which implement ConfigurationSerializable (this is Bukkit's API.) The list serializes just fine, but when it comes to deserializing it from the configuration file, it's trying to convert a <code>List&lt;?&gt;</code> to a <code>List&lt;? extends Superclass&gt;</code>, which doesn't work.</p> <p>I have four classes:</p> <pre><code>Superclass SubclassOne extends Superclass implements ConfigurationSerializable SubclassTwo extends Superclass implements ConfigurationSerializable SubclassList implements ConfigurationSerializable </code></pre> <p>SubclassList contains a <code>List&lt;? extends Superclass&gt;</code> called "superclasses". This is the serialize method:</p> <pre><code>@Override public Map&lt;String, Object&gt; serialize() { Map&lt;String, Object&gt; results = new HashMap&lt;String, Object&gt;(); results.put("supers", superclasses); return results; } </code></pre> <p>This is the deserialize method:</p> <pre><code>@SuppressWarnings("unchecked") public static SubclassList deserialize(Map&lt;String, Object&gt; args){ return new SubclassList((List&lt;? super Superclass&gt;) args.get("supers")); } </code></pre> <p>As said, the data serializes correctly to the configuration file. I've done some experimenting with this and when I print out the class types of the objects within args.get("supers"), it DOES print out the correct class names, but I can't convert these <code>?</code> classes into <code>? extends Superclass</code> classes so that they can be passed to SubclassList's constructor.</p> <p>The reason I am not serializing Superclass is because I want to use it to retrieve data associated with its subclasses only. I want to use <code>List&lt;Superclass&gt;</code> to store all types of Superclass (in regards to its subclasses).</p> <p>If there's no simple and efficient way to overcome this, I am open to other suggestions to help me achieve what I am trying to do. I would prefer to be as efficient as possible.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload