-
Notifications
You must be signed in to change notification settings - Fork 176
8386972: [lworld] Finalizer should not be registered for value objects #2589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fparain
wants to merge
5
commits into
openjdk:lworld
Choose a base branch
from
fparain:finalizer_registration
base: lworld
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+151
−4
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bcbb589
Prevent registration of finalizers for value classes
fparain 1182320
Fix test to run with product builds
fparain e07bf86
Full fix for testing with product builds
fparain d8af541
Merge remote-tracking branch 'upstream/lworld' into finalizer_registr…
fparain 01d26c9
Re-enable tests
fparain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
test/hotspot/jtreg/runtime/valhalla/inlinetypes/TestFinalizableValues.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| /* | ||
| * @test TestFinalizableValues | ||
| * @library /test/lib | ||
| * @enablePreview | ||
| * @run main TestFinalizableValues | ||
| */ | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import jdk.test.lib.process.OutputAnalyzer; | ||
| import jdk.test.lib.process.ProcessTools; | ||
|
|
||
| public class TestFinalizableValues { | ||
|
|
||
|
|
||
| public static class TestHelper { | ||
| static boolean finalizer1WasCalled = false; | ||
| static boolean finalizer2WasCalled = false; | ||
|
|
||
| static value class MyVal { | ||
| static volatile boolean finalizerWasCalled = false; | ||
| int i = 0; | ||
|
|
||
| @SuppressWarnings("deprecation") | ||
| protected void finalize() { | ||
| finalizerWasCalled = true; | ||
| } | ||
| } | ||
|
|
||
| static abstract value class MyAbstractVal { | ||
| int i = 0; | ||
|
|
||
| @SuppressWarnings("deprecation") | ||
| protected void finalize() { | ||
| finalizer1WasCalled = true; | ||
| } | ||
| } | ||
|
|
||
| static value class MyVal2 extends MyAbstractVal {} | ||
|
|
||
| static class MyId extends MyAbstractVal {} | ||
|
|
||
| static class MyId2 extends MyAbstractVal { | ||
| int i = 0; | ||
|
|
||
| @SuppressWarnings("deprecation") | ||
| protected void finalize() { | ||
| finalizer2WasCalled = true; | ||
| } | ||
| } | ||
|
|
||
| static void create(Class<?> c) { | ||
| try { | ||
| c.newInstance(); | ||
| } catch(InstantiationException | IllegalAccessException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) throws ClassNotFoundException { | ||
| Class<?> c = Class.forName(args[0]); | ||
| boolean expectFinalizer1 = Boolean.valueOf(args[1]); | ||
| boolean expectFinalizer2 = Boolean.valueOf(args[2]); | ||
|
|
||
| create(c); | ||
| for (int i = 0; i < 100; i++) { | ||
| System.gc(); | ||
| try { | ||
| Thread.sleep(10L); | ||
| } catch (InterruptedException ie) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| // if (finalizer1WasCalled) { | ||
| // break; | ||
| // } | ||
| } | ||
| if (finalizer1WasCalled != expectFinalizer1) { | ||
| throw new RuntimeException("Finalizer1 was " | ||
| + (finalizer1WasCalled ? "" : "not ") | ||
| + "executed"); | ||
| } | ||
| if (finalizer2WasCalled != expectFinalizer2) { | ||
| throw new RuntimeException("Finalizer2 was " | ||
| + (finalizer2WasCalled ? "" : "not ") | ||
| + "executed"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void test(String classname, boolean expectRegistration, String... args) throws IOException { | ||
| List<String> argsList = new ArrayList<>(); | ||
| Collections.addAll(argsList, "--enable-preview"); | ||
| Collections.addAll(argsList, "--add-exports", "java.base/jdk.internal.value=ALL-UNNAMED"); | ||
| Collections.addAll(argsList, "-Dtest.class.path=" + System.getProperty("test.class.path", ".")); | ||
| Collections.addAll(argsList, "-XX:+TraceFinalizerRegistration"); | ||
| Collections.addAll(argsList, "TestFinalizableValues$TestHelper"); | ||
| Collections.addAll(argsList, "TestFinalizableValues$TestHelper$" + classname); | ||
| Collections.addAll(argsList, args); | ||
| ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(argsList); | ||
| OutputAnalyzer out = new OutputAnalyzer(pb.start()); | ||
| if (expectRegistration) { | ||
| out.shouldContain("as finalizable"); | ||
| } else { | ||
| out.shouldNotContain("as finalizable"); | ||
| } | ||
| out.shouldHaveExitValue(0); | ||
| } | ||
| public static void main(String[] args) throws IOException { | ||
| test("MyVal", false, "false", "false"); | ||
| test("MyVal2", false, "false", "false"); | ||
| test("MyId", false, "false", "false"); | ||
| test("MyId2", true, "false", "true"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we call ClassFileParser's
is_identity_class()here instead of going through the method_holder?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fred pointed out my proposed change actually changes the meaning as my proposal checks the current class, not the class that defines the finalize method. This is different for a finalize method from an abstract value class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A finalizer in an abstract value class should probably still be registered for identity subclasses; as otherwise, changing an abstract identity class with a finalizer to an abstract value class with a finalizer would be a breaking change as long as finalizers continue to exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An abstract class with final fields may be good candidate to migrate to an abstract value class. If the abstract class relies on finalization then it is probably not a good candidate. If someone were to migrate without removing the dependency on finalization then it's not going to work as they might think as presumably the motivation for migrating the abstract class is to allow for subclasses to be value classes.
Right now, compiling an abstract value class with a finalize method emits a warning at compile-time to say that class should not have a finalize method as it will not be invoked (this is in addition to the removal warning that finalization is deprecated for removal). This will hopefully make developers aware that it doesn't work as they might think.