Wednesday 25 June 2008

Java null casting

This afternoon I was making a code review and I saw something in the code that drew my attention. It was a 'null' casting in the code I was checking.
It was something like:

(Object[]) null
Even if I remembered having seen that null casting in the past and knowing it wouldn't fail, I didn't realize immediately why the other developer had included this "unnecessary" (at first sight) casting in the code.
Looking for a quick explanation to this, I found nothing useful and it was then when I tried to remove the "unnecessary" code and I figured out what this was all about. I finally found the answer to my question: "When is it required to make such 'null' casting in the code?"

The reason is the result of a situation similar to this:


Suppose we have a method
myMethod(String a, String b)

and an the overloaded method
myMethod(String a, Object[] b)

When we call the method like this:
anObject.myMethod("WhatAPlate!", null)
;

the JVM will have no way of identifying which of the methods should be invoked for the message received since both of them match perfectly. As a consequence, that's the case where you could call the method using a null casting in this way:
anObject.
myMethod("WhatAPlate!", (Object[])null);


Spanish version...

Haciendo un Code Review hoy a la tarde me encontré con algo que me llamó la atención. Se trataba de un casteo de 'null' en la mitad del código Java que estaba revisando.
Algo así:
(Object[]) null
Si bien recordé que en algún momento había visto eso y sabía que no fallaba, no entendía por qué lo habían incluído en el código. A simple vista no parecía algo útil. Después de leer un poco y no encontrarle la vuelta rápido intenté quitar el casteo que para mí en ese momento estaba de más y me dí cuenta de lo que no veía hasta el momento: "¿En qué situación puede ser necesario castear 'null'?".
El tema era:
Supongamos que tengo un método:
miMetodo(String a, String b)
y una sobrecarga:
miMetodo(String a, Object[] b)

En la llamada al método, si realizo la llamada:
unObjeto.miMetodo("quePlato!", null);
la JVM no tendrá manera de darse cuenta a qué método invocar debido a ambos métodos podrían responder a ese mensaje.
En tal caso, puede realizarse la llamada enviando el mensaje:
unObjeto.miMetodo("quePlato!", (Object[])null);
que haría que la llamada pueda resolverse correctamente.

Wednesday 18 June 2008

License plates chaser 4

Here, another of my geek license plates!... This time Team Foundation Server is present in my blog. :)


Spanish version...

Acá otra de mis fotos patentes ñoñas. Esta vez le tocó a Team Foundation Server estar presente en mi blog :)

Documenting software architectures

Let me share this time an InfoQ video in which Markus Voelter talks about documenting software architectures. It's really interesting.


You can also get deeper information
here. See Gustavo Brey's comment there. It adds an interesting topic to the subject.

Spanish version...

El video que comparto acá es una entrevista a Markus Voelter (lo obtuve de InfoQ) hablando sobre documentación de arquitecturas de software. Muy interesante.
Pueden encontrar más información acá.
Fíjense el comentario de Gustavo Brey que agrega un punto muy interesante al tema.
Ah... como PD: Markus no tiene un aire a Rodrigo Palacio? :P