Search in sources :

Example 1 with Support_Field

use of tests.support.Support_Field in project j2objc by google.

the class TestAccess method testProtectedFieldAccess.

/**
     * java.lang.reflect.Field#get(java.lang.Object)
     * java.lang.reflect.Field#getByte(java.lang.Object)
     * java.lang.reflect.Field#getBoolean(java.lang.Object)
     * java.lang.reflect.Field#getShort(java.lang.Object)
     * java.lang.reflect.Field#getInt(java.lang.Object)
     * java.lang.reflect.Field#getLong(java.lang.Object)
     * java.lang.reflect.Field#getFloat(java.lang.Object)
     * java.lang.reflect.Field#getDouble(java.lang.Object)
     * java.lang.reflect.Field#getChar(java.lang.Object)
     * java.lang.reflect.Field#set(java.lang.Object, java.lang.Object)
     * java.lang.reflect.Field#setByte(java.lang.Object, byte)
     * java.lang.reflect.Field#setBoolean(java.lang.Object, boolean)
     * java.lang.reflect.Field#setShort(java.lang.Object, short)
     * java.lang.reflect.Field#setInt(java.lang.Object, int)
     * java.lang.reflect.Field#setLong(java.lang.Object, long)
     * java.lang.reflect.Field#setFloat(java.lang.Object, float)
     * java.lang.reflect.Field#setDouble(java.lang.Object, double)
     * java.lang.reflect.Field#setChar(java.lang.Object, char)
     */
public void testProtectedFieldAccess() {
    Class fieldClass = Support_Field.class;
    String fieldName = null;
    Field objectField = null;
    Field booleanField = null;
    Field byteField = null;
    Field charField = null;
    Field shortField = null;
    Field intField = null;
    Field longField = null;
    Field floatField = null;
    Field doubleField = null;
    try {
        fieldName = "objectField";
        objectField = fieldClass.getDeclaredField(fieldName);
        fieldName = "booleanField";
        booleanField = fieldClass.getDeclaredField(fieldName);
        fieldName = "byteField";
        byteField = fieldClass.getDeclaredField(fieldName);
        fieldName = "charField";
        charField = fieldClass.getDeclaredField(fieldName);
        fieldName = "shortField";
        shortField = fieldClass.getDeclaredField(fieldName);
        fieldName = "intField";
        intField = fieldClass.getDeclaredField(fieldName);
        fieldName = "longField";
        longField = fieldClass.getDeclaredField(fieldName);
        fieldName = "floatField";
        floatField = fieldClass.getDeclaredField(fieldName);
        fieldName = "doubleField";
        doubleField = fieldClass.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        fail("missing field " + fieldName + " in test support class " + fieldClass.getName());
    }
    // create the various objects that might or might not have an instance
    // of the field
    Support_Field parentClass = new Support_Field();
    SupportSubClass subclass = new SupportSubClass();
    SupportSubClass otherSubclass = new SupportSubClass();
    Object plainObject = new Object();
    Class illegalAccessExceptionClass = IllegalAccessException.class;
    Class illegalArgumentExceptionClass = IllegalArgumentException.class;
    // The test will attempt to use pass an object to set for object, byte,
    // short, ..., float and double fields
    // and pass a byte to to setByte for byte, short, ..., float and double
    // fields and so on.
    // It will also test if IllegalArgumentException is thrown when the
    // field does not exist in the given object and that
    // IllegalAccessException is thrown when trying to access an
    // inaccessible protected field.
    // The test will also check that IllegalArgumentException is thrown for
    // all other attempts.
    // Ordered by widening conversion, except for 'L' at the beg (which
    // stands for Object).
    // If the object provided to set can be unwrapped to a primitive, then
    // the set method can set
    // primitive fields.
    char[] types = { 'L', 'B', 'S', 'C', 'I', 'J', 'F', 'D' };
    Field[] fields = { objectField, byteField, shortField, charField, intField, longField, floatField, doubleField };
    Object[] values = { new Byte((byte) 1), new Byte((byte) 1), new Short((short) 1), new Character((char) 1), new Integer(1), new Long(1), new Float(1), new Double(1) };
    // test set methods
    for (int i = 0; i < types.length; i++) {
        char type = types[i];
        Object value = values[i];
        for (int j = i; j < fields.length; j++) {
            Field field = fields[j];
            fieldName = field.getName();
            if (field == charField && type != 'C') {
                // the exception is that bytes and shorts CANNOT be
                // converted into chars even though chars CAN be
                // converted into ints, longs, floats and doubles
                subclass.setField(type, subclass, field, illegalArgumentExceptionClass, value);
            } else {
                // setting type into field);
                subclass.setField(type, subclass, field, null, value);
                subclass.setField(type, otherSubclass, field, null, value);
                subclass.setField(type, parentClass, field, illegalAccessExceptionClass, value);
            // Fails on JRE 1.7.0_55.
            //                    subclass.setField(type, plainObject, field,
            //                            illegalArgumentExceptionClass, value);
            }
        }
        for (int j = 0; j < i; j++) {
            Field field = fields[j];
            fieldName = field.getName();
            // not setting type into field);
            subclass.setField(type, subclass, field, illegalArgumentExceptionClass, value);
        }
    }
    // test setBoolean
    Boolean booleanValue = Boolean.TRUE;
    subclass.setField('Z', subclass, booleanField, null, booleanValue);
    subclass.setField('Z', otherSubclass, booleanField, null, booleanValue);
    subclass.setField('Z', parentClass, booleanField, illegalAccessExceptionClass, booleanValue);
    //                illegalArgumentExceptionClass, booleanValue);
    for (int j = 0; j < fields.length; j++) {
        Field listedField = fields[j];
        fieldName = listedField.getName();
        // not setting boolean into listedField
        subclass.setField('Z', subclass, listedField, illegalArgumentExceptionClass, booleanValue);
    }
    for (int i = 0; i < types.length; i++) {
        char type = types[i];
        Object value = values[i];
        subclass.setField(type, subclass, booleanField, illegalArgumentExceptionClass, value);
    }
    // We perform the analogous test on the get methods.
    // ordered by widening conversion, except for 'L' at the end (which
    // stands for Object), to which all primitives can be converted by
    // wrapping
    char[] newTypes = new char[] { 'B', 'S', 'C', 'I', 'J', 'F', 'D', 'L' };
    Field[] newFields = { byteField, shortField, charField, intField, longField, floatField, doubleField, objectField };
    fields = newFields;
    types = newTypes;
    // test get methods
    for (int i = 0; i < types.length; i++) {
        char type = types[i];
        for (int j = 0; j <= i; j++) {
            Field field = fields[j];
            fieldName = field.getName();
            if (type == 'C' && field != charField) {
                // the exception is that bytes and shorts CANNOT be
                // converted into chars even though chars CAN be
                // converted into ints, longs, floats and doubles
                subclass.getField(type, subclass, field, illegalArgumentExceptionClass);
            } else {
                // getting type from field
                subclass.getField(type, subclass, field, null);
                subclass.getField(type, otherSubclass, field, null);
                subclass.getField(type, parentClass, field, illegalAccessExceptionClass);
            // Fails on JRE 1.7.0_55.
            //                    subclass.getField(type, plainObject, field,
            //                            illegalArgumentExceptionClass);
            }
        }
        for (int j = i + 1; j < fields.length; j++) {
            Field field = fields[j];
            fieldName = field.getName();
            subclass.getField(type, subclass, field, illegalArgumentExceptionClass);
        }
    }
    // test getBoolean
    subclass.getField('Z', subclass, booleanField, null);
    subclass.getField('Z', otherSubclass, booleanField, null);
    subclass.getField('Z', parentClass, booleanField, illegalAccessExceptionClass);
    //                illegalArgumentExceptionClass);
    for (int j = 0; j < fields.length; j++) {
        Field listedField = fields[j];
        fieldName = listedField.getName();
        // not getting boolean from listedField
        subclass.getField('Z', subclass, listedField, illegalArgumentExceptionClass);
    }
    for (int i = 0; i < types.length - 1; i++) {
        char type = types[i];
        subclass.getField(type, subclass, booleanField, illegalArgumentExceptionClass);
    }
    Object res = subclass.getField('L', subclass, booleanField, null);
    assertTrue("unexpected object " + res, res instanceof Boolean);
}
Also used : Support_Field(tests.support.Support_Field) Field(java.lang.reflect.Field) Support_Field(tests.support.Support_Field)

Example 2 with Support_Field

use of tests.support.Support_Field in project robovm by robovm.

the class TestAccess method testProtectedFieldAccess.

/**
     * java.lang.reflect.Field#get(java.lang.Object)
     * java.lang.reflect.Field#getByte(java.lang.Object)
     * java.lang.reflect.Field#getBoolean(java.lang.Object)
     * java.lang.reflect.Field#getShort(java.lang.Object)
     * java.lang.reflect.Field#getInt(java.lang.Object)
     * java.lang.reflect.Field#getLong(java.lang.Object)
     * java.lang.reflect.Field#getFloat(java.lang.Object)
     * java.lang.reflect.Field#getDouble(java.lang.Object)
     * java.lang.reflect.Field#getChar(java.lang.Object)
     * java.lang.reflect.Field#set(java.lang.Object, java.lang.Object)
     * java.lang.reflect.Field#setByte(java.lang.Object, byte)
     * java.lang.reflect.Field#setBoolean(java.lang.Object, boolean)
     * java.lang.reflect.Field#setShort(java.lang.Object, short)
     * java.lang.reflect.Field#setInt(java.lang.Object, int)
     * java.lang.reflect.Field#setLong(java.lang.Object, long)
     * java.lang.reflect.Field#setFloat(java.lang.Object, float)
     * java.lang.reflect.Field#setDouble(java.lang.Object, double)
     * java.lang.reflect.Field#setChar(java.lang.Object, char)
     */
public void testProtectedFieldAccess() {
    Class fieldClass = new Support_Field().getClass();
    String fieldName = null;
    Field objectField = null;
    Field booleanField = null;
    Field byteField = null;
    Field charField = null;
    Field shortField = null;
    Field intField = null;
    Field longField = null;
    Field floatField = null;
    Field doubleField = null;
    try {
        fieldName = "objectField";
        objectField = fieldClass.getDeclaredField(fieldName);
        fieldName = "booleanField";
        booleanField = fieldClass.getDeclaredField(fieldName);
        fieldName = "byteField";
        byteField = fieldClass.getDeclaredField(fieldName);
        fieldName = "charField";
        charField = fieldClass.getDeclaredField(fieldName);
        fieldName = "shortField";
        shortField = fieldClass.getDeclaredField(fieldName);
        fieldName = "intField";
        intField = fieldClass.getDeclaredField(fieldName);
        fieldName = "longField";
        longField = fieldClass.getDeclaredField(fieldName);
        fieldName = "floatField";
        floatField = fieldClass.getDeclaredField(fieldName);
        fieldName = "doubleField";
        doubleField = fieldClass.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        fail("missing field " + fieldName + " in test support class " + fieldClass.getName());
    }
    // create the various objects that might or might not have an instance
    // of the field
    Support_Field parentClass = new Support_Field();
    SupportSubClass subclass = new SupportSubClass();
    SupportSubClass otherSubclass = new SupportSubClass();
    Object plainObject = new Object();
    Class illegalAccessExceptionClass = new IllegalAccessException().getClass();
    Class illegalArgumentExceptionClass = new IllegalArgumentException().getClass();
    // The test will attempt to use pass an object to set for object, byte,
    // short, ..., float and double fields
    // and pass a byte to to setByte for byte, short, ..., float and double
    // fields and so on.
    // It will also test if IllegalArgumentException is thrown when the
    // field does not exist in the given object and that
    // IllegalAccessException is thrown when trying to access an
    // inaccessible protected field.
    // The test will also check that IllegalArgumentException is thrown for
    // all other attempts.
    // Ordered by widening conversion, except for 'L' at the beg (which
    // stands for Object).
    // If the object provided to set can be unwrapped to a primitive, then
    // the set method can set
    // primitive fields.
    char[] types = { 'L', 'B', 'S', 'C', 'I', 'J', 'F', 'D' };
    Field[] fields = { objectField, byteField, shortField, charField, intField, longField, floatField, doubleField };
    Object[] values = { new Byte((byte) 1), new Byte((byte) 1), new Short((short) 1), new Character((char) 1), new Integer(1), new Long(1), new Float(1), new Double(1) };
    // test set methods
    for (int i = 0; i < types.length; i++) {
        char type = types[i];
        Object value = values[i];
        for (int j = i; j < fields.length; j++) {
            Field field = fields[j];
            fieldName = field.getName();
            if (field == charField && type != 'C') {
                // the exception is that bytes and shorts CANNOT be
                // converted into chars even though chars CAN be
                // converted into ints, longs, floats and doubles
                subclass.setField(type, subclass, field, illegalArgumentExceptionClass, value);
            } else {
                // setting type into field);
                subclass.setField(type, subclass, field, null, value);
                subclass.setField(type, otherSubclass, field, null, value);
                subclass.setField(type, parentClass, field, illegalAccessExceptionClass, value);
                subclass.setField(type, plainObject, field, // Failed on JDK.
                illegalAccessExceptionClass, value);
            }
        }
        for (int j = 0; j < i; j++) {
            Field field = fields[j];
            fieldName = field.getName();
            // not setting type into field);
            subclass.setField(type, subclass, field, illegalArgumentExceptionClass, value);
        }
    }
    // test setBoolean
    Boolean booleanValue = Boolean.TRUE;
    subclass.setField('Z', subclass, booleanField, null, booleanValue);
    subclass.setField('Z', otherSubclass, booleanField, null, booleanValue);
    subclass.setField('Z', parentClass, booleanField, illegalAccessExceptionClass, booleanValue);
    subclass.setField('Z', plainObject, booleanField, // Failed on JDK
    illegalAccessExceptionClass, booleanValue);
    for (int j = 0; j < fields.length; j++) {
        Field listedField = fields[j];
        fieldName = listedField.getName();
        // not setting boolean into listedField
        subclass.setField('Z', subclass, listedField, illegalArgumentExceptionClass, booleanValue);
    }
    for (int i = 0; i < types.length; i++) {
        char type = types[i];
        Object value = values[i];
        subclass.setField(type, subclass, booleanField, illegalArgumentExceptionClass, value);
    }
    // We perform the analagous test on the get methods.
    // ordered by widening conversion, except for 'L' at the end (which
    // stands for Object), to which all primitives can be converted by
    // wrapping
    char[] newTypes = new char[] { 'B', 'S', 'C', 'I', 'J', 'F', 'D', 'L' };
    Field[] newFields = { byteField, shortField, charField, intField, longField, floatField, doubleField, objectField };
    fields = newFields;
    types = newTypes;
    // test get methods
    for (int i = 0; i < types.length; i++) {
        char type = types[i];
        for (int j = 0; j <= i; j++) {
            Field field = fields[j];
            fieldName = field.getName();
            if (type == 'C' && field != charField) {
                // the exception is that bytes and shorts CANNOT be
                // converted into chars even though chars CAN be
                // converted into ints, longs, floats and doubles
                subclass.getField(type, subclass, field, illegalArgumentExceptionClass);
            } else {
                // getting type from field
                subclass.getField(type, subclass, field, null);
                subclass.getField(type, otherSubclass, field, null);
                subclass.getField(type, parentClass, field, illegalAccessExceptionClass);
                subclass.getField(type, plainObject, field, illegalAccessExceptionClass);
            }
        }
        for (int j = i + 1; j < fields.length; j++) {
            Field field = fields[j];
            fieldName = field.getName();
            subclass.getField(type, subclass, field, illegalArgumentExceptionClass);
        }
    }
    // test getBoolean
    subclass.getField('Z', subclass, booleanField, null);
    subclass.getField('Z', otherSubclass, booleanField, null);
    subclass.getField('Z', parentClass, booleanField, illegalAccessExceptionClass);
    subclass.getField('Z', plainObject, booleanField, illegalAccessExceptionClass);
    for (int j = 0; j < fields.length; j++) {
        Field listedField = fields[j];
        fieldName = listedField.getName();
        // not getting boolean from listedField
        subclass.getField('Z', subclass, listedField, illegalArgumentExceptionClass);
    }
    for (int i = 0; i < types.length - 1; i++) {
        char type = types[i];
        subclass.getField(type, subclass, booleanField, illegalArgumentExceptionClass);
    }
    Object res = subclass.getField('L', subclass, booleanField, null);
    assertTrue("unexpected object " + res, res instanceof Boolean);
}
Also used : Support_Field(tests.support.Support_Field) Field(java.lang.reflect.Field) Support_Field(tests.support.Support_Field)

Aggregations

Field (java.lang.reflect.Field)2 Support_Field (tests.support.Support_Field)2