R
Robert Dodier
Hello, I have a program that looks something like this:
public class A
{
public static void main (String [] aa) throws Exception
{
Object foo = new Object () { public double times (double x,
double y) { return x*y; } };
java.lang.reflect.Method f = foo.getClass().getMethod
("times", new Class [] {Double.TYPE, Double.TYPE});
bar.baz.Quux q = new bar.baz.Quux ();
q.mumble (foo, f)
}
}
where bar.baz.Quux.mumble eventually calls f.invoke(foo, <stuff>) and
<stuff> are some arguments.
My program compiles OK but when I run it I get an
IllegalAccessException
something like "Class bar.baz.Quux cannot access a member of class A
$1".
I'm not sure I understand the reason for that. bar.baz.Quux.mumble
isn't
attempting any member access in foo's class, neither explicitly via
foo.<whatever> nor implicitly via Class.getMethod or Class.getField.
If f.invoke(foo, <stuff>) fails, how can any call from another package
succeed? It seems that would rule out all the call backs in, say,
Swing
code which are implemented by anonymous inner classes.
Aside from just understanding the problem, I am hoping to find a way
to make the code work.
Thanks for any light you can shed on this.
Robert Dodier
public class A
{
public static void main (String [] aa) throws Exception
{
Object foo = new Object () { public double times (double x,
double y) { return x*y; } };
java.lang.reflect.Method f = foo.getClass().getMethod
("times", new Class [] {Double.TYPE, Double.TYPE});
bar.baz.Quux q = new bar.baz.Quux ();
q.mumble (foo, f)
}
}
where bar.baz.Quux.mumble eventually calls f.invoke(foo, <stuff>) and
<stuff> are some arguments.
My program compiles OK but when I run it I get an
IllegalAccessException
something like "Class bar.baz.Quux cannot access a member of class A
$1".
I'm not sure I understand the reason for that. bar.baz.Quux.mumble
isn't
attempting any member access in foo's class, neither explicitly via
foo.<whatever> nor implicitly via Class.getMethod or Class.getField.
If f.invoke(foo, <stuff>) fails, how can any call from another package
succeed? It seems that would rule out all the call backs in, say,
Swing
code which are implemented by anonymous inner classes.
Aside from just understanding the problem, I am hoping to find a way
to make the code work.
Thanks for any light you can shed on this.
Robert Dodier