public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* A protected slot is not accessible in a subclass
@ 2022-03-24 22:14 Panicz Maciej Godek
  2022-03-25  4:56 ` Per Bothner
  0 siblings, 1 reply; 5+ messages in thread
From: Panicz Maciej Godek @ 2022-03-24 22:14 UTC (permalink / raw)
  To: kawa

Hi,
I'm trying to sub-class the class gnu.mapping.Symbol from within Kawa.
In the source code, the class has a protected field:

protected String name;

But when I subclass it like this:

(define-simple-class S (gnu.mapping.Symbol)
  ((*init*)
   (invoke-special gnu.mapping.Symbol (("foo":toString):intern)
gnu.mapping.Namespace:EmptyNamespace)
   (set! name (("bar":toString):intern))))

I get the following error:

slot 'name' in gnu.mapping.Symbol not accessible here

What are the rules for accessing protected members in Kawa?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: A protected slot is not accessible in a subclass
  2022-03-24 22:14 A protected slot is not accessible in a subclass Panicz Maciej Godek
@ 2022-03-25  4:56 ` Per Bothner
  2022-03-25  9:59   ` Panicz Maciej Godek
  0 siblings, 1 reply; 5+ messages in thread
From: Per Bothner @ 2022-03-25  4:56 UTC (permalink / raw)
  To: Panicz Maciej Godek, kawa



On 3/24/22 15:14, Panicz Maciej Godek via Kawa wrote:
> I get the following error:
> 
> slot 'name' in gnu.mapping.Symbol not accessible here
> 
> What are the rules for accessing protected members in Kawa?

The rules are supposed to be the same as in Java
(because those rules are enforced by the JVM).
However, those rules are actually a bit complicated:
https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html#jls-6.6.2

In this case there is a bug in Kawa. It treats
   (set! name ...)
as equivalent to:
   (set! (this):name ...)
which is fine.

However, there seems to a problem where Kawa thinks (this) has the type Symbol (because that is
where name is defined) rather than class S.  And this violates the rule:

     If the access is by (i) a qualified name of the form ExpressionName.Id or TypeName.Id, or
     (ii) a field access expression of the form Primary.Id, then access to the instance field Id
     is permitted if and only if the qualifying type is S or a subclass of S.

     The qualifying type is the type of the ExpressionName or Primary, or the type denoted by TypeName.

A work-around is to use the field special procedure:

    (set! (field (this) 'name) ...)
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: A protected slot is not accessible in a subclass
  2022-03-25  4:56 ` Per Bothner
@ 2022-03-25  9:59   ` Panicz Maciej Godek
  2022-03-25 18:46     ` Per Bothner
  0 siblings, 1 reply; 5+ messages in thread
From: Panicz Maciej Godek @ 2022-03-25  9:59 UTC (permalink / raw)
  To: Per Bothner; +Cc: kawa

Thanks for the reply!

I tried this:


(define-simple-class S (gnu.mapping.Symbol)
   ((*init*)
    (invoke-special gnu.mapping.Symbol (this) '*init*
(("foo":toString):intern) gnu.mapping.Namespace:EmptyNamespace)
 (set! (field (this) 'name) (("bar":toString):intern))))


(define s::S(S))

but it gives a similar error:

java.lang.RuntimeException: no such field name in S
        at gnu.kawa.reflect.SlotSet.apply(SlotSet.java:115)
        at gnu.kawa.reflect.SlotSet.apply3(SlotSet.java:120)
        at S.<init>(tty:14)

I also tried replacing

(field (this) 'name)

with



(field (as gnu.mapping.Symbol (this) 'name)

but it didn't change anything.

Outside the setter context, (field (this) 'name) works fine.

BTW my previous message contained an error - instead of

(invoke-special gnu.mapping.Symbol (("foo":toString):intern)
gnu.mapping.Namespace:EmptyNamespace)

I should of course have written

(invoke-special gnu.mapping.Symbol (this) '*init* (("foo":toString):intern)
gnu.mapping.Namespace:EmptyNamespace)

I mistakenly omitted it while I was writing the message.

pt., 25 mar 2022, 05:56 użytkownik Per Bothner <per@bothner.com> napisał:

>
> On 3/24/22 15:14, Panicz Maciej Godek via Kawa wrote:
> > I get the following error:
> >
> > slot 'name' in gnu.mapping.Symbol not accessible here
> >
> > What are the rules for accessing protected members in Kawa?
>
> The rules are supposed to be the same as in Java
> (because those rules are enforced by the JVM).
> However, those rules are actually a bit complicated:
> https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html#jls-6.6.2
>
> In this case there is a bug in Kawa. It treats
>    (set! name ...)
> as equivalent to:
>    (set! (this):name ...)
> which is fine.
>
> However, there seems to a problem where Kawa thinks (this) has the type
> Symbol (because that is
> where name is defined) rather than class S.  And this violates the rule:
>
>      If the access is by (i) a qualified name of the form
> ExpressionName.Id or TypeName.Id, or
>      (ii) a field access expression of the form Primary.Id, then access to
> the instance field Id
>      is permitted if and only if the qualifying type is S or a subclass of
> S.
>
>      The qualifying type is the type of the ExpressionName or Primary, or
> the type denoted by TypeName.
>
> A work-around is to use the field special procedure:
>
>     (set! (field (this) 'name) ...)


>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: A protected slot is not accessible in a subclass
  2022-03-25  9:59   ` Panicz Maciej Godek
@ 2022-03-25 18:46     ` Per Bothner
  2022-03-25 20:53       ` Panicz Maciej Godek
  0 siblings, 1 reply; 5+ messages in thread
From: Per Bothner @ 2022-03-25 18:46 UTC (permalink / raw)
  To: Panicz Maciej Godek; +Cc: kawa



On 3/25/22 02:59, Panicz Maciej Godek wrote:
> I tried this:
> (define-simple-class S (gnu.mapping.Symbol)
>     ((*init*)
>      (invoke-special gnu.mapping.Symbol (this) '*init* (("foo":toString):intern) gnu.mapping.Namespace:EmptyNamespace)
>   (set! (field (this) 'name) (("bar":toString):intern))))
> but it gives a similar error:
> java.lang.RuntimeException: no such field name in S
>          at gnu.kawa.reflect.SlotSet.apply(SlotSet.java:115)                        at gnu.kawa.reflect.SlotSet.apply3(SlotSet.java:120)
>          at S.<init>(tty:14)

This fails because the (set! (field ...) ...) isn't inlined, so we end up using
run-time reflection.  This is a missing Kawa optimization.

Using set-field! instead does work:

  (set-field! (this) 'name (("bar":toString):intern))

Note in this case you need to add:

(import (kawa reflect)) ; for set-field!

I checked in a fix so either of these now work:

  (set! (this):name (("bar":toString):intern))
  (set! name (("bar":toString):intern))

-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: A protected slot is not accessible in a subclass
  2022-03-25 18:46     ` Per Bothner
@ 2022-03-25 20:53       ` Panicz Maciej Godek
  0 siblings, 0 replies; 5+ messages in thread
From: Panicz Maciej Godek @ 2022-03-25 20:53 UTC (permalink / raw)
  To: Per Bothner; +Cc: kawa

pt., 25 mar 2022 o 19:46 Per Bothner <per@bothner.com> napisał(a):

>
>
> On 3/25/22 02:59, Panicz Maciej Godek wrote:
> > I tried this:
> > (define-simple-class S (gnu.mapping.Symbol)
> >     ((*init*)
> >      (invoke-special gnu.mapping.Symbol (this) '*init*
> (("foo":toString):intern) gnu.mapping.Namespace:EmptyNamespace)
> >   (set! (field (this) 'name) (("bar":toString):intern))))
> > but it gives a similar error:
> > java.lang.RuntimeException: no such field name in S
> >          at gnu.kawa.reflect.SlotSet.apply(SlotSet.java:115)
>             at gnu.kawa.reflect.SlotSet.apply3(SlotSet.java:120)
> >          at S.<init>(tty:14)
>
> This fails because the (set! (field ...) ...) isn't inlined, so we end up
> using
> run-time reflection.  This is a missing Kawa optimization.
>
> Using set-field! instead does work:
>
>   (set-field! (this) 'name (("bar":toString):intern))
>
> Note in this case you need to add:
>
> (import (kawa reflect)) ; for set-field!
>

Yes, that one works! Thanks!


>
> I checked in a fix so either of these now work:
>
>   (set! (this):name (("bar":toString):intern))
>   (set! name (("bar":toString):intern))
>
>
This one works now too, thanks a lot!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-03-25 20:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-24 22:14 A protected slot is not accessible in a subclass Panicz Maciej Godek
2022-03-25  4:56 ` Per Bothner
2022-03-25  9:59   ` Panicz Maciej Godek
2022-03-25 18:46     ` Per Bothner
2022-03-25 20:53       ` Panicz Maciej Godek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).