Note that there are some explanatory texts on larger screens.

plurals
  1. POjava generics type parameters and operations on those types
    primarykey
    data
    text
    <p>In searching for an answer to an interesting situation which I had recently encountered I came upon the following question: <a href="https://stackoverflow.com/questions/869169/type-safety-java-generics-and-querying">Type safety, Java generics and querying</a></p> <p>I have written the following class (cleaned up a bit)</p> <pre><code>public abstract class BaseDaoImpl&lt;T extends Serializable&gt; extends HibernateDaoSupport implements BaseDao&lt;T&gt; { /** * Finds and Returns a list of persistent objects by a collection of criterions * @param criterions * @return list of persistent objects * @throws DBException */ @SuppressWarnings("unchecked") protected List&lt;T&gt; findByCriteria(Collection&lt;Criterion&gt; criterions) throws DBException { try { DetachedCriteria criteria = DetachedCriteria.forClass(T.class); // BAD!!! for (Criterion criterion : criterions) { criteria.add(criterion); } List&lt;T&gt; result = getHibernateTemplate().findByCriteria(criteria); return result; } catch (Exception e) { throw new DBException(T.class + " lookup by " + criterions + " failed", e); // BAD!!! } } } </code></pre> <p>For some (probably good reason) <code>T.class</code> causes a compile time error.</p> <p>My first question is why?</p> <p>If I change it to <code>T.getClass()</code> which obviously shouldn't compile - because no 'T' when "expanded" or goes through "erasure" - should have a static method such as that. The eclipse IDE gives the following compilation message:</p> <blockquote> <p>Cannot make a static reference to the non-static method getClass() from the type Object</p> </blockquote> <p>My second question is why? And what does this error imply actually?</p> <p>Finally, would solving this in the manner specified in the link above (or rather my interpretation of) be the most optimal way?</p> <pre><code>public abstract class BaseDaoImpl&lt;T extends Serializable&gt; extends HibernateDaoSupport implements BaseDao&lt;T&gt;, MyGenericHelper&lt;T&gt; { /** * Finds and Returns a list of persistent objects by a collection of criterions * @param criterions * @return list of persistent objects * @throws DBException */ @SuppressWarnings("unchecked") protected List&lt;T&gt; findByCriteria(Collection&lt;MyCriterion&gt; criterions) throws DBException { try { DetachedCriteria criteria = DetachedCriteria.forClass(getGenericClass()); // BAD!!! for (Criterion criterion : criterions) { criteria.add(criterion); } List&lt;T&gt; result = getHibernateTemplate().findByCriteria(criteria); return result; } catch (Exception e) { throw new DBException(getGenericClass() + " lookup by " + criterions + " failed", e); // BAD!!! } } } public interface MyGenericHelper&lt;T extends Serializable&gt; { public Class&lt;T&gt; getGenericClass(); } </code></pre> <p>Thanks!</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.
 

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