From 5ac71b76b4b1b963bdc65ead1c39b36aaa00355d Mon Sep 17 00:00:00 2001 From: clay_shooter Date: Sun, 3 Dec 2006 14:53:49 +0000 Subject: [PATCH] SF 1607878 Variant getJavaDateRef() failes -- programmer error SF 1602188 stack overflow on toString() for object that can't be converted toJavaObject() -- toJavaObject did wrong thing when couldn't convert and couldn't convert basic types byRef() . That has been fixed. --- src/com/jacob/com/Variant.java | 110 ++++++++++++++--------- unittest/com/jacob/com/VariantTest.java | 114 ++++++++++++++++++++++++ 2 files changed, 184 insertions(+), 40 deletions(-) diff --git a/src/com/jacob/com/Variant.java b/src/com/jacob/com/Variant.java index 766e38e..81776e5 100644 --- a/src/com/jacob/com/Variant.java +++ b/src/com/jacob/com/Variant.java @@ -482,7 +482,7 @@ public class Variant extends JacobObject { if (windowsDate == 0){ return null; } else { - return DateUtilities.convertWindowsTimeToDate(getDate()); + return DateUtilities.convertWindowsTimeToDate(windowsDate); } } @@ -1320,7 +1320,10 @@ public class Variant extends JacobObject { private native void changeVariantType(short in); /** - * cover for native method so we can cover it. + * Cover for native method so we can cover it. + *

+ * This cannot convert an object to a byRef. + * It can convert from byref to not byref * @param in type to convert this variant too * @return Variant returns this same object so folks can change when replacing calls * toXXX() with changeType().getXXX() @@ -1664,7 +1667,8 @@ public class Variant extends JacobObject { * Convert a JACOB Variant value to a Java object * (type conversions). * provided in Sourceforge feature request 959381. - * + * A fix was done to handle byRef bug report 1607878. + *

* Unlike other toXXX() methods, it does not do a type conversion * except for special data types (it shouldn't do any!) * @@ -1677,7 +1681,7 @@ public class Variant extends JacobObject { short type = this.getvt(); //variant type - if (type >= Variant.VariantArray) { //array returned? + if ((type & Variant.VariantArray) == VariantArray) { //array returned? SafeArray array = null; type = (short) (type - Variant.VariantArray); array = this.toSafeArray(false); @@ -1689,58 +1693,84 @@ public class Variant extends JacobObject { case Variant.VariantNull : //1 break; case Variant.VariantShort : //2 - result = new Short(this.getShort()); - break; + result = new Short(this.getShort()); + break; + case Variant.VariantShort | Variant.VariantByref : //2 + result = new Short(this.getShortRef()); + break; case Variant.VariantInt : //3 - result = new Integer(this.getInt()); - break; + result = new Integer(this.getInt()); + break; + case Variant.VariantInt | Variant.VariantByref: //3 + result = new Integer(this.getIntRef()); + break; case Variant.VariantFloat : //4 - result = new Float(this.getFloat()); - break; + result = new Float(this.getFloat()); + break; + case Variant.VariantFloat | Variant.VariantByref: //4 + result = new Float(this.getFloatRef()); + break; case Variant.VariantDouble : //5 - result = new Double(this.getDouble()); - break; + result = new Double(this.getDouble()); + break; + case Variant.VariantDouble | Variant.VariantByref: //5 + result = new Double(this.getDoubleRef()); + break; case Variant.VariantCurrency : //6 - result = new Long(this.getCurrency()); - break; + result = new Long(this.getCurrency()); + break; + case Variant.VariantCurrency | Variant.VariantByref: //6 + result = new Long(this.getCurrencyRef()); + break; case Variant.VariantDate : //7 - result = this.getJavaDate(); - break; + result = this.getJavaDate(); + break; + case Variant.VariantDate | Variant.VariantByref : //7 + result = this.getJavaDateRef(); + break; case Variant.VariantString : //8 - result = this.getString(); - break; + result = this.getString(); + break; + case Variant.VariantString | Variant.VariantByref: //8 + result = this.getStringRef(); + break; case Variant.VariantDispatch : //9 - result = this.getDispatchRef(); - break; + result = this.getDispatchRef(); + break; case Variant.VariantError : //10 - result = new NotImplementedException("toJavaObject() Not implemented for VariantError"); - break; + result = new NotImplementedException("toJavaObject() Not implemented for VariantError"); + break; case Variant.VariantBoolean : //11 - result = new Boolean(this.getBoolean()); - break; + result = new Boolean(this.getBoolean()); + break; + case Variant.VariantBoolean | Variant.VariantByref: //11 + result = new Boolean(this.getBooleanRef()); + break; case Variant.VariantVariant : //12 - result = new NotImplementedException("toJavaObject() Not implemented for VariantVariant"); - break; + result = new NotImplementedException("toJavaObject() Not implemented for VariantVariant"); + break; case Variant.VariantObject : //13 - result = new NotImplementedException("toJavaObject() Not implemented for VariantObject"); - break; + result = new NotImplementedException("toJavaObject() Not implemented for VariantObject"); + break; case Variant.VariantByte : //17 - result = new Byte(this.getByte()); - //result = new IllegalStateException("toJavaObject() Not implemented for VariantByte"); - break; + result = new Byte(this.getByte()); + break; + case Variant.VariantByte | Variant.VariantByref: //17 + result = new Byte(this.getByteRef()); + break; case Variant.VariantTypeMask : //4095 - result = new NotImplementedException("toJavaObject() Not implemented for VariantTypeMask"); - break; + result = new NotImplementedException("toJavaObject() Not implemented for VariantTypeMask"); + break; case Variant.VariantArray : //8192 - result = new NotImplementedException("toJavaObject() Not implemented for VariantArray"); - break; + result = new NotImplementedException("toJavaObject() Not implemented for VariantArray"); + break; case Variant.VariantByref : //16384 - result = new NotImplementedException("toJavaObject() Not implemented for VariantByref"); - break; + result = new NotImplementedException("toJavaObject() Not implemented for VariantByref"); + break; default : - result = new NotImplementedException("Unknown return type: " + type); - result = this; - break; + result = new NotImplementedException("Unknown return type: " + type); + // there was a "return result" here that caused defect 1602118 so it was removed + break; }//switch (type) if (result instanceof JacobException) { diff --git a/unittest/com/jacob/com/VariantTest.java b/unittest/com/jacob/com/VariantTest.java index bef8fbf..6232629 100644 --- a/unittest/com/jacob/com/VariantTest.java +++ b/unittest/com/jacob/com/VariantTest.java @@ -20,6 +20,8 @@ class VariantTest { testJig.testSafeReleaseConstant(); testJig.testSafeReleaseString(); testJig.testObjectIsAConstant(); + testJig.testSomeChangeVT(); + testJig.testByRefToJavaObject(); System.out.println("Testing Complete"); } @@ -32,6 +34,118 @@ class VariantTest { } + /** + * This verifies that toJavaObject() works for all of the + * main data types when they exist as a byRef version. + *

+ * It compares the toJavaObject() for a byref against the + * toJavaObject() for the regular. + * + */ + private void testByRefToJavaObject(){ + Variant v = null; + Variant vByRef = null; + + v = new Variant(new Float(53.3),false); + vByRef = new Variant(new Float(53.3),true); + if (!v.toJavaObject().equals(vByRef.toJavaObject())){ + System.out.println(v.toString() + " could not make type " + + v.getvt() +" and "+ vByRef.getvt() + +" java objects come out the same"); + } + v = new Variant(new Double(53.3),false); + vByRef = new Variant(new Double(53.3),true); + if (!v.toJavaObject().equals(vByRef.toJavaObject())){ + System.out.println(v.toString() + " could not make type " + + v.getvt() +" and "+ vByRef.getvt() + +" java objects come out the same"); + } + + v = new Variant(new Boolean(true),false); + vByRef = new Variant(new Boolean(true),true); + if (!v.toJavaObject().equals(vByRef.toJavaObject())){ + System.out.println(v.toString() + " could not make type " + + v.getvt() +" and "+ vByRef.getvt() + +" java objects come out the same"); + } + + v = new Variant(new Integer(53),false); + vByRef = new Variant(new Integer(53),true); + if (!v.toJavaObject().equals(vByRef.toJavaObject())){ + System.out.println(v.toString() + " could not make type " + + v.getvt() +" and "+ vByRef.getvt() + +" java objects come out the same"); + } + + v = new Variant(new Short((short)53),false); + vByRef = new Variant(new Short((short)53),true); + if (!v.toJavaObject().equals(vByRef.toJavaObject())){ + System.out.println(v.toString() + " could not make type " + + v.getvt() +" and "+ vByRef.getvt() + +" java objects come out the same"); + } + + v = new Variant("53.33",false); + vByRef = new Variant("53.33",true); + if (!v.toJavaObject().equals(vByRef.toJavaObject())){ + System.out.println(v.toString() + " could not make type " + + v.getvt() +" and "+ vByRef.getvt() + +" java objects come out the same"); + } + + Date now = new Date(); + v = new Variant(now,false); + vByRef = new Variant(now,true); + if (!v.toJavaObject().equals(vByRef.toJavaObject())){ + System.out.println(v.toString() + " could not make type " + + v.getvt() +" and "+ vByRef.getvt() + +" java objects come out the same"); + } + + // need to do currency also + } + + /** + * see what happens when we conver to by ref + * + */ + private void testSomeChangeVT(){ + Variant v; + // the code shows e shouldn't need to use a returned Variant but the test says we do + Variant vConverted; + v = new Variant(53.3); + short originalVT = v.getvt(); + short modifier; + + modifier = Variant.VariantShort; + vConverted = v.changeType(modifier); + if (vConverted.getvt() != modifier){ + System.out.println("Failed to change Variant "+originalVT + + " using mask "+modifier + + " resulted in "+vConverted.getvt() + ); + } + + modifier = Variant.VariantString; + vConverted = v.changeType(modifier); + if (vConverted.getvt() != modifier){ + System.out.println("Failed to change Variant "+originalVT + + " using mask "+modifier + + " resulted in "+vConverted.getvt() + ); + } + + // can't convert to byref! + modifier = Variant.VariantByref | Variant.VariantShort; + vConverted = v.changeType(modifier); + if (vConverted.getvt() == modifier){ + System.out.println("Should not have been able to change Variant "+originalVT + + " using mask "+modifier + + " resulted in "+vConverted.getvt() + ); + } + } + /** * make sure variant with no backing store works. *