The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Addition to the java language; dynamic/runtime casting

I hate classloader issues and basically the only fix around them is to wait for the next release of a particular piece of software(see JBoss 3.2.x to 4.0) or come up with all kinds of hacks.  I say put some form of reflection/dynamic cast method class, ie:

Method m = this.getClass().getMethod("mult",
                            new Class[]{o.getClass(), int.class)};
              m.invoke(new Object[]{o, new Integer(factor));

but cleaner:

Class c = obj.getClass();
Object o2 = session.getAttribute("some.str");
String someStr = (String) c.cast(o2);
Bot Berlin Send private email
Monday, September 25, 2006
 
 
Can you explain the problem a little more? i'm curious to understand what it is...
Vince Send private email
Monday, September 25, 2006
 
 
Well; typically when you want to cast, you do:

String str = (String) session.getAttribute("str");

Note the use of something comment like getting data out of session scope.

The problem with the code above is that it may not work 100% of the time, especially in a J2EE environment and working with EJBs, blah, blah.  It may work in theory but just doent work, mainly because of classloader/jvm issues.

Sure for the simple cases.

But wouldnt it be nice to dynamically cast and have it work.  Almost makes it seem more like a dynamic typed language.

Object obj = session.getAttribute("str")
Class c = getClassLoader().getClass("java.lang.String");
String str = c.cast(obj);

2 extra lines, but would be very flexible.
Bot Berlin Send private email
Monday, September 25, 2006
 
 
I don't personally care for it. If you have a problem casting something then you obviously have a bug that needs to be fixed. At one extreme end you could be trying to cast to a Widget but actually be getting a Gidget instead. And that would be plain awful!  ;)

Something that could potentially work is casting of primitives (expecting an Integer but getting a String instead). But you are still looking at an issue that needs to be fixed. Not just ignored by way of some dynamic casting cludge.
dood mcdoogle
Tuesday, September 26, 2006
 
 
Bot, I'm not sure I entirely understand your problem, but would it be solved with code like this:

http://surguy.net/articles/communication-across-classloaders.xml

which uses the Proxy class?
Inigo Send private email
Tuesday, September 26, 2006
 
 
Has casting anything to do with the JVM? AFAIK casting is only necessary for the compiler, so "dynamic casting" sounds stupid.
smalltalk Send private email
Tuesday, September 26, 2006
 
 
class PassThroughProxyHandler implements InvocationHandler {
    private final Object delegate;
    public PassThroughProxyHandler(Object delegate) {
        this.delegate = delegate;
    }
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        Method delegateMethod = delegate.getClass().getMethod(method.getName(), method.getParameterTypes());
        return delegateMethod.invoke(delegate, args);
    }
}

That is exactlying what I am doing.
Bot Berlin Send private email
Tuesday, September 26, 2006
 
 
"Object obj = session.getAttribute("str")
Class c = getClassLoader().getClass("java.lang.String");
String str = c.cast(obj);"
That is nonsense.  It's painful to look at.  It would just be extra code.

If you want to do anything like this, you could create a variant map using generics.
private static final Key str = new Key<String>("str");
private static final Key strbuffer = new Key<StringBuffer>("strbuffer");
String strVal = map.get(str);
StringBuffer strbufferVal = map.get(strbuffer);

And this has the upside of actually working.
Dave
Wednesday, September 27, 2006
 
 
It may not work with multiple classloaders and in a container environment may not work with EJBs.

That was my original problem and why I came up with the cast thing.
Bot Berlin Send private email
Wednesday, September 27, 2006
 
 
And works with JDK < 1.4+
Bot Berlin Send private email
Wednesday, September 27, 2006
 
 
And on top of that, jython is much easier to work with and can accomplish the same thing.
Bot Berlin Send private email
Wednesday, September 27, 2006
 
 
Aha. Multiple classloader problems. Now I get it. Java's typesafety will not permit any easy solution here. Changes to the language and/or api aren't really possible without majorly undermining the typesafety and wiping out backwards compatibility. You could use ducktyping (If it looks like a string, acts like a string and quacks like a string, it's a IString (or charsequence, or...)), but in the end that is just a lot more work for little return. You would be almost forced to create a interface for every problemcase you encounter. And then we haven't talked about the static methods yet. :eek: 

Having said that, ducktyping, jython, groovy; any of those would be my prefered(sp?) possible solutions.
Dave
Thursday, September 28, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz