class FieldIterator implements Iterator<PDField>
    {
        private final Queue<PDField> queue = new ArrayDeque<>();
        
        // PDFBOX-5044: to prevent recursion
        // must be COSDictionary and not PDField, because PDField is newly created each time
        private final Set<COSDictionary> set =
                Collections.newSetFromMap(new IdentityHashMap<>());

        private FieldIterator(PDAcroForm form)
        {
            List<PDField> fields = form.getFields();
            for (PDField field : fields)
            {
                enqueueKids(field);
            }
        }

        @Override
        public boolean hasNext()
        {
            return !queue.isEmpty();
        }

        @Override
        public PDField next()
        {
            if(!hasNext())
            {
                throw new NoSuchElementException();
            }
            
            return queue.poll();
        }

        @Override
        public void remove()
        {
            throw new UnsupportedOperationException();
        }
        
        private void enqueueKids(PDField node)
        {<FILL_FUNCTION_BODY>}
    }

            queue.add(node);
            set.add(node.getCOSObject());
            if (node instanceof PDNonTerminalField)
            {
                List<PDField> kids = ((PDNonTerminalField) node).getChildren();
                for (PDField kid : kids)
                {
                    if (set.contains(kid.getCOSObject()))
                    {
                        LOG.error(
                                "Child of field '{}' already exists elsewhere, ignored to avoid recursion",
                                node.getFullyQualifiedName());
                    }
                    else
                    {
                        enqueueKids(kid);
                    }
                }
            }
        
<no_super_class>