diff --git a/lib/src/main/java/com/diffplug/spotless/DelegateFormatterStep.java b/lib/src/main/java/com/diffplug/spotless/DelegateFormatterStep.java new file mode 100644 index 0000000000..829652a5dc --- /dev/null +++ b/lib/src/main/java/com/diffplug/spotless/DelegateFormatterStep.java @@ -0,0 +1,32 @@ +/* + * Copyright 2022 DiffPlug + * + * 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.diffplug.spotless; + +import java.util.Objects; + +/** Superclass of all compound FormatterSteps necessary for {@link com.diffplug.spotless.LazyForwardingEquality#unlazy(java.lang.Object)}. */ +abstract class DelegateFormatterStep implements FormatterStep { + protected final FormatterStep delegateStep; + + DelegateFormatterStep(FormatterStep delegateStep) { + this.delegateStep = Objects.requireNonNull(delegateStep); + } + + @Override + public final String getName() { + return delegateStep.getName(); + } +} diff --git a/lib/src/main/java/com/diffplug/spotless/FilterByContentPatternFormatterStep.java b/lib/src/main/java/com/diffplug/spotless/FilterByContentPatternFormatterStep.java index 9b39361719..d9a6fe4093 100644 --- a/lib/src/main/java/com/diffplug/spotless/FilterByContentPatternFormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/FilterByContentPatternFormatterStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,27 +22,19 @@ import javax.annotation.Nullable; -final class FilterByContentPatternFormatterStep implements FormatterStep { - private final FormatterStep delegateStep; +final class FilterByContentPatternFormatterStep extends DelegateFormatterStep { final Pattern contentPattern; FilterByContentPatternFormatterStep(FormatterStep delegateStep, String contentPattern) { - this.delegateStep = Objects.requireNonNull(delegateStep); + super(delegateStep); this.contentPattern = Pattern.compile(Objects.requireNonNull(contentPattern)); } - @Override - public String getName() { - return delegateStep.getName(); - } - @Override public @Nullable String format(String raw, File file) throws Exception { Objects.requireNonNull(raw, "raw"); Objects.requireNonNull(file, "file"); - Matcher matcher = contentPattern.matcher(raw); - if (matcher.find()) { return delegateStep.format(raw, file); } else { diff --git a/lib/src/main/java/com/diffplug/spotless/FilterByFileFormatterStep.java b/lib/src/main/java/com/diffplug/spotless/FilterByFileFormatterStep.java index 2fd221220c..04a06a4673 100644 --- a/lib/src/main/java/com/diffplug/spotless/FilterByFileFormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/FilterByFileFormatterStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,20 +20,14 @@ import javax.annotation.Nullable; -final class FilterByFileFormatterStep implements FormatterStep { - private final FormatterStep delegateStep; +final class FilterByFileFormatterStep extends DelegateFormatterStep { private final SerializableFileFilter filter; FilterByFileFormatterStep(FormatterStep delegateStep, SerializableFileFilter filter) { - this.delegateStep = Objects.requireNonNull(delegateStep); + super(delegateStep); this.filter = Objects.requireNonNull(filter); } - @Override - public String getName() { - return delegateStep.getName(); - } - @Override public @Nullable String format(String raw, File file) throws Exception { Objects.requireNonNull(raw, "raw"); diff --git a/lib/src/main/java/com/diffplug/spotless/LazyForwardingEquality.java b/lib/src/main/java/com/diffplug/spotless/LazyForwardingEquality.java index e29464bd91..e2d6ba8988 100644 --- a/lib/src/main/java/com/diffplug/spotless/LazyForwardingEquality.java +++ b/lib/src/main/java/com/diffplug/spotless/LazyForwardingEquality.java @@ -113,7 +113,16 @@ static byte[] toBytes(Serializable obj) { } /** Ensures that the lazy state has been evaluated. */ - public static void unlazy(LazyForwardingEquality in) { - in.state(); + public static void unlazy(Object in) { + if (in instanceof LazyForwardingEquality) { + ((LazyForwardingEquality) in).state(); + } else if (in instanceof DelegateFormatterStep) { + unlazy(((DelegateFormatterStep) in).delegateStep); + } else if (in instanceof Iterable) { + Iterable cast = (Iterable) in; + for (Object c : cast) { + unlazy(c); + } + } } }