JDK-4896651 : (reflect) poor diagnostic for IllegalAccessException for package-private field
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.4.0,5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic,solaris_8
  • CPU: generic
  • Submitted: 2003-07-25
  • Updated: 2025-10-13
  • Resolved: 2025-10-08
Related Reports
Relates :  
Description
Date: Fri, 25 Jul 2003 02:46:40 -0700 (PDT)
From: Olivier Lefevre <###@###.###>
Subject: Odd compiler message
To: Neal M Gafter <###@###.###>
Reply-to: ###@###.###
Message-id: <###@###.###>

I was trying to access via reflection a public field in a class
that had nomodifier and thus was package-private, from a class
(its parent) in another package. The compiler said:

  java.lang.IllegalAccessException: Class <caller> can not access 
  a member of class <target> with modifiers "public"

Try as I might, I could not make heads or tails of that. So I tried
accessing the field "manually" instead, hoping to get a different
message, and indeed I got

  <target> is not public in <package>; cannot be accessed from 
  outside package

  <field> in <target> is not defined in a public class or interface;
  cannot be accessed from outside package

Now _that_ made sense! The problem happened because the
parent was once in the same package but was later moved 
out. In general, woulnd't it be a desirable goal to get
the same error message (although not the same stack trace,
of course) whether one is trying to access a field directly
or through reflection?

Best,

-- O.L.


Comments
Issues > 10 years old, please reopen and escalate if you want the issue to be re-evaluated.
08-10-2025

SUGGESTED FIX The string in the exception should be <field> in <target> is not defined in a public class or interface; cannot be accessed from outside package
11-06-2004

EVALUATION The message: java.lang.IllegalAccessException: Class <caller> can not access a member of class <target> with modifiers "public" occurs during runtime from sun.reflect.Reflection.ensureMemberAccess(). It was introduced in during the reflection re-write which occurred during merlin (1.4). This generically-worded exception is generated when a number of different error conditions are detected for constructors, methods, fields, and classes by Reflection.verifyMemberAccess. Given the large set of cases where this exception is generated, a trivial re-word is not obvious. One possible solution would be to change the return type of verifyMemberAccess to String instead of boolean. Rather than returning false, a more descriptive sentence describing the error condition could be provided. Though verifyMemberAccess is public, a complete search of the j2se and hotspot workspaces indicates that it is only called by Reflection.ensureMemberAccess. We should consider the possibility of changing this method from public to private. In general, it is very difficult to get the same error messages from javac and reflection because the compiler has access to more information than is available at runtime. As such, access analysis is done by completely different code which is factored in completely different ways. ###@###.### 2003-10-29
29-10-2003