Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Casting Vector/Hashtable Elements

2 views
Skip to first unread message

bl...@net.com

unread,
May 4, 2000, 3:00:00 AM5/4/00
to
I've searched the group here and Java's site and am still befuddled.

I know I can use instanceOf to find out what type a vector element is.
I'm trying to code a debug bean that can print out the elements of any
kind of hashtable or vector. I want to use overloading to make the
calls generic. E.g., I have a printItem with a hashtable signature,
vector signature, string signature, etc. So I want to be able to call
printItem with whatever type I've discivered in the vector or
hashtable I'm working on without having to do an instanceOf.

My question is, isn't there some way to use getClass() or
getClass().getName() to actually do the casting on the element I'm
looking at? Something like
printItem(((vector.get(i).getClass().getName()).vector.get(i)) to use
real time typing to call the appropriate overloaded method.
I get an "Invalid Type Expression" compile error whenever I try
anything like this. Am I tied to use a linear programming method like
instanceOf to do what I want to do with vectors and hashtables?

TIA,
Tracy

Patricia Shanahan

unread,
May 4, 2000, 3:00:00 AM5/4/00
to
There is a much more basic problem with this than the casting issue. The
type of the parameter is used only at compile time, in determining the
signature of the method. It is not considered at all at run time, so
even if you could cast at run time it would not change which method is
called.

Surely the solution to this problem, like most problems for which people
want run time casting, is reflection. You have a class with a range of
printItem methods. Get the Class object for that class, call that
printClass. Get the class object for the item from the vector using its
getClass() method, call that operandClass.

Now use printClass.getDeclaredMethod("printItem",new
Class[]{operandClass}) to get the Method object for the right printItem
method, and call it using the Method's invoke method.

Patricia

blues...@my-deja.com

unread,
May 4, 2000, 3:00:00 AM5/4/00
to
Patricia,

Thanks for the info. Very enlightening. However, I'm still having a
problem. Here's the code I've got so far:

private void runTimePrintItem(Object runTimeValue) {
Object[] tmpArray = {runTimeValue};

try{
printMethod = printClass.getDeclaredMethod("printItem", new
Class[]{operandClass});
System.out.println(operandClass.toString());
System.out.println(tmpArray[0].toString());
System.out.println(printMethod.toString());
Object tmp = printMethod.invoke(printClass.getDeclaredMethod
("printItem", new

Class[]{operandClass}), tmpArray);
}
catch(NoSuchMethodException e) {
outputLine("ERROR!!!");
outputLine("No such method available - " + operandClass);
}
catch(IllegalAccessException e) {
outputLine("ERROR!!!");
outputLine("No method type available (1) to print class " +
operandClass);
}
catch(InvocationTargetException e) {
outputLine("ERROR!!!");
outputLine("No method type available (2) to print class " +
operandClass);
}
}

private void printItem(String debugString) {
System.out.println("FOUND STRING");
}

where 'runTimeValue' is the String 'Test1'.

Here's my output:

class java.lang.String
Test1
private void com.id.PSTech.Debug.printItem(java.lang.String)
object is not an instance of declaring class
java.lang.IllegalArgumentException: object is not an instance of
declaring class

at java.lang.reflect.Method.invoke(Native Method)
at com.id.PSTech.Debug.runTimePrintItem(Debug.java:72)
at com.id.PSTech.Debug.printItem(Debug.java, Compiled Code)
at DebugTestScript.main(DebugTestScript.java:20)

where the first 3 lines are the System.out's fro the code. Everything
looks fine, but I don't under stand what I'm being told. It looks like
the method signature matches, but 'invoke' is throwing up on it. Do you
see anything obvious that I'm doing wrong?

TIA,
Tracy


In article <3911003A...@acm.org>,


Sent via Deja.com http://www.deja.com/
Before you buy.

blues...@my-deja.com

unread,
May 4, 2000, 3:00:00 AM5/4/00
to
Reposting this article since I don't think the first one's body made it
to the server.

Patricia Shanahan

unread,
May 5, 2000, 3:00:00 AM5/5/00
to
I think you have an error in how you are using invoke. Reread its
documentation in e.g.
http://java.sun.com/products/jdk/1.2/docs/api/index.html focussing on
what the first parameter is supposed to be.

As a matter of style, I would not try to do quite so much in one
statement. It would be easier to see what is going on if you separated
things out a bit and used more temporaries.

Patricia


blues...@my-deja.com wrote:
>
> Patricia,
>
> Thanks for the info. Very enlightening. However, I'm still having a
> problem. Here's the code I've got so far:

...

0 new messages