Reflecting our own class as a Field

Discussion in 'Java' started by tchou, Nov 7, 2006.

  1. tchou

    tchou Guest

    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.
    tchou, Nov 7, 2006
    #1
    1. Advertising

  2. 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.
    Andrew Thompson, Nov 7, 2006
    #2
    1. Advertising

  3. tchou

    tchou Guest

    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 :

    > 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.
    tchou, Nov 7, 2006
    #3
  4. tchou

    voorth Guest

    tchou wrote:
    > 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);
    voorth, Nov 7, 2006
    #4
  5. tchou

    tchou Guest

    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
    > 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);
    tchou, Nov 7, 2006
    #5
  6. tchou wrote:
    > 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
    Patricia Shanahan, Nov 7, 2006
    #6
  7. tchou

    tchou Guest

    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
    > 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, Nov 7, 2006
    #7
  8. tchou

    Daniel Pitts Guest

    tchou wrote:
    > 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
    > > 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> Indeed, you're right. The field is not found as it takes the value of

    > 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?
    Daniel Pitts, Nov 7, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jake Barnes
    Replies:
    1
    Views:
    407
    Andy Dingley
    Nov 14, 2005
  2. Pradeep

    Implementing our own providers

    Pradeep, Jun 7, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,791
    john_teague
    Jun 7, 2006
  3. Kamilche

    Our OWN 'Deep C Secrets'

    Kamilche, May 8, 2004, in forum: C Programming
    Replies:
    21
    Views:
    709
    goose
    May 14, 2004
  4. bala
    Replies:
    0
    Views:
    239
  5. Jeremy Targett

    reflecting bit field

    Jeremy Targett, Aug 2, 2005, in forum: C Programming
    Replies:
    4
    Views:
    292
    Alex Fraser
    Aug 4, 2005
Loading...

Share This Page