Skip to content

Commit

Permalink
feat: no guava - java 21 version (Math.clamp) (#329)
Browse files Browse the repository at this point in the history
* feat: no guava - java 21 version (Math.clamp)

* feat: enhance NoGuavaJava21 testcases
  • Loading branch information
SimonVerhoeven authored Oct 23, 2023
1 parent 8616c45 commit c4e9680
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/main/resources/META-INF/rewrite/no-guava.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ recipeList:
- org.openrewrite.java.migrate.guava.PreferJavaUtilObjectsRequireNonNullElse
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.guava.NoGuavaJava21
displayName: Prefer the Java 21 standard library instead of Guava
description: >
Guava filled in important gaps in the Java standard library and still does. But at least some of Guava's API surface
area is covered by the Java standard library now, and some projects may be able to remove Guava altogether if they
migrate to standard library for these functions.
tags:
- guava
- java21
recipeList:
- org.openrewrite.java.migrate.guava.NoGuavaJava11
- org.openrewrite.java.migrate.guava.PreferMathClamp
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.guava.PreferJavaNioCharsetStandardCharsets
displayName: Prefer `java.nio.charset.StandardCharsets`
description: Prefer `java.nio.charset.StandardCharsets` instead of using `com.google.common.base.Charsets`.
Expand Down Expand Up @@ -454,3 +468,31 @@ recipeList:
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: com.google.common.math.IntMath multiplyExact(..)
fullyQualifiedTargetTypeName: java.lang.Math

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.guava.PreferMathClamp
displayName: Prefer `Math#clamp`
description: Prefer `java.lang.Math#clamp` instead of using `com.google.common.primitives.*#constrainToRange`.
tags:
- guava
- java21
recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.google.common.primitives.Doubles constrainToRange(..)
newMethodName: clamp
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: com.google.common.primitives.Doubles clamp(..)
fullyQualifiedTargetTypeName: java.lang.Math
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.google.common.primitives.Floats constrainToRange(..)
newMethodName: clamp
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: com.google.common.primitives.Floats clamp(..)
fullyQualifiedTargetTypeName: java.lang.Math
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.google.common.primitives.Longs constrainToRange(..)
newMethodName: clamp
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: com.google.common.primitives.Longs clamp(..)
fullyQualifiedTargetTypeName: java.lang.Math
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.guava;

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.config.Environment;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.version;

class NoGuavaJava21Test implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(
Environment.builder()
.scanRuntimeClasspath("org.openrewrite.java.migrate.guava")
.build()
.activateRecipes("org.openrewrite.java.migrate.guava.NoGuavaJava21")
)
.parser(JavaParser.fromJavaVersion().classpath("guava"));
}

@Test
void preferMathClampForDouble() {
//language=java
rewriteRun(
version(
java(
"""
import com.google.common.primitives.Doubles;
class Test {
public double testMethod() {
return Doubles.constrainToRange(20D, 10D, 100D);
}
}
""",
"""
class Test {
public double testMethod() {
return Math.clamp(20D, 10D, 100D);
}
}
"""
),
21)
);
}

@Test
void preferMathClampForLongs() {
//language=java
rewriteRun(
version(
java(
"""
import com.google.common.primitives.Longs;
class Test {
public long testMethod() {
return Longs.constrainToRange(20L, 10L, 100L);
}
}
""",
"""
class Test {
public long testMethod() {
return Math.clamp(20L, 10L, 100L);
}
}
"""
),
21)
);
}

@Test
void preferMathClampForFloats() {
//language=java
rewriteRun(
version(
java(
"""
import com.google.common.primitives.Floats;
class Test {
public float testMethod() {
return Floats.constrainToRange(20F, 10F, 100F);
}
}
""",
"""
class Test {
public float testMethod() {
return Math.clamp(20F, 10F, 100F);
}
}
"""
),
21)
);
}
}

0 comments on commit c4e9680

Please sign in to comment.