|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.event.ActionListener; |
| 25 | +import java.awt.event.ActionEvent; |
| 26 | +import java.awt.AWTEventMulticaster; |
| 27 | + |
| 28 | +/* |
| 29 | + * @test |
| 30 | + * @bug 8342782 |
| 31 | + * @summary Tests large AWTEventMulticasters for StackOverflowErrors |
| 32 | + * @run main LargeAWTEventMulticasterTest |
| 33 | + */ |
| 34 | +public class LargeAWTEventMulticasterTest { |
| 35 | + |
| 36 | + /** |
| 37 | + * This is an empty ActionListener that also has a numeric index. |
| 38 | + */ |
| 39 | + static class IndexedActionListener implements ActionListener { |
| 40 | + private final int index; |
| 41 | + |
| 42 | + public IndexedActionListener(int index) { |
| 43 | + this.index = index; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void actionPerformed(ActionEvent e) { |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + public int getIndex() { |
| 52 | + return index; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String toString() { |
| 57 | + return Integer.toString(index); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static void main(String[] args) { |
| 62 | + int maxA = 0; |
| 63 | + try { |
| 64 | + for (int a = 1; a < 200_000; a *= 2) { |
| 65 | + maxA = a; |
| 66 | + testAddingActionListener(a); |
| 67 | + } |
| 68 | + } finally { |
| 69 | + System.out.println("maximum a = " + maxA); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static void testAddingActionListener(int numberOfListeners) { |
| 74 | + // step 1: create the large AWTEventMulticaster |
| 75 | + ActionListener l = null; |
| 76 | + for (int a = 0; a < numberOfListeners; a++) { |
| 77 | + l = AWTEventMulticaster.add(l, new IndexedActionListener(a)); |
| 78 | + } |
| 79 | + |
| 80 | + // Prior to 8342782 we could CREATE a large AWTEventMulticaster, but we couldn't |
| 81 | + // always interact with it. |
| 82 | + |
| 83 | + // step 2: dispatch an event |
| 84 | + // Here we're making sure we don't get a StackOverflowError when we traverse the tree: |
| 85 | + l.actionPerformed(null); |
| 86 | + |
| 87 | + // step 3: make sure getListeners() returns elements in the correct order |
| 88 | + // The resolution for 8342782 introduced a `rebalance` method; we want to |
| 89 | + // double-check that the rebalanced tree preserves the appropriate order. |
| 90 | + IndexedActionListener[] array = AWTEventMulticaster.getListeners(l, IndexedActionListener.class); |
| 91 | + for (int b = 0; b < array.length; b++) { |
| 92 | + if (b != array[b].getIndex()) |
| 93 | + throw new Error("the listeners are in the wrong order. " + b + " != " + array[b].getIndex()); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments