5

I'm looking for a simple solution to pass values of attributes/objects between two java programs. The programs are identical (running on separated nodes) and can't set/get variables though call of method. They can communicate only through external channel like file or network. There are many various objects which should be shared. My idea is to pass the data as text and encode/decode with xml. I can also send a name of object and its class.

My problem is: the decode method returns variables of type Object. I've to move the value to the target object but without cast I'm getting compiler error 'incompatible cast'. So I've to do a cast. But there are many possible objects and I've to do a huge set of if or switch statement. I have the name of class and it would be so nice to do some kind of dynamic cast.

This thread discuss similar topic and suggest to use Class.cast() but I've got no success:

java: how can i do dynamic casting of a variable from one type to another?

I you prefer code oriented question here you are:

  Object decode( String str )
  {
    return( str );
  }

  String in = "abc";
  String out;

// out = decode( in );           // compiler error 'incompatible types'
// out = (String)decode( in );   // normal cast but I'm looking for dynamic one
// out = ('String')decode( in ); // it would be perfect

Cheers, Annie

Community
  • 1
  • 1
Annie W.
  • 328
  • 4
  • 22

4 Answers4

3

If your problem is with the assign instruction commented within your code sample, you could implement something with generics:

public <T> T decode(String str) {
    ... decode logic
    return (T)decodedObject;
}

This approach could let you do something like:

public void foo1(String bar) {
    String s = decode(par);
}

public void foo2(String bar) {
    Integer s = decode(par);
}

<T> T decode(String serializedRepresentation) {
    Object inflatedObject;

    // logic to unserialize object

    return (T)inflatedObject;
}
Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
  • Oh, wait, you forgot to supply the class to the method. Amend it to include the `Class c` param. Otherwise, the compiler has no way of knowing what `T` is for a given call to the method. – Alvin Thompson Apr 27 '13 at 13:43
  • With this approach you don't need to give the class but there will be an unchecked cast – Francisco Spaeth Apr 27 '13 at 13:46
  • Ah! I don't like warnings. And you can avoid it since you have the class name. – Alvin Thompson Apr 27 '13 at 13:50
  • Besides, that defeats the purpose of using generics in the first place. – Alvin Thompson Apr 27 '13 at 13:51
  • I don't know if I got your point, but you don't have the class... the question was exactly how to do it without every time inform the class... otherwise you could do a regular cast like `(Class)obj` – Francisco Spaeth Apr 27 '13 at 13:55
  • He has the class name at run time, not compile time, so he would need a `switch` statement with every possible class with that approach. That's what he's trying to avoid. – Alvin Thompson Apr 27 '13 at 14:03
  • @java mentor sorry i am not able to interpret this specific code public T decode(String str) {..} what we are trying to achieve here?Will return object will not always be of type String ?Looks like i am missing something here but not able to spot its purpose. – M Sach Apr 27 '13 at 14:09
0

if you are already passing XML, then why don't you use JAXB to marshall and unmarshall the text

http://docs.oracle.com/javase/tutorial/jaxb/intro/examples.html

But if you are saying that both programs are java, then use RMI

http://www.javacoffeebreak.com/articles/javarmi/javarmi.html

http://docs.oracle.com/javase/6/docs/technotes/guides/rmi/hello/hello-world.html

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

You can use generics:

public static <T> T decode(Class<T> c, String str) {
  return (T)str;
}

...

Class<?> c = Class.forName(className); // throws CNFE
out = decode(String.class, in);

Of course, your decode method would need to do something more than that.

Alvin Thompson
  • 5,388
  • 3
  • 26
  • 39
0

you can go for something like this

public static <T> T decode(T obj) {

    return (T)(obj);
}



public static void main(String [] args){
    Integer int1 = decode(123);
    String str1 = decode("abc");

}
M Sach
  • 33,416
  • 76
  • 221
  • 314