Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

Stinginess with privileges is kindness in disguise. -- Guide to VAX/VMS Security, Sep. 1984


devel / comp.lang.java.programmer / Re: Java 21 and [this-escape] warnings?

SubjectAuthor
* Java 21 and [this-escape] warnings?Knute Johnson
+* Java 21 and [this-escape] warnings?Daniele Futtorovic
|`* Java 21 and [this-escape] warnings?Knute Johnson
| `- Java 21 and [this-escape] warnings?Daniele Futtorovic
+* Java 21 and [this-escape] warnings?Eric Sosman
|`- Java 21 and [this-escape] warnings?Knute Johnson
`- Java 21 and [this-escape] warnings?Arne_Vajhøj

1
Java 21 and [this-escape] warnings?

<u60jra$2275l$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=251&group=comp.lang.java.programmer#251

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: knute2023@585ranch.com (Knute Johnson)
Newsgroups: comp.lang.java.programmer
Subject: Java 21 and [this-escape] warnings?
Date: Fri, 9 Jun 2023 20:32:25 -0500
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <u60jra$2275l$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 10 Jun 2023 01:32:26 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e0b6284631dce2eaa66de7affc6a43ba";
logging-data="2170037"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19+He7hZiQbi66TOzA9j4yV"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:8kNh+eLY1JWu6rWJd1in6dtH28A=
Content-Language: en-US
 by: Knute Johnson - Sat, 10 Jun 2023 01:32 UTC

I've been playing with Java21ea getting ready for its release this fall
and have found some warnings that I don't really know how to deal with.
I understand why you don't want 'this' escaping the constructor but I
don't understand how Java21 think that a lot of the methods we have
always used are going to cause that to happen. Please see the code
below and the script to compile it. I've commented out several lines
but you can un-comment them and see the warnings from the compiler. The
first one that I really don't understand is
Frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE). I would appreciate
any comments or maybe I just don't know what I'm doing and there is a
better way. If so please feel free to critique. It compiles and will
run but not work correctly with the lines commented out. Put them back
in and compile them to see the warnings.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class test extends JFrame {
private static final long serialVersionUID = 1L;

public test() {
super("test");
//setLayout(new GridBagLayout()); // [this-escape]
//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //
[this-escape]

GridBagConstraints c = new GridBagConstraints();

JLabel l = new JLabel("this is a JLabel");
l.setBorder(new EmptyBorder(20,20,20,20)); // [this-escape]
sometimes
//add(l,c); // [this-escape]
//add(l); // [this-escape]
}

public static void main(String[] args) {
EventQueue.invokeLater(() -> {
test t = new test();
t.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
t.dispose();
}
});
t.pack();
t.setVisible(true);
});
}
}

A script to compile the program above:

#!/bin/bash

if javac21 -Xlint:all -Xdiags:verbose -Xmaxerrs 10 -Xmaxwarns 10 test.java
then
jar cvfe test.jar test *.class
rm *.class
fi

javac21 is a link to the javac program from OpenJDK 21ea26. You can get
it here: https://jdk.java.net/21/

Thanks!

--

Knute Johnson

Re: Java 21 and [this-escape] warnings?

<u61nr4$29s54$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=252&group=comp.lang.java.programmer#252

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: da.futt.news@laposte-dot-net.invalid (Daniele Futtorovic)
Newsgroups: comp.lang.java.programmer
Subject: Re: Java 21 and [this-escape] warnings?
Date: Sat, 10 Jun 2023 13:46:44 +0200
Organization: A noiseless patient Spider
Lines: 85
Message-ID: <u61nr4$29s54$1@dont-email.me>
References: <u60jra$2275l$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 10 Jun 2023 11:46:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c2babe42fc9047713e25682e5351f87c";
logging-data="2420900"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX193yH+X0egh9bZIhc2IcaZa"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.2
Cancel-Lock: sha1:e2wRXSowLXVC+3TNQ0AEQdYXJ1s=
Content-Language: en-US
In-Reply-To: <u60jra$2275l$1@dont-email.me>
 by: Daniele Futtorovic - Sat, 10 Jun 2023 11:46 UTC

On 10/06/2023 03:32, Knute Johnson wrote:
> I've been playing with Java21ea getting ready for its release this fall
> and have found some warnings that I don't really know how to deal with.
> I understand why you don't want 'this' escaping the constructor but I
> don't understand how Java21 think that a lot of the methods we have
> always used are going to cause that to happen.  Please see the code
> below and the script to compile it.  I've commented out several lines
> but you can un-comment them and see the warnings from the compiler.  The
> first one that I really don't understand is
> Frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE).  I would appreciate
> any comments or maybe I just don't know what I'm doing and there is a
> better way.  If so please feel free to critique.  It compiles and will
> run but not work correctly with the lines commented out.  Put them back
> in and compile them to see the warnings.
>
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import javax.swing.border.*;
>
> public class test extends JFrame {
>     private static final long serialVersionUID = 1L;
>
>     public test() {
>         super("test");
>         //setLayout(new GridBagLayout());  // [this-escape]
>         //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  //
> [this-escape]
>
>         GridBagConstraints c = new GridBagConstraints();
>
>         JLabel l = new JLabel("this is a JLabel");
>         l.setBorder(new EmptyBorder(20,20,20,20));  // [this-escape]
> sometimes
>         //add(l,c);  // [this-escape]
>         //add(l);  // [this-escape]
>     }
>
>     public static void main(String[] args) {
>         EventQueue.invokeLater(() -> {
>             test t = new test();
>             t.addWindowListener(new WindowAdapter() {
>                 public void windowClosing(WindowEvent we) {
>                     t.dispose();
>                 }
>             });
>             t.pack();
>             t.setVisible(true);
>         });
>     }
> }

Hi Knute,

Still Swing-ing? :-)

These errors/warning make perfect sense to me (bar one): you're
manipulating the object tree before the constructor's end.

I reckon the reason #setDefaultCloseOperation gets flagged is that it
potentially fires a PropertyChangeEvent, the source of which is your
Frame. It wouldn't fire if there are no listeners attached, but the
compiler probably can't (or can't be arsed to) infer that.

The one I don't get (and haven't been able to reproduce) is the
#setBorder call.

Is [this-escape] a warning/error that's been specifically added in
JDK21? I reckon the problem itself isn't new or changed, and that it's
always been there.

As such, you would have two choices, as before: ignore the warning or
change your habits.

For illustration, with Swing I'd made it a habit to construct my UI in
#addNotify, for self-constructing components. That wouldn't be
applicable to the frame, which you could for instance construct
externally (I mean in a factory) with a self-constructing content pane.

Regards,

--
DF.

Re: Java 21 and [this-escape] warnings?

<65a6bfdc-3cfa-424f-a042-e712ee50e781n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=253&group=comp.lang.java.programmer#253

  copy link   Newsgroups: comp.lang.java.programmer
X-Received: by 2002:a37:884:0:b0:758:95df:7459 with SMTP id 126-20020a370884000000b0075895df7459mr406677qki.13.1686405528340;
Sat, 10 Jun 2023 06:58:48 -0700 (PDT)
X-Received: by 2002:a25:4084:0:b0:ba8:97f8:620f with SMTP id
n126-20020a254084000000b00ba897f8620fmr2235175yba.8.1686405528000; Sat, 10
Jun 2023 06:58:48 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.java.programmer
Date: Sat, 10 Jun 2023 06:58:47 -0700 (PDT)
In-Reply-To: <u60jra$2275l$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:197:180:3d70:9414:cf84:203b:d0d4;
posting-account=lT6HywoAAABy78apffHGZc7J4VlbclxH
NNTP-Posting-Host: 2601:197:180:3d70:9414:cf84:203b:d0d4
References: <u60jra$2275l$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <65a6bfdc-3cfa-424f-a042-e712ee50e781n@googlegroups.com>
Subject: Re: Java 21 and [this-escape] warnings?
From: donalhambra@gmail.com (Eric Sosman)
Injection-Date: Sat, 10 Jun 2023 13:58:48 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2432
 by: Eric Sosman - Sat, 10 Jun 2023 13:58 UTC

On Friday, June 9, 2023 at 9:32:41 PM UTC-4, Knute Johnson wrote:
> [...] I've commented out several lines
> but you can un-comment them and see the warnings from the compiler. The
> first one that I really don't understand is
> Frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE). [...]

I've not tried Java21 and am just guessing wildly, so take
my observations with a grain -- nay, a tonne -- of salt:

> public class test extends JFrame {

Have you tried making the class final? Alternatively, ...

> public test() {
> super("test");
> //setLayout(new GridBagLayout()); // [this-escape]
> //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //
> [this-escape]

.... have you tried super.setXXX instead of bare setXXX?

> GridBagConstraints c = new GridBagConstraints();
>
> JLabel l = new JLabel("this is a JLabel");
> l.setBorder(new EmptyBorder(20,20,20,20)); // [this-escape]
> sometimes

I don't understand (or even guess at) this one, not at all.

> //add(l,c); // [this-escape]
> //add(l); // [this-escape]

.... but again: try using super.add (or a final class).

--
esosman@comcast-dot-net.invalid
Look on my code, ye Hackers, and guffaw!

Re: Java 21 and [this-escape] warnings?

<u62f3u$2ck76$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=254&group=comp.lang.java.programmer#254

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: knute2023@585ranch.com (Knute Johnson)
Newsgroups: comp.lang.java.programmer
Subject: Re: Java 21 and [this-escape] warnings?
Date: Sat, 10 Jun 2023 13:23:58 -0500
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <u62f3u$2ck76$1@dont-email.me>
References: <u60jra$2275l$1@dont-email.me> <u61nr4$29s54$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 10 Jun 2023 18:23:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e0b6284631dce2eaa66de7affc6a43ba";
logging-data="2511078"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+yUzRBRRDPz4n+A46+0II2"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:IXPoGBZ3ugWu7FQvzDyJfje5cHc=
Content-Language: en-US
In-Reply-To: <u61nr4$29s54$1@dont-email.me>
 by: Knute Johnson - Sat, 10 Jun 2023 18:23 UTC

On 6/10/23 06:46, Daniele Futtorovic wrote:

> Hi Knute,
>
> Still Swing-ing? :-)
>

Nice to hear from you Daniele. Yes, they keep paying me and I keep
writing Swing code. Works well and it is quick to write.

>
> The one I don't get (and haven't been able to reproduce) is the
> #setBorder call.

I saw it once and couldn't make the combination right to get it to show
up again.

> Is [this-escape] a warning/error that's been specifically added in
> JDK21? I reckon the problem itself isn't new or changed, and that it's
> always been there.

It's a feature of the Java 21 compiler. Things keep getting
better/tighter all the time.

> As such, you would have two choices, as before: ignore the warning or
> change your habits.

See Eric Sosman's post.

> For illustration, with Swing I'd made it a habit to construct my UI in
> #addNotify, for self-constructing components. That wouldn't be
> applicable to the frame, which you could for instance construct
> externally (I mean in a factory) with a self-constructing content pane.

I'll have to try that. It is just so different from the way I've been
writing Swing code for the last 20 years:-).

Thanks for the post.

--

Knute Johnson

Re: Java 21 and [this-escape] warnings?

<u62f9g$2ck76$2@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=255&group=comp.lang.java.programmer#255

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: knute2023@585ranch.com (Knute Johnson)
Newsgroups: comp.lang.java.programmer
Subject: Re: Java 21 and [this-escape] warnings?
Date: Sat, 10 Jun 2023 13:26:56 -0500
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <u62f9g$2ck76$2@dont-email.me>
References: <u60jra$2275l$1@dont-email.me>
<65a6bfdc-3cfa-424f-a042-e712ee50e781n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 10 Jun 2023 18:26:57 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e0b6284631dce2eaa66de7affc6a43ba";
logging-data="2511078"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/X1Ab35+KFV+G5ys+RY1Uw"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:3jMR8NIy9TIKMCGwHxY92hfZE84=
Content-Language: en-US
In-Reply-To: <65a6bfdc-3cfa-424f-a042-e712ee50e781n@googlegroups.com>
 by: Knute Johnson - Sat, 10 Jun 2023 18:26 UTC

On 6/10/23 08:58, Eric Sosman wrote:
> On Friday, June 9, 2023 at 9:32:41 PM UTC-4, Knute Johnson wrote:
>> [...] I've commented out several lines
>> but you can un-comment them and see the warnings from the compiler. The
>> first one that I really don't understand is
>> Frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE). [...]
>
> I've not tried Java21 and am just guessing wildly, so take
> my observations with a grain -- nay, a tonne -- of salt:
>
>> public class test extends JFrame {
>
> Have you tried making the class final? Alternatively, ...

At your suggestion I did and all the errors go away. You get the prize
Eric!

>> public test() {
>> super("test");
>> //setLayout(new GridBagLayout()); // [this-escape]
>> //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //
>> [this-escape]
>
> ... have you tried super.setXXX instead of bare setXXX?

super doesn't seem to help.

>> l.setBorder(new EmptyBorder(20,20,20,20)); // [this-escape]
>> sometimes
>
> I don't understand (or even guess at) this one, not at all.

I think that one was me somewhere mixing things up.

final is the answer!

Thanks!

--

Knute Johnson

Re: Java 21 and [this-escape] warnings?

<u62v4a$2ek0u$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=259&group=comp.lang.java.programmer#259

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: arne@vajhoej.dk (Arne Vajhøj)
Newsgroups: comp.lang.java.programmer
Subject: Re: Java 21 and [this-escape] warnings?
Date: Sat, 10 Jun 2023 18:57:14 -0400
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <u62v4a$2ek0u$1@dont-email.me>
References: <u60jra$2275l$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 10 Jun 2023 22:57:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ebf8e1725a8267cd212038fbdad1e6ce";
logging-data="2576414"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Ewb9JW2jUdSmnDmbxWSnUcn9VyKfHhYY="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.2
Cancel-Lock: sha1:OeTfce9Dn0UTsqwoQe9tOvcN68A=
Content-Language: en-US
In-Reply-To: <u60jra$2275l$1@dont-email.me>
 by: Arne Vajhøj - Sat, 10 Jun 2023 22:57 UTC

On 6/9/2023 9:32 PM, Knute Johnson wrote:
> I've been playing with Java21ea getting ready for its release this fall
> and have found some warnings that I don't really know how to deal with.
> I understand why you don't want 'this' escaping the constructor but I
> don't understand how Java21 think that a lot of the methods we have
> always used are going to cause that to happen.

I think the following example illustrates the problem
they are trying to solve.

public class ThisLeak {
public static class A {
protected boolean a;
public A() {
a = true;
}
public void m() {
System.out.printf("a=%b\n", a);
}
}
public static class B extends A {
protected boolean b1;
protected boolean b2;
public B() {
super();
b1 = true;
m();
b2 = true;
}
}
public static class C extends B {
private boolean c;
public C() {
c = true;
}
public void m() {
System.out.printf("a=%b b1=%b b2=%b c=%b\n", a, b1, b2, c);
}
}
public static void main(String[] args) {
new C();
}
}

And it also explains why final solve the problem.

Arne

Re: Java 21 and [this-escape] warnings?

<u64fku$2msjl$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=261&group=comp.lang.java.programmer#261

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: da.futt.news@laposte-dot-net.invalid (Daniele Futtorovic)
Newsgroups: comp.lang.java.programmer
Subject: Re: Java 21 and [this-escape] warnings?
Date: Sun, 11 Jun 2023 14:45:18 +0200
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <u64fku$2msjl$1@dont-email.me>
References: <u60jra$2275l$1@dont-email.me> <u61nr4$29s54$1@dont-email.me>
<u62f3u$2ck76$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 11 Jun 2023 12:45:18 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="fec0c774fefa734f9fd9705e5e525c7f";
logging-data="2847349"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+06KDG/hANfzs+zDdS5+Jg"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.11.2
Cancel-Lock: sha1:vNjBUK85OhDqCq8Ek2jLdy09y7s=
In-Reply-To: <u62f3u$2ck76$1@dont-email.me>
Content-Language: en-US
 by: Daniele Futtorovic - Sun, 11 Jun 2023 12:45 UTC

On 10/06/2023 20:23, Knute Johnson wrote:
> On 6/10/23 06:46, Daniele Futtorovic wrote:
>> As such, you would have two choices, as before: ignore the warning or
>> change your habits.
>
> See Eric Sosman's post.

A swing and a miss! :-D

Thanks to Eric and Arne for clarifying the behaviour.

--
DF.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor