1 /* 2 * Copyright (c) 2006, 2015, 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 /* 25 @test 26 @bug 6418028 27 @author oleg.sukhodolsky: area=awt.focus 28 @library ../../regtesthelpers 29 @modules java.desktop/java.awt.peer 30 java.desktop/sun.awt 31 @build Util 32 @run main RequestOnCompWithNullParent1 33 */ 34 35 import java.awt.*; 36 import java.awt.event.*; 37 import java.awt.peer.ButtonPeer; 38 import java.awt.peer.ComponentPeer; 39 import java.lang.reflect.Field; 40 import java.lang.reflect.InvocationHandler; 41 import java.lang.reflect.InvocationTargetException; 42 import java.lang.reflect.Method; 43 import java.lang.reflect.Proxy; 44 45 import sun.awt.AWTAccessor; 46 47 public class RequestOnCompWithNullParent1 { 48 49 public static void main(final String[] args) throws Exception { 50 Frame frame = new Frame("test for 6418028"); 51 try { 52 test(frame); 53 } finally { 54 frame.dispose(); 55 } 56 } 57 58 private static void test(final Frame frame) throws Exception { 59 frame.setLayout(new FlowLayout()); 60 Button btn1 = new Button("Button1"); 61 frame.add(btn1); 62 TestButton btn2 = new TestButton("Button2"); 63 frame.add(btn2); 64 frame.pack(); 65 frame.addWindowListener(new WindowAdapter() { 66 @Override 67 public void windowClosing(WindowEvent we) { 68 we.getWindow().dispose(); 69 } 70 }); 71 frame.setVisible(true); 72 73 new Robot().waitForIdle(); 74 75 btn2.instrumentPeer(); 76 btn2.requestFocusInWindow(); 77 btn2.restorePeer(); 78 } 79 } 80 81 class TestButton extends Button { 82 ButtonPeer origPeer; 83 ButtonPeer proxiedPeer; 84 85 /** Creates a new instance of TestButton */ 86 TestButton(String text) { 87 super(text); 88 } 89 90 public void instrumentPeer() { 91 origPeer = AWTAccessor.getComponentAccessor().getPeer(this); 92 93 InvocationHandler handler = new InvocationHandler() { 94 public Object invoke(Object proxy, Method method, Object[] args) { 95 if (method.getName().equals("requestFocus")) { 96 Container parent = getParent(); 97 parent.remove(TestButton.this); 98 System.err.println("parent = " + parent); 99 System.err.println("target = " + TestButton.this); 100 System.err.println("new parent = " + TestButton.this.getParent()); 101 } 102 Object ret = null; 103 try { 104 ret = method.invoke(origPeer, args); 105 } catch (IllegalAccessException iae) { 106 throw new Error("Test error.", iae); 107 } catch (InvocationTargetException ita) { 108 throw new Error("Test error.", ita); 109 } 110 return ret; 111 } 112 }; 113 114 proxiedPeer = (ButtonPeer) Proxy.newProxyInstance( 115 ButtonPeer.class.getClassLoader(), 116 new Class[] {ButtonPeer.class}, handler); 117 setPeer(proxiedPeer); 118 } 119 120 private void setPeer(final ComponentPeer newPeer) { 121 try { 122 Field peer_field = Component.class.getDeclaredField("peer"); 123 peer_field.setAccessible(true); 124 peer_field.set(this, newPeer); 125 } catch (IllegalArgumentException ex) { 126 throw new Error("Test error.", ex); 127 } catch (SecurityException ex) { 128 throw new Error("Test error.", ex); 129 } catch (IllegalAccessException ex) { 130 throw new Error("Test error.", ex); 131 } catch (NoSuchFieldException ex) { 132 throw new Error("Test error.", ex); 133 } 134 } 135 136 public void restorePeer() { 137 if (origPeer != null) { 138 setPeer(origPeer); 139 proxiedPeer = null; 140 } 141 } 142 }