• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic Interface  RSS feed

Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends,

Can any one explain this code

GenericDaoImpl is the constructor.

Please explain these line of code in detail.

Thank you

Posts: 10575
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hardik sanghani wrote:Can any one explain this code

Well, it would appear to be code that retrieves the type of a GenericDaoImpl object, which is presumably declared as a GenericDaoImpl<T>.

I think the way it works is this (and if I'm wrong, I'm sure I'll be flamed ):

  • Someone creates a typed GenericDaoImpl object - eg, a GenericDaoImpl<String>.
  • The constructor then gets its generic superclass - ie, GenericDaoImpl<T> (or equivalent).
  • It then casts it to a ParameterizedType in order to retrieve the type that was supplied to the constructor - ie, String.
  • It then casts that to a Class.

  • The upshot of which is that 'type' will contain the class that was supplied to 'new' as a type; or throw a TypeNotPresentException (I think) if it was just created with:
    new GenericDaoImpl();

    However, I'm not sure I like it too much, because it seems to make a lot of assumptions along the way.



    PS: My question, to anyone "in the know" about these things: Why doesn't getGenericSuperclass() simply return a ParameterizedType itself? If the class isn't generic, for whatever reason, it could surely just return null, or a ParameterizedType with an empty array couldn't it?
    Posts: 56600
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch

    That sounds pretty disastrous code. You are using reflection to get details of some object or other. Reflection can be dangerous because you can breach the type‑safety usually provided by Java®.

    You start by getting the Class<T> object for this object.
    Then you get its superclass.
    Then you cast that to ParametrisedType.
    Then you get the first of its actual type arguments and assign that value to type, which I presume is a field declared as Type.

    I suggest you start by reading all those links underlined above, by contacting whoever wrote that code and getting them to explain it to you (if possible) and by reminding yourself about reflection, maybe in this tutorial.

    It looks as if you declare that class as GenericDaoImpl<Foo, Bar> and that assigns whatever Foo is to the Type field called type. I have no idea what the purpose of such a class is. Maybe it is so you know the type when rebuilding the data later. In which case why isn't the data object initialised as a field in that constructor? What happens if you instantiate that class as a raw type? Will you end up with nulls there? What if you can't execute the cast?
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!