Hi,
I've done some work in this area.

I don't know the context in which you are working, but my sense is that your approach is not correct.

In step 2, an object is being instantiated via newInstance, but then in step 3 it is being thrown away immediately for garbage collection, because its only reference, o1, is being pointed to the 'getValue' return, which is a different object.

(a couple of minutes later)

This can't possibly work, you're risking a class cast exception.

'getValue' returns an concrete child of the abstract CSpValue class.  It is what it is, CSpString, CSpInteger, whatever, you can't assume that it will be one particular class, nor can you force it to be your desired class.  All you can do is use conversion methods that operate on CSpValue to give you an object of a particular concrete class, be it CSpString, String or whatever.

If you want a guaranteed CSpString, you would have to do something like this (stripping away your abstraction):

CSpString o1 = new CSpString(cspSltTest.getValue(iRowIndex, COLUMN_NAME).stringValue());

May I suggest that you ditch CSpValue as much as you can in any abstracted code that you write.  It will go away, or at least you will be able to avoid it, some day.  I work exclusively with Object.  For the time being, I convert Object->CSpValue before executing ND 'setValue' methods, and CSpValue->Object after executing ND 'getValue' methods.

Immediately below are some static utility conversion routines that people might find handy.

-- Curt Springer, Team ND

//note:  HMSLog argument can be removed from method arguments, it's just our convention for writing log messages
  writeLog statements can be converted to CSpLog.send statements, note that first argument can not be 'this'
  because the methods are static.

    /**
* This utility function converts non-proprietary java Objects into
* equivalent
* NetDynamics CSpValues:
* <BR><BR>
* If the passed Object is a CSpValue, it is returned 'as is'.
* If the passed Object is null, a CSpNull is returned
    * <BR><BR>
* The following java Objects are converted:
    * <br>
* <BR>String -> CSpString
* <BR>Integer -> CSpInteger
* <BR>Double -> CSpDouble
* <BR>Float -> CSpFloat
* <BR>Long -> CSpLong
* <BR>Boolean -> CSpBoolean
* <BR>Date -> CSpDatetime
* <BR>Calendar -> CSpDatetime
* <BR>Vector -> CSpVector (getCSpValue will also be applied to each member)
* <br>Hashtable -> CSpHashTable (getCSpValue will also be applied to each member)
* <BR>HMSNull -> CSpNull
* <BR><BR>
* If any other Object is passed in, an Exception will be thrown.
    *
* @param caller  handle to calling object for logging (because this is static)
    * @param  value - The java Object to be converted
* @return  CSpValue - The equivalent CSpValue.
* @exception Exception
    *
    */
    //*****************************************************************************
public static CSpValue getCSpValue(HMSLog caller, Object value)
throws Exception
//********************************************************************
//* Get value from buffer.  Return it as a CSpValue.
//********************************************************************
{
try
{
if (value instanceof CSpValue)
{
return((CSpValue) value); //return 'as is'
} //if (value instanceof CSpValue)
if (value instanceof String)
{
return(new CSpString((String) value));
} //if (value instanceof String)

if (value instanceof Integer)
{
return(new CSpInteger(((Integer) value).intValue()));
} //if (value instanceof Integer)

if (value instanceof Double)
{
return(new CSpDouble(((Double) value).doubleValue()));
} //if (value instanceof Double)

if (value instanceof Float)
{
return(new CSpFloat(((Float) value).floatValue()));
} //if (value instanceof Float)

if (value instanceof Long)
{
return(new CSpLong(((Long) value).longValue()));
} //if (value instanceof Long)

if (value instanceof Boolean)
{
return(new CSpBoolean(((Boolean) value).booleanValue()));
} //if (value instanceof Boolean)

if (value instanceof Date)
{
    //this now deprecated:
return(new CSpDatetime((Date) value));
} //if (value instanceof Date)

if (value instanceof Calendar)
{
return(new CSpDatetime((Calendar) value));
} //if (value instanceof Calendar)

if ((value == null)|(value instanceof HMSNull))
{
return(new CSpNull());
} //((value == null)|(value instanceof HMSNull))

if (value instanceof Vector)
{
CSpVector v = new CSpVector();
//now convert the contents into CSpValues:
for (Enumeration e= ((Vector) value).elements();e.hasMoreElements();)
{
  Object nextvalue = e.nextElement();
  v.put(getCSpValue(caller, nextvalue));

}//for (Enumeration e= ((Vector) value).elements();e.hasMoreElements;)
return(v);
} //if (value instanceof Vector)

if (value instanceof Hashtable)
{
CSpHashTable h = new CSpHashTable();
//now convert the contents into CSpValues:
Enumeration k= ((Hashtable) value).keys();
for (Enumeration e= ((Hashtable) value).elements();e.hasMoreElements();)
{
  Object key = k.nextElement();
  Object nextvalue = e.nextElement();
  h.put(key.toString(), getCSpValue(caller, nextvalue));

}//for (Enumeration e= ((Hashtable) value).elements();e.hasMoreElements;)
return(h);
} //if (value instanceof Hashtable)

throw (new Exception()); //fall-through

} //try
catch (Exception ex)
{
caller.writeLog(caller,"getCSpValue(): " +ex);
throw(ex);
} //catch
} //public static CSpValue getCSpValue(HMSLog caller, Object value)
//throws Exception

    //*****************************************************************************
    /**
* If the passed Object is not a CSpValue, it will be returned 'as is', except
* that for Vectors, convertCSpValue will be applied recursively to each
* entry that is a CSpValue.
*
* If it is a CSpValue, it will be converted into an equivalent generic java object
*
* @param caller  handle to calling object for logging (because this is static)
    * @param  value - The java Object to be checked
* @return  Object - The generic java object.
* @exception Exception
    *
    */
    //*****************************************************************************
public static Object getGenericJavaObject(HMSLog caller, Object value)
throws Exception
{
try
{
if (value instanceof CSpValue)
{
return convertCSpValue(caller, (CSpValue) value);
} // if (value instanceof CSpValue)

else if (value instanceof Vector)
{
Vector valuevector = (Vector) value;
for (int i = 0; i<valuevector.size(); i++)
{
Object nextobject = valuevector.elementAt(i);
if (nextobject instanceof CSpValue)
{
Object genericobject = convertCSpValue(caller, (CSpValue) nextobject);
valuevector.setElementAt(genericobject, i);
}//if (nextobject instanceof CSpValue())
}//for (int i = 0; i<vector.size(); i++)

return(value);

}//else if (value instanceof Vector)

else
{
return(value);
} //else


} //try

catch (Exception ex)
{
caller.writeLog(caller," getGenericJavaObect(): " +ex);
throw(ex);
} //catch
} // public static Object getGenericJavaObject(HMSLog caller, Object value)
//throws Exception

//*****************************************************************************
    /**
* This utility function converts NetDynamics CSpValues into
* equivalent non-proprietary java Objects:
* <BR><BR>
* If the passed CSpValue is a CSpNull, the return will be null.
    * <BR><BR>
* The following CSpValues are converted:
    * <br>
* <BR>CSpString -> String
* <BR>CSpInteger -> Integer
* <BR>CSpDouble -> Double
* <BR>CSpFloat -> Float
* <BR>CSpLong -> Long
* <BR>CSpBoolean -> Boolean
* <BR>CSpDatetime -> Datetime
* <BR>CSpVector -> Vector (convertCSpValue will be executed on each member)
* <br>CSpHashTable ->Hashtable (convertCSpValue will be executed on each member)
* <BR><BR>
* If any other CSpValue is passed in, an Exception will be thrown.
    *
* @param caller  handle to calling object for logging (because this is static)
    * @param  value - The CSpValue to be converted
* @return  Object - The equivalent generic java Object.
* @exception Exception
    *
    */
    //*****************************************************************************
public static Object convertCSpValue(HMSLog caller, CSpValue value)
throws Exception
{
try
{
//note this would be more efficient if the CSpValue 'getType' or whatever were used
if(value == null)
{
return(value);
} // if(value == null)
if (value instanceof CSpString)
{
return(((CSpString) value).toString());
} //if (value instanceof CSpString)

if (value instanceof CSpInteger)
{
return(new Integer(((CSpInteger) value).intValue()));
} //if (value instanceof CSpInteger)

if (value instanceof CSpDouble)
{
return(new Double(((CSpDouble) value).doubleValue()));
} //if (value instanceof CSpDouble)

if (value instanceof CSpFloat)
{
return(new Float(((CSpFloat) value).floatValue()));
} //if (value instanceof CSpFloat)

if (value instanceof CSpLong)
{
return(new Long(((CSpLong) value).longValue()));
} //if (value instanceof CSpLong)

if (value instanceof CSpBoolean)
{
return(new Boolean(((CSpBoolean) value).booleanValue()));
} //if (value instanceof CSpBoolean)

if (value instanceof CSpDatetime)
{
return(((CSpDatetime) value).getCalendar());
} //if (value instanceof CSpDatetime)

if (value instanceof CSpNull)
{
return(null);
} //if (value instanceof CSpNull)

if (value instanceof CSpVector)
{
Vector v = new Vector();
for (Enumeration e = ((CSpVector) value).elements();e.hasMoreElements();)
{
CSpValue nextvalue = (CSpValue) e.nextElement();
v.addElement(convertCSpValue(caller, nextvalue));
}//for (Enumeration e = ((CSpVector) value).elements();e.hasMoreElements();)
return(v);
} //if (value instanceof CSpVector)

if (value instanceof CSpHashTable)
{
Hashtable h = new Hashtable();
Enumeration k = ((CSpHashTable) value).keys();
for (Enumeration e = ((CSpHashTable) value).elements();e.hasMoreElements();)
{
Object key = k.nextElement();
CSpValue nextvalue = (CSpValue) e.nextElement();
h.put(key, convertCSpValue(caller, nextvalue));
}//for (Enumeration e = ((CSpVector) value).elements();e.hasMoreElements();)
return(h);
} //if (value instanceof CSpHashTable)

caller.writeLog(caller,"convertCSpValue():  Unknown CSpValue type!!!");
throw(new Exception()); //for undefined CSpValues
} //try

catch (Exception ex)
{
caller.writeLog(caller,"convertCSpValue(): " +ex);
throw(ex);
} //catch
} //public static Object convertCSpValue(HMSLog caller, CSpValue value)
// throws Exception






At 07:36 PM 5/18/99 -0400, Mathew, Aby wrote:
>Hi Rajesh,
>
>A little curious about what you are trying to do.
>In the code:
> 1. Class cls = Class.forName(strClassType);
> ����� 2.�� Object o1 = cls.newInstance();
> ����� 3.�� o1 = cspSltTest.getValue(iRowIndex,
>COLUMN_NAME);
>
> I see that the assignment to o1 in line2 is being overridden in
>line3. Did you miss some line of code?
>Anyway, out of curiosity I tried this code without line 3. I put it into a
>Hashtable and pulled it out later as an empty CSpString without any probelm
>(and also confirmed that I got a CSpString using instanceof.)
>
>I think the problem lies elsewhere.
>
>Regards,
>
>Aby
>
>
>> -----Original Message-----
>> From: Lakshmanamurthy, Rajesh [SMTP:[EMAIL PROTECTED]]
>> Sent: Tuesday, May 18, 1999 6:31 PM
>> To: [EMAIL PROTECTED]
>> Cc: [EMAIL PROTECTED]
>> Subject: [ND] Problem with reflection & dynamic object creation
>>
>> Hello NetDyners,
>>
>> I am trying to use the java.lang.reflection to dynamically instance
>> a class object based on a variable, contains the name of the class.
>> as shown. The strClassName can be anything, spider.session.CSpString,
>> spider.session.CSpInteger, etc.
>>
>>
>> cspSltTest�� -is a dataObject.
>> strClassName - "spider.session.CSpString"
>>
>>
>> Class cls = Class.forName(strClassType);
>> ������������� Object o1 = cls.newInstance();
>> ������������� o1 = cspSltTest.getValue(iRowIndex, COLUMN_NAME);
>>
>> & try using it like say,
>>
>> cspH1.put(strKey, (CSpValue) o1)
>>
>> where cspH1 is a CSpHashTable.
>>
>> This throws the following error.( shown at the end)
>>
>> The error message says: Local class not compatible
>>
>> Any help or suggestions will be greatly appreciated.
>>
>> Thanks,
>> Raj
>>
>>
>> CSpFileBased.deserializeObject: Unexpected Exception
>> (java.io.InvalidClassException: DIAMONDWEB.NDDiamondPage; Local class not
>>�� compatible: stream classdesc serialVersionUID=-8717881427890931675 local
>> class serialVersionUID=-2504985847882855499) caught. Top of
>>���������� stack: |java.io.InvalidClassException: DIAMONDWEB.NDDiamondPage;
>> Local class not compatible: stream classdesc
>>� serialVersionUID=-8717881427890931675 local class
>> serialVersionUID=-2504985847882855499 at
>> java.lang.Throwable.<init>(Compiled
>> Code) at
>>����������������� java.lang.Exception.<init>(Compiled Code) at
>> java.io.IOException.<init>(IOException.java:45) at
>>��� java.io.ObjectStreamException.<init>(ObjectStreamException.java:30) at
>> java.io.InvalidClassException.<init>(InvalidClassException.java:49) at
>>��������� java.io.ObjectStreamClass.setClass(Compiled Code) at
>> java.io.ObjectInputStream.inputClassDescriptor(Compiled Code) at
>>����������� java.io.ObjectInputStream.readObject(Compiled Code) at
>> java.io.ObjectInputStream.readObject(Compiled Code) at
>>�������� java.io.ObjectInputStream.inputClassDescriptor(Compiled Code) at
>> java.io.ObjectInputStream.readObject(Compiled Code) at
>>����������� java.io.ObjectInputStream.readObject(Compiled Code) at
>> java.io.ObjectInputStream.inputObject(Compiled Code) at
>>����������� java.io.ObjectInputStream.readObject(Compiled Code) at
>> java.io.ObjectInputStream.readObject(Compiled Code) at
>>�������� spider.CSpFileBased.deserializeObject(Compiled Code) at
>> spider.CSpProject.deserializeAndInitAnObject(Compiled Code) at
>>������������� spider.CSpProject.getCommonPage(Compiled Code) at
>> spider.CSpider.getCommonPage(Compiled Code) at
>>������� spider.CSpider.getCommonPage(Compiled Code) at
>> spider.util.CSpCommonPageRef.resolveObjectReference(Compiled Code) at
>>����������� spider.CSpFileBased.resolveObjectRef(Compiled Code) at
>> spider.visual.CSpHtmlFrame.afterInit(Compiled Code) at
>>�������������� spider.CSpFileBased.afterInitList(Compiled Code) at
>> spider.visual.CSpVisual.afterInit(Compiled Code) at
>>�������� spider.visual.CSpCommonPage.afterInit(Compiled Code) at
>> spider.visual.CSpCommonHtmlPage.afterInit(Compiled Code) at
>>��������� s
>> _________________________________________________________________________
>>
>> For help in using, subscribing, and unsubscribing to the discussion
>> forums, please go to: http://www.netdynamics.com/support/visitdevfor.html
>>
>> For dire need help, email: [EMAIL PROTECTED]
>_________________________________________________________________________
>
>For help in using, subscribing, and unsubscribing to the discussion
>forums, please go to: http://www.netdynamics.com/support/visitdevfor.html
>
>For dire need help, email: [EMAIL PROTECTED]
> _________________________________________________________________________ For help in using, subscribing, and unsubscribing to the discussion forums, please go to: http://www.netdynamics.com/support/visitdevfor.html For dire need help, email: [EMAIL PROTECTED]

Reply via email to