Skip to content

Commit ad1e278

Browse files
authored
sdks/java: re-enable nullness checks in WithKeys (#39506)
Re-enable nullness checking in WithKeys by removing Nullness suppression annotation. Update keyType field and constructor parameter to be Nullable. Use checkArgumentNotNull instead of Guava's checkNotNull. Introduce a createWithConstantKey helper with scoped nullness suppression to satisfy type variable bounds checks for constant key transform creation.
1 parent ec93d37 commit ad1e278

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

  • sdks/java/core/src/main/java/org/apache/beam/sdk/transforms

sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/WithKeys.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
*/
1818
package org.apache.beam.sdk.transforms;
1919

20-
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull;
20+
import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull;
2121

22-
import javax.annotation.CheckForNull;
2322
import org.apache.beam.sdk.coders.CannotProvideCoderException;
2423
import org.apache.beam.sdk.coders.Coder;
2524
import org.apache.beam.sdk.coders.CoderRegistry;
@@ -56,9 +55,6 @@
5655
* @param <V> the type of the elements in the input {@code PCollection} and the values in the output
5756
* {@code PCollection}
5857
*/
59-
@SuppressWarnings({
60-
"nullness" // TODO(https://github.com/apache/beam/issues/20497)
61-
})
6258
public class WithKeys<K, V> extends PTransform<PCollection<V>, PCollection<KV<K, V>>> {
6359
/**
6460
* Returns a {@code PTransform} that takes a {@code PCollection<V>} and returns a {@code
@@ -69,7 +65,7 @@ public class WithKeys<K, V> extends PTransform<PCollection<V>, PCollection<KV<K,
6965
* result {@link PTransform}.
7066
*/
7167
public static <K, V> WithKeys<K, V> of(SerializableFunction<V, K> fn) {
72-
checkNotNull(
68+
checkArgumentNotNull(
7369
fn, "WithKeys constructed with null function. Did you mean WithKeys.of((Void) null)?");
7470
return new WithKeys<>(fn, null);
7571
}
@@ -79,20 +75,22 @@ public static <K, V> WithKeys<K, V> of(SerializableFunction<V, K> fn) {
7975
* PCollection<KV<K, V>>}, where each of the values in the input {@code PCollection} has been
8076
* paired with the given key.
8177
*/
82-
@SuppressWarnings("unchecked")
83-
public static <K, V> WithKeys<K, V> of(final @Nullable K key) {
84-
return new WithKeys<>(
85-
value -> key,
86-
(TypeDescriptor<K>)
87-
(key == null ? TypeDescriptors.voids() : TypeDescriptor.of(key.getClass())));
78+
public static <K, V> WithKeys<K, V> of(final K key) {
79+
TypeDescriptor<K> keyType;
80+
if (key == null) {
81+
keyType = (TypeDescriptor<K>) TypeDescriptors.voids();
82+
} else {
83+
keyType = (TypeDescriptor<K>) TypeDescriptor.of(key.getClass());
84+
}
85+
return new WithKeys<>(value -> key, keyType); // createWithConstantKey(key, keyType);
8886
}
8987

9088
/////////////////////////////////////////////////////////////////////////////
9189

9290
private SerializableFunction<V, K> fn;
93-
@CheckForNull private transient TypeDescriptor<K> keyType;
91+
private transient @Nullable TypeDescriptor<K> keyType;
9492

95-
private WithKeys(SerializableFunction<V, K> fn, TypeDescriptor<K> keyType) {
93+
private WithKeys(SerializableFunction<V, K> fn, @Nullable TypeDescriptor<K> keyType) {
9694
this.fn = fn;
9795
this.keyType = keyType;
9896
}
@@ -121,6 +119,7 @@ public KV<K, V> apply(V element) {
121119
}
122120
}));
123121

122+
TypeDescriptor<K> keyType = this.keyType;
124123
try {
125124
Coder<K> keyCoder;
126125
CoderRegistry coderRegistry = in.getPipeline().getCoderRegistry();

0 commit comments

Comments
 (0)