Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

[Reflection] Exception by setting a field value

866231Jun 24 2015 — edited Jun 30 2015

Hi all!

I didn't work a lot with Reflection and now I'm having troubles by writing a method to set a field value of an unknown type.

I wrote a little example program to show, where I cannot find a solution. I explain the problem in the following points:

- there is a class with 4 fields, which 2 are of type primitive int and 2 are Integer

- at line 27 is set the name of the field I want to modify

    - if is set field1 or field2 (of type int) there is no problem. In this case the 'case "int"' of the following switch is executed

    - if is set field3 or field4 (of type Integer) is executed the 'default' case of the following switch and at line 56 is thrown the cast exception that "Cannot cast java.lang.String to java.lang.Integer"

Can anyone help me?

Thanks a lot in advance.

*****************************************

import java.lang.reflect.Field;

public class TestClass {

    public int field1 = 1;

    public int field2 = 2;

    public Integer field3 = new Integer(3);

    public Integer field4 = new Integer("4");

  

    public static void main(String[] args) {

        try {

            TestClass object = new TestClass();

            Object newValue = "9999999";

            Field[] fields = object.getClass().getDeclaredFields();

            System.out.println("Execution 1");

            for (Field field : fields) {

                System.out.print("Field name: ");

                System.out.print(field.getName());

                System.out.print(" - Type: ");

                System.out.print(field.getType().getName());

                System.out.print(" - Value: ");

                System.out.print(field.get(object).toString());

                Object value = field.get(object);

                System.out.print(" - Value type: ");

                System.out.print(value.getClass().getName());

                System.out.println();

            }

            Field f = object.getClass().getField("field3");

            f.setAccessible(true);

            switch (f.getType().getName()) {

                case "boolean":

                    f.setBoolean(object, Boolean.parseBoolean(newValue.toString()));

                    break;

                case "byte":

                    f.setByte(object, (newValue.toString().getBytes()[0]));

                    break;

                case "char":

                    f.setChar(object, (newValue.toString().charAt(0)));

                    break;

                case "double":

                    f.setDouble(object, Double.parseDouble(newValue.toString()));

                    break;

                case "float":

                    f.setFloat(object, Float.parseFloat(newValue.toString()));

                    break;

                case "int":

                    f.setInt(object, Integer.parseInt(newValue.toString()));

                    break;

                case "long":

                    f.setLong(object, Long.parseLong(newValue.toString()));

                    break;

                case "short":

                    f.setShort(object, Short.parseShort(newValue.toString()));

                    break;

                default:

                    newValue = f.getType().cast(newValue);

                    f.set(object, newValue);

                    break;

            }

          

            System.out.println("Execution 2");

            for (Field field : fields) {

                System.out.print("Field name: ");

                System.out.print(field.getName());

                System.out.print(" - Type: ");

                System.out.print(field.getType().getName());

                System.out.print(" - Value: ");

                System.out.print(field.get(object).toString());

                Object value = field.get(object);

                System.out.print(" - Value type: ");

                System.out.print(value.getClass().getName());

                System.out.println();

            }

        } catch (NoSuchFieldException ex) {

            System.out.println("NoSuchFieldException: " + ex.getMessage());

        } catch (SecurityException ex) {

            System.out.println("SecurityException: " + ex.getMessage());

        } catch (Throwable ex) {

            System.out.println("Throwable: " + ex.getMessage());

            ex.printStackTrace();

        }

    }

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 28 2015
Added on Jun 24 2015
7 comments
2,353 views