United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4533479 (reflect) invoke() may fail even for public method on IllegalAccessException
JDK-4533479 : (reflect) invoke() may fail even for public method on IllegalAccessException

Details
Type:
Bug
Submit Date:
2001-11-30
Status:
Closed
Updated Date:
2012-09-28
Project Name:
JDK
Resolved Date:
2002-04-25
Component:
core-libs
OS:
linux
Sub-Component:
java.lang:reflect
CPU:
x86
Priority:
P4
Resolution:
Duplicate
Affected Versions:
1.3.1
Fixed Versions:

Related Reports
Duplicate:

Sub Tasks

Description
http://openide.netbeans.org/issues/show_bug.cgi?id=11679

A public method is defined in package private class. The package private class is then extended by a public class ni the same package. So:

  Method theMethod = publicIntance.getClass().getMethod("theMethod");

returns proper method (quering it for accessibility returns true). And it is possible to call the method without reflection:

  publicInstance.theMethod();

but reflection invocation

  theMethod.invoke(publicInstance, new Object[0])

throws the exception.

                                    

Comments
EVALUATION

Using the provided description, I constructed the following test:

FILE: Test.java
package p;

import java.lang.reflect.Method;

class A {
    public void m() {
	System.out.println("a");
    }
}

public class Test extends A {
    public static void main(String [] args) throws Exception {
	Test t = new Test();
	Method m = t.getClass().getMethod("m", null);
	System.out.println(m + ": " + m.isAccessible());

	t.m();

	m.invoke(t, new Object[0]);
    }
}

Unfortunately I've been unable to reproduce the reported problem on linux,
windows, or solaris using jdk1.2.2 - jdk1.4.1-b10.  Running the above code
always produces the following output:

$ java p.Test
public void p.A.m(): false
a
a

Please provide a complete test case which reproduces the problem under at least
jdk1.4.  I read the comments at the netbeans.org issue page.  It references a 
possible problem with the native code for Method.invoke.  As of jdk1.4, this
method is no longer implemented using native code.

-- iag@sfbay 2002-04-24

Sorry I think your test is false (it does not perform cross package call). Well I am attaching test archive that contains bytecode that reproduces it on 1.4 and 1.3 as well.

I also boubt that there is no native code anymore see stacktrace:

public void bt4533.j479.A.m(): false
java.lang.Exception: m called
        at bt4533.j479.A.m(Invoke.java:18)
        at bt4533.j479.Invoke.main(Invoke.java:28)
java.lang.Exception: m called
        at bt4533.j479.A.m(Invoke.java:18)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at bt4533.j479.Invoke.main(Invoke.java:30)
 
-- ###@###.### 2002-04-25

The above stack trace does not match the provided example.  The only main entry
point in the example is in academy.reflect.TestAccessible.  Using the provided
class files (which appear to have been generated using jdk1.3 or the jdk1.4
compiler using the non-default behaviour),  the output for this example using
the last few jdk's is as follows:

$ 13          
$ java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)
(iag@ribbit) ~///lang/reflect/4533479 $ java academy.reflect.TestAccessible
>OK
Exception in thread "main" java.lang.IllegalAccessException
        at java.lang.reflect.Method.invoke(Native Method)
        at academy.reflect.TestAccessible.main(TestAccessible.java:36)
$ 131 
$ java -version  
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
(iag@ribbit) ~///lang/reflect/4533479 $ java academy.reflect.TestAccessible
>OK
Exception in thread "main" java.lang.IllegalAccessException
        at java.lang.reflect.Method.invoke(Native Method)
        at academy.reflect.TestAccessible.main(TestAccessible.java:36)
$ 14
$ java -version                      
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
(iag@ribbit) ~///lang/reflect/4533479 $ java academy.reflect.TestAccessible
>OK
Exception in thread "main" java.lang.IllegalAccessException: Class academy.reflect.TestAccessible can not access a member of class academy.reflect.x.PackagePrivate with modifiers "public"
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57)
        at java.lang.reflect.Method.invoke(Method.java:317)
        at academy.reflect.TestAccessible.main(TestAccessible.java:36)
$ 141
$ java -version
java version "1.4.1-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b10)
Java HotSpot(TM) Client VM (build 1.4.1-beta-b10, mixed mode)
$ java academy.reflect.TestAccessible
>OK
Exception in thread "main" java.lang.IllegalAccessException: Class academy.reflect.TestAccessible can not access a member of class academy.reflect.x.PackagePrivate with modifiers "public"
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57)
        at java.lang.reflect.Method.invoke(Method.java:317)
        at academy.reflect.TestAccessible.main(TestAccessible.java:36)

There is no difference in behaviour when the files are compiled using jdk1.4.x.
As you can see from the stack traces and as stated in my previous evaluation,
there is no more native code in  Method.invoke as of jdk1.4.  Based on the
provided example and the above behaviour, I conclude that this is a duplicate of
4071957.

-- iag@sfbay 2002-04-25

                                     
2002-04-25
WORK AROUND

setAccesible(true) to workaround it
                                     
2004-06-11



Hardware and Software, Engineered to Work Together