java reflection issues

Discussion in 'Java' started by porter, Dec 10, 2005.

  1. porter

    porter Guest

    Hi,

    Could someone help me with this: I'm trying to get 2 applets to talk to
    one another via reflection: I have 2 jar archives - one is third party,
    the other is mine. The third party archive contains a class derived
    from Applet (DerivedApplet say) that contains a public method called
    'execute' and a public field called 'viewer'. I can use reflection to
    run the execute() method just fine, but trying to use reflection to
    gain access to the public field fails

    // This works fine
    Method meth =
    app.getClass().getMethod("execute",types);
    meth.invoke(app,new String("set color green"));

    // But this fails at the cast below
    Field field = app.getClass().getField("viewer");

    // This prints out "xxx.Viewer"
    System.out.println(
    field.get(app).getClass().getName() );

    Object ob = field.get(app);
    // This throws ClassCastException
    xxx.Viewer viewer = (xxx.Viewer) ob;

    Can anyone see anything wrong? TIA

    Jason
     
    porter, Dec 10, 2005
    #1
    1. Advertising

  2. Maybe that's because the object retreive from reflection, although
    being of type xxx.Viewer,
    is an instance of a class loaded from another class loader than the one
    your applet is
    currently using.

    A quick test would be:

    System.out.println(field.get(app).getClass() == xxx.Viewer.class);

    If it returns false, then there is two class loaders involved for
    xxx.Viewer.

    Regards
     
    Jean-Francois Briere, Dec 10, 2005
    #2
    1. Advertising

  3. porter

    porter Guest

    Did the test - this does appear to be the case. Any thought as to how
    this might arise?
     
    porter, Dec 10, 2005
    #3
  4. porter

    Roedy Green Guest

    On 10 Dec 2005 11:17:23 -0800, "porter" <> wrote,
    quoted or indirectly quoted someone who said :

    >Could someone help me with this: I'm trying to get 2 applets to talk to
    >one another via reflection:


    Reflection is not the normal tool for inter-Applet communication. You
    more commonly use code like this:

    for ( Enumeration other = getAppletContext().getApplets(); other
    .hasMoreElements(); )
    {
    Object otherApplet = other.nextElement();
    {
    if ( otherApplet instanceof CurrCon )
    {
    CurrCon otherCurrConApplet = (CurrCon)otherApplet;
    otherCurrConApplet.currencyChangeListener();
    }
    }
    }


    Reflection likely requires a signed Applet. See
    http://mindprod.com/jgloss/signedapplets.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 10, 2005
    #4
  5. porter

    Roedy Green Guest

    On 10 Dec 2005 13:00:50 -0800, "porter" <> wrote,
    quoted or indirectly quoted someone who said :

    >Did the test - this does appear to be the case. Any thought as to how
    >this might arise?


    How the browser works inside is its own business, but consider that
    you can't very well recover the RAM from an Applet's static
    code/fields unless you load it with a custom class loader.

    Also consider that Applets running on different pages are not supposed
    to see each other. So they should not be sharing common static
    fields. I think they would have to be running under different class
    loaders.

    I have not yet taken the time to poke around to figure out exactly how
    the various browsers work vis a vis multiple Applets per page, per
    window, with tracking what really happens with destroy and init.

    I have discovered I can communicate between Applets on the same page
    with getAppletContext().getApplets(); and just directly calling
    methods, all of which appears to happen on the same thread.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 10, 2005
    #5
  6. porter

    porter Guest

    Hi,

    Thanks for this - it's quite interesting. My applets are in the same
    web page intentionally - the point is that one of them is third party -
    I can't change it, but it does expose components of itself in its
    Applet A.P.I. which theoretically allow me to interface to it.

    As you say above, the class loader issue does not affect you when
    calling methods - in my example, the 'Method' based reflection using
    invoke worked just fine.

    In follow up to the earlier messages I went back and tried tweaking my
    applet definitions - If I ensure that the code base and archive
    instructions in the applet tags are identical, then the class cast
    works fine in firefox. I'll have to try out some other browsers.

    Thanks

    Jase
     
    porter, Dec 10, 2005
    #6
  7. porter

    porter Guest

    Oh yeah - P.S> I havent had to use signed applets to get java
    reflection to work - the third party Applet I'm using provides a
    reflection based API that allows the extender to add in commands
    implemented as classes but called either via a javascript call or by
    calling execute() on the Applet . The Applet has no signing in it, but
    I've had no problems deploying it as an untrusted applet? I'm only ever
    accessing reflection classes from the associated web server, so that
    may be why.
     
    porter, Dec 10, 2005
    #7
    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. MDBloemker
    Replies:
    1
    Views:
    383
    Rick Spiewak
    May 10, 2004
  2. A. Wiebenga

    Reflection in Java - Please help..

    A. Wiebenga, Feb 6, 2004, in forum: Java
    Replies:
    2
    Views:
    502
    Tony Morris
    Feb 8, 2004
  3. Replies:
    23
    Views:
    5,576
  4. Benjamin Johnston
    Replies:
    1
    Views:
    484
    Mike Schilling
    Jan 6, 2005
  5. pek
    Replies:
    2
    Views:
    1,077
Loading...

Share This Page