• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class with generic types

 
daniele licitra
Ranch Hand
Posts: 81
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all.

In java you can use generic types


In java 8, is introduced a simple way to get the class type of T?

For example, in JPA if I want a TypedQuery , I must now the class of T

Another example are the criteria api and the selection of root entity.
 
Ivan Jozsef Balazs
Rancher
Posts: 992
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Due to erasure, the compile-time type disappears by run time. If need be, you can use class Literals as runtime-type tokens:

Class Literals as Runtime-Type Tokens
 
daniele licitra
Ranch Hand
Posts: 81
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ivan, it is what i will use.
The code above simply become:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would make it something like this:

Avoid using raw types (i.e. generic types such as java.lang.Class but without specifying type parameters) as much as possible. Java allows you to use raw types, but that's really only for backward compatibility with Java 1.4 and older.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!