1 /*
   2  * Copyright (c) 2006, 2014, 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 package test.java.awt.regtesthelpers;
  25 
  26 import java.awt.peer.FramePeer;
  27 import java.lang.reflect.Constructor;
  28 import java.lang.reflect.Field;
  29 import java.lang.reflect.InvocationTargetException;
  30 import java.lang.reflect.Method;
  31 import java.awt.Toolkit;
  32 import java.awt.Frame;
  33 
  34 
  35 
  36 /**
  37    Class with static methods using internal/proprietary API by necessity.
  38 */
  39 public 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 = (FramePeer) embedder.getPeer();
  49         System.out.println("frame's peer = " + frame_peer);
  50         if ("sun.awt.windows.WToolkit".equals(tk.getClass().getName())) {
  51             Class comp_peer_class =
  52                 Class.forName("sun.awt.windows.WComponentPeer");
  53             System.out.println("comp peer class = " + comp_peer_class);
  54             Field hwnd_field = comp_peer_class.getDeclaredField("hwnd");
  55             hwnd_field.setAccessible(true);
  56             System.out.println("hwnd_field =" + hwnd_field);
  57             long hwnd = hwnd_field.getLong(frame_peer);
  58             System.out.println("hwnd = " + hwnd);
  59 
  60             Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame");
  61             Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE});
  62             return (Frame) constructor.newInstance (new Object[] {hwnd});
  63         } else if ("sun.awt.X11.XToolkit".equals(tk.getClass().getName())) {
  64             Class x_base_window_class = Class.forName("sun.awt.X11.XBaseWindow");
  65             System.out.println("x_base_window_class = " + x_base_window_class);
  66             Method get_window = x_base_window_class.getMethod("getWindow", new Class[0]);
  67             System.out.println("get_window = " + get_window);
  68             long window = (Long) get_window.invoke(frame_peer, new Object[0]);
  69             System.out.println("window = " + window);
  70             Class clazz = Class.forName("sun.awt.X11.XEmbeddedFrame");
  71             Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE, Boolean.TYPE});
  72             return (Frame) constructor.newInstance (new Object[] {window, true});
  73         }
  74 
  75         throw new RuntimeException("Unexpected toolkit - " + tk);
  76     }
  77 }