Reflection limitations

Managed Objects support java.lang.reflect except for modification of an array element. Array element modifications are not updated in shared memory. Example 5.31, “Reflection behavior” shows this behavior.

Example 5.31. Reflection behavior

//     $Revision: 1.1.2.1 $

package com.kabira.snippets.managedobjects;

import com.kabira.platform.Transaction;
import com.kabira.platform.annotation.Managed;
import java.lang.reflect.Array;

/**
 *  This snippet is used to demonstrate reflection limitations
 * <p>
 * <h2> Target Nodes</h2>
 * <ul>
 * <li> <b>domainnode</b> = A
 * </ul>
 */
public class Reflection
{
    /**
     * A managed object
     */
    @Managed
    public static class MyObject
    {
        MyObject()
        {
            super();

            intArray = new int [10];
            int        i;

            for (i = 0; i < 10; i++)
            {
                intArray[i] = i;
            }
        }
        int    []    intArray;
    }
    /**
     * Main entry point
     * @param args  Not used
     */
    public static void main(String [] args)
    {
        new Transaction("Reflection")
        {
            @Override
            protected void run() throws Rollback
            {
                 MyObject    i = new MyObject();

                //
                //    Read the 5th element
                //
                System.out.println("intArray[5] == " + Array.getInt(i.intArray, 5));

                //
                //    Modify the 5th element
                //
                Array.setInt(i.intArray, 5, 0);


                //
                //    Read the 5th element - still 5
                //
                System.out.println("intArray[5] == " + Array.getInt(i.intArray, 5));
           }
        }.execute();
    }
}

When run Example 5.31, “Reflection behavior” outputs (annotation added):

Example 5.32. Reflection behavior output

#
#    Original value of 5th element of intArray
#
[A] intArray[5] == 5

#
#    5th element still contains a value of 5 even after being set to 0 by Reflection API
#
[A] intArray[5] == 5