Skip to content

Commit e8e3ade

Browse files
ragudikotimtebeekgithub-actions[bot]
authored
Delete deprecated finalize (#506)
* Add DeleteDeprecatedFinalize recipe for removing Empty finalize() Methods in java.desktop Module * remove dummy foo method * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update DeleteDeprecatedFinalizeTest.java * Update DeleteDeprecatedFinalizeTest.java --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <[email protected]>
1 parent 6e212fd commit e8e3ade

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/main/resources/META-INF/rewrite/java-version-21.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ recipeList:
3434
- org.openrewrite.staticanalysis.ReplaceDeprecatedRuntimeExecMethods
3535
- org.openrewrite.github.SetupJavaUpgradeJavaVersion
3636
- org.openrewrite.java.migrate.UpgradePluginsForJava21
37+
- org.openrewrite.java.migrate.DeleteDeprecatedFinalize
3738

3839
---
3940
type: specs.openrewrite.org/v1beta/recipe
@@ -110,3 +111,17 @@ recipeList:
110111
- org.openrewrite.java.ChangeMethodName:
111112
methodPattern: javax.security.auth.Subject callAs()
112113
newMethodName: doAs
114+
---
115+
type: specs.openrewrite.org/v1beta/recipe
116+
name: org.openrewrite.java.migrate.DeleteDeprecatedFinalize
117+
displayName: Avoid using the deprecated empty `finalize()` method in `java.desktop`
118+
description: The java.desktop module had a few implementations of finalize() that did nothing and have been removed. This recipe will remove these methods.
119+
tags:
120+
- java21
121+
recipeList:
122+
- org.openrewrite.java.migrate.RemoveMethodInvocation:
123+
methodPattern: java.awt.color.ICC_Profile finalize()
124+
- org.openrewrite.java.migrate.RemoveMethodInvocation:
125+
methodPattern: java.awt.image.ColorModel finalize()
126+
- org.openrewrite.java.migrate.RemoveMethodInvocation:
127+
methodPattern: java.awt.image.IndexColorModel finalize()
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.java.Assertions.java;
24+
25+
public class DeleteDeprecatedFinalizeTest implements RewriteTest {
26+
27+
@Override
28+
public void defaults(RecipeSpec spec) {
29+
spec.recipeFromResource("/META-INF/rewrite/java-version-21.yml",
30+
"org.openrewrite.java.migrate.DeleteDeprecatedFinalize");
31+
}
32+
33+
@DocumentExample
34+
@Test
35+
void deleteDeprecatedFinalize() {
36+
rewriteRun(
37+
//language=java
38+
java(
39+
"""
40+
package java.awt.color;
41+
42+
import java.awt.color.ICC_Profile;
43+
import java.awt.image.ColorModel;
44+
import java.awt.image.IndexColorModel;
45+
46+
public class Test {
47+
public static void main(String[] args) {
48+
byte ff = (byte) 0xff;
49+
byte[] r = { ff, 0, 0, ff, 0 };
50+
byte[] g = { 0, ff, 0, ff, 0 };
51+
byte[] b = { 0, 0, ff, ff, 0 };
52+
53+
ICC_Profile profile = ICC_Profile.getInstance(ICC_Profile.CLASS_COLORSPACECONVERSION);
54+
// flag
55+
profile.finalize();
56+
57+
ColorModel cm = new IndexColorModel(3, 5, r, g, b);
58+
59+
// flag
60+
cm.finalize();
61+
62+
IndexColorModel icm = new IndexColorModel(3, 5, r, g, b);
63+
// flag
64+
icm.finalize();
65+
66+
}
67+
}
68+
""",
69+
"""
70+
package java.awt.color;
71+
72+
import java.awt.color.ICC_Profile;
73+
import java.awt.image.ColorModel;
74+
import java.awt.image.IndexColorModel;
75+
76+
public class Test {
77+
public static void main(String[] args) {
78+
byte ff = (byte) 0xff;
79+
byte[] r = { ff, 0, 0, ff, 0 };
80+
byte[] g = { 0, ff, 0, ff, 0 };
81+
byte[] b = { 0, 0, ff, ff, 0 };
82+
83+
ICC_Profile profile = ICC_Profile.getInstance(ICC_Profile.CLASS_COLORSPACECONVERSION);
84+
85+
ColorModel cm = new IndexColorModel(3, 5, r, g, b);
86+
87+
IndexColorModel icm = new IndexColorModel(3, 5, r, g, b);
88+
89+
}
90+
}
91+
"""));
92+
}
93+
}

0 commit comments

Comments
 (0)