Refleksja

…gdyby nawet sam Bóg powiedział, że PiS się myli, to by to po nich spłynęło jak woda po kaczce… [poza tym to by był skoordynowany atak polityczny, a takim oligarchom jak Bóg nie kłaniamy się w pasie, o nie! i gdyby pan Bóg łaskawie się uciszył, to by było dobrze]

Daemony przeszłości

Się mówi, że trzeba komentować kod, bo to, co się teraz wydaje oczywiste, za jakiś (zazwyczaj bardzo niedługi) czas będzie wyglądało jak popis (nie)sprawności intelektualnej taniego konsultanta. Takoż więc czynię (ale tylko tam, gdzie nie wszystko jest jasne z samego kodu, na szczęście nie tak często). A dzisiaj na przykład wróciłem do pewnego fragmentu, który napisałem na rzecz pracy inżynierskiej, i bardzo mi się spodobały komentarze (na szczęście fragment jest jednym z najciekawszych, jakie napisałem, i wcale nie wygląda jakby to napisał programista pracujący zazwyczaj z Visual Basic).

   1:    /**
   2:     * Converts a XML-RPC-produced Object[] into an expected correct runtime
   3:     * array type. That's a necessary evil, because XML-RPC knows only about
   4:     * "arrays"; these arrays can have elements of any type and there's no
   5:     * notion of "runtime array type", hence all arrays are Object[], and that's
   6:     * not only declared type, but also the runtime type. This method tries to
   7:     * fix that. Note that it is only <em>expected</em> runtime type - the
   8:     * other side might have sent a complete mish-mash of objects in the array,
   9:     * and so it will throw {@link ClassCastException} if at least one of them
  10:     * can't be properly stored in the target array.
  11:     * <p>
  12:     * If the original array already <em>is</em> of the proper runtime type,
  13:     * it is returned intact.
  14:     * 
  15:     * @param type
  16:     *            the {@link Class} representing the target array's type
  17:     * @param objs
  18:     *            the original array
  19:     * @param <T>
  20:     *            target array component's type
  21:     * @return the array with corrected runtime type
  22:     */
  23:    //sorry about that...
  24:    @SuppressWarnings("unchecked")
  25:    public static <T> T[] arrayFixup(Class<T[]> type, Object... objs)
  26:    {
  27:        if (type.isAssignableFrom(objs.getClass()))
  28:            return type.cast(objs);
  29:        //errr...
  30:        Class<T> componentType=(Class<T>)type.getComponentType();
  31:        //urgh
  32:        T[] ret=(T[])Array.newInstance(componentType, objs.length);
  33:        if (componentType.isArray())
  34:            for (int i=0; i < objs.length; i++)
  35:                //uhm...
  36:                ret[i]=componentType.cast(
  37:                    arrayFixup(
  38:                        (Class<Object[]>)componentType,
  39:                        (Object[])objs[i]));
  40:        else
  41:            for (int i=0; i < objs.length; i++)
  42:                ret[i]=componentType.cast(objs[i]);
  43:        return ret;
  44:    }
  45:

Bardzo pomocne, zdecydowanie ;)