Skip to content

Commit 5037eac

Browse files
JohannisKtimtebeekclaude
authored
Use ZipException instead of the deprecated ZipError (#841)
* Added recipe, fixed preconditions * small polish * Condense tests to just what makes sense * Apply OpenRewrite recipe best practices Updated recipe examples with automatic ordering and formatting 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Tim te Beek <tim@moderne.io> Co-authored-by: Claude <noreply@anthropic.com>
1 parent d192070 commit 5037eac

File tree

3 files changed

+272
-37
lines changed

3 files changed

+272
-37
lines changed

src/main/resources/META-INF/rewrite/examples.yml

Lines changed: 132 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,37 @@ examples:
692692
language: java
693693
---
694694
type: specs.openrewrite.org/v1beta/example
695+
recipeName: org.openrewrite.java.migrate.MigrateZipErrorToZipException
696+
examples:
697+
- description: ''
698+
sources:
699+
- before: |
700+
import java.util.zip.ZipError;
701+
702+
class Test {
703+
void test() {
704+
try {
705+
// Some zip operation
706+
} catch (ZipError e) {
707+
System.out.println("Zip error occurred: " + e.getMessage());
708+
}
709+
}
710+
}
711+
after: |
712+
import java.util.zip.ZipException;
713+
714+
class Test {
715+
void test() {
716+
try {
717+
// Some zip operation
718+
} catch (ZipException e) {
719+
System.out.println("Zip error occurred: " + e.getMessage());
720+
}
721+
}
722+
}
723+
language: java
724+
---
725+
type: specs.openrewrite.org/v1beta/example
695726
recipeName: org.openrewrite.java.migrate.ReferenceCloneMethod
696727
examples:
697728
- description: ''
@@ -1025,31 +1056,34 @@ recipeName: org.openrewrite.java.migrate.UpgradePluginsForJava11
10251056
examples:
10261057
- description: ''
10271058
sources:
1059+
- before: project
1060+
language: mavenProject
10281061
- before: |
10291062
<project>
10301063
<groupId>com.mycompany.app</groupId>
10311064
<artifactId>my-app</artifactId>
10321065
<version>1</version>
1033-
<properties>
1034-
<wro4j.version>1.8.0</wro4j.version>
1035-
</properties>
10361066
<build>
10371067
<plugins>
10381068
<plugin>
1039-
<groupId>ro.isdc.wro4j</groupId>
1040-
<artifactId>wro4j-maven-plugin</artifactId>
1041-
<version>${wro4j.version}</version>
1069+
<groupId>org.codehaus.mojo</groupId>
1070+
<artifactId>jaxb2-maven-plugin</artifactId>
1071+
<version>2.3.1</version>
10421072
</plugin>
10431073
</plugins>
10441074
</build>
10451075
</project>
1046-
after: |
1076+
path: pom.xml
1077+
language: xml
1078+
- description: ''
1079+
sources:
1080+
- before: |
10471081
<project>
10481082
<groupId>com.mycompany.app</groupId>
10491083
<artifactId>my-app</artifactId>
10501084
<version>1</version>
10511085
<properties>
1052-
<wro4j.version>1.10.1</wro4j.version>
1086+
<wro4j.version>1.8.0</wro4j.version>
10531087
</properties>
10541088
<build>
10551089
<plugins>
@@ -1061,23 +1095,20 @@ examples:
10611095
</plugins>
10621096
</build>
10631097
</project>
1064-
path: pom.xml
1065-
language: xml
1066-
- description: ''
1067-
sources:
1068-
- before: project
1069-
language: mavenProject
1070-
- before: |
1098+
after: |
10711099
<project>
10721100
<groupId>com.mycompany.app</groupId>
10731101
<artifactId>my-app</artifactId>
10741102
<version>1</version>
1103+
<properties>
1104+
<wro4j.version>1.10.1</wro4j.version>
1105+
</properties>
10751106
<build>
10761107
<plugins>
10771108
<plugin>
1078-
<groupId>org.codehaus.mojo</groupId>
1079-
<artifactId>jaxb2-maven-plugin</artifactId>
1080-
<version>2.3.1</version>
1109+
<groupId>ro.isdc.wro4j</groupId>
1110+
<artifactId>wro4j-maven-plugin</artifactId>
1111+
<version>${wro4j.version}</version>
10811112
</plugin>
10821113
</plugins>
10831114
</build>
@@ -3330,25 +3361,6 @@ examples:
33303361
type: specs.openrewrite.org/v1beta/example
33313362
recipeName: org.openrewrite.java.migrate.guava.NoGuava
33323363
examples:
3333-
- description: ''
3334-
sources:
3335-
- before: |
3336-
import com.google.common.base.MoreObjects;
3337-
3338-
class A {
3339-
Object foo(Object obj) {
3340-
return MoreObjects.firstNonNull(obj, "default");
3341-
}
3342-
}
3343-
after: |
3344-
import java.util.Objects;
3345-
3346-
class A {
3347-
Object foo(Object obj) {
3348-
return Objects.requireNonNullElse(obj, "default");
3349-
}
3350-
}
3351-
language: java
33523364
- description: ''
33533365
sources:
33543366
- before: |
@@ -3395,6 +3407,25 @@ examples:
33953407
}
33963408
}
33973409
language: java
3410+
- description: ''
3411+
sources:
3412+
- before: |
3413+
import com.google.common.base.MoreObjects;
3414+
3415+
class A {
3416+
Object foo(Object obj) {
3417+
return MoreObjects.firstNonNull(obj, "default");
3418+
}
3419+
}
3420+
after: |
3421+
import java.util.Objects;
3422+
3423+
class A {
3424+
Object foo(Object obj) {
3425+
return Objects.requireNonNullElse(obj, "default");
3426+
}
3427+
}
3428+
language: java
33983429
---
33993430
type: specs.openrewrite.org/v1beta/example
34003431
recipeName: org.openrewrite.java.migrate.guava.NoGuavaAtomicsNewReference
@@ -6137,6 +6168,37 @@ examples:
61376168
language: java
61386169
---
61396170
type: specs.openrewrite.org/v1beta/example
6171+
recipeName: org.openrewrite.java.migrate.lang.ReplaceUnusedVariablesWithUnderscore
6172+
examples:
6173+
- description: ''
6174+
sources:
6175+
- before: |
6176+
import java.util.List;
6177+
6178+
class Test {
6179+
int countOrders(List<String> orders) {
6180+
int total = 0;
6181+
for (String order : orders) {
6182+
total++;
6183+
}
6184+
return total;
6185+
}
6186+
}
6187+
after: |
6188+
import java.util.List;
6189+
6190+
class Test {
6191+
int countOrders(List<String> orders) {
6192+
int total = 0;
6193+
for (String _ : orders) {
6194+
total++;
6195+
}
6196+
return total;
6197+
}
6198+
}
6199+
language: java
6200+
---
6201+
type: specs.openrewrite.org/v1beta/example
61406202
recipeName: org.openrewrite.java.migrate.lang.StringRulesRecipes
61416203
examples:
61426204
- description: ''
@@ -7384,6 +7446,39 @@ examples:
73847446
language: java
73857447
---
73867448
type: specs.openrewrite.org/v1beta/example
7449+
recipeName: org.openrewrite.java.migrate.util.MigrateInflaterDeflaterToClose
7450+
examples:
7451+
- description: ''
7452+
sources:
7453+
- before: |
7454+
import java.util.zip.Inflater;
7455+
7456+
class Test {
7457+
public void test(byte[] arg) {
7458+
Inflater inflater = new Inflater();
7459+
try {
7460+
inflater.inflate(arg);
7461+
} finally {
7462+
inflater.end();
7463+
}
7464+
}
7465+
}
7466+
after: |
7467+
import java.util.zip.Inflater;
7468+
7469+
class Test {
7470+
public void test(byte[] arg) {
7471+
Inflater inflater = new Inflater();
7472+
try {
7473+
inflater.inflate(arg);
7474+
} finally {
7475+
inflater.close();
7476+
}
7477+
}
7478+
}
7479+
language: java
7480+
---
7481+
type: specs.openrewrite.org/v1beta/example
73877482
recipeName: org.openrewrite.java.migrate.util.OptionalStreamRecipe
73887483
examples:
73897484
- description: ''

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ recipeList:
3535
- org.openrewrite.java.migrate.RemoveSecurityPolicy
3636
- org.openrewrite.java.migrate.RemoveSecurityManager
3737
- org.openrewrite.java.migrate.SystemGetSecurityManagerToNull
38+
- org.openrewrite.java.migrate.MigrateZipErrorToZipException
3839

3940
---
4041
type: specs.openrewrite.org/v1beta/recipe
@@ -134,3 +135,16 @@ recipeList:
134135
methodPattern: java.lang.System#getSecurityManager()
135136
replacement: "null"
136137
- org.openrewrite.staticanalysis.SimplifyConstantIfBranchExecution
138+
139+
---
140+
type: specs.openrewrite.org/v1beta/recipe
141+
name: org.openrewrite.java.migrate.MigrateZipErrorToZipException
142+
displayName: Use `ZipException` instead of `ZipError`
143+
description: Use `ZipException` instead of the deprecated `ZipError` in Java 9 or higher.
144+
preconditions:
145+
- org.openrewrite.java.search.HasJavaVersion:
146+
version: "[25,)"
147+
recipeList:
148+
- org.openrewrite.java.ChangeType:
149+
oldFullyQualifiedTypeName: java.util.zip.ZipError
150+
newFullyQualifiedTypeName: java.util.zip.ZipException
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
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.lang;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.java.JavaParser;
21+
import org.openrewrite.test.RecipeSpec;
22+
import org.openrewrite.test.RewriteTest;
23+
24+
import static org.openrewrite.java.Assertions.java;
25+
import static org.openrewrite.java.Assertions.javaVersion;
26+
27+
class MigrateZipErrorToZipExceptionTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec
32+
.recipeFromResource("/META-INF/rewrite/java-version-25.yml", "org.openrewrite.java.migrate.MigrateZipErrorToZipException")
33+
.parser(JavaParser.fromJavaVersion())
34+
.allSources(s -> s.markers(javaVersion(25)));
35+
}
36+
37+
@DocumentExample
38+
@Test
39+
void migrateZipErrorInCatchClause() {
40+
rewriteRun(
41+
//language=java
42+
java(
43+
"""
44+
import java.util.zip.ZipError;
45+
46+
class Test {
47+
void test() {
48+
try {
49+
// Some zip operation
50+
} catch (ZipError e) {
51+
System.out.println("Zip error occurred: " + e.getMessage());
52+
}
53+
}
54+
}
55+
""",
56+
"""
57+
import java.util.zip.ZipException;
58+
59+
class Test {
60+
void test() {
61+
try {
62+
// Some zip operation
63+
} catch (ZipException e) {
64+
System.out.println("Zip error occurred: " + e.getMessage());
65+
}
66+
}
67+
}
68+
"""
69+
)
70+
);
71+
}
72+
73+
@Test
74+
void migrateZipErrorInThrowsClause() {
75+
rewriteRun(
76+
//language=java
77+
java(
78+
"""
79+
import java.util.zip.ZipError;
80+
81+
class Test {
82+
void processZip() throws ZipError {
83+
throw new ZipError("Invalid zip file");
84+
}
85+
}
86+
""",
87+
"""
88+
import java.util.zip.ZipException;
89+
90+
class Test {
91+
void processZip() throws ZipException {
92+
throw new ZipException("Invalid zip file");
93+
}
94+
}
95+
"""
96+
)
97+
);
98+
}
99+
100+
@Test
101+
void noChangeWhenZipErrorNotUsed() {
102+
rewriteRun(
103+
//language=java
104+
java(
105+
"""
106+
import java.util.zip.ZipException;
107+
108+
class Test {
109+
void test(boolean flag) {
110+
try {
111+
if (flag) {
112+
throw new ZipException("This is a zip error");
113+
}
114+
throw new Exception("This is a general exception");
115+
} catch (ZipException e) {
116+
System.out.println("ZipException: " + e.getMessage());
117+
} catch (Exception e) {
118+
System.out.println("Exception: " + e.getMessage());
119+
}
120+
}
121+
}
122+
"""
123+
)
124+
);
125+
}
126+
}

0 commit comments

Comments
 (0)