From 9fc60d57f6482c147aa544d710f4336b5f94a5b8 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 14 Sep 2009 06:15:04 -0700 Subject: NEWT: More elegant custom constructor type verification and setting by Window class impl. ; AWTWindow allow pure Container in cstr, ie an Applet --- src/newt/classes/com/sun/javafx/newt/Window.java | 59 +++++++++++++++- .../classes/com/sun/javafx/newt/awt/AWTWindow.java | 82 +++++++++++++--------- 2 files changed, 107 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/newt/classes/com/sun/javafx/newt/Window.java b/src/newt/classes/com/sun/javafx/newt/Window.java index 49cd49a4d..2b82cb887 100755 --- a/src/newt/classes/com/sun/javafx/newt/Window.java +++ b/src/newt/classes/com/sun/javafx/newt/Window.java @@ -40,6 +40,7 @@ import com.sun.nativewindow.impl.NWReflection; import java.util.ArrayList; import java.util.Iterator; +import java.lang.reflect.Method; public abstract class Window implements NativeWindow { @@ -102,7 +103,15 @@ public abstract class Window implements NativeWindow protected static Window create(String type, Object[] cstrArguments, Screen screen, Capabilities caps, boolean undecorated) { try { Class windowClass = getWindowClass(type); - Window window = (Window) NWReflection.createInstance( windowClass, cstrArguments ) ; + Class[] cstrArgumentTypes = getCustomConstructorArgumentTypes(windowClass); + if(null==cstrArgumentTypes) { + throw new NativeWindowException("WindowClass "+windowClass+" doesn't support custom arguments in constructor"); + } + int argsChecked = verifyConstructorArgumentTypes(cstrArgumentTypes, cstrArguments); + if ( argsChecked < cstrArguments.length ) { + throw new NativeWindowException("WindowClass "+windowClass+" constructor mismatch at argument #"+argsChecked+"; Constructor: "+getTypeStrList(cstrArgumentTypes)+", arguments: "+getArgsStrList(cstrArguments)); + } + Window window = (Window) NWReflection.createInstance( windowClass, cstrArgumentTypes, cstrArguments ) ; window.invalidate(); window.screen = screen; window.setUndecorated(undecorated); @@ -744,4 +753,52 @@ public abstract class Window implements NativeWindow l.exposed(e); } } + + // + // Reflection helper .. + // + + private static Class[] getCustomConstructorArgumentTypes(Class windowClass) { + Class[] argTypes = null; + try { + Method m = windowClass.getDeclaredMethod("getCustomConstructorArgumentTypes", new Class[] {}); + argTypes = (Class[]) m.invoke(null, null); + } catch (Throwable t) {} + return argTypes; + } + + private static int verifyConstructorArgumentTypes(Class[] types, Object[] args) { + if(types.length != args.length) { + return -1; + } + for(int i=0; i