[kaffe] CVS kaffe (guilhem): Fixed class accessibility.

Ito Kazumitsu kaz at maczuka.gcd.org
Wed Dec 21 18:08:15 PST 2005


From: Kaffe CVS <cvs-commits at kaffe.org>
Date: Wed, 21 Dec 2005 10:13:12 -0800

> Fixed class accessibility.
>         * kaffe/kaffevm/access.c
>         (checkAccess): If target and context are in the same package they are
>         always visible.

>  		same_package = 1;
>  		/* Package */
> -		if (!(target->accflags & ACC_PROTECTED))
> -		{
> -			class_acc = 1;
> -		}
> +		class_acc = 1;

It was stupid of me to check "!(target->accflags & ACC_PROTECTED)"
here.  Maybe I meant "!(target->accflags & ACC_PRIVATE)".

But anyway, the correct way is not to check target->accflags
because, if target and context are in the same package, they are
always visible.

Having looked at Sun's documents "The Java Virtual Machine Specification"
and "The Java Language Specification",  I have got some idea of
how the run time access control differs from the compile time access
control.

A class or interface C is accessible to a class or interface D if and only if
either of the following conditions are true:

(Runtime):
   - C is public.
   - C and D are members of the same runtime package.

(Compile time):
   - C is public.
   - C is a top level class, and C is not public, and C belongs to the
     same package as D.
   - C is an inner class, and any of the following conditions are true:
      o C is public.
      o C is protected and ...(omitted).
      o C is private and C and D belong to the same top level class.
      o C is none of the above, and C and D belong to the same package.

A field or method (or a construntor) R is accessible to a class or
interface D if and only if any of the following conditions is true:

(Runtime):
  - R is public.
  - R is protected and is declared in a class C, and D is either a subclass of
    C or C itself.
  - R is either protected or package private (that is, neither public nor
    protected nor private), and is declared by a class in the same runtime
    package as D.
  - R is private and is declared in D.

(Compile time):
  - R is declared in a class or interface C, and C is accessible to D
    and any of the following conditions is true:
      o R is public.
      o R is protected and ...(omitted).
      o R is private and R and D belong to the same top level class.
      o R is none of the above, and R and D belong to the same package.

According to this, the behavior of Sun's JDK throwing
IllegalAccessException for the following may be correct.

public class A {
  private static class A1 {
    private A1() {}
  }
  public static void main(String[] args) throws Exception {
    new A1();
    A1.class.newInstance();
  }
}

new A1() is allowed because the private constructor is within the
top level class A, but A1.class.newInstance() is illegal because
the private constructor is not declared in A.

The runtime access control is much simpler than the compile-time
access control.  And we may be able to omit the checking of nested
relations of classes at runtime.



More information about the kaffe mailing list