Skip to content

Commit e5177d0

Browse files
yorickhenningGoogle Java Core Libraries
authored andcommitted
Added @InlineMe annotations to all deprecated collection factory functions that directly delegate to JDK constructors.
RELNOTES=Added @InlineMe annotations to all deprecated collection factory functions that directly delegate to JDK constructors. PiperOrigin-RevId: 928611528
1 parent f5495f6 commit e5177d0

8 files changed

Lines changed: 314 additions & 72 deletions

File tree

android/guava/src/com/google/common/collect/Lists.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ private Lists() {}
7777
*
7878
* <p><b>Note:</b> if mutability is not required, use {@link ImmutableList#of()} instead.
7979
*
80-
* <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated. Instead,
81-
* use the {@code ArrayList} {@linkplain ArrayList#ArrayList() constructor} directly, taking
80+
* @deprecated Use the {@code ArrayList} {@linkplain ArrayList#ArrayList() constructor} directly, taking
8281
* advantage of <a
8382
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
8483
* syntax</a>.
8584
*/
8685
@SuppressWarnings("NonApiType") // acts as a direct substitute for a constructor call
86+
@Deprecated
87+
@InlineMe(replacement = "new ArrayList<>()", imports = "java.util.ArrayList")
8788
public static <E extends @Nullable Object> ArrayList<E> newArrayList() {
8889
return new ArrayList<>();
8990
}
@@ -213,8 +214,7 @@ static int computeArrayListCapacity(int arraySize) {
213214
* outperform {@code LinkedList} except in certain rare and specific situations. Unless you have
214215
* spent a lot of time benchmarking your specific needs, use one of those instead.
215216
*
216-
* <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated. Instead,
217-
* use the {@code LinkedList} {@linkplain LinkedList#LinkedList() constructor} directly, taking
217+
* @deprecated Use the {@code LinkedList} {@linkplain LinkedList#LinkedList() constructor} directly, taking
218218
* advantage of <a
219219
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
220220
* syntax</a>.
@@ -223,6 +223,8 @@ static int computeArrayListCapacity(int arraySize) {
223223
"NonApiType", // acts as a direct substitute for a constructor call
224224
"JdkObsolete", // We recommend against this method but need to keep it for compatibility.
225225
})
226+
@Deprecated
227+
@InlineMe(replacement = "new LinkedList<>()", imports = "java.util.LinkedList")
226228
public static <E extends @Nullable Object> LinkedList<E> newLinkedList() {
227229
return new LinkedList<>();
228230
}
@@ -260,12 +262,17 @@ static int computeArrayListCapacity(int arraySize) {
260262
*
261263
* @return a new, empty {@code CopyOnWriteArrayList}
262264
* @since 12.0
265+
* @deprecated Use the {@code CopyOnWriteArrayList} {@linkplain
266+
* CopyOnWriteArrayList#CopyOnWriteArrayList() constructor} directly, taking advantage of <a
267+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
268+
* syntax</a>.
263269
*/
264270
@J2ktIncompatible
265271
@GwtIncompatible // CopyOnWriteArrayList
266272
@InlineMe(
267273
replacement = "new CopyOnWriteArrayList<>()",
268274
imports = {"java.util.concurrent.CopyOnWriteArrayList"})
275+
@Deprecated
269276
public static <E extends @Nullable Object> CopyOnWriteArrayList<E> newCopyOnWriteArrayList() {
270277
return new CopyOnWriteArrayList<>();
271278
}

android/guava/src/com/google/common/collect/Maps.java

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.google.common.collect.MapDifference.ValueDifference;
5454
import com.google.common.primitives.Ints;
5555
import com.google.errorprone.annotations.CanIgnoreReturnValue;
56+
import com.google.errorprone.annotations.InlineMe;
5657
import com.google.errorprone.annotations.concurrent.LazyInit;
5758
import com.google.j2objc.annotations.RetainedWith;
5859
import com.google.j2objc.annotations.Weak;
@@ -216,14 +217,15 @@ public static <K extends Enum<K>, V> ImmutableMap<K, V> immutableEnumMap(
216217
*
217218
* <p><b>Note:</b> if {@code K} is an {@code enum} type, use {@link #newEnumMap} instead.
218219
*
219-
* <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated. Instead,
220-
* use the {@code HashMap} constructor directly, taking advantage of <a
221-
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
222-
* syntax</a>.
223-
*
224220
* @return a new, empty {@code HashMap}
221+
* @deprecated Use the {@code HashMap} {@linkplain
222+
* HashMap#HashMap() constructor} directly, taking advantage of <a
223+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
224+
* syntax</a>.
225225
*/
226226
@SuppressWarnings("NonApiType") // acts as a direct substitute for a constructor call
227+
@Deprecated
228+
@InlineMe(replacement = "new HashMap<>()", imports = "java.util.HashMap")
227229
public static <K extends @Nullable Object, V extends @Nullable Object>
228230
HashMap<K, V> newHashMap() {
229231
return new HashMap<>();
@@ -299,14 +301,15 @@ static int capacity(int expectedSize) {
299301
*
300302
* <p><b>Note:</b> if mutability is not required, use {@link ImmutableMap#of()} instead.
301303
*
302-
* <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated. Instead,
303-
* use the {@code LinkedHashMap} constructor directly, taking advantage of <a
304-
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
305-
* syntax</a>.
306-
*
307304
* @return a new, empty {@code LinkedHashMap}
305+
* @deprecated Use the {@code LinkedHashMap} {@linkplain
306+
* LinkedHashMap#LinkedHashMap() constructor} directly, taking advantage of <a
307+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
308+
* syntax</a>.
308309
*/
309310
@SuppressWarnings("NonApiType") // acts as a direct substitute for a constructor call
311+
@Deprecated
312+
@InlineMe(replacement = "new LinkedHashMap<>()", imports = "java.util.LinkedHashMap")
310313
public static <K extends @Nullable Object, V extends @Nullable Object>
311314
LinkedHashMap<K, V> newLinkedHashMap() {
312315
return new LinkedHashMap<>();
@@ -353,13 +356,16 @@ LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) {
353356
/**
354357
* Creates a new empty {@link ConcurrentHashMap} instance.
355358
*
356-
* <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated. Instead,
357-
* use the {@code ConcurrentHashMap} constructor directly, taking advantage of <a
358-
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
359-
* syntax</a>.
360-
*
361359
* @since 3.0
360+
* @deprecated Use the {@code ConcurrentHashMap} {@linkplain
361+
* ConcurrentHashMap#ConcurrentHashMap() constructor} directly, taking advantage of <a
362+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
363+
* syntax</a>.
362364
*/
365+
@Deprecated
366+
@InlineMe(
367+
replacement = "new ConcurrentHashMap<>()",
368+
imports = "java.util.concurrent.ConcurrentHashMap")
363369
public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {
364370
return new ConcurrentHashMap<>();
365371
}
@@ -438,7 +444,16 @@ TreeMap<K, V> newTreeMap(@Nullable Comparator<C> comparator) {
438444
*
439445
* @param type the key type for this map
440446
* @return a new, empty {@code EnumMap}
447+
* @deprecated Use the {@code EnumMap} {@linkplain EnumMap#EnumMap(Class) constructor} directly,
448+
* taking advantage of <a
449+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
450+
* syntax</a>.
441451
*/
452+
@Deprecated
453+
@InlineMe(
454+
replacement = "new EnumMap<>(checkNotNull(type))",
455+
imports = "java.util.EnumMap",
456+
staticImports = "com.google.common.base.Preconditions.checkNotNull")
442457
public static <K extends Enum<K>, V extends @Nullable Object> EnumMap<K, V> newEnumMap(
443458
Class<K> type) {
444459
return new EnumMap<>(checkNotNull(type));
@@ -465,13 +480,14 @@ TreeMap<K, V> newTreeMap(@Nullable Comparator<C> comparator) {
465480
/**
466481
* Creates an {@code IdentityHashMap} instance.
467482
*
468-
* <p><b>Note:</b> this method is now unnecessary and should be treated as deprecated. Instead,
469-
* use the {@code IdentityHashMap} constructor directly, taking advantage of <a
470-
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
471-
* syntax</a>.
472-
*
473483
* @return a new, empty {@code IdentityHashMap}
484+
* @deprecated Use the {@code IdentityHashMap} {@linkplain
485+
* IdentityHashMap#IdentityHashMap() constructor} directly, taking advantage of <a
486+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
487+
* syntax</a>.
474488
*/
489+
@Deprecated
490+
@InlineMe(replacement = "new IdentityHashMap<>()", imports = "java.util.IdentityHashMap")
475491
public static <K extends @Nullable Object, V extends @Nullable Object>
476492
IdentityHashMap<K, V> newIdentityHashMap() {
477493
return new IdentityHashMap<>();

android/guava/src/com/google/common/collect/Queues.java

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.annotations.J2ktIncompatible;
2323
import com.google.common.base.Preconditions;
2424
import com.google.errorprone.annotations.CanIgnoreReturnValue;
25+
import com.google.errorprone.annotations.InlineMe;
2526
import java.time.Duration;
2627
import java.util.ArrayDeque;
2728
import java.util.Collection;
@@ -54,9 +55,18 @@ private Queues() {}
5455
/**
5556
* Creates an empty {@code ArrayBlockingQueue} with the given (fixed) capacity and nonfair access
5657
* policy.
58+
*
59+
* @deprecated Use the {@code ArrayBlockingQueue} {@linkplain
60+
* ArrayBlockingQueue#ArrayBlockingQueue(int) constructor} directly, taking advantage of <a
61+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
62+
* syntax</a>.
5763
*/
5864
@J2ktIncompatible
5965
@GwtIncompatible // ArrayBlockingQueue
66+
@Deprecated
67+
@InlineMe(
68+
replacement = "new ArrayBlockingQueue<>(capacity)",
69+
imports = "java.util.concurrent.ArrayBlockingQueue")
6070
public static <E> ArrayBlockingQueue<E> newArrayBlockingQueue(int capacity) {
6171
return new ArrayBlockingQueue<>(capacity);
6272
}
@@ -67,7 +77,13 @@ public static <E> ArrayBlockingQueue<E> newArrayBlockingQueue(int capacity) {
6777
* Creates an empty {@code ArrayDeque}.
6878
*
6979
* @since 12.0
80+
* @deprecated Use the {@code ArrayDeque} {@linkplain
81+
* ArrayDeque#ArrayDeque() constructor} directly, taking advantage of <a
82+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
83+
* syntax</a>.
7084
*/
85+
@Deprecated
86+
@InlineMe(replacement = "new ArrayDeque<>()", imports = "java.util.ArrayDeque")
7187
public static <E> ArrayDeque<E> newArrayDeque() {
7288
return new ArrayDeque<>();
7389
}
@@ -89,9 +105,20 @@ public static <E> ArrayDeque<E> newArrayDeque(Iterable<? extends E> elements) {
89105

90106
// ConcurrentLinkedQueue
91107

92-
/** Creates an empty {@code ConcurrentLinkedQueue}. */
108+
/**
109+
* Creates an empty {@code ConcurrentLinkedQueue}.
110+
*
111+
* @deprecated Use the {@code ConcurrentLinkedQueue} {@linkplain
112+
* ConcurrentLinkedQueue#ConcurrentLinkedQueue() constructor} directly, taking advantage of <a
113+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
114+
* syntax</a>.
115+
*/
93116
@J2ktIncompatible
94117
@GwtIncompatible // ConcurrentLinkedQueue
118+
@Deprecated
119+
@InlineMe(
120+
replacement = "new ConcurrentLinkedQueue<>()",
121+
imports = "java.util.concurrent.ConcurrentLinkedQueue")
95122
public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue() {
96123
return new ConcurrentLinkedQueue<>();
97124
}
@@ -118,9 +145,17 @@ public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue(
118145
* Creates an empty {@code LinkedBlockingDeque} with a capacity of {@link Integer#MAX_VALUE}.
119146
*
120147
* @since 12.0
148+
* @deprecated Use the {@code LinkedBlockingDeque} {@linkplain
149+
* LinkedBlockingDeque#LinkedBlockingDeque() constructor} directly, taking advantage of <a
150+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
151+
* syntax</a>.
121152
*/
122153
@J2ktIncompatible
123154
@GwtIncompatible // LinkedBlockingDeque
155+
@Deprecated
156+
@InlineMe(
157+
replacement = "new LinkedBlockingDeque<>()",
158+
imports = "java.util.concurrent.LinkedBlockingDeque")
124159
public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque() {
125160
return new LinkedBlockingDeque<>();
126161
}
@@ -130,9 +165,17 @@ public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque() {
130165
*
131166
* @throws IllegalArgumentException if {@code capacity} is less than 1
132167
* @since 12.0
168+
* @deprecated Use the {@code LinkedBlockingDeque} {@linkplain
169+
* LinkedBlockingDeque#LinkedBlockingDeque(int) constructor} directly, taking advantage of <a
170+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
171+
* syntax</a>.
133172
*/
134173
@J2ktIncompatible
135174
@GwtIncompatible // LinkedBlockingDeque
175+
@Deprecated
176+
@InlineMe(
177+
replacement = "new LinkedBlockingDeque<>(capacity)",
178+
imports = "java.util.concurrent.LinkedBlockingDeque")
136179
public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque(int capacity) {
137180
return new LinkedBlockingDeque<>(capacity);
138181
}
@@ -157,9 +200,20 @@ public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque(Iterable<? exten
157200

158201
// LinkedBlockingQueue
159202

160-
/** Creates an empty {@code LinkedBlockingQueue} with a capacity of {@link Integer#MAX_VALUE}. */
203+
/**
204+
* Creates an empty {@code LinkedBlockingQueue} with a capacity of {@link Integer#MAX_VALUE}.
205+
*
206+
* @deprecated Use the {@code LinkedBlockingQueue} {@linkplain
207+
* LinkedBlockingQueue#LinkedBlockingQueue() constructor} directly, taking advantage of <a
208+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
209+
* syntax</a>.
210+
*/
161211
@J2ktIncompatible
162212
@GwtIncompatible // LinkedBlockingQueue
213+
@Deprecated
214+
@InlineMe(
215+
replacement = "new LinkedBlockingQueue<>()",
216+
imports = "java.util.concurrent.LinkedBlockingQueue")
163217
public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue() {
164218
return new LinkedBlockingQueue<>();
165219
}
@@ -168,9 +222,17 @@ public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue() {
168222
* Creates an empty {@code LinkedBlockingQueue} with the given (fixed) capacity.
169223
*
170224
* @throws IllegalArgumentException if {@code capacity} is less than 1
225+
* @deprecated Use the {@code LinkedBlockingQueue} {@linkplain
226+
* LinkedBlockingQueue#LinkedBlockingQueue(int) constructor} directly, taking advantage of <a
227+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
228+
* syntax</a>.
171229
*/
172230
@J2ktIncompatible
173231
@GwtIncompatible // LinkedBlockingQueue
232+
@Deprecated
233+
@InlineMe(
234+
replacement = "new LinkedBlockingQueue<>(capacity)",
235+
imports = "java.util.concurrent.LinkedBlockingQueue")
174236
public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(int capacity) {
175237
return new LinkedBlockingQueue<>(capacity);
176238
}
@@ -204,10 +266,18 @@ public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(Iterable<? exten
204266
*
205267
* @since 11.0 (but the bound of {@code E} was changed from {@code Object} to {@code Comparable}
206268
* in 15.0)
269+
* @deprecated Use the {@code PriorityBlockingQueue} {@linkplain
270+
* PriorityBlockingQueue#PriorityBlockingQueue() constructor} directly, taking advantage of <a
271+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
272+
* syntax</a>.
207273
*/
208274
@SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
209275
@J2ktIncompatible
210276
@GwtIncompatible // PriorityBlockingQueue
277+
@Deprecated
278+
@InlineMe(
279+
replacement = "new PriorityBlockingQueue<>()",
280+
imports = "java.util.concurrent.PriorityBlockingQueue")
211281
public static <E extends Comparable> PriorityBlockingQueue<E> newPriorityBlockingQueue() {
212282
return new PriorityBlockingQueue<>();
213283
}
@@ -242,8 +312,14 @@ public static <E extends Comparable> PriorityBlockingQueue<E> newPriorityBlockin
242312
*
243313
* @since 11.0 (but the bound of {@code E} was changed from {@code Object} to {@code Comparable}
244314
* in 15.0)
315+
* @deprecated Use the {@code PriorityQueue} {@linkplain
316+
* PriorityQueue#PriorityQueue() constructor} directly, taking advantage of <a
317+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
318+
* syntax</a>.
245319
*/
246320
@SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
321+
@Deprecated
322+
@InlineMe(replacement = "new PriorityQueue<>()", imports = "java.util.PriorityQueue")
247323
public static <E extends Comparable> PriorityQueue<E> newPriorityQueue() {
248324
return new PriorityQueue<>();
249325
}
@@ -270,9 +346,20 @@ public static <E extends Comparable> PriorityQueue<E> newPriorityQueue(
270346

271347
// SynchronousQueue
272348

273-
/** Creates an empty {@code SynchronousQueue} with nonfair access policy. */
349+
/**
350+
* Creates an empty {@code SynchronousQueue} with nonfair access policy.
351+
*
352+
* @deprecated Use the {@code SynchronousQueue} {@linkplain
353+
* SynchronousQueue#SynchronousQueue() constructor} directly, taking advantage of <a
354+
* href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation">"diamond"
355+
* syntax</a>.
356+
*/
274357
@J2ktIncompatible
275358
@GwtIncompatible // SynchronousQueue
359+
@Deprecated
360+
@InlineMe(
361+
replacement = "new SynchronousQueue<>()",
362+
imports = "java.util.concurrent.SynchronousQueue")
276363
public static <E> SynchronousQueue<E> newSynchronousQueue() {
277364
return new SynchronousQueue<>();
278365
}

0 commit comments

Comments
 (0)