problem with class accessiblity check in invoke (natMethod.cc)

Erik Groeneveld erik@cq2.nl
Wed Dec 23 20:57:00 GMT 2009


>>     java::util::ArrayList* l = new java::util::ArrayList();
>>     java::util::Iterator* i = l->iterator();
>>     i = l->iterator();
>>     i->hasNext();
>> Right, because CNI is bypassing the controls.

Ok. CNI behaves just as both OpenJDK and GCJ do on corresponding Java code:
 ArrayList l = new ArrayList();
 Iterator i = l.iterator();
 i.hasNext();
And your example indeed raises IllegalAccessException in both OpenJDK and GCJ:
> import java.lang.reflect.*;
> import java.util.*;
>> public class Problem
> {
>  public static void main(String[] args)
>    throws Throwable
>  {
>    ArrayList l = new ArrayList();
>    Iterator i = l.iterator();
>    Method m = (i.getClass().getDeclaredMethod
>                ("hasNext", null));
>    m.invoke(i, null);
>  }
> }

I am convinced but still unsure how to deal with it. I don't see a way
to translate the direct Java call into the corresponding reflection
part. I think I settle for the setAccessible() option.
Best regards
Erik


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /