JDK-4071593 : (reflect) Method.invoke() throws spurious IllegalAccessException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.1.3,1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_nt
  • CPU: generic,x86
  • Submitted: 1997-08-13
  • Updated: 2012-09-28
  • Resolved: 2002-04-24
Related Reports
Duplicate :  

Name: rlT66838			Date: 08/13/97

I am using JDK 1.1.3 with the JDK 1.1.3 VM on NT 4.0.
I am trying to use invoke() to call hasMoreElements()
on the Enumeration (java.util.VectorEnumerator) that I
obtained from a Vector. I am getting an llegalAccessException,
although I clearly have access to call this function
through the Enumeration interface.
I believe that the problem is related to the fact that
the class VectorEnumerator is not accessible to me,
even though I can call its public functions when it
is passed to me as an Enumeration. I think that invoke()
may be checking to see if the VectorEnumeration class
is accessible, and complaining when it discovers that 
it is not. I don't know if this is true (invoke() is native,
so I can't check the source.)

(Speculation follows: ) If this is the problem, I recommend modifying 
java.lang.reflect.Method.invokeinvoke(Object obj, Object args[])
so that it only checks to see if I have access to
the class in which Method is defined if the 
Method is static, and if it is non-static then 
it just checks the function that I am calling,
to better mimic the security behaviour that the VM
uses for a regular function invocation.
(End of speculation.)

Sample code is below.

import java.util.*;
import java.lang.reflect.*;
import java.io.*;

// Tries to use the reflection APIs to invoke Enumeration.hasMoreElements(),
// and fails miserably.	Sample output:
// >java test
//	java.lang.IllegalAccessException: java/util/VectorEnumerator
//		at test.main(test.java:46)

class test
	final static boolean HACK = false;
	public static void main(String[] argc)
		throws NoSuchMethodException,
		Vector myVector = new Vector();
		//foo myFoo = new foo();
		Enumeration myEnum;
		if (HACK)
			myEnum = new enumerationWrapper(myVector.elements());
			myEnum = myVector.elements();
		// Create the Vector & Enumeration
		// If HACK, then we wrap it with out (accessible) Enumeration
		Class[] theArgTypes = new Class[0];
			// hasMoreElements() takes 0 parameters
		Class enumClass = myEnum.getClass();
			// get the class
        Method hasMoreElements =
			enumClass.getDeclaredMethod("hasMoreElements", theArgTypes);
			// create the Method from the class & param list
		Object[] theArgs = new Object[0];
			// create the parameters (there are none of them)
		Object result =  hasMoreElements.invoke(myEnum, theArgs);
			// try to invoke Enumeration.hasMoreElements()
		System.out.println("m_Method.invoke(myEnum, theArgs); == " + result);
			// we never get here unless HACK == true
	public final static class enumerationWrapper implements Enumeration
		Enumeration java_util_VectorEnumerator;
		// The Enumeration we're wrapping
		public enumerationWrapper(Enumeration java_util_VectorEnumerator)
			this.java_util_VectorEnumerator = java_util_VectorEnumerator;
		public boolean hasMoreElements()
			return java_util_VectorEnumerator.hasMoreElements();
		public Object nextElement()
			return java_util_VectorEnumerator.nextElement();			



WORK AROUND Name: rlT66838 Date: 08/13/97 A (hack) workaround is shown in my sample code above. change final static boolean HACK to true, recompile, and I can access Enumeration through the wrapper class that I established. This works for VectorEnumerator, but I'll have to do this again for the next class that I trip over that also exhibits this behaviour. I doubt that VectorEnumerator is the only class that will cause this problem. ======================================================================

EVALUATION $ 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 test Exception in thread "main" java.lang.IllegalAccessException: Class test can not access a member of class java.util.Vector$1 with modifiers "public" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57) at java.lang.reflect.Method.invoke(Method.java:317) at test.main(test.java:46) Duplicate of bug 4071957. -- iag@sfbay 2002-04-24