Context inside default method body

Peter Levart peter.levart at marand.si
Fri Jul 20 01:32:50 PDT 2012


I think JLS could be revised for this case. Currently it says:


6.6.2. Details on protected Access 
A protected member or constructor of an object may be accessed from outside 
the package in which it is declared only by code that is responsible for the 
implementation of that object. 

6.6.2.1. Access to a protected Member 
Let C be the class in which a protected member is declared. Access is 
permitted only within the body of a subclass S of C. 

In addition, if Id denotes an instance field or instance method, then: 
If the access is by a qualified name Q.Id, where Q is an ExpressionName, then 
the access is permitted if and only if the type of the expression Q is S or a 
subclass of S. 
If the access is by a field access expression E.Id, where E is a Primary 
expression, or by a method invocation expression E.Id(. . .), where E is a 
Primary expression, then the access is permitted if and only if the type of E 
is S or a subclass of S. 

With introduction of default methods in interfaces, "code responsible for the 
implementation of an object" does not exclusively belong to subclasses of a 
class where the protected member is declared  but also to interfaces that the 
class (or subclasses) directly or indirectly implement.

This currently only applies to 2 protected Object methods, but it would have 
to be (re)considered if/when interfaces get protected methods.

Regards, Peter


On Friday, July 20, 2012 11:13:05 AM Deepak S Patwardhan wrote:
> Hi all,
> 
> The following doesn't compile with the latest build of lambda :
> 
> public interface Islander {
> 
>     public void replaceLiver() default {
>         try {
>             Object myClone = this.clone(); //.. Compile error
>             //.. get the replacement from myClone
>         } catch (CloneNotSupportedException cnse) {
>             throw new UnsupportedOperationException();
>         }
>     }
> }
> 
> Basically, I cannot access clone(), which is protected in Object, in spite
> of having the *this* reference.
> 
> I think that's ok and not a big deal since Object has only two protected
> methods, and the other method (finalize) shouldn't be invoked by anyone,
> except the GC.  But, it just feels like a minor anomaly that default bodies
> can do everything that normal (ie Class) methods can do, except that they
> cannot clone their callees. (Don't hold limitations of Interfaces against
> them, like access to instance variables)
> 
> Regards,
> Deepak S Patwardhan.


More information about the lambda-dev mailing list