JDK-4238091 : (reflect) Class.getFields returns fields which are not public
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.1.8
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1999-05-13
  • Updated: 2012-09-28
  • Resolved: 2002-05-09
Related Reports
Duplicate :  
Description

Name: dbT83986			Date: 05/13/99


/*
method check.showProblem raises IllegalAccessException
even though getFields should only return "all the accessible public fields of the class"

This is ambiguous for member notQuitePublic because it has
default package access.

I need a way to distinguish the member publicField
from the member notQuitePublic.
*/



// file packageAxs\PublicBase.java
package packageAxs;
public class PublicBase { 
	 public int publicField = 456;
}

// file packageAxs\Class1.java
package packageAxs;
import java.lang.reflect.*;
import check;

public class Class1
{
	public static void main (String[] args) {
		check.showProblem(new packageDerived());
	}
}
// note this class is defined locally in file 
// packageAxs\Class1.java
class packageDerived extends PublicBase {
	public int notQuitePublic = 123;
	}

// file check.java
import java.lang.reflect.*;

public class check {
	public static void showProblem(Object o) {
		Class c = o.getClass();
		System.out.println("Class " + c + " " + Modifier.toString(c.getModifiers()));

		// when called with an object from packageDerived getModifiers tells
		// me it is not public. But I can access the field publicField because
		// it comes from a public base class.

		Field[] fa = c.getFields();
		for (int i = 0; i < fa.length; i++) {
			System.out.println("Field " + fa[i] + " is " + Modifier.toString(fa[i].getModifiers()));
			try {
				Object or = fa[i].get(o);
				System.out.println("Field named " + fa[i].getName() + " returned " + or);
				}
			catch (IllegalAccessException e) {
				// IllegalAccessException is raised even though the field
				// notQuitePublic appears in the list of public fields as returned
				// from c.getFields
				System.out.println("IllegalAccessException: How can i prevent this for " + fa[i].getName());
				}
			}
		}
}
(Review ID: 58102) 
======================================================================

Comments
WORK AROUND Name: dbT83986 Date: 05/13/99 I havent tried it but it should be possible to look at the class which in the case of packageDerived is not public and then search the base classes until I have a public base class. However this is clumsy and I haven't checked more complex cases, e.g. inheritance from local Base to a public derived to another local derived class. ======================================================================
11-06-2004

EVALUATION $ java -cp . packageAxs.Class1 Class class packageAxs.packageDerived Field public int packageAxs.packageDerived.notQuitePublic is public java.lang.IllegalAccessException: Class ck.check can not access a member of class packageAxs.packageDerived with modifiers "public" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57) at java.lang.reflect.Field.doSecurityCheck(Field.java:811) at java.lang.reflect.Field.getFieldAccessor(Field.java:758) at java.lang.reflect.Field.get(Field.java:228) at ck.check.showProblem(check.java:19) at packageAxs.Class1.main(Class1.java:9) IllegalAccessException: How can i prevent this for notQuitePublic Field public int packageAxs.PublicBase.publicField is public Field named publicField returned 456 Duplicate of 4071957. -- iag@sfbay 2002-05-09
09-05-2002