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
24package test.java.awt.regtesthelpers;
25
26import java.awt.peer.FramePeer;
27import java.lang.reflect.Constructor;
28import java.lang.reflect.Field;
29import java.lang.reflect.InvocationTargetException;
30import java.lang.reflect.Method;
31import java.awt.Toolkit;
32import java.awt.Frame;
33
34import sun.awt.AWTAccessor;
35
36/**
37   Class with static methods using internal/proprietary API by necessity.
38*/
39public final class UtilInternal {
40
41    private UtilInternal () {} // this is a helper class with static methods :)
42
43    public static Frame createEmbeddedFrame(final Frame embedder)
44        throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException,
45               InstantiationException, InvocationTargetException
46    {
47        Toolkit tk = Toolkit.getDefaultToolkit();
48        FramePeer frame_peer = AWTAccessor.getComponentAccessor()
49                                          .getPeer(embedder);
50        System.out.println("frame's peer = " + frame_peer);
51        if ("sun.awt.windows.WToolkit".equals(tk.getClass().getName())) {
52            java.awt.Helper.addExports("sun.awt.windows", UtilInternal.class.getModule());
53
54            Class comp_peer_class =
55                Class.forName("sun.awt.windows.WComponentPeer");
56            System.out.println("comp peer class = " + comp_peer_class);
57            Field hwnd_field = comp_peer_class.getDeclaredField("hwnd");
58            hwnd_field.setAccessible(true);
59            System.out.println("hwnd_field =" + hwnd_field);
60            long hwnd = hwnd_field.getLong(frame_peer);
61            System.out.println("hwnd = " + hwnd);
62
63            Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame");
64            Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE});
65            return (Frame) constructor.newInstance (new Object[] {hwnd});
66        } else if ("sun.awt.X11.XToolkit".equals(tk.getClass().getName())) {
67            java.awt.Helper.addExports("sun.awt.X11", UtilInternal.class.getModule());
68            Class x_base_window_class = Class.forName("sun.awt.X11.XBaseWindow");
69            Method get_window = x_base_window_class.getMethod("getWindow", new Class[0]);
70            System.out.println("get_window = " + get_window);
71            long window = (Long) get_window.invoke(frame_peer, new Object[0]);
72            System.out.println("window = " + window);
73            Class clazz = Class.forName("sun.awt.X11.XEmbeddedFrame");
74            Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE, Boolean.TYPE});
75            return (Frame) constructor.newInstance (new Object[] {window, true});
76        }
77
78        throw new RuntimeException("Unexpected toolkit - " + tk);
79    }
80}
81