From fa66c599403c61f3c3b260d5987ea17c624d13f2 Mon Sep 17 00:00:00 2001 From: clay_shooter Date: Sun, 18 Feb 2007 16:11:38 +0000 Subject: [PATCH] SF 1662887 protect Dispatch static methods --- docs/ReleaseNotes.html | 281 +++++++++--------- docs/UsingJacob.html | 15 +- src/com/jacob/com/Dispatch.java | 106 ++++++- .../jacob/com/DispatchValidDispatchTest.java | 31 ++ 4 files changed, 280 insertions(+), 153 deletions(-) create mode 100644 unittest/com/jacob/com/DispatchValidDispatchTest.java diff --git a/docs/ReleaseNotes.html b/docs/ReleaseNotes.html index cfa2edd..4b7c681 100644 --- a/docs/ReleaseNotes.html +++ b/docs/ReleaseNotes.html @@ -13,31 +13,42 @@

Tracked Changes

- +
- - + + - - + + + + + + - - + + - - + + + + + +
Bugs
1651926 ActiveXComponent: getPropertyAsString should call getString() instead of toString()1651926 ActiveXComponent: getPropertyAsString should call getString() instead of toString()
  1504120 Microsoft 2003 Server crashes. Server crashes on MS 2003 Server have been reported over time. + Some users have had say that Jacob must be built on MS2003 Server to run reliably. + The Jacob distribution on Sourceforge is built on Windows XP SP2.
  
Patches
    
Feature Requests
  1662887Dispatch static methods should throw runtime exceptions when + null is passed in for the Dispatch object and when the Dispatch object is in an invalid state.
  
@@ -51,42 +62,42 @@ Bug fix release Bugs - 1602188 - Variant toString() causes stack overflow for byRef() Variants + 1602188 + Variant toString() causes stack overflow for byRef() Variants (This show stopper defect forced the 1.11.1 release) - 1611487 - Variant toJavaObject() doesn't work for byRef Variables and + 1611487 + Variant toJavaObject() doesn't work for byRef Variables and returned the wrong value for unrecognized Variant types. - 1607878 - Variant getJavaDateRef() fails. + 1607878 + Variant getJavaDateRef() fails. -   -   +   +   Patches - SF1603631 - Concurrent modification of ROT causes VM crashes. Access + SF1603631 + Concurrent modification of ROT causes VM crashes. Access to the ROT has been sychronized. No performance impact analysis has been done -   -   +   +   Feature Requests -   -   +   +   @@ -125,61 +136,61 @@ Bug fix release Bugs - 1550604 - Build process died with confusing error if version not set in properties file(pre1) + 1550604 + Build process died with confusing error if version not set in properties file(pre1) - 1511033 - Fix array index out of bounds problem due to coding error (pre1) + 1511033 + Fix array index out of bounds problem due to coding error (pre1) - 1570270 - ~Event method in EventProxy may unhook java thread from VM. + 1570270 + ~Event method in EventProxy may unhook java thread from VM. Can get JNI error because unhooking listner detatched Java VM thread (pre1) - 1538011 - toString() non compliant with java standards. The toString() method + 1538011 + toString() non compliant with java standards. The toString() method converted the underlying data to a string and it shouldn't. This caused a rethinking of all toXXX() methods other than toDispatch(). Most of the toXXX() methods have now been deprecated and should be replaced with getXXX() methods. (pre1) - 1478162 - Variant does not warn user if methods called after released. + 1478162 + Variant does not warn user if methods called after released. All putXXX() and getXXX() methods now check to see if they've been released prior to calling the JNI code. toXXX() methods are deprecated but protected in the same way.(pre1) -   -   +   +   Patches - SF1493647 - Support command line parameter dll location specification. + SF1493647 + Support command line parameter dll location specification. Applets and other tools can now specificy the dll location that is fed to a System.load() rather than System.loadLibrary for the situation where the app can't write the dll to a library path directory.(pre1) -   -   +   +   Feature Requests - 1580993 - Modify the build process to support 64 bit dll construction(pre2) + 1580993 + Modify the build process to support 64 bit dll construction(pre2) - 1550628 - Moved all LoadLibrary requests into LibraryLoader. Classes not subclassed + 1550628 + Moved all LoadLibrary requests into LibraryLoader. Classes not subclassed off of JacobObject make calls to a static method on LibraryLoader to make sure DLL is loaded(pre1) @@ -205,23 +216,23 @@ Bug fix release Bugs - 1474474 - Static constants in the Variant class can no longer have SafeRelease() called on them. + 1474474 + Static constants in the Variant class can no longer have SafeRelease() called on them. - 1477793 - obj2variant should accept java.util.Date the same way the Variant constructor does + 1477793 + obj2variant should accept java.util.Date the same way the Variant constructor does -   -   +   +   Patches -   -   +   +   Feature Requests @@ -275,73 +286,73 @@ Bug fix release Bugs - 1340233 - Null Program Id in Dispatch Constructor does bad things to VM(pre1) + 1340233 + Null Program Id in Dispatch Constructor does bad things to VM(pre1) - 1341763 - Removed Variant serializable interface because it is not + 1341763 + Removed Variant serializable interface because it is not actually serializable on 2000/xp(pre1) - 1435215 - Incorrect memory release in SafeArray.cpp (pre4) + 1435215 + Incorrect memory release in SafeArray.cpp (pre4) - 1224219 - Memory leak in SafeArray.GetString() + 1224219 + Memory leak in SafeArray.GetString() Olivier Laurent, Software AG Luxembourg and David Pierron, Software AG Luxembourg (pre3) - 1224219 - Change from UTF to UNICODE SafeArray.fromStringArray() + 1224219 + Change from UTF to UNICODE SafeArray.fromStringArray() Olivier Laurent, Software AG Luxembourg and David Pierron, Software AG Luxembourg (pre3) -   -   +   +   Patches - 1386454 - Return values from event callbacks (pre3) + 1386454 + Return values from event callbacks (pre3) - 1394001 - Missing variable initialization (pre3) + 1394001 + Missing variable initialization (pre3) - 1208570 - Support Excel and other objects events(pre1) + 1208570 + Support Excel and other objects events(pre1) -   -   +   +   Feature Requests - 1241037 - Support NULL VT_DISPATCH.(pre2) + 1241037 + Support NULL VT_DISPATCH.(pre2) - 1169851 - Support of VB's Nothing.(pre2) + 1169851 + Support of VB's Nothing.(pre2) - 1185167 - Provide methods to connect to running instance.(pre1) + 1185167 + Provide methods to connect to running instance.(pre1) - 959381 - Auto variant to object conversion method method added to Variant.(pre1) + 959381 + Auto variant to object conversion method method added to Variant.(pre1) - 1341779 - Variant should accept java.util.Date in Variant(Object) constructor(pre1) + 1341779 + Variant should accept java.util.Date in Variant(Object) constructor(pre1) @@ -386,38 +397,38 @@ Bug fix release Bugs - 1335897 - SafeArray() called toString() on objects when debug was turned on + 1335897 + SafeArray() called toString() on objects when debug was turned on -   -   +   +   Patches - 1314116 - putString UNICODE string length not detected + 1314116 + putString UNICODE string length not detected -   -   +   +   Feature Requests - 1264850 - Custom Event Dispatching + 1264850 + Custom Event Dispatching - 959382 - Time Conversion + 959382 + Time Conversion - 1313892 - Multi-Dimensional SafeArray + 1313892 + Multi-Dimensional SafeArray @@ -538,90 +549,90 @@ running this from inside of Eclipse Bugs - 1116101 - jacob-msg 0284 : Access Violation while garbage collecting + 1116101 + jacob-msg 0284 : Access Violation while garbage collecting - 1114159 - Problem with COM Error Trapping in JACOB DLL + 1114159 + Problem with COM Error Trapping in JACOB DLL - 1113610 - Bad error check in SafeArray.cpp + 1113610 + Bad error check in SafeArray.cpp - 1066698 - Minor Memory leak in Dispatch.cpp + 1066698 + Minor Memory leak in Dispatch.cpp - 1065533 - Problem with unicode conversion + 1065533 + Problem with unicode conversion - 1053871 - solution for memory leak in 1.7 + 1053871 + solution for memory leak in 1.7 - 1053870 - JACOB0msg 2019 - Safe Array + 1053870 + JACOB0msg 2019 - Safe Array - 1053866 - getHResult only returns 80020009 + 1053866 + getHResult only returns 80020009 - 960646 - But in SafeArray:: getBoolean for 2D arrays + 960646 + But in SafeArray:: getBoolean for 2D arrays -   -   +   +   Patches - 1115187 - EventCallbacks fail w/Variant ClassNotFoundException in JWS + 1115187 + EventCallbacks fail w/Variant ClassNotFoundException in JWS - 1105915 - Fix for event handling memory corruption + 1105915 + Fix for event handling memory corruption - 1090104 - Weak Reference in teh ROT + 1090104 + Weak Reference in teh ROT - 1068544 - in/out parameter support for event handlers + 1068544 + in/out parameter support for event handlers - 981540 - jre 1.4.2 fix as patch + 981540 + jre 1.4.2 fix as patch -   -   +   +   Feature Requests - 1049390 - static Version information + 1049390 + static Version information - 1049224 - Javadocs or at least script to generate it + 1049224 + Javadocs or at least script to generate it - 1049158 - API to get ProgId of ActiveXComponent + 1049158 + API to get ProgId of ActiveXComponent -   -   +   +   diff --git a/docs/UsingJacob.html b/docs/UsingJacob.html index f115ada..4477b01 100644 --- a/docs/UsingJacob.html +++ b/docs/UsingJacob.html @@ -10,14 +10,19 @@ Section not yet written. Jacob.jar relies on a DLL file that it loads off of the library path or classpath. The code is written so that the jacob.dll is only loaded one time per classloader. This works fine in the standard application but can cause problems if jacob.jar -is loaded from more than one class loader as in the situation where multiple -jacob dependent web applications run in the same web server. -Jacob is put in the WEB-INF/lib directory of each application's war file. +is loaded from more than one class loader. This can happen in the situation where multiple +jacob dependent web applications run in the same container like a web server or JWS runtime. +

+In the case of a web server, Jacob is normally put in the application specific WEB-INF/lib directory. +This is the "right" way to do it and works in most situations. +But, if Jacob is put in the WEB-INF/lib directory of each application's war file for more than +one application then a problem occurs. In this situation, the web server uses a different classloader for each applicaiton. This means that each application will attempt to load the jacob.dll and errors are generated. The only way around this at this time (1.11) is to put the jacob.jar in the common/lib because that classloader is inherited by all of the applicaitons -so the DLLs will only get loaded once. (This problem is described in SF 1645463) +so the DLLs will only get loaded once. This problem is described in SF 1645463 and +should be fixed in some future release, fix method and time not yet determined.


Jacob Command Line Settings

@@ -49,7 +54,7 @@ This library supports several different : The default is "no additional checking" Example: -XCheck:jni -Last Modified 10/2005 +Last Modified 2/2007 \ No newline at end of file diff --git a/src/com/jacob/com/Dispatch.java b/src/com/jacob/com/Dispatch.java index e10a25d..bde922d 100644 --- a/src/com/jacob/com/Dispatch.java +++ b/src/com/jacob/com/Dispatch.java @@ -110,9 +110,11 @@ public class Dispatch extends JacobObject * it or make it private */ public int m_pDispatch; + /** program Id passed in by ActiveX components in their constructor */ private String programId = null; + private static int NOT_ATTACHED = 0; /** * Dummy empty array used one doesn't have to be created on every @@ -133,9 +135,10 @@ public class Dispatch extends JacobObject /** * zero argument constructor that sets the dispatch pointer to 0 + * This is the only way to create a Dispatch without a value in the pointer field. */ public Dispatch() { - m_pDispatch = 0; + m_pDispatch = NOT_ATTACHED; } /** @@ -147,7 +150,7 @@ public class Dispatch extends JacobObject * This constructor always creates a new windows/program object * because it is based on the CoCreate() windows function. *

- * Fails silently if null is passed in as the program id + * @throws IllegalArgumentException if null is passed in as the program id *

* @param requestedProgramId */ @@ -160,7 +163,7 @@ public class Dispatch extends JacobObject "Dispatch(String) does not accept null or an empty string as a parameter"); } } - + /** * native call createInstance only used by the constructor with the same parm * type. This probably should be private. It is the wrapper for the @@ -270,7 +273,7 @@ public class Dispatch extends JacobObject //TAKE OVER THE IDispatch POINTER this.m_pDispatch = dispatchToBeDisplaced.m_pDispatch; //NULL OUT THE INPUT POINTER - dispatchToBeDisplaced.m_pDispatch = 0; + dispatchToBeDisplaced.m_pDispatch = NOT_ATTACHED; } /** @@ -297,15 +300,42 @@ public class Dispatch extends JacobObject public void safeRelease() { super.safeRelease(); - if (m_pDispatch != 0){ + if (isAttached()){ release(); - m_pDispatch = 0; + m_pDispatch = NOT_ATTACHED; } else { // looks like a double release if (isDebugEnabled()){debug(this.getClass().getName()+":"+this.hashCode()+" double release");} } } + /** + * + * @return true if there is an underlying windows dispatch object + */ + protected boolean isAttached(){ + if (m_pDispatch == NOT_ATTACHED){ + return false; + } else { + return true; + } + } + + /** + * @throws IllegalStateException if this dispatch isn't hooked up + * @throws IllegalArgumentException if null the dispatch under test is null + * @param theOneInQuestion dispatch being tested + */ + private static void throwIfUnattachedDispatch(Dispatch theOneInQuestion){ + if (theOneInQuestion == null) { + throw new IllegalArgumentException("Can't pass in null Dispatch object"); + } else if (theOneInQuestion.isAttached()){ + return; + } else { + throw new IllegalStateException("Dispatch not hooked to windows memory"); + } + } + /** * Map args based on msdn doc * This method relies on the variant constructor except for arrays @@ -411,6 +441,7 @@ public class Dispatch extends JacobObject */ public static void invokeSubv(Dispatch dispatchTarget, String name, int dispID, int lcid, int wFlags, Variant[] vArg, int[] uArgErr) { + throwIfUnattachedDispatch(dispatchTarget); invokev(dispatchTarget, name, dispID, lcid, wFlags, vArg, uArgErr); } @@ -423,6 +454,7 @@ public class Dispatch extends JacobObject */ public static void invokeSubv(Dispatch dispatchTarget, String name, int wFlags, Variant[] vArg, int[] uArgErr) { + throwIfUnattachedDispatch(dispatchTarget); invokev(dispatchTarget, name, 0, Dispatch.LOCALE_SYSTEM_DEFAULT, wFlags, vArg, uArgErr); } @@ -435,6 +467,7 @@ public class Dispatch extends JacobObject */ public static void invokeSubv(Dispatch dispatchTarget, int dispID, int wFlags, Variant[] vArg, int[] uArgErr) { + throwIfUnattachedDispatch(dispatchTarget); invokev(dispatchTarget, null, dispID, Dispatch.LOCALE_SYSTEM_DEFAULT, wFlags, vArg, uArgErr); } @@ -458,6 +491,7 @@ public class Dispatch extends JacobObject * @param args an array of argument objects */ public static void callSubN(Dispatch dispatchTarget, String name, Object[] args) { + throwIfUnattachedDispatch(dispatchTarget); invokeSubv(dispatchTarget, name, Dispatch.Method | Dispatch.Get, obj2variant(args), new int[args.length]); } @@ -468,6 +502,7 @@ public class Dispatch extends JacobObject * @param args an array of argument objects */ public static void callSubN(Dispatch dispatchTarget, int dispID, Object[] args) { + throwIfUnattachedDispatch(dispatchTarget); invokeSubv(dispatchTarget, dispID, Dispatch.Method | Dispatch.Get, obj2variant(args), new int[args.length]); } @@ -519,6 +554,7 @@ public class Dispatch extends JacobObject * @return Variant returned by call */ public static Variant callN(Dispatch dispatchTarget, String name, Object[] args) { + throwIfUnattachedDispatch(dispatchTarget); return invokev(dispatchTarget, name, Dispatch.Method | Dispatch.Get, obj2variant(args), new int[args.length]); @@ -531,6 +567,7 @@ public class Dispatch extends JacobObject * @return Variant returned by call */ public static Variant callN(Dispatch dispatchTarget, int dispID, Object[] args) { + throwIfUnattachedDispatch(dispatchTarget); return invokev(dispatchTarget, dispID, Dispatch.Method | Dispatch.Get, obj2variant(args), new int[args.length]); @@ -548,6 +585,7 @@ public class Dispatch extends JacobObject */ public static Variant invoke(Dispatch dispatchTarget, String name, int dispID, int lcid, int wFlags, Object[] oArg, int[] uArgErr) { + throwIfUnattachedDispatch(dispatchTarget); return invokev(dispatchTarget, name, dispID, lcid, wFlags, obj2variant(oArg), uArgErr); } @@ -562,6 +600,7 @@ public class Dispatch extends JacobObject */ public static Variant invoke(Dispatch dispatchTarget, String name, int wFlags, Object[] oArg, int[] uArgErr) { + throwIfUnattachedDispatch(dispatchTarget); return invokev(dispatchTarget, name, wFlags, obj2variant(oArg), uArgErr); } @@ -575,6 +614,7 @@ public class Dispatch extends JacobObject */ public static Variant invoke(Dispatch dispatchTarget, int dispID, int wFlags, Object[] oArg, int[] uArgErr) { + throwIfUnattachedDispatch(dispatchTarget); return invokev(dispatchTarget, dispID, wFlags, obj2variant(oArg), uArgErr); } @@ -589,6 +629,7 @@ public class Dispatch extends JacobObject * @return Variant retuned by underlying callN */ public static Variant call(Dispatch dispatchTarget, String name) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, name, NO_VARIANT_ARGS); } @@ -599,6 +640,7 @@ public class Dispatch extends JacobObject * @return Variant retuned by underlying callN */ public static Variant call(Dispatch dispatchTarget, String name, Object a1) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, name, new Object[] { a1 }); } @@ -610,6 +652,7 @@ public class Dispatch extends JacobObject * @return Variant retuned by underlying callN */ public static Variant call(Dispatch dispatchTarget, String name, Object a1, Object a2) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, name, new Object[] { a1, a2 }); } @@ -623,6 +666,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, name, new Object[] { a1, a2, a3 }); } @@ -637,6 +681,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3, Object a4) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, name, new Object[] { a1, a2, a3, a4 }); } @@ -652,6 +697,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3, Object a4, Object a5) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, name, new Object[] { a1, a2, a3, a4, a5 }); } @@ -668,6 +714,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, name, new Object[] { a1, a2, a3, a4, a5, a6 }); } @@ -685,6 +732,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, name, new Object[] { a1, a2, a3, a4, a5, a6, a7 }); } @@ -703,6 +751,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7, Object a8) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, name, new Object[] { a1, a2, a3, a4, a5, a6, a7, a8 }); } @@ -713,6 +762,7 @@ public class Dispatch extends JacobObject * @return Variant retuned by underlying callN */ public static Variant call(Dispatch dispatchTarget, int dispid) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, dispid, NO_VARIANT_ARGS); } @@ -723,7 +773,8 @@ public class Dispatch extends JacobObject * @return Variant retuned by underlying callN */ public static Variant call(Dispatch dispatchTarget, int dispid, Object a1) { - return callN(dispatchTarget, dispid, new Object[] { a1 }); + throwIfUnattachedDispatch(dispatchTarget); + return callN(dispatchTarget, dispid, new Object[] { a1 }); } /** @@ -734,6 +785,7 @@ public class Dispatch extends JacobObject * @return Variant retuned by underlying callN */ public static Variant call(Dispatch dispatchTarget, int dispid, Object a1, Object a2) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, dispid, new Object[] { a1, a2 }); } @@ -747,6 +799,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, int dispid, Object a1, Object a2, Object a3) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, dispid, new Object[] { a1, a2, a3 }); } @@ -761,6 +814,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, int dispid, Object a1, Object a2, Object a3, Object a4) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, dispid, new Object[] { a1, a2, a3, a4 }); } @@ -776,6 +830,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, int dispid, Object a1, Object a2, Object a3, Object a4, Object a5) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, dispid, new Object[] { a1, a2, a3, a4, a5 }); } @@ -792,6 +847,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, int dispid, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, dispid, new Object[] { a1, a2, a3, a4, a5, a6 }); } @@ -809,6 +865,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, int dispid, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, dispid, new Object[] { a1, a2, a3, a4, a5, a6, a7 }); } @@ -827,6 +884,7 @@ public class Dispatch extends JacobObject */ public static Variant call(Dispatch dispatchTarget, int dispid, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7, Object a8) { + throwIfUnattachedDispatch(dispatchTarget); return callN(dispatchTarget, dispid, new Object[] { a1, a2, a3, a4, a5, a6, a7, a8 }); } @@ -841,6 +899,7 @@ public class Dispatch extends JacobObject * @param val */ public static void put(Dispatch dispatchTarget, String name, Object val) { + throwIfUnattachedDispatch(dispatchTarget); invoke(dispatchTarget, name, Dispatch.Put, new Object[] { val }, new int[1]); } @@ -850,6 +909,7 @@ public class Dispatch extends JacobObject * @param val */ public static void put(Dispatch dispatchTarget, int dispid, Object val) { + throwIfUnattachedDispatch(dispatchTarget); invoke(dispatchTarget, dispid, Dispatch.Put, new Object[] { val }, new int[1]); } @@ -881,8 +941,7 @@ public class Dispatch extends JacobObject */ public static Variant invokev(Dispatch dispatchTarget, String name, int wFlags, Variant[] vArg, int[] uArgErr) { - if (!(dispatchTarget instanceof Dispatch)) - throw new ClassCastException("Dispatch object expected"); + throwIfUnattachedDispatch(dispatchTarget); return invokev(dispatchTarget, name, 0, Dispatch.LOCALE_SYSTEM_DEFAULT, wFlags, vArg, uArgErr); } @@ -898,8 +957,7 @@ public class Dispatch extends JacobObject */ public static Variant invokev(Dispatch dispatchTarget, String name, int wFlags, Variant[] vArg, int[] uArgErr, int wFlagsEx) { - if (!(dispatchTarget instanceof Dispatch)) - throw new ClassCastException("Dispatch object expected"); + throwIfUnattachedDispatch(dispatchTarget); // do not implement IDispatchEx for now return invokev(dispatchTarget, name, 0, Dispatch.LOCALE_SYSTEM_DEFAULT, wFlags, vArg, uArgErr); @@ -915,8 +973,7 @@ public class Dispatch extends JacobObject */ public static Variant invokev(Dispatch dispatchTarget, int dispID, int wFlags, Variant[] vArg, int[] uArgErr) { - if (!(dispatchTarget instanceof Dispatch)) - throw new ClassCastException("Dispatch object expected"); + throwIfUnattachedDispatch(dispatchTarget); return invokev(dispatchTarget, null, dispID, Dispatch.LOCALE_SYSTEM_DEFAULT, wFlags, vArg, uArgErr); @@ -939,6 +996,7 @@ public class Dispatch extends JacobObject */ public static void invokeSub(Dispatch dispatchTarget, String name, int dispid, int lcid, int wFlags, Object[] oArg, int[] uArgErr) { + throwIfUnattachedDispatch(dispatchTarget); invokeSubv(dispatchTarget, name, dispid, lcid, wFlags, obj2variant(oArg), uArgErr); } @@ -955,6 +1013,7 @@ public class Dispatch extends JacobObject */ public static void invokeSub(Dispatch dispatchTarget, String name, int wFlags, Object[] oArg, int[] uArgErr) { + throwIfUnattachedDispatch(dispatchTarget); invokeSub(dispatchTarget, name, 0, Dispatch.LOCALE_SYSTEM_DEFAULT, wFlags, oArg, uArgErr); } @@ -967,6 +1026,7 @@ public class Dispatch extends JacobObject */ public static void invokeSub(Dispatch dispatchTarget, int dispid, int wFlags, Object[] oArg, int[] uArgErr) { + throwIfUnattachedDispatch(dispatchTarget); invokeSub(dispatchTarget, null, dispid, Dispatch.LOCALE_SYSTEM_DEFAULT, wFlags, oArg, uArgErr); } @@ -981,6 +1041,7 @@ public class Dispatch extends JacobObject * @param name */ public static void callSub(Dispatch dispatchTarget, String name) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, name, NO_OBJECT_ARGS); } @@ -991,6 +1052,7 @@ public class Dispatch extends JacobObject * @param a1 */ public static void callSub(Dispatch dispatchTarget, String name, Object a1) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, name, new Object[] { a1 }); } @@ -1002,6 +1064,7 @@ public class Dispatch extends JacobObject * @param a2 */ public static void callSub(Dispatch dispatchTarget, String name, Object a1, Object a2) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, name, new Object[] { a1, a2 }); } @@ -1015,6 +1078,7 @@ public class Dispatch extends JacobObject */ public static void callSub(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, name, new Object[] { a1, a2, a3 }); } @@ -1029,6 +1093,7 @@ public class Dispatch extends JacobObject */ public static void callSub(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3, Object a4) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, name, new Object[] { a1, a2, a3, a4 }); } @@ -1044,6 +1109,7 @@ public class Dispatch extends JacobObject */ public static void callSub(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3, Object a4, Object a5) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, name, new Object[] { a1, a2, a3, a4, a5 }); } @@ -1060,6 +1126,7 @@ public class Dispatch extends JacobObject */ public static void callSub(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, name, new Object[] { a1, a2, a3, a4, a5, a6 }); } @@ -1077,6 +1144,7 @@ public class Dispatch extends JacobObject */ public static void callSub(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, name, new Object[] { a1, a2, a3, a4, a5, a6, a7 }); } @@ -1095,6 +1163,7 @@ public class Dispatch extends JacobObject */ public static void callSub(Dispatch dispatchTarget, String name, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7, Object a8) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, name, new Object[] { a1, a2, a3, a4, a5, a6, a7, a8 }); } @@ -1104,6 +1173,7 @@ public class Dispatch extends JacobObject * @param dispid */ public static void callSub(Dispatch dispatchTarget, int dispid) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, dispid, NO_OBJECT_ARGS); } @@ -1114,6 +1184,7 @@ public class Dispatch extends JacobObject * @param a1 */ public static void callSub(Dispatch dispatchTarget, int dispid, Object a1) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, dispid, new Object[] { a1 }); } @@ -1125,6 +1196,7 @@ public class Dispatch extends JacobObject * @param a2 */ public static void callSub(Dispatch dispatchTarget, int dispid, Object a1, Object a2) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, dispid, new Object[] { a1, a2 }); } @@ -1152,6 +1224,7 @@ public class Dispatch extends JacobObject */ public static void callSub(Dispatch dispatchTarget, int dispid, Object a1, Object a2, Object a3, Object a4) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, dispid, new Object[] { a1, a2, a3, a4 }); } @@ -1167,6 +1240,7 @@ public class Dispatch extends JacobObject */ public static void callSub(Dispatch dispatchTarget, int dispid, Object a1, Object a2, Object a3, Object a4, Object a5) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, dispid, new Object[] { a1, a2, a3, a4, a5 }); } @@ -1183,6 +1257,7 @@ public class Dispatch extends JacobObject */ public static void callSub(Dispatch dispatchTarget, int dispid, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, dispid, new Object[] { a1, a2, a3, a4, a5, a6 }); } @@ -1200,6 +1275,7 @@ public class Dispatch extends JacobObject */ public static void callSub(Dispatch dispatchTarget, int dispid, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) { + throwIfUnattachedDispatch(dispatchTarget); callSubN(dispatchTarget, dispid, new Object[] { a1, a2, a3, a4, a5, a6, a7 }); } @@ -1232,6 +1308,7 @@ public class Dispatch extends JacobObject * @return Variant returned by the request for retrieval of parameter */ public static Variant get(Dispatch dispatchTarget, String name) { + throwIfUnattachedDispatch(dispatchTarget); return invokev(dispatchTarget, name, Dispatch.Get, NO_VARIANT_ARGS, NO_INT_ARGS); } @@ -1242,6 +1319,7 @@ public class Dispatch extends JacobObject * @return Variant returned by the request for retrieval of parameter */ public static Variant get(Dispatch dispatchTarget, int dispid) { + throwIfUnattachedDispatch(dispatchTarget); return invokev(dispatchTarget, dispid, Dispatch.Get, NO_VARIANT_ARGS, NO_INT_ARGS); } @@ -1256,6 +1334,7 @@ public class Dispatch extends JacobObject * @param val */ public static void putRef(Dispatch dispatchTarget, String name, Object val) { + throwIfUnattachedDispatch(dispatchTarget); invoke(dispatchTarget, name, Dispatch.PutRef, new Object[] { val }, new int[1]); } @@ -1266,6 +1345,7 @@ public class Dispatch extends JacobObject * @param val */ public static void putRef(Dispatch dispatchTarget, int dispid, Object val) { + throwIfUnattachedDispatch(dispatchTarget); invoke(dispatchTarget, dispid, Dispatch.PutRef, new Object[] { val }, new int[1]); } diff --git a/unittest/com/jacob/com/DispatchValidDispatchTest.java b/unittest/com/jacob/com/DispatchValidDispatchTest.java new file mode 100644 index 0000000..b1962a2 --- /dev/null +++ b/unittest/com/jacob/com/DispatchValidDispatchTest.java @@ -0,0 +1,31 @@ +package com.jacob.com; + +/** + * Test armoring of dispatch static methods + * May need to run with some command line options (including from inside Eclipse). + * If so, then try these + *

+ *      -Djava.library.path=d:/jacob/release/x86 
+ *      -Dcom.jacob.autogc=false 
+ *      -Dcom.jacob.debug=false 
+ *      -Xcheck:jni
+ *  
+ */ +public class DispatchValidDispatchTest { + + public static void main(String[] args) + { + try { + Dispatch.call(null, 0); + System.out.println("Failed to throw IllegalArgumentException"); + } catch (IllegalArgumentException iae){ + System.out.println("Caught correct IllegalArgumentException: "+iae); + } + try { + Dispatch foo = new Dispatch(); + Dispatch.call(foo, 0); + } catch (IllegalStateException ise){ + System.out.println("Caught correct IllegalStateException "+ise); + } + } +}