Skip to content

Conversation

@tobiasmelcher
Copy link
Contributor

which takes care of creation and disposal of the GC object

Fixes: #955

@tobiasmelcher tobiasmelcher marked this pull request as draft February 26, 2024 07:47
@BeckerWdf BeckerWdf requested a review from HannesWell February 26, 2024 08:03
@tobiasmelcher
Copy link
Contributor Author

@HannesWell : could you please take a look at this change? I had to introduce a new Runnable interface which passes the GC as argument. Is there already an existing interface I could re-use? Do we have to introduce the static method GC#drawOn in all platform specific GC implementations?

@github-actions
Copy link
Contributor

github-actions bot commented Feb 26, 2024

Test Results

  176 files  ±0    176 suites  ±0   28m 53s ⏱️ + 1m 24s
4 693 tests ±0  4 671 ✅ ±0  22 💤 ±0  0 ❌ ±0 
  489 runs  ±0    483 ✅ ±0   6 💤 ±0  0 ❌ ±0 

Results for commit 4ab1b13. ± Comparison against base commit 9c79cf6.

♻️ This comment has been updated with latest results.

@HannesWell
Copy link
Member

Hello @tobiasmelcher, thank you for this PR. Sorry for my late reply but I just started my almost three week of vacation when you created this PR and when I came back a full TODO list was ready to be worked off.

Do we have to introduce the static method GC#drawOn in all platform specific GC implementations?

Yes we do, but you can just import/open the fragments for the other platforms and they will compile in your workspace. For you it is probably sufficient to only have one arch per OS since they only differ in the embedded native binaries, but the code is the same for all archs on one OS.

I had to introduce a new Runnable interface which passes the GC as argument. Is there already an existing interface I could re-use?

Yes there is already SWTRunnable. But as I have just written in #955 (comment), I think this is not the optimal solution, which I only noticed when reviewing your changes to apply the new method (applying it immediately can therefore be very helpful 👍🏽 )

Copy link
Member

@HannesWell HannesWell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. In general it looks good, but I added some remarks below.

But the most important missing part is the implementation for the linux- and mac-platform as written in my previous comment.

@tobiasmelcher
Copy link
Contributor Author

Thanks a lot @HannesWell for all the feedback. Could you please take a look and review my latest change eb1dc57 ? As already mentioned, I had some troubles with the formatting; I executed then the format action only on the changed coding parts. Is that ok?

Copy link
Member

@HannesWell HannesWell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As already mentioned, I had some troubles with the formatting; I executed then the format action only on the changed coding parts. Is that ok?

Yes, that's IMO the perfect decision.

Could you please also update the doc of the GC class to mention the new GC.create() factory (for all platforms).

With that I think this change is complete (I'll mark it as ready for review), but I would like give others some time to review.

* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
*/
public final class GC extends Resource {
public sealed class GC extends Resource {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to also update the doc of this class (where it mentions GC.dispose()) and recommend to use the new factory, ideally with the little example also used in the Image class.
And I think Windows95 and Windows98 are not the most relevant target OS anymore. Maybe that just should be generalized.

Comment on lines +167 to +281
* try (var gc = GC.create(i)) {
* gc.drawRectangle(0, 0, 50, 50));
* }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please apply this doc update also to the Image implementations for Linux and Mac.

@HannesWell HannesWell marked this pull request as ready for review March 29, 2024 13:38
}
}

public static GC.Closeable create(Drawable drawable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new method needs some java-doc on all platforms. The existing single argument constructor of GC is probably a good source of inspiration.
The existing GC constructor could also be adjusted to recommend this method for safety.

What's important is to add the tag @since 3.126 in the Java-doc in order to make the API-tools aware this method was just added.

Copy link
Contributor Author

@tobiasmelcher tobiasmelcher Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint. I hope it is fine with 6443eca

public final class GC extends Resource {
public sealed class GC extends Resource {

public static final class Closeable extends GC implements AutoCloseable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new class needs some java-doc on all platforms. I think a short text that mentions that this is an auto-closable extension of the GC class is sufficient. The create method could be linked with @see.
What's important is to add the tag @since 3.126 in the Java-doc in order to make the API-tools aware this method was just added.

Copy link
Member

@HannesWell HannesWell Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just stumbled upon this again and thought if it would be better to name this
AutoDisposable? Since it's an extension of AutoClosable that just calls dispose().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-pick this does not extends AutoClosable (what is an interface) but implements it.

It also feels like AutoDisposable actually should be an interface so others can implement it as well.

And then it probabbly should become AutoDisposableGC here...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed to AutoDisposableGC with [326fd48]

@tobiasmelcher tobiasmelcher force-pushed the GC.drawOn branch 2 times, most recently from eb1dc57 to 6443eca Compare April 2, 2024 07:01
@fedejeanne fedejeanne changed the title introduce static helper method GC#drawOn introduce static helper method GC#create Jan 22, 2025
@tobiasmelcher
Copy link
Contributor Author

Can anyone give guidance how to solve the build error:

Error:  Failed to execute goal org.eclipse.tycho:tycho-apitools-plugin:5.0.2:verify (verify) on project org.eclipse.swt.gtk.linux.ppc64le: There are API errors:
Error:  The field org.eclipse.swt.graphics.GC.handle has been added to a class
Error:  META-INF/MANIFEST.MF:1 The major version should be incremented in version 3.133.0, since API breakage occurred since version 3.132.0

I assume that changing the final class GC to a sealed class introduces the API breakage? So, the current idea and approach will not work?

@merks
Copy link
Contributor

merks commented Feb 9, 2026

The message says The field org.eclipse.swt.graphics.GC.handle has been added to a class which seems bogus, so I kind of doubt the issue is related to sealed. For starters, I would rebase this on master before anything else.

@merks
Copy link
Contributor

merks commented Feb 9, 2026

These are pretty clear and indicate you have the wrong values, so change that as well.

image

@tobiasmelcher tobiasmelcher force-pushed the GC.drawOn branch 2 times, most recently from 782fcbc to ed45377 Compare February 9, 2026 09:14
@tobiasmelcher
Copy link
Contributor Author

Should I really change to 4.0.0 ? I haven't changed GC.handle. The message irritates me.

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.eclipse.tycho:tycho-apitools-plugin:5.0.2:verify (verify) on project org.eclipse.swt.cocoa.macosx.aarch64: There are API errors:
The field org.eclipse.swt.graphics.GC.handle has been added to a class
META-INF/MANIFEST.MF:1 The major version should be incremented in version 3.133.0, since API breakage occurred since version 3.132.0
org.eclipse.swt.graphics.GC.handle declared as non-API type org.eclipse.swt.internal.cocoa.NSGraphicsContext
`´`

@HeikoKlare
Copy link
Contributor

Should I really change to 4.0.0 ? I haven't changed GC.handle. The message irritates me.

There is definitely no need for bumping to 4.0.0 here. I also don't think it related to making the class sealed, as we did something similar with the Point/Rectangle classes several releases ago and did not have this kind of error: #1711
It might be fine to just add an API filter to silence the error.

One thing I would like mention is that making a final class sealed will probably somehow break compatibility with consumers using Kotlin (see #1711 (comment)). In my opinion, that's a flaw of the Kotlin language, still we might see issues being raised for SWT from Kotlin developers using SWT and instantiating a GC.

@merks
Copy link
Contributor

merks commented Feb 9, 2026

It looks more like an API tools bug. I can't reproduce the error in my IDE so I'm not sure what we need to add to suppress the message. I can reproduce an error in the IDE only if I add a new public field:

image

@laeubi @HannesWell

Any ideas?

@merks
Copy link
Contributor

merks commented Feb 9, 2026

It might/probably would work to add a filter like this:

image

Like this:

    <resource path="Eclipse SWT/cocoa/org/eclipse/swt/graphics/GC.java" type="org.eclipse.swt.graphics.GC">
        <filter comment="Bogus problem" id="336658481">
            <message_arguments>
                <message_argument value="org.eclipse.swt.graphics.GC"/>
                <message_argument value="handle"/>
            </message_arguments>
        </filter>
    </resource>
```

@tobiasmelcher
Copy link
Contributor Author

Could we let the GC class implement AutoCloseable? So, there would be no need to change from final to sealed? Do you see any issues by doing it this way?

@merks
Copy link
Contributor

merks commented Feb 9, 2026

Could we let the GC class implement AutoCloseable?

I wondered that as well. That seems simpler to me...

@HeikoKlare
Copy link
Contributor

Could we let the GC class implement AutoCloseable? So, there would be no need to change from final to sealed? Do you see any issues by doing it this way?

I would expect a bunch of resource leak warnings because of all existing creators of GC instances not closing them.

I am not sure if that was discussed already, but an alternative to an AutoClosable might be a kind of execute method that just takes a Drawable and a lambda that is executed on a temporary GC for that drawable. Then you could wrap everything inside that method instead of adapting the type hierarchy.
The AutoClosable solution is in my opinion definitely more elegant, so just consider it an alternative in exploring the design space which might avoid some of the drawbacks with the AutoClosable solution.

@tobiasmelcher
Copy link
Contributor Author

I am not sure if that was discussed already, but an alternative to an AutoClosable might be a kind of execute method that >>just takes a Drawable and a lambda that is executed on a temporary GC for that drawable. Then you could wrap >>everything inside that method instead of adapting the type hierarchy.

The very first proposal was implemented in this way.
https://github.com/eclipse-platform/eclipse.platform.swt/compare/d4fb76fdc63bf1d38b2b834dad679f7dc6f04e29..37519e2ade4acb878e50c8a78a9931524707854f

Heiko, I get from your comment that implementing AutoClosable in GC would lead to a big list of "resource leak warnings" which is not what we want?

@HeikoKlare
Copy link
Contributor

The very first proposal was implemented in this way. https://github.com/eclipse-platform/eclipse.platform.swt/compare/d4fb76fdc63bf1d38b2b834dad679f7dc6f04e29..37519e2ade4acb878e50c8a78a9931524707854f

I see. Then I don't want to open up that discussion again.

Heiko, I get from your comment that implementing AutoClosable in GC would lead to a big list of "resource leak warnings" which is not what we want?

Yes, every call to new GC(...) will give you this:
image

In the Eclipse platform repos, this would already lead to around 130 of such warnings.

@tobiasmelcher tobiasmelcher force-pushed the GC.drawOn branch 2 times, most recently from 66d72ba to bc0ad9a Compare February 10, 2026 08:51
which takes care of creation and disposal of the GC object

Fixes: eclipse-platform#955
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement java.lang.AutoCloseable in class org.eclipse.swt.graphics.GC

6 participants