Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Reflecting our own class as a Field

Thread view: 
tchou - 07 Nov 2006 13:06 GMT
Hello everyone,

I want to be able to dynamically load the attributes of a class Turtle.
That Turtle class have some Book attributes, Book being a class I made.

I want to load a list of those attributes but :

        Field f = getClass().getField(book);     // won't work.

because it is said that the getField method can't take this kind of
arguments.

The cause seems to be that Book is a class I made. How can I make
reflection work also on my own class?

I tried to implements Type and Member in my Book class, but it did not
work.

What am I doing wrong?

Thanks,
Tchouye.
Andrew Thompson - 07 Nov 2006 13:41 GMT
...
> That Turtle class have some Book attributes, Book being a class I made.

Out of curiosity, is there supposed to be a
connection between Turtles and Books?
Do the turtles read the books?  Loan them to frogs?
Or are the books merely turtle-food?

Andrew T.
tchou - 07 Nov 2006 13:46 GMT
My application is a financial one and I found it more understandable to
replace the attributes with words that I like :-))

But it my imaginary world, turtles live vey long and have learnt to
read and are leaders in book publishing.

Any ideas to solve the problem?

Andrew Thompson a ?crit :

> ...
> > That Turtle class have some Book attributes, Book being a class I made.
[quoted text clipped - 5 lines]
>
> Andrew T.
voorth - 07 Nov 2006 13:48 GMT
> Hello everyone,
>
[quoted text clipped - 15 lines]
>
> What am I doing wrong?
getField() expects the fields _name_.  See
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getField(java.lang.
String
)

after retrieving a Field object, you can then get your Book object with
the Field.get() method.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Field.html#get(java.la
ng.Object
)

In your case, this would be:
Field f = getClass().getField("whateverYourBookFieldisCalled");
Book b = (Book) f.get(this);
tchou - 07 Nov 2006 14:00 GMT
As I said, that does not work for Book but it's fine for the other
attributes.

class Turtle {
   Book book;
   String name;
}

Field f = getClass().getField(name);   //ok
Field f = getClass().getField(book);    //error :

Mmm my mistake.
I gave the attribute to the getField method, instead of a STRING...
Apparently the cast was implicit for String, as it's a String but not
for Book...

Thank you very much !

voorth a ?crit :

> > What am I doing wrong?
> getField() expects the fields _name_.  See
[quoted text clipped - 7 lines]
> Field f = getClass().getField("whateverYourBookFieldisCalled");
> Book b = (Book) f.get(this);
Patricia Shanahan - 07 Nov 2006 14:24 GMT
> As I said, that does not work for Book but it's fine for the other
> attributes.
[quoted text clipped - 11 lines]
> Apparently the cast was implicit for String, as it's a String but not
> for Book...

There was no cast involved, for the first call, because getField takes a
String parameter, and name is a String. However, I'm not 100% sure you
are expecting what will happen at run time. getField will look for a
field whose identifier matches the value of the name field at the time
of the call.

Patricia
tchou - 07 Nov 2006 14:44 GMT
Indeed, you're right. The field is not found as it takes the value of
the string.

Thank you Patricia,

Florence.

Patricia Shanahan a ?crit :

> There was no cast involved, for the first call, because getField takes a
> String parameter, and name is a String. However, I'm not 100% sure you
[quoted text clipped - 3 lines]
>
> Patricia
Daniel Pitts - 07 Nov 2006 19:50 GMT
> Patricia Shanahan a écrit :
>
[quoted text clipped - 10 lines]
>
> Florence.

No one has thought to point out that Reflection is a dangerous path to
go down.

There is probably a better solution to what you are trying to do. Why
do you need access to the Field through reflection?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.