Note that there are some explanatory texts on larger screens.

plurals
  1. POType-parameterized field of a generic class becomes invisible after upgrading to Java 7
    primarykey
    data
    text
    <p>Now <a href="http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/indigosr1">Eclipse Indigo SR1</a> with builtin <a href="http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-432154.html">Java 7</a> support is finally out since a week or two, I'm migrating my playground projects from Helios SR2 + JDK 1.6_23 to Indigo SR1 + JDK 1.7.0. After a full rebuild of all projects, only one class has failed to compile. It's the following class which compiles and runs perfectly fine on Java 1.6 (and 1.5):</p> <pre class="lang-java prettyprint-override"><code>public abstract class Area&lt;A extends Area&lt;?&gt;&gt; implements Comparable&lt;Area&lt;?&gt;&gt; { private String name; private Area&lt;?&gt; parent; private Set&lt;A&gt; areas; protected Area(String name, A... areas) { this.name = name; this.areas = new TreeSet&lt;A&gt;(); for (A area : areas) { area.parent = this; this.areas.add(area); } } public Set&lt;A&gt; getAreas() { return areas; } // ... } </code></pre> <p>The line <code>area.parent = this;</code> fails with the following error on <code>parent</code>:</p> <blockquote> <p>The field Area&lt;capture#1-of ?>.parent is not visible </p> </blockquote> <p>After first suspecting the Eclipse compiler, I tried with plain <code>javac</code> from JDK 1.7.0, but it gives basically the same error whereas the <code>javac</code> from JDK 1.6.0_23 succeeds without errors.</p> <p>Changing the visibility to <code>protected</code> or default solves the problem. But the <strong>why</strong> is completely beyond me. I peeked around on <a href="http://bugs.sun.com">http://bugs.sun.com</a>, but I couldn't find any similar report. </p> <p>Another way to fix the compilation error is to replace all used <code>A</code> declarations inside the class by <code>Area&lt;?&gt;</code> (or to remove it altogether):</p> <pre class="lang-java prettyprint-override"><code>public abstract class Area&lt;A extends Area&lt;?&gt;&gt; implements Comparable&lt;Area&lt;?&gt;&gt; { private String name; private Area&lt;?&gt; parent; private Set&lt;Area&lt;?&gt;&gt; areas; protected Area(String name, Area&lt;?&gt;... areas) { this.name = name; this.areas = new TreeSet&lt;Area&lt;?&gt;&gt;(); for (Area&lt;?&gt; area : areas) { area.parent = this; this.areas.add(area); } } public Set&lt;Area&lt;?&gt;&gt; getAreas() { return areas; } // ... } </code></pre> <p>But this breaks the purpose of the getter. In case of for example the following class:</p> <pre><code>public class Country extends Area&lt;City&gt; { public Country(String name, City... cities) { super(name, cities); } } </code></pre> <p>I'd expect it to return <code>Set&lt;City&gt;</code>, not <code>Set&lt;Area&lt;?&gt;&gt;</code>.</p> <p>Which change in Java 7 has caused those type-parameterized fields to become invisible?</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.
 

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