Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Focus/RequestOnCompWithNullParent/RequestOnCompWithNullParent1.java
41152 views
1
/*
2
* Copyright (c) 2006, 2016, 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
@key headful
27
@bug 6418028
28
@author oleg.sukhodolsky: area=awt.focus
29
@library ../../regtesthelpers
30
@modules java.desktop/java.awt.peer
31
java.desktop/sun.awt
32
java.desktop/java.awt:open
33
@build Util
34
@run main RequestOnCompWithNullParent1
35
*/
36
37
import java.awt.*;
38
import java.awt.event.*;
39
import java.awt.peer.ButtonPeer;
40
import java.awt.peer.ComponentPeer;
41
import java.lang.reflect.Field;
42
import java.lang.reflect.InvocationHandler;
43
import java.lang.reflect.InvocationTargetException;
44
import java.lang.reflect.Method;
45
import java.lang.reflect.Proxy;
46
47
import sun.awt.AWTAccessor;
48
49
public class RequestOnCompWithNullParent1 {
50
51
public static void main(final String[] args) throws Exception {
52
Frame frame = new Frame("test for 6418028");
53
try {
54
test(frame);
55
} finally {
56
frame.dispose();
57
}
58
}
59
60
private static void test(final Frame frame) throws Exception {
61
frame.setLayout(new FlowLayout());
62
Button btn1 = new Button("Button1");
63
frame.add(btn1);
64
TestButton btn2 = new TestButton("Button2");
65
frame.add(btn2);
66
frame.pack();
67
frame.addWindowListener(new WindowAdapter() {
68
@Override
69
public void windowClosing(WindowEvent we) {
70
we.getWindow().dispose();
71
}
72
});
73
frame.setVisible(true);
74
75
new Robot().waitForIdle();
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 = AWTAccessor.getComponentAccessor().getPeer(this);
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 void setPeer(final ComponentPeer newPeer) {
123
try {
124
Field peer_field = Component.class.getDeclaredField("peer");
125
peer_field.setAccessible(true);
126
peer_field.set(this, newPeer);
127
} catch (IllegalArgumentException ex) {
128
throw new Error("Test error.", ex);
129
} catch (SecurityException ex) {
130
throw new Error("Test error.", ex);
131
} catch (IllegalAccessException ex) {
132
throw new Error("Test error.", ex);
133
} catch (NoSuchFieldException ex) {
134
throw new Error("Test error.", ex);
135
}
136
}
137
138
public void restorePeer() {
139
if (origPeer != null) {
140
setPeer(origPeer);
141
proxiedPeer = null;
142
}
143
}
144
}
145
146