Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To answer my own question: It IS possible to create a non-generic TypeToken using the static constructor <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/reflect/TypeToken.html#of%28java.lang.Class%29" rel="nofollow">TypeToken.of(Class)</a>, but this wont work for deeper generic types.</p> <p>Here's the Spring xml config:</p> <pre><code>&lt;bean class="com.google.common.reflect.TypeToken" factory-method="of"&gt; &lt;constructor-arg type="java.lang.Class" value="java.lang.Integer" /&gt; &lt;/bean&gt; </code></pre> <p>Which is equivelent to:</p> <pre><code>TypeToken.of(Integer.class) </code></pre> <p>and</p> <pre><code>new TypeToken&lt;Integer&gt;() {} </code></pre> <p>I also found a way to use the <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/reflect/TypeToken.html#of%28java.lang.reflect.Type%29" rel="nofollow">TypeToken.of(Type)</a> constructor with a <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/ParameterizedType.html" rel="nofollow">ParameterizedType</a> constructed using <a href="https://code.google.com/p/google-guice/" rel="nofollow">Google Guice</a>'s <a href="http://google-guice.googlecode.com/git/javadoc/com/google/inject/util/Types.html#newParameterizedType%28java.lang.reflect.Type,%20java.lang.reflect.Type...%29" rel="nofollow">Types</a> utility. <a href="https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/reflect/Types.java" rel="nofollow">Guava has one too</a>, but it's not public. :'( I'm not sure if this is quite as robust as using TypeToken/TypeCapture, but it seems to work. Unfortunately it's pretty ugly and long... (maybe someone can simplify it?)</p> <pre><code>&lt;bean class="com.google.common.reflect.TypeToken" factory-method="of"&gt; &lt;constructor-arg index="0"&gt; &lt;bean class="com.google.inject.util.Types" factory-method="newParameterizedType"&gt; &lt;constructor-arg index="0"&gt; &lt;value type="java.lang.Class"&gt;java.util.List&lt;/value&gt; &lt;/constructor-arg&gt; &lt;constructor-arg index="1"&gt; &lt;array&gt;&lt;value type="java.lang.Class"&gt;java.lang.String&lt;/value&gt;&lt;/array&gt; &lt;/constructor-arg&gt; &lt;/bean&gt; &lt;/constructor-arg&gt; &lt;/bean&gt; </code></pre> <p>Which is equivelent to:</p> <pre><code>new TypeToken&lt;List&lt;String&gt;() {} </code></pre>
 

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