From 90ed907a06c55e47b224b7ce36dacf1215d9bea2 Mon Sep 17 00:00:00 2001 From: Felipe Orozco Date: Wed, 20 Nov 2019 13:27:27 -0500 Subject: [PATCH] Refaster rule to convert Stream.of() -> Stream.empty() (#1061) Add Refaster rule to convert Stream.of() -> Stream.empty() --- .../baseline/refaster/StreamEmpty.java | 35 ++++++++++++++++ .../baseline/refaster/StreamEmptyTest.java | 41 +++++++++++++++++++ changelog/@unreleased/pr-1061.v2.yml | 5 +++ 3 files changed, 81 insertions(+) create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/StreamEmpty.java create mode 100644 baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/StreamEmptyTest.java create mode 100644 changelog/@unreleased/pr-1061.v2.yml diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/StreamEmpty.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/StreamEmpty.java new file mode 100644 index 000000000..3d19b1155 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/StreamEmpty.java @@ -0,0 +1,35 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 com.palantir.baseline.refaster; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.util.stream.Stream; + +public final class StreamEmpty { + + @BeforeTemplate + Stream streamNoParams() { + return Stream.of(); + } + + @AfterTemplate + Stream streamEmpty() { + return Stream.empty(); + } + +} diff --git a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/StreamEmptyTest.java b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/StreamEmptyTest.java new file mode 100644 index 000000000..48d737713 --- /dev/null +++ b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/StreamEmptyTest.java @@ -0,0 +1,41 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 com.palantir.baseline.refaster; + +import org.junit.Test; + +public class StreamEmptyTest { + + @Test + public void test() { + RefasterTestHelper + .forRefactoring(StreamEmpty.class) + .withInputLines( + "Test", + "import java.util.*;", + "import java.util.stream.Stream;", + "public class Test {", + " Stream i = Stream.of();", + "}") + .hasOutputLines( + "import java.util.*;", + "import java.util.stream.Stream;", + "public class Test {", + " Stream i = Stream.empty();", + "}"); + } +} diff --git a/changelog/@unreleased/pr-1061.v2.yml b/changelog/@unreleased/pr-1061.v2.yml new file mode 100644 index 000000000..f877e155b --- /dev/null +++ b/changelog/@unreleased/pr-1061.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Add Refaster rule to convert Stream.of() -> Stream.empty() + links: + - https://github.com/palantir/gradle-baseline/pull/1061