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