This discussion is archived
8 Replies Latest reply: Jul 10, 2008 8:13 AM by 807589 RSS

Casting Unknown type at Runtime

807589 Newbie
Currently Being Moderated
I need to know if this approach is possible, please don't go off topic discussing ways around this (as I've found in other threads).

I have an application (in theory) that operates as a generic communications device between various servers and clients that all send various and unrelated data to each other.

If I send in some data object to this application, which would take in just an Object, I want it to spit out the casted version (from Object back into the data object's type) on the other side, but there is no way to list out every type possible and use an massive if clause to determine the type, it must be cast directly at runtime with no hints at compile time.

So, basically I need the code for the castIt method to make it work and compile.

public class Test {
public static void main(String[] args) {
castIt(new Temp());
}
     
public static void castIt(Object obj) {
done(/**code here**/obj);
}
     
public static void done(Temp obj) {
          
}
     
private static class Temp {
public Temp() {
               
}
}
}

Edited by: Neoro on Jul 10, 2008 7:52 AM -- Somehow hit submit too early

Edited by: Neoro on Jul 10, 2008 7:55 AM -- minor error fix
  • 1. Re: Casting Unknown type at Runtime
    807589 Newbie
    Currently Being Moderated
            Temp temp = new Temp();
            try {
                    Class c = Class.forName(temp.getClass().toString().substring(6));
                    Object o = c.cast(user);
            } catch (Exception e) {}
    That should work. Mind that this only works in Java 6 and beyond.

    Vincent

    Edited by: enter_name on Jul 10, 2008 8:02 AM
    Made a more complete solution.
  • 2. Re: Casting Unknown type at Runtime
    795426 Newbie
    Currently Being Moderated
    There is no general way to do that cleanly. Especially when you have given no details on what your clients are doing. Presumably they know how to handle the data they receive. Let them handle the translation from Object to whatever. Part of this is in the protocol they use.
  • 3. Re: Casting Unknown type at Runtime
    807589 Newbie
    Currently Being Moderated
    Neoro wrote:
    I need to know if this approach is possible, please don't go off topic discussing ways around this (as I've found in other threads).
    Be prepared for that to happen anyway
    I have an application (in theory) that operates as a generic communications device between various servers and clients that all send various and unrelated data to each other.
    In what format is this data?
  • 4. Re: Casting Unknown type at Runtime
    807589 Newbie
    Currently Being Moderated
    No, the idea is meaningless when you really think about it.

    When you define a reference variable to be of a given class or interface, or cast a reference, you do it to tell the compiler it can use the methods and fields peculiar to that class. But that information can only be useful at compile time. If the compiler doesn't know about a class then it doesn't know about the fields and methods of that class, so they can only be invoked by reflection, or by casting the instance to a class or interface the compiler does know about.
  • 5. Re: Casting Unknown type at Runtime
    807589 Newbie
    Currently Being Moderated
    enter_name wrote:
    Temp temp = new Temp();
    try {
    Class c = Class.forName(temp.getClass().toString().substring(6));
    Object o = c.cast(user);
    } catch (Exception e) {}
    That should work. Mind that this only works in Java 6 and beyond.

    Vincent
    This doesn't seem to result in a Temp object, passable into a method that takes a Temp object.
    georgemc wrote:

    In what format is this data?
    Just as any single object with any amount of internal fields and methods.
  • 6. Re: Casting Unknown type at Runtime
    807589 Newbie
    Currently Being Moderated
    Just as any single object with any amount of internal fields and methods.
    One can abstract past the point of usefulness, as is the case here. To repeat a previously-given answer, no, you can't cast to some arbitrary type at runtime. The desire to do so makes no sense, as casting is for handling known types at compile-time.

    Since you've expressed that you're not interested in alternatives, I won't bother to ask for more information in order to offer helpful suggestions.

    Good luck.

    ~
  • 7. Re: Casting Unknown type at Runtime
    807589 Newbie
    Currently Being Moderated
    Neoro wrote:
    This doesn't seem to result in a Temp object, passable into a method that takes a Temp object.
    Since, if the Temp class is not known at compile time, the only way you can be accessing a method that takes a Temp object is through reflection, and Method.invoke takes all it's parameters as Objects, it already is passable.
  • 8. Re: Casting Unknown type at Runtime
    807589 Newbie
    Currently Being Moderated
    Neoro wrote:
    georgemc wrote:

    In what format is this data?
    Just as any single object with any amount of internal fields and methods.
    I meant, how are you passing it around between these "servers"