<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Some notes on discovering your type parameter using the Reflection API</title>
	<atom:link href="http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/</link>
	<description>A weblog about software engineering, Architecture, Technology an other things we like.</description>
	<lastBuildDate>Sat, 28 Jan 2012 20:11:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Ben</title>
		<link>http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/comment-page-1/#comment-29693</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Mon, 30 Nov 2009 23:19:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/#comment-29693</guid>
		<description>Johann,

Sorry for not responding earlier -- got a little distracted.

The reason for not propagating downward, is that the fundamental nature of generics is that you really &lt;b&gt;do not have&lt;/b&gt; fully parameterized types of the superclasses of (for example) ExampleMap&lt;Integer,String&gt;. Creating a fully parameterized subtype is no different at all from &quot;regular&quot; subclassing -- it does &lt;b&gt;not&lt;/b&gt; suddenly create a whole, parallel class hierarchy. That is, Java does &lt;b&gt;not&lt;/b&gt; end up with Example&lt;T&gt;, Example&lt;Integer&gt;, Example&lt;String&gt; and all sorts of other, parallel types just to support subtypes. Java introduces fully parameterized types only when the type hierarchy actually stops being generic.

Note that this is fundamentally different from genericity in C++ (for example) where the compiler really just performs macro expansion and actually does introduce parallel hierarchies. Java, by contrast, uses &quot;real&quot; inheritance to move from generic to parameterized types. So, in effect, the reason for &quot;no propagation&quot; is the same as the reason that subclasses in Java don&#039;t have copies of their parent&#039;s methods but really do delegate to their parents in case of inheritance: because Java uses real inheritance and not &quot;simulated inheritance.&quot;</description>
		<content:encoded><![CDATA[<p>Johann,</p>
<p>Sorry for not responding earlier &#8212; got a little distracted.</p>
<p>The reason for not propagating downward, is that the fundamental nature of generics is that you really <b>do not have</b> fully parameterized types of the superclasses of (for example) ExampleMap&lt;Integer,String&gt;. Creating a fully parameterized subtype is no different at all from &#8220;regular&#8221; subclassing &#8212; it does <b>not</b> suddenly create a whole, parallel class hierarchy. That is, Java does <b>not</b> end up with Example&lt;T&gt;, Example&lt;Integer&gt;, Example&lt;String&gt; and all sorts of other, parallel types just to support subtypes. Java introduces fully parameterized types only when the type hierarchy actually stops being generic.</p>
<p>Note that this is fundamentally different from genericity in C++ (for example) where the compiler really just performs macro expansion and actually does introduce parallel hierarchies. Java, by contrast, uses &#8220;real&#8221; inheritance to move from generic to parameterized types. So, in effect, the reason for &#8220;no propagation&#8221; is the same as the reason that subclasses in Java don&#8217;t have copies of their parent&#8217;s methods but really do delegate to their parents in case of inheritance: because Java uses real inheritance and not &#8220;simulated inheritance.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johann Tienhaara</title>
		<link>http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/comment-page-1/#comment-29674</link>
		<dc:creator>Johann Tienhaara</dc:creator>
		<pubDate>Sat, 28 Nov 2009 19:55:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/#comment-29674</guid>
		<description>WordPress is not my forte...  Let&#039;s try this one more time:

With a type hierarchy that has a “concrete” parameter at the top level (such as &lt;code&gt;ExampleSerializable&lt;String&gt;&lt;/code&gt;), why not propagate that “concrete” parameter down the type reflection hierarchy? So that instead of reflecting to retrieve supertype &lt;code&gt;Example&lt;T&gt;&lt;/code&gt;, reflection would return supertype &lt;code&gt;Example&lt;String&gt;&lt;/code&gt;?

It seems to me being able to discover the parameter at generic levels of the type hierarchy would be very handy — such as in cases where you need to find out the parameter type for generic class &lt;code&gt;Example&lt;T&gt;&lt;/code&gt;, but don’t know anything about subclass(es).

Consider an example:

&lt;code&gt; public class ExampleIntegerToString extends ExampleMap&lt;Integer,String&gt; &lt;/code&gt;
&lt;code&gt; public class ExampleMap&lt;X,Y&gt; extends Example&lt;Y&gt; &lt;/code&gt;
&lt;code&gt; public class Example&lt;T&gt; &lt;/code&gt;

In this example, if &lt;code&gt;Example&lt;T&gt;&lt;/code&gt; wanted to reflect on the “concrete” type of &lt;code&gt;&lt;T&gt;&lt;/code&gt; (in this case, &lt;code&gt;&lt;String&gt;&lt;/code&gt;), it would not be able to do so safely, even if it did try to reflect on the subclass &lt;code&gt;ExampleMap&lt;Integer,String&gt;&lt;/code&gt;.

Any idea why this is not feasible? I’m sure there was some reason inside the JVM to make it impractical…

Or perhaps I have completely misunderstood the nature of the problem. :)

Thanks again Ben! Cheers,

Johann</description>
		<content:encoded><![CDATA[<p>WordPress is not my forte&#8230;  Let&#8217;s try this one more time:</p>
<p>With a type hierarchy that has a “concrete” parameter at the top level (such as <code>ExampleSerializable&lt;String&gt;</code>), why not propagate that “concrete” parameter down the type reflection hierarchy? So that instead of reflecting to retrieve supertype <code>Example&lt;T&gt;</code>, reflection would return supertype <code>Example&lt;String&gt;</code>?</p>
<p>It seems to me being able to discover the parameter at generic levels of the type hierarchy would be very handy — such as in cases where you need to find out the parameter type for generic class <code>Example&lt;T&gt;</code>, but don’t know anything about subclass(es).</p>
<p>Consider an example:</p>
<p><code> public class ExampleIntegerToString extends ExampleMap&lt;Integer,String&gt; </code><br />
<code> public class ExampleMap&lt;X,Y&gt; extends Example&lt;Y&gt; </code><br />
<code> public class Example&lt;T&gt; </code></p>
<p>In this example, if <code>Example&lt;T&gt;</code> wanted to reflect on the “concrete” type of <code>&lt;T&gt;</code> (in this case, <code>&lt;String&gt;</code>), it would not be able to do so safely, even if it did try to reflect on the subclass <code>ExampleMap&lt;Integer,String&gt;</code>.</p>
<p>Any idea why this is not feasible? I’m sure there was some reason inside the JVM to make it impractical…</p>
<p>Or perhaps I have completely misunderstood the nature of the problem. <img src='http://www.gridshore.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks again Ben! Cheers,</p>
<p>Johann</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johann Tienhaara</title>
		<link>http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/comment-page-1/#comment-29673</link>
		<dc:creator>Johann Tienhaara</dc:creator>
		<pubDate>Sat, 28 Nov 2009 19:50:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/#comment-29673</guid>
		<description>Whoops, sorry about the HTML formatting Ben!  Hopefully this comment doesn&#039;t get so mangled...

Your clear explanation is what I was clumsily trying to get across in my comment about &quot;sub-typing&quot;.  :)

With a type hierarchy that has a &quot;concrete&quot; parameter at the top level (such as &lt;code&gt;ExampleSerializable&lt;/code&gt;), why not propagate that &quot;concrete&quot; parameter &lt;code&gt;&lt;/code&gt; down the type reflection hierarchy?  So that instead of reflecting to retrieve supertype &lt;code&gt;Example&lt;/code&gt;, reflection would return supertype &lt;code&gt;Example&lt;/code&gt;?

It seems to me being able to discover the &lt;code&gt;&lt;/code&gt; parameter at generic levels of the type hierarchy would be very handy -- such as in cases where you need to find out the parameter type for generic class &lt;code&gt;Example&lt;/code&gt;, but don&#039;t know anything about subclass(es).

Consider an example:

&lt;code&gt; public class ExampleIntegerToString extends ExampleMap &lt;/code&gt;
&lt;code&gt; public class ExampleMap extends Example &lt;/code&gt;
&lt;code&gt; public class Example &lt;/code&gt;

In this example, if &lt;code&gt;Example&lt;/code&gt; wanted to reflect on the &quot;concrete&quot; type of &lt;code&gt;&lt;/code&gt; (in this case, &lt;code&gt;&lt;/code&gt;), it would not be able to do so safely, even if it did try to reflect on the subclass &lt;code&gt;ExampleMap&lt;/code&gt;.

Any idea why this is not feasible?  I&#039;m sure there was some reason inside the JVM to make it impractical...

Or perhaps I have completely misunderstood the nature of the problem.  :)

Thanks again Ben!  Cheers,

Johann</description>
		<content:encoded><![CDATA[<p>Whoops, sorry about the HTML formatting Ben!  Hopefully this comment doesn&#8217;t get so mangled&#8230;</p>
<p>Your clear explanation is what I was clumsily trying to get across in my comment about &#8220;sub-typing&#8221;.  <img src='http://www.gridshore.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>With a type hierarchy that has a &#8220;concrete&#8221; parameter at the top level (such as <code>ExampleSerializable</code>), why not propagate that &#8220;concrete&#8221; parameter <code></code> down the type reflection hierarchy?  So that instead of reflecting to retrieve supertype <code>Example</code>, reflection would return supertype <code>Example</code>?</p>
<p>It seems to me being able to discover the <code></code> parameter at generic levels of the type hierarchy would be very handy &#8212; such as in cases where you need to find out the parameter type for generic class <code>Example</code>, but don&#8217;t know anything about subclass(es).</p>
<p>Consider an example:</p>
<p><code> public class ExampleIntegerToString extends ExampleMap </code><br />
<code> public class ExampleMap extends Example </code><br />
<code> public class Example </code></p>
<p>In this example, if <code>Example</code> wanted to reflect on the &#8220;concrete&#8221; type of <code></code> (in this case, <code></code>), it would not be able to do so safely, even if it did try to reflect on the subclass <code>ExampleMap</code>.</p>
<p>Any idea why this is not feasible?  I&#8217;m sure there was some reason inside the JVM to make it impractical&#8230;</p>
<p>Or perhaps I have completely misunderstood the nature of the problem.  <img src='http://www.gridshore.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks again Ben!  Cheers,</p>
<p>Johann</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/comment-page-1/#comment-29424</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Sat, 21 Nov 2009 18:19:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/#comment-29424</guid>
		<description>Johann,

First of all, thanks for the compliment.

Your post was partially obliterated by WordPress&#039; HTML processor, so I hope I have correctly interpreted your comment to be about the following situation:

&lt;code&gt;
  StringExample extends ExampleSerializable&lt;String&gt;
  ExampleSerializable&lt;T&gt; extends Example&lt;T&gt;
  Example&lt;T&gt;
&lt;/code&gt;

No, they didn&#039;t limit the depth of subtyping to 1. The problem is more fundamental than that: the JVM never creates a ParameterizedType for Example&lt;String&gt;. And the reason is that in your type hierarchy, the first fully parameterized, non-generic type is ExampleSerializable&lt;String&gt; -- up to that point the entire hierarchy is generic and there is no concept of a non-generic form of the Example type.

You can see this by running the following program:

&lt;code&gt;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

class Example&lt;T&gt; {
&#160;&#160;&#160;&#160;public void reportType()
&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Class thisClass = getClass();
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ParameterizedType pType = (ParameterizedType)thisClass.getGenericSuperclass();
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ParameterizedType pSuperType = (ParameterizedType) ((Class)pType.getRawType()).getGenericSuperclass();
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.out.println(pType);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.out.println(pSuperType);
&#160;&#160;&#160;&#160;}
}

class ExampleSerializable&lt;T&gt; extends Example&lt;T&gt; implements Serializable {}

class StringExample extends ExampleSerializable&lt;String&gt; {
&#160;&#160;&#160;&#160;public static void main(String[] args) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;new StringExample().reportType();
&#160;&#160;&#160;&#160;}
}
&lt;/code&gt;

which prints the two lines

&lt;code&gt;
ExampleSerializable&lt;java.lang.String&gt;
Example&lt;T&gt;
&lt;/code&gt;

As you can see, the parameterized supertype of ExampleSerializable&lt;java.lang.String&gt; is the generic, non-instantiated type Example&lt;T&gt;. Which brings us to the reason for the error you are getting: when you request the first type parameter of Example&lt;T&gt;, you don&#039;t get a class, but a type variable (which is not a supertype of java.lang.Class).

Hope that helps (or at least, doesn&#039;t disappoint too badly).

Best regards,

Ben.</description>
		<content:encoded><![CDATA[<p>Johann,</p>
<p>First of all, thanks for the compliment.</p>
<p>Your post was partially obliterated by WordPress&#8217; HTML processor, so I hope I have correctly interpreted your comment to be about the following situation:</p>
<p><code><br />
  StringExample extends ExampleSerializable&lt;String&gt;<br />
  ExampleSerializable&lt;T&gt; extends Example&lt;T&gt;<br />
  Example&lt;T&gt;<br />
</code></p>
<p>No, they didn&#8217;t limit the depth of subtyping to 1. The problem is more fundamental than that: the JVM never creates a ParameterizedType for Example&lt;String&gt;. And the reason is that in your type hierarchy, the first fully parameterized, non-generic type is ExampleSerializable&lt;String&gt; &#8212; up to that point the entire hierarchy is generic and there is no concept of a non-generic form of the Example type.</p>
<p>You can see this by running the following program:</p>
<p><code><br />
import java.io.Serializable;<br />
import java.lang.reflect.ParameterizedType;<br />
import java.lang.reflect.Type;</p>
<p>class Example&lt;T&gt; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;public void reportType()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class thisClass = getClass();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ParameterizedType pType = (ParameterizedType)thisClass.getGenericSuperclass();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ParameterizedType pSuperType = (ParameterizedType) ((Class)pType.getRawType()).getGenericSuperclass();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(pType);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(pSuperType);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</p>
<p>class ExampleSerializable&lt;T&gt; extends Example&lt;T&gt; implements Serializable {}</p>
<p>class StringExample extends ExampleSerializable&lt;String&gt; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new StringExample().reportType();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
</code></p>
<p>which prints the two lines</p>
<p><code><br />
ExampleSerializable&lt;java.lang.String&gt;<br />
Example&lt;T&gt;<br />
</code></p>
<p>As you can see, the parameterized supertype of ExampleSerializable&lt;java.lang.String&gt; is the generic, non-instantiated type Example&lt;T&gt;. Which brings us to the reason for the error you are getting: when you request the first type parameter of Example&lt;T&gt;, you don&#8217;t get a class, but a type variable (which is not a supertype of java.lang.Class).</p>
<p>Hope that helps (or at least, doesn&#8217;t disappoint too badly).</p>
<p>Best regards,</p>
<p>Ben.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johann Tienhaara</title>
		<link>http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/comment-page-1/#comment-29368</link>
		<dc:creator>Johann Tienhaara</dc:creator>
		<pubDate>Sat, 21 Nov 2009 01:10:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.gridshore.nl/2009/10/27/some-notes-on-discovering-your-type-parameter-using-the-reflection-api/#comment-29368</guid>
		<description>Thanks very much for the article Ben!

Interesting: if we derive from Example a more specific class ExampleSerializable, and then try to determine the parameterized type of Example, it seems to be impossible.  (Or &quot;non-reifiable&quot; I guess.)

To test, take the final example code, and after line 7 add:

    pType = (ParameterizedType) ( (Class) pType.getRawType () ).getGenericSuperclass ();

That will get the superclass of ExampleSerializable (which is Example) as a parameterized type, so that we can reflect on the parameter .

Then change StringExample to derive from ExampleSerializable (rather than directly from Example).

    public class StringExample extends ExampleSerializable {}

Then create a new class &quot;ExampleSerializable&quot;:

    import java.io.Serializable;

    public class ExampleSerializable
        extends Example
    {
    }

So now we have a hierarchy of 3 derived classes:

    StringExample extends ExampleSerializable
    ExampleSerializable extends Example
    Example

The reportType() code chokes at runtime trying to cast the first &quot;actual type argument&quot;  to a Class:

    Exception in thread &quot;main&quot; java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
        at Example.reportType(Example.java:14)
        at StringExample.main(StringExample.java:4)

So far in my explorations I have not found anyway to determine what the type  of Example is.  The only remedy is to remove the &quot;interim&quot; class ExampleSerializable.

So I guess rather than create more reflective types at runtime, the powers-that-be decided to limit the depth of sub-typing to 1?

Seems somewhat arbitrary, considering the Type objects are only really instantiated when the reflection user actually asks for them...  They&#039;re not &quot;real&quot; types inside the JVM.

Or am I all muddled?

I don&#039;t suppose there are any workarounds for the &quot;2 levels of sub-typing&quot; case?

Thanks again Ben &amp; cheers,

Johann Tienhaara</description>
		<content:encoded><![CDATA[<p>Thanks very much for the article Ben!</p>
<p>Interesting: if we derive from Example a more specific class ExampleSerializable, and then try to determine the parameterized type of Example, it seems to be impossible.  (Or &#8220;non-reifiable&#8221; I guess.)</p>
<p>To test, take the final example code, and after line 7 add:</p>
<p>    pType = (ParameterizedType) ( (Class) pType.getRawType () ).getGenericSuperclass ();</p>
<p>That will get the superclass of ExampleSerializable (which is Example) as a parameterized type, so that we can reflect on the parameter .</p>
<p>Then change StringExample to derive from ExampleSerializable (rather than directly from Example).</p>
<p>    public class StringExample extends ExampleSerializable {}</p>
<p>Then create a new class &#8220;ExampleSerializable&#8221;:</p>
<p>    import java.io.Serializable;</p>
<p>    public class ExampleSerializable<br />
        extends Example<br />
    {<br />
    }</p>
<p>So now we have a hierarchy of 3 derived classes:</p>
<p>    StringExample extends ExampleSerializable<br />
    ExampleSerializable extends Example<br />
    Example</p>
<p>The reportType() code chokes at runtime trying to cast the first &#8220;actual type argument&#8221;  to a Class:</p>
<p>    Exception in thread &#8220;main&#8221; java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class<br />
        at Example.reportType(Example.java:14)<br />
        at StringExample.main(StringExample.java:4)</p>
<p>So far in my explorations I have not found anyway to determine what the type  of Example is.  The only remedy is to remove the &#8220;interim&#8221; class ExampleSerializable.</p>
<p>So I guess rather than create more reflective types at runtime, the powers-that-be decided to limit the depth of sub-typing to 1?</p>
<p>Seems somewhat arbitrary, considering the Type objects are only really instantiated when the reflection user actually asks for them&#8230;  They&#8217;re not &#8220;real&#8221; types inside the JVM.</p>
<p>Or am I all muddled?</p>
<p>I don&#8217;t suppose there are any workarounds for the &#8220;2 levels of sub-typing&#8221; case?</p>
<p>Thanks again Ben &amp; cheers,</p>
<p>Johann Tienhaara</p>
]]></content:encoded>
	</item>
</channel>
</rss>

