• Post Reply Bookmark Topic Watch Topic
  • New Topic

[Generics and relfection] cast  RSS feed

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I need some help with generics and relfection. In a method is passed an Object which can be a user type like: partner, employee, ecc. all children of Usr object. At the moment I only need to do some checks on the Usr properties common to all objects.

I write


what's the correct sintax?
Thanks in advance
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically if you get an error, it's helpful to tell us what the error message says. This makes it easier for us to understand the problem.

The first line could be made to compile with either

or

The latter may lead to a ClassCastException later if obj is not really a Usr. I would use the first, unless you're sure obj is a Usr.

However, neither of these will help you run the second line, which is probably what you want. A Class object does not have a getUserName() method. It's possible to access the getUserName() method in Usr using getMethod() (in class Class) but that's probably more work than you need. If I understand your goals correctly, then it will be simpler to do this without reflection:
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a method is passed an Object which can be a user type like: partner, employee, ecc. all children of Usr object

Is there a reason why you are passing the method an Object instead of a Usr and thus eliminating the casts altogether?
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically if you get an error, it's helpful to tell us what the error message says. This makes it easier for us to understand the problem.

Apologize
Incompatible types


Is there a reason why you are passing the method an Object instead of a Usr and thus eliminating the casts altogether?

It's an EJB3 which persists POJO classes, if I cast the object I risk to loose the additional properties which need to be written in the database.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alessandro Ilardo:

if I cast the object I risk to loose the additional properties


No, you don't. Casting won't have any effect on the object at all. It only effects the type of the *reference* to that object. (This is very different from casting primtive types.)
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so if the client pass the object Employe (exteds Usr) to the following method



then the Employe will successfully written into my database without loosing additional properties that are not present in the Usr object.
Am I correct?
[ April 18, 2007: Message edited by: Alessandro Ilardo ]
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just tried to pass an Employe object (which extends an Usr) to my method.
this is what it came up with:
javax.ejb.EJBException: java.lang.NullPointerException
at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:69)
...

Caused by: java.lang.NullPointerException
at com.myPackage.UsrBean.addUsr(UsrSessionBean.java:376)
...

My Bean code:


the error is caused by .getUsername()
Is it not a method inherit by the super class?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!