diff --git a/build.gradle.kts b/build.gradle.kts index 9a680584c1..65d6a5a739 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ recipeDependencies { parserClasspath("javax.persistence:javax.persistence-api:2.2") parserClasspath("org.glassfish:javax.servlet:3.0") parserClasspath("javax.annotation:javax.annotation-api:1.3.2") - parserClasspath("com.google.guava:guava:33.5.0-jre") + parserClasspath("com.google.guava:guava:33.7.0-jre") parserClasspath("com.google.errorprone:error_prone_core:2.+") testParserClasspath("com.sun.faces:jsf-api:2.1.29-11") diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaAtomicsNewReference.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaAtomicsNewReference.java deleted file mode 100644 index 844571b2c1..0000000000 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaAtomicsNewReference.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - *

- * Licensed under the Moderne Source Available License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://docs.moderne.io/licensing/moderne-source-available-license - *

- * 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 lombok.Getter; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Preconditions; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; -import org.openrewrite.java.JavaTemplate; -import org.openrewrite.java.JavaVisitor; -import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.search.UsesMethod; -import org.openrewrite.java.tree.J; - -import java.util.Set; - -import static java.util.Collections.singleton; - -public class NoGuavaAtomicsNewReference extends Recipe { - private static final MethodMatcher NEW_ATOMIC_REFERENCE = new MethodMatcher("com.google.common.util.concurrent.Atomics newReference(..)"); - - @Getter - final String displayName = "Prefer `new AtomicReference<>()`"; - - @Getter - final String description = "Prefer the Java standard library over third-party usage of Guava in simple cases like this."; - - @Getter - final Set tags = singleton( "guava" ); - - @Override - public TreeVisitor getVisitor() { - return Preconditions.check(new UsesMethod<>(NEW_ATOMIC_REFERENCE), new JavaVisitor() { - @Override - public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - if (NEW_ATOMIC_REFERENCE.matches(method)) { - maybeRemoveImport("com.google.common.util.concurrent.Atomics"); - maybeAddImport("java.util.concurrent.atomic.AtomicReference"); - return ((J.NewClass) JavaTemplate.builder("new AtomicReference<>()") - .imports("java.util.concurrent.atomic.AtomicReference") - .build() - .apply(getCursor(), method.getCoordinates().replace())) - .withArguments(method.getArguments()); - } - return super.visitMethodInvocation(method, ctx); - } - }); - } -} diff --git a/src/main/resources/META-INF/rewrite/classpath.tsv.gz b/src/main/resources/META-INF/rewrite/classpath.tsv.gz index abb860fda3..b902a2d0fe 100644 Binary files a/src/main/resources/META-INF/rewrite/classpath.tsv.gz and b/src/main/resources/META-INF/rewrite/classpath.tsv.gz differ diff --git a/src/main/resources/META-INF/rewrite/examples.yml b/src/main/resources/META-INF/rewrite/examples.yml index 1dfc38ed48..329fc5e5c0 100644 --- a/src/main/resources/META-INF/rewrite/examples.yml +++ b/src/main/resources/META-INF/rewrite/examples.yml @@ -4006,27 +4006,6 @@ examples: language: java --- type: specs.openrewrite.org/v1beta/example -recipeName: org.openrewrite.java.migrate.guava.NoGuavaAtomicsNewReference -examples: -- description: '`NoGuavaAtomicsNewReferenceTest#noNewReference`' - sources: - - before: | - import com.google.common.util.concurrent.Atomics; - - class Test { - Object o1 = Atomics.newReference(); - Object o2 = Atomics.newReference(0); - } - after: | - import java.util.concurrent.atomic.AtomicReference; - - class Test { - Object o1 = new AtomicReference<>(); - Object o2 = new AtomicReference<>(0); - } - language: java ---- -type: specs.openrewrite.org/v1beta/example recipeName: org.openrewrite.java.migrate.guava.NoGuavaCollections2Transform examples: - description: '`NoGuavaCollections2TransformTest#replaceCollections2Transform`' diff --git a/src/main/resources/META-INF/rewrite/inline-guava-33-methods.yml b/src/main/resources/META-INF/rewrite/inline-guava-33-methods.yml index 8ec736e726..824ae16189 100644 --- a/src/main/resources/META-INF/rewrite/inline-guava-33-methods.yml +++ b/src/main/resources/META-INF/rewrite/inline-guava-33-methods.yml @@ -15,7 +15,8 @@ # # -# Recipes generated for `@InlineMe` annotated methods in `com.google.guava:guava:33.5.0-jre` +# Recipes generated for `@InlineMe` annotated methods in `com.google.guava:guava:33.7.0-jre` +# by `InlineMethodCallsRecipeGenerator` in https://github.com/openrewrite/rewrite-third-party # type: specs.openrewrite.org/v1beta/recipe @@ -105,12 +106,12 @@ recipeList: classpathFromResources: - 'guava-33' - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.google.common.collect.Range apply(java.lang.Comparable)' + methodPattern: 'com.google.common.collect.Range apply(*)' replacement: 'this.contains(input)' classpathFromResources: - 'guava-33' - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.google.common.collect.Range test(java.lang.Comparable)' + methodPattern: 'com.google.common.collect.Range test(*)' replacement: 'this.contains(input)' classpathFromResources: - 'guava-33' @@ -172,7 +173,7 @@ recipeList: classpathFromResources: - 'guava-33' - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.google.common.collect.Ordering binarySearch(java.util.List, java.lang.Object)' + methodPattern: 'com.google.common.collect.Ordering binarySearch(java.util.List, *)' replacement: 'Collections.binarySearch(sortedList, key, this)' imports: - 'java.util.Collections' @@ -205,7 +206,7 @@ recipeList: classpathFromResources: - 'guava-33' - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.google.common.base.Equivalence test(java.lang.Object, java.lang.Object)' + methodPattern: 'com.google.common.base.Equivalence test(*, *)' replacement: 'this.equivalent(t, u)' classpathFromResources: - 'guava-33' @@ -229,12 +230,12 @@ recipeList: classpathFromResources: - 'guava-33' - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.google.common.hash.BloomFilter apply(java.lang.Object)' + methodPattern: 'com.google.common.hash.BloomFilter apply(*)' replacement: 'this.mightContain(input)' classpathFromResources: - 'guava-33' - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.google.common.hash.BloomFilter test(java.lang.Object)' + methodPattern: 'com.google.common.hash.BloomFilter test(*)' replacement: 'this.mightContain(input)' classpathFromResources: - 'guava-33' @@ -318,7 +319,7 @@ recipeList: classpathFromResources: - 'guava-33' - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.google.common.base.Converter apply(java.lang.Object)' + methodPattern: 'com.google.common.base.Converter apply(*)' replacement: 'this.convert(a)' classpathFromResources: - 'guava-33' @@ -403,3 +404,31 @@ recipeList: replacement: 'Math.multiplyExact(a, b)' classpathFromResources: - 'guava-33' + - org.openrewrite.java.InlineMethodCalls: + methodPattern: 'com.google.common.util.concurrent.Atomics newReference()' + replacement: 'new AtomicReference<>()' + imports: + - 'java.util.concurrent.atomic.AtomicReference' + classpathFromResources: + - 'guava-33' + - org.openrewrite.java.InlineMethodCalls: + methodPattern: 'com.google.common.util.concurrent.Atomics newReference(*)' + replacement: 'new AtomicReference<>(initialValue)' + imports: + - 'java.util.concurrent.atomic.AtomicReference' + classpathFromResources: + - 'guava-33' + - org.openrewrite.java.InlineMethodCalls: + methodPattern: 'com.google.common.util.concurrent.Atomics newReferenceArray(int)' + replacement: 'new AtomicReferenceArray<>(length)' + imports: + - 'java.util.concurrent.atomic.AtomicReferenceArray' + classpathFromResources: + - 'guava-33' + - org.openrewrite.java.InlineMethodCalls: + methodPattern: 'com.google.common.util.concurrent.Atomics newReferenceArray(*[])' + replacement: 'new AtomicReferenceArray<>(array)' + imports: + - 'java.util.concurrent.atomic.AtomicReferenceArray' + classpathFromResources: + - 'guava-33' diff --git a/src/main/resources/META-INF/rewrite/no-guava.yml b/src/main/resources/META-INF/rewrite/no-guava.yml index ebef7d0c61..bd20eae66e 100644 --- a/src/main/resources/META-INF/rewrite/no-guava.yml +++ b/src/main/resources/META-INF/rewrite/no-guava.yml @@ -77,7 +77,6 @@ recipeList: - org.openrewrite.java.migrate.guava.PreferMathAddExact - org.openrewrite.java.migrate.guava.PreferMathSubtractExact - org.openrewrite.java.migrate.guava.PreferMathMultiplyExact - - org.openrewrite.java.migrate.guava.NoGuavaAtomicsNewReference - tech.picnic.errorprone.refasterrules.InputStreamRulesRecipes diff --git a/src/main/resources/META-INF/rewrite/recipes.csv b/src/main/resources/META-INF/rewrite/recipes.csv index dbb7a45637..8072608e9d 100644 --- a/src/main/resources/META-INF/rewrite/recipes.csv +++ b/src/main/resources/META-INF/rewrite/recipes.csv @@ -1,5 +1,5 @@ ecosystem,packageName,name,displayName,description,recipeCount,category1,category2,category3,category4,category1Description,category2Description,category3Description,category4Description,options,dataTables -maven,org.openrewrite.recipe:rewrite-migrate-java,com.google.guava.InlineGuavaMethods,Inline `guava` methods annotated with `@InlineMe`,Automatically generated recipes to inline method calls based on `@InlineMe` annotations discovered in the type table.,66,,,Guava,Google,,,,,, +maven,org.openrewrite.recipe:rewrite-migrate-java,com.google.guava.InlineGuavaMethods,Inline `guava` methods annotated with `@InlineMe`,Automatically generated recipes to inline method calls based on `@InlineMe` annotations discovered in the type table.,70,,,Guava,Google,,,,,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.JSpecifyBestPractices,JSpecify best practices,"Apply JSpecify best practices, such as migrating off of alternatives, and adding missing `@Nullable` annotations.",40,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateFromJakartaAnnotationApi,Migrate from Jakarta annotation API to JSpecify,Migrate from Jakarta annotation API to JSpecify.,6,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.jspecify.MigrateFromJavaxAnnotationApi,Migrate from javax annotation API to JSpecify,Migrate from javax annotation API to JSpecify.,9,,,JSpecify,Java,,,Recipes for adopting [JSpecify](https://jspecify.dev/) nullability annotations.,Basic building blocks for transforming Java code.,, @@ -51,7 +51,7 @@ maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.J maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.JREThrowableFinalMethods,Rename final method declarations `getSuppressed()` and `addSuppressed(Throwable exception)` in classes that extend `Throwable`,The recipe renames `getSuppressed()` and `addSuppressed(Throwable exception)` methods in classes that extend `java.lang.Throwable` to `myGetSuppressed` and `myAddSuppressed(Throwable)`. These methods were added to Throwable in Java 7 and are marked final which cannot be overridden.,1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.JREWrapperInterface,Add missing `isWrapperFor` and `unwrap` methods,Add method implementations stubs to classes that implement `java.sql.Wrapper`.,3,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.Java8toJava11,Migrate to Java 11,"This recipe will apply changes commonly needed when upgrading to Java 11. Specifically, for those applications that are built on Java 8, this recipe will update and add dependencies on J2EE libraries that are no longer directly bundled with the JDK. This recipe will also replace deprecated API with equivalents when there is a clear migration strategy. Build files will also be updated to use Java 11 as the target/source and plugins will be also be upgraded to versions that are compatible with Java 11.",324,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""instanceName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" -maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.JavaBestPractices,Java best practices,"Applies opinionated best practices for Java projects targeting Java 25. This recipe includes the full Java 25 upgrade chain plus additional improvements to code style, API usage, and third-party dependency reduction that go beyond what the version migration recipes apply.",1760,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""instanceName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" +maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.JavaBestPractices,Java best practices,"Applies opinionated best practices for Java projects targeting Java 25. This recipe includes the full Java 25 upgrade chain plus additional improvements to code style, API usage, and third-party dependency reduction that go beyond what the version migration recipes apply.",1763,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,,"[{""name"":""org.openrewrite.maven.table.MavenMetadataFailures"",""displayName"":""Maven metadata failures"",""instanceName"":""Maven metadata failures"",""description"":""Attempts to resolve maven metadata that failed."",""columns"":[{""name"":""group"",""type"":""String"",""displayName"":""Group id"",""description"":""The groupId of the artifact for which the metadata download failed.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact id"",""description"":""The artifactId of the artifact for which the metadata download failed.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The version of the artifact for which the metadata download failed.""},{""name"":""mavenRepositoryUri"",""type"":""String"",""displayName"":""Maven repository"",""description"":""The URL of the Maven repository that the metadata download failed on.""},{""name"":""snapshots"",""type"":""String"",""displayName"":""Snapshots"",""description"":""Does the repository support snapshots.""},{""name"":""releases"",""type"":""String"",""displayName"":""Releases"",""description"":""Does the repository support releases.""},{""name"":""failure"",""type"":""String"",""displayName"":""Failure"",""description"":""The reason the metadata download failed.""}]}]" maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.JpaCacheProperties,Disable the persistence unit second-level cache,Sets an explicit value for the shared cache mode.,1,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.Jre17AgentMainPreMainPublic,Set visibility of `premain` and `agentmain` methods to `public`,Check for a behavior change in Java agents.,5,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.Krb5LoginModuleClass,Use `com.sun.security.auth.module.Krb5LoginModule` instead of `com.ibm.security.auth.module.Krb5LoginModule`,Do not use the `com.ibm.security.auth.module.Krb5LoginModule` class.,2,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, @@ -130,8 +130,7 @@ maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.d maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.datanucleus.UpgradeDataNucleus_5_0,Migrate to DataNucleus 5.0,"Migrate DataNucleus 4.x applications to 5.0. This recipe handles package relocations, type renames, property key changes, and dependency updates.",55,,DataNucleus,Modernize,Java,,Recipes for migrating [DataNucleus](https://www.datanucleus.org/) JDO/JPA persistence applications.,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.datanucleus.UpgradeDataNucleus_5_1,Migrate to DataNucleus 5.1,"Migrate DataNucleus applications to 5.1. This recipe first applies the 5.0 migration, then handles the transaction namespace reorganization and other property renames introduced in 5.1.",76,,DataNucleus,Modernize,Java,,Recipes for migrating [DataNucleus](https://www.datanucleus.org/) JDO/JPA persistence applications.,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.datanucleus.UpgradeDataNucleus_5_2,Migrate to DataNucleus 5.2,"Migrate DataNucleus applications to 5.2. This recipe first applies the 5.1 migration, then handles the column mapping package move and query-related property renames introduced in 5.2.",87,,DataNucleus,Modernize,Java,,Recipes for migrating [DataNucleus](https://www.datanucleus.org/) JDO/JPA persistence applications.,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.guava.NoGuava,Prefer the Java standard library instead of Guava,"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.",189,,Guava,Modernize,Java,,Recipes for migrating from [Google Guava](https://github.com/google/guava) to Java standard library.,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.guava.NoGuavaAtomicsNewReference,Prefer `new AtomicReference<>()`,Prefer the Java standard library over third-party usage of Guava in simple cases like this.,1,,Guava,Modernize,Java,,Recipes for migrating from [Google Guava](https://github.com/google/guava) to Java standard library.,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, +maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.guava.NoGuava,Prefer the Java standard library instead of Guava,"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.",192,,Guava,Modernize,Java,,Recipes for migrating from [Google Guava](https://github.com/google/guava) to Java standard library.,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.guava.NoGuavaCollections2Transform,Prefer `Collection.stream().map(Function)` over `Collections2.transform`,"Prefer `Collection.stream().map(Function)` over `Collections2.transform(Collection, Function)`.",1,,Guava,Modernize,Java,,Recipes for migrating from [Google Guava](https://github.com/google/guava) to Java standard library.,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.guava.NoGuavaCreateTempDir,Prefer `Files#createTempDirectory()`,Replaces Guava `Files#createTempDir()` with Java `Files#createTempDirectory(..)`. Transformations are limited to scopes throwing or catching `java.io.IOException`.,1,,Guava,Modernize,Java,,Recipes for migrating from [Google Guava](https://github.com/google/guava) to Java standard library.,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.guava.NoGuavaDirectExecutor,Prefer `Runnable::run`,"`Executor` is a SAM-compatible interface, so `Runnable::run` is just as succinct as `MoreExecutors.directExecutor()` but without the third-party library requirement.",1,,Guava,Modernize,Java,,Recipes for migrating from [Google Guava](https://github.com/google/guava) to Java standard library.,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,, diff --git a/src/test/java/com/google/guava/InlineGuavaMethodsTest.java b/src/test/java/com/google/guava/InlineGuavaMethodsTest.java index da0f326eee..afdb6025ba 100644 --- a/src/test/java/com/google/guava/InlineGuavaMethodsTest.java +++ b/src/test/java/com/google/guava/InlineGuavaMethodsTest.java @@ -75,4 +75,209 @@ String repeatString(String s, int n) { ) ); } + + @Test + void atomics() { + rewriteRun( + java( + """ + import com.google.common.util.concurrent.Atomics; + + import java.util.concurrent.atomic.AtomicReference; + import java.util.concurrent.atomic.AtomicReferenceArray; + + class Atomic { + AtomicReference empty() { + return Atomics.newReference(); + } + + AtomicReference initial(String value) { + return Atomics.newReference(value); + } + + AtomicReferenceArray sized(int length) { + return Atomics.newReferenceArray(length); + } + + AtomicReferenceArray fromArray(String[] array) { + return Atomics.newReferenceArray(array); + } + } + """, + """ + import java.util.concurrent.atomic.AtomicReference; + import java.util.concurrent.atomic.AtomicReferenceArray; + + class Atomic { + AtomicReference empty() { + return new AtomicReference<>(); + } + + AtomicReference initial(String value) { + return new AtomicReference<>(value); + } + + AtomicReferenceArray sized(int length) { + return new AtomicReferenceArray<>(length); + } + + AtomicReferenceArray fromArray(String[] array) { + return new AtomicReferenceArray<>(array); + } + } + """ + ) + ); + } + + @Test + void range() { + rewriteRun( + java( + """ + import com.google.common.collect.Range; + + class R { + boolean applied(Range range, Integer value) { + return range.apply(value); + } + + boolean tested(Range range, Integer value) { + return range.test(value); + } + } + """, + """ + import com.google.common.collect.Range; + + class R { + boolean applied(Range range, Integer value) { + return range.contains(value); + } + + boolean tested(Range range, Integer value) { + return range.contains(value); + } + } + """ + ) + ); + } + + @Test + void bloomFilter() { + rewriteRun( + java( + """ + import com.google.common.hash.BloomFilter; + + class B { + boolean applied(BloomFilter filter, String value) { + return filter.apply(value); + } + + boolean tested(BloomFilter filter, String value) { + return filter.test(value); + } + } + """, + """ + import com.google.common.hash.BloomFilter; + + class B { + boolean applied(BloomFilter filter, String value) { + return filter.mightContain(value); + } + + boolean tested(BloomFilter filter, String value) { + return filter.mightContain(value); + } + } + """ + ) + ); + } + + @Test + void converter() { + rewriteRun( + java( + """ + import com.google.common.base.Converter; + + class C { + Integer applied(Converter converter, String value) { + return converter.apply(value); + } + } + """, + """ + import com.google.common.base.Converter; + + class C { + Integer applied(Converter converter, String value) { + return converter.convert(value); + } + } + """ + ) + ); + } + + @Test + void equivalence() { + rewriteRun( + java( + """ + import com.google.common.base.Equivalence; + + class E { + boolean tested(Equivalence equivalence, String left, String right) { + return equivalence.test(left, right); + } + } + """, + """ + import com.google.common.base.Equivalence; + + class E { + boolean tested(Equivalence equivalence, String left, String right) { + return equivalence.equivalent(left, right); + } + } + """ + ) + ); + } + + @Test + void orderingBinarySearch() { + rewriteRun( + java( + """ + import com.google.common.collect.Ordering; + + import java.util.List; + + class O { + int search(Ordering ordering, List list, String key) { + return ordering.binarySearch(list, key); + } + } + """, + """ + import com.google.common.collect.Ordering; + + import java.util.Collections; + import java.util.List; + + class O { + int search(Ordering ordering, List list, String key) { + return Collections.binarySearch(list, key, ordering); + } + } + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaAtomicsNewReferenceTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaAtomicsNewReferenceTest.java deleted file mode 100644 index 7ab306cbc3..0000000000 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaAtomicsNewReferenceTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - *

- * Licensed under the Moderne Source Available License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://docs.moderne.io/licensing/moderne-source-available-license - *

- * 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.DocumentExample; -import org.openrewrite.InMemoryExecutionContext; -import org.openrewrite.java.JavaParser; -import org.openrewrite.test.RecipeSpec; -import org.openrewrite.test.RewriteTest; - -import static org.openrewrite.java.Assertions.java; - -class NoGuavaAtomicsNewReferenceTest implements RewriteTest { - @Override - public void defaults(RecipeSpec spec) { - spec - .recipe(new NoGuavaAtomicsNewReference()) - .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava")); - } - - @DocumentExample - @Test - void noNewReference() { - //language=java - rewriteRun( - java( - """ - import com.google.common.util.concurrent.Atomics; - - class Test { - Object o1 = Atomics.newReference(); - Object o2 = Atomics.newReference(0); - } - """, - """ - import java.util.concurrent.atomic.AtomicReference; - - class Test { - Object o1 = new AtomicReference<>(); - Object o2 = new AtomicReference<>(0); - } - """ - ) - ); - } -}