File tree Expand file tree Collapse file tree 3 files changed +97
-0
lines changed
java/org/openrewrite/java/migrate/util
resources/META-INF/rewrite
test/java/org/openrewrite/java/migrate/util Expand file tree Collapse file tree 3 files changed +97
-0
lines changed Original file line number Diff line number Diff line change 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+
17+ package org .openrewrite .java .migrate .util ;
18+
19+ import com .google .errorprone .refaster .annotation .AfterTemplate ;
20+ import com .google .errorprone .refaster .annotation .BeforeTemplate ;
21+ import org .openrewrite .java .template .RecipeDescriptor ;
22+
23+ import java .util .concurrent .ThreadLocalRandom ;
24+
25+ @ RecipeDescriptor (
26+ name = "Replace `java.lang.Math random()` with `ThreadLocalRandom nextDouble()`" ,
27+ description = "Replace `java.lang.Math random()` with `ThreadLocalRandom nextDouble()` to reduce contention."
28+ )
29+ public class ReplaceMathRandomWithThreadLocalRandom {
30+ @ BeforeTemplate
31+ double javaMathRandom () {
32+ return Math .random ();
33+ }
34+
35+ @ AfterTemplate
36+ double threadLocalRandomNextDouble () {
37+ return ThreadLocalRandom .current ().nextDouble ();
38+ }
39+ }
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ recipeList:
2626 - org.openrewrite.java.migrate.UpgradeToJava6
2727 - org.openrewrite.java.migrate.JREJdbcInterfaceNewMethods
2828 - org.openrewrite.java.migrate.JREThrowableFinalMethods
29+ - org.openrewrite.java.migrate.util.ReplaceMathRandomWithThreadLocalRandomRecipe
2930---
3031type : specs.openrewrite.org/v1beta/recipe
3132name : org.openrewrite.java.migrate.JREJdbcInterfaceNewMethods
Original file line number Diff line number Diff line change 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 .util ;
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+ class ReplaceMathRandomWithThreadLocalRandomRecipeTest implements RewriteTest {
26+
27+ @ Override
28+ public void defaults (RecipeSpec spec ) {
29+ spec .recipe (new ReplaceMathRandomWithThreadLocalRandomRecipe ());
30+ }
31+
32+ @ Test
33+ @ DocumentExample
34+ void replacesMathRandom () {
35+ rewriteRun (
36+ //language=java
37+ java (
38+ """
39+ class Example {
40+ double test() {
41+ return Math.random();
42+ }
43+ }
44+ """ ,
45+ """
46+ import java.util.concurrent.ThreadLocalRandom;
47+
48+ class Example {
49+ double test() {
50+ return ThreadLocalRandom.current().nextDouble();
51+ }
52+ }
53+ """
54+ )
55+ );
56+ }
57+ }
You can’t perform that action at this time.
0 commit comments