Skip to content

Commit

Permalink
SF 1607878 Variant getJavaDateRef() failes -- programmer error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
clay_shooter committed Dec 3, 2006
1 parent fc42eae commit d9b7eb9
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 40 deletions.
110 changes: 70 additions & 40 deletions jacob/src/com/jacob/com/Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public Date getJavaDateRef(){
if (windowsDate == 0){
return null;
} else {
return DateUtilities.convertWindowsTimeToDate(getDate());
return DateUtilities.convertWindowsTimeToDate(windowsDate);
}
}

Expand Down Expand Up @@ -1320,7 +1320,10 @@ public Variant[] getVariantArrayRef() {
private native void changeVariantType(short in);

/**
* cover for native method so we can cover it.
* Cover for native method so we can cover it.
* <p>
* 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()
Expand Down Expand Up @@ -1664,7 +1667,8 @@ public boolean isNull(){
* 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.
* <p>
* Unlike other toXXX() methods, it does not do a type conversion
* except for special data types (it shouldn't do any!)
*
Expand All @@ -1677,7 +1681,7 @@ protected Object toJavaObject() throws JacobException {

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);
Expand All @@ -1689,58 +1693,84 @@ protected Object toJavaObject() throws JacobException {
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) {
Expand Down
114 changes: 114 additions & 0 deletions jacob/unittest/com/jacob/com/VariantTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static void main(String[] args) {
testJig.testSafeReleaseConstant();
testJig.testSafeReleaseString();
testJig.testObjectIsAConstant();
testJig.testSomeChangeVT();
testJig.testByRefToJavaObject();
System.out.println("Testing Complete");

}
Expand All @@ -32,6 +34,118 @@ public VariantTest(){

}

/**
* This verifies that toJavaObject() works for all of the
* main data types when they exist as a byRef version.
* <p>
* 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.
*
Expand Down

0 comments on commit d9b7eb9

Please sign in to comment.