Reflecting our own class as a Field

T

tchou

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.
 
A

Andrew Thompson

tchou wrote:
....
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.
 
T

tchou

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 :
 
V

voorth

tchou said:
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?
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.lang.Object)

In your case, this would be:
Field f = getClass().getField("whateverYourBookFieldisCalled");
Book b = (Book) f.get(this);
 
T

tchou

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 :
 
P

Patricia Shanahan

tchou said:
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...

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
 
T

tchou

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 :
 
D

Daniel Pitts

tchou said:
Patricia Shanahan a écrit :

the string.

Thank you Patricia,

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top