Skip to content

Commit

Permalink
Deprecate Subject.Factory methods for Java 8 types.
Browse files Browse the repository at this point in the history
They are no longer necessary.

RELNOTES=Deprecated `Subject.Factory` methods for Java 8 types. We won't remove them, but you can simplify your code by migrating off them: Just replace `assertAbout(foos()).that(foo)` with `assertThat(foo)`.
PiperOrigin-RevId: 625870580
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Apr 18, 2024
1 parent b74edad commit 59e7a50
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ protected String actualCustomStringRepresentation() {
* Obsolete factory instance. This factory was previously necessary for assertions like {@code
* assertWithMessage(...).about(intStreams()).that(stream)....}. Now, you can perform assertions
* like that without the {@code about(...)} call.
*
* @deprecated Instead of {@code about(intStreams()).that(...)}, use just {@code that(...)}.
* Similarly, instead of {@code assertAbout(intStreams()).that(...)}, use just {@code
* assertThat(...)}.
*/
@Deprecated
@SuppressWarnings("InlineMeSuggester") // We want users to remove the surrounding call entirely.
public static Factory<IntStreamSubject, IntStream> intStreams() {
return IntStreamSubject::new;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ protected String actualCustomStringRepresentation() {
* Obsolete factory instance. This factory was previously necessary for assertions like {@code
* assertWithMessage(...).about(longStreams()).that(stream)....}. Now, you can perform assertions
* like that without the {@code about(...)} call.
*
* @deprecated Instead of {@code about(longStreams()).that(...)}, use just {@code that(...)}.
* Similarly, instead of {@code assertAbout(longStreams()).that(...)}, use just {@code
* assertThat(...)}.
*/
@Deprecated
@SuppressWarnings("InlineMeSuggester") // We want users to remove the surrounding call entirely.
public static Factory<LongStreamSubject, LongStream> longStreams() {
return LongStreamSubject::new;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ public void hasValue(double expected) {
* Obsolete factory instance. This factory was previously necessary for assertions like {@code
* assertWithMessage(...).about(optionalDoubles()).that(optional)....}. Now, you can perform
* assertions like that without the {@code about(...)} call.
*
* @deprecated Instead of {@code about(optionalDoubles()).that(...)}, use just {@code that(...)}.
* Similarly, instead of {@code assertAbout(optionalDoubles()).that(...)}, use just {@code
* assertThat(...)}.
*/
public static Subject.Factory<OptionalDoubleSubject, OptionalDouble> optionalDoubles() {
@Deprecated
@SuppressWarnings("InlineMeSuggester") // We want users to remove the surrounding call entirely.
public static Factory<OptionalDoubleSubject, OptionalDouble> optionalDoubles() {
return (metadata, subject) -> new OptionalDoubleSubject(metadata, subject, "optionalDouble");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ public void hasValue(int expected) {
* Obsolete factory instance. This factory was previously necessary for assertions like {@code
* assertWithMessage(...).about(optionalInts()).that(optional)....}. Now, you can perform
* assertions like that without the {@code about(...)} call.
*
* @deprecated Instead of {@code about(optionalInts()).that(...)}, use just {@code that(...)}.
* Similarly, instead of {@code assertAbout(optionalInts()).that(...)}, use just {@code
* assertThat(...)}.
*/
public static Subject.Factory<OptionalIntSubject, OptionalInt> optionalInts() {
@Deprecated
@SuppressWarnings("InlineMeSuggester") // We want users to remove the surrounding call entirely.
public static Factory<OptionalIntSubject, OptionalInt> optionalInts() {
return (metadata, subject) -> new OptionalIntSubject(metadata, subject, "optionalInt");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ public void hasValue(long expected) {
* Obsolete factory instance. This factory was previously necessary for assertions like {@code
* assertWithMessage(...).about(optionalLongs()).that(optional)....}. Now, you can perform
* assertions like that without the {@code about(...)} call.
*
* @deprecated Instead of {@code about(optionalLongs()).that(...)}, use just {@code that(...)}.
* Similarly, instead of {@code assertAbout(optionalLongs()).that(...)}, use just {@code
* assertThat(...)}.
*/
public static Subject.Factory<OptionalLongSubject, OptionalLong> optionalLongs() {
@Deprecated
@SuppressWarnings("InlineMeSuggester") // We want users to remove the surrounding call entirely.
public static Factory<OptionalLongSubject, OptionalLong> optionalLongs() {
return (metadata, subject) -> new OptionalLongSubject(metadata, subject, "optionalLong");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@ public void hasValue(@Nullable Object expected) {
* Obsolete factory instance. This factory was previously necessary for assertions like {@code
* assertWithMessage(...).about(paths()).that(path)....}. Now, you can perform assertions like
* that without the {@code about(...)} call.
*
* @deprecated Instead of {@code about(optionals()).that(...)}, use just {@code that(...)}.
* Similarly, instead of {@code assertAbout(optionals()).that(...)}, use just {@code
* assertThat(...)}.
*/
public static Subject.Factory<OptionalSubject, Optional<?>> optionals() {
@Deprecated
@SuppressWarnings("InlineMeSuggester") // We want users to remove the surrounding call entirely.
public static Factory<OptionalSubject, Optional<?>> optionals() {
return (metadata, subject) -> new OptionalSubject(metadata, subject, "optional");
}
}
7 changes: 6 additions & 1 deletion core/src/main/java/com/google/common/truth/PathSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ public final class PathSubject extends Subject {
* Obsolete factory instance. This factory was previously necessary for assertions like {@code
* assertWithMessage(...).about(intStreams()).that(stream)....}. Now, you can perform assertions
* like that without the {@code about(...)} call.
*
* @deprecated Instead of {@code about(paths()).that(...)}, use just {@code that(...)}. Similarly,
* instead of {@code assertAbout(paths()).that(...)}, use just {@code assertThat(...)}.
*/
public static Subject.Factory<PathSubject, Path> paths() {
@Deprecated
@SuppressWarnings("InlineMeSuggester") // We want users to remove the surrounding call entirely.
public static Factory<PathSubject, Path> paths() {
return PathSubject::new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ protected String actualCustomStringRepresentation() {
* Obsolete factory instance. This factory was previously necessary for assertions like {@code
* assertWithMessage(...).about(streams()).that(stream)....}. Now, you can perform assertions like
* that without the {@code about(...)} call.
*
* @deprecated Instead of {@code about(streams()).that(...)}, use just {@code that(...)}.
* Similarly, instead of {@code assertAbout(streams()).that(...)}, use just {@code
* assertThat(...)}.
*/
public static Subject.Factory<StreamSubject, Stream<?>> streams() {
@Deprecated
@SuppressWarnings("InlineMeSuggester") // We want users to remove the surrounding call entirely.
public static Factory<StreamSubject, Stream<?>> streams() {
return StreamSubject::new;
}

Expand Down
26 changes: 14 additions & 12 deletions core/src/main/java/com/google/common/truth/Truth8.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.google.common.truth;

import static com.google.common.truth.Truth.assertAbout;

import com.google.common.annotations.GwtIncompatible;
import com.google.j2objc.annotations.J2ObjCIncompatible;
import java.nio.file.Path;
Expand All @@ -40,43 +38,47 @@
* such static imports will become ambiguous in Truth 1.4.2, breaking your build.
*/
@Deprecated
// The methods here are no more dangerous that wherever the user got the (e.g.) Stream.
@SuppressWarnings("Java7ApiChecker")
@SuppressWarnings({
// The methods here are no more dangerous that wherever the user got the (e.g.) Stream.
"Java7ApiChecker",
// Replacing "Truth.assertThat" with "assertThat" would produce an infinite loop.
"StaticImportPreferred",
})
public final class Truth8 {
@SuppressWarnings("AssertAboutOptionals") // suggests infinite recursion
public static OptionalSubject assertThat(@Nullable Optional<?> target) {
return assertAbout(OptionalSubject.optionals()).that(target);
return Truth.assertThat(target);
}

public static OptionalIntSubject assertThat(@Nullable OptionalInt target) {
return assertAbout(OptionalIntSubject.optionalInts()).that(target);
return Truth.assertThat(target);
}

public static OptionalLongSubject assertThat(@Nullable OptionalLong target) {
return assertAbout(OptionalLongSubject.optionalLongs()).that(target);
return Truth.assertThat(target);
}

public static OptionalDoubleSubject assertThat(@Nullable OptionalDouble target) {
return assertAbout(OptionalDoubleSubject.optionalDoubles()).that(target);
return Truth.assertThat(target);
}

public static StreamSubject assertThat(@Nullable Stream<?> target) {
return assertAbout(StreamSubject.streams()).that(target);
return Truth.assertThat(target);
}

public static IntStreamSubject assertThat(@Nullable IntStream target) {
return assertAbout(IntStreamSubject.intStreams()).that(target);
return Truth.assertThat(target);
}

public static LongStreamSubject assertThat(@Nullable LongStream target) {
return assertAbout(LongStreamSubject.longStreams()).that(target);
return Truth.assertThat(target);
}

@GwtIncompatible
@J2ObjCIncompatible
@J2ktIncompatible
public static PathSubject assertThat(@Nullable Path target) {
return assertAbout(PathSubject.paths()).that(target);
return Truth.assertThat(target);
}

private Truth8() {}
Expand Down

0 comments on commit 59e7a50

Please sign in to comment.