Replace java.util.Stack with java.util.Deque

Proprietary

From the Javadoc of Stack:

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class.

This recipe declares a Stack that a method creates and keeps in a local variable as a Deque and creates an ArrayDeque instead, when program analysis shows that no other thread or method can reach it (it is not returned, stored in a field, passed to another method or constructor, or captured by a lambda that may run later or on another thread), and that it is used only as a stack: push, pop, peek, isEmpty, empty (which becomes isEmpty), size, clear and contains. ArrayDeque adds to the other end with add, iterates and prints from the top down, and rejects null, so a stack that is iterated, printed, used through Vector's methods, or given a value that may be null is left as it is, and so is one in a method that catches EmptyStackException. Popping an empty ArrayDeque throws NoSuchElementException instead, and peeking one returns null. Fields, and Kotlin and other non-Java source, are left as they are.

io.moderne.staticanalysis.ReplaceStackWithDeque
mod run . --recipe=io.moderne.staticanalysis.ReplaceStackWithDeque

Install it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.