Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I call methods on items in a ReferenceQueue if type erasure has allowed me to only call Object() methods?
    primarykey
    data
    text
    <p>I have a reference queue, declared as:</p> <pre><code>ReferenceQueue&lt;MapChunk&gt; rqueue = new ReferenceQueue&lt;&gt;(); </code></pre> <p>and I have some soft references constructed in the form of</p> <pre><code>SoftReference ref=new SoftReference&lt;MapChunk&gt;(ourChunk, rqueue); </code></pre> <p>As per the docs, <code>rqueue</code> is <code>ReferenceQueue&lt;? extends MapChunk&gt;</code></p> <p>I wish to call a handler on their garbage collection, which is why I put them into the queue. However, when I begin processing the queue from another thread, I retrieve the object from the queue via</p> <pre><code>Reference ref=rqueue.poll(); synchronized (chunks) { synchronized (MapDatabase.class) { chunks.put(ref.get().getPosition(), ref.get()); } } </code></pre> <p>My compiler errors indicate type erasure has occurred and I'm left with <code>ref.get()</code> being an object(due to the raw type of the queue). I can neither call methods on the object that I need nor can I pass it to a method that requires a <code>MapChunk</code> itself. Surely this impedes many uses of the reference queue, and I'm likely doing something wrong in that case. I am certain that I will only get <code>MapChunk</code> objects at runtime, so should I use a cast? Do something else? Not use the reference queue at all?</p> <p>Edit: If I use <code>Reference&lt;MapChunk&gt; ref=rqueue.poll()</code> I get the following:</p> <pre><code>cannot convert from Reference&lt;capture#1-of ? extends MapChunk&gt; to Reference&lt;MapChunk&gt; </code></pre> <p>When I use <code>Reference&lt;? extends MapChunk&gt; ref=rqueue.poll();</code> the error on that line is resolved, but another error comes up when I try to use <code>ref</code>:</p> <pre><code>The method addMapChunk(Position, SoftReference&lt;MapChunk&gt;) in the type HashMap&lt;Position,SoftReference&lt;MapChunk&gt;&gt; is not applicable for the arguments (Position, capture#3-of ? extends MapChunk). </code></pre> <p>Edit: I guess it was simply a buggy error message as the type did convert properly when another bug was fixed.</p>
    singulars
    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.
 

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