Cast Object type....

I have a some Objects :

1) String className; which value will be name of some classes during runtime.

2)Object someObject;

How cast someObject to another type of class when I have only className object and not to lost someObject's referance.

Message was edited by:

tamri

[316 byte] By [tamria] at [2007-11-27 10:31:13]
# 1

My initial response was "fix your design so you don't have to do that", but if you can't do that, then use reflection.

hunter9000a at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 2

You can't. Java is not magic. You cannot just "convert" any type to any other type.

If it makes sense to cast, you do it at compile-time. If you don't know the types until runtime, your code can only invoke methods on the object by reflection anyway, in which case no casting is necessary

What is it you're actually trying to do?

georgemca at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 3

thank's replay, but how can I use reflection for this problem?

can you help me?

tamria at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 4

> You can't. Java is not magic. You cannot just

> "convert" any type to any other type.

>

> If it makes sense to cast, you do it at compile-time.

> If you don't know the types until runtime, your code

> can only invoke methods on the object by reflection

> anyway, in which case no casting is necessary

>

> What is it you're actually trying to do?

I assumed he meant that the object is actually of the type that the String says, but he only has an Object reference to it. I could be wrong though. Is that what you meant OP?

hunter9000a at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 5

> thank's replay, but how can I use reflection for this

> problem?

> can you help me?

We don't know exactly what your problem is, can you clarify please?

georgemca at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 6

> thank's replay, but how can I use reflection for this

> problem?

> can you help me?

You haven't told us what the problem is, all you've told us is the way you are thinking of solving it (and you're wrong, whatever it is you can't do it that way).

malcolmmca at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 7

ok I have a class

class A

{

public void method1();

}

class B , class C and so ...

then class CastObject extends SomeJavaClass

{

@override

public void execute(Object arg)

{

// now i want to call some method of arg, but i don't know type, so I

// need casting

//I dont want to use "instanceof" key to gett type of object which i need

}

}

tamria at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 8

> ok I have a class

> class A

> {

> public void method1();

> }

> class B , class C and so ...

>

> hen class CastObject extends SomeJavaClass

> {

> @override

> public void execute(Object arg)

> {

> // now i want to call some method of arg, but i don't

> know type, so I

> // need casting

> //I dont want to use "instanceof" key to gett type

> of object which i need

> }

> }

So you don't know what type it is. How do you know what method to call? You can query the object for it's class, then query the class for a particular method, but how do you know which method?

georgemca at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 9

Yes I know every each classes has a same object objTest.

class A

{

Test objTest;

}

and I want to call objTest's method. this is common!

class CastObject extends SomeJavaClass

{

@override

public void execute(Object arg)

{

// for exaple ((A)arg).objTest.somemethod();

}

}

tamria at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 10

I can make some Interface or a common class and cast on this interface or class, but i need to write extends class or implements interface in every requested classes. i don't know do so.

tamria at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 11

> Yes I know every each classes has a same object

> objTest.

> class A

> {

> Test objTest;

> }

> and I want to call objTest's method. this is common!

>

> class CastObject extends SomeJavaClass

> {

> @override

> public void execute(Object arg)

> {

> / for exaple ((A)arg).objTest.somemethod();

> }

> }

Object? Or method?

If all these classes will have this method, then it sounds like you could make use of interfaces here. Define "somemethod" on an interface, have all the classes implement that and have execute take that interface. EG

inteface MyInterface {

void somemethod();

}

...

//other class

void execute(MyInterface obj) {

obj.somemethod();

}

// later that day

class MyClass implements MyInterface {

public void somemethod() {

// do work

}

// etc

}

No need for reflection at all, then

georgemca at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 12

> I can make some Interface or a common class and cast

> on this interface or class, but i need to write

> extends class or implements interface in every

> requested classes. i don't know do so.

Why would that be a problem? Using reflection simply to avoid doing some re-factoring is a poor use of reflection!

georgemca at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 13

I can't do so

void execute(MyInterface obj) {

obj.somemethod();

}

because I can't remake execute method

tamria at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 14

thank's , i will think how it to do, if casting is not possible in this case

tamria at 2007-7-28 18:07:52 > top of Java-index,Java Essentials,Java Programming...
# 15

The best approach would be to have an interface like:

public interface TestContainer {

Test getObjTest();

}

And make all your dynamic objects implement that. You can then cast your dynamically loaded objects to that, and use the getter to retrieve your Test object.

Otherwise you can only call getField() on the Class object you loaded and access the field that way (by reflection). Much nastier.

malcolmmca at 2007-7-28 18:07:59 > top of Java-index,Java Essentials,Java Programming...
# 16

> I can't do so

> void execute(MyInterface obj) {

>

> obj.somemethod();

>

> }

> because I can't remake execute method

Why not? Not arguing, just wondering why you can't refactor it. Casting isn't a good idea, it could lead to runtime errors here. If you really can't refactor that method, reflection is a possible solution

georgemca at 2007-7-28 18:07:59 > top of Java-index,Java Essentials,Java Programming...