mardi 26 avril 2016

Java - Reflection

I want to use Java Reflection to set some data at RunTime. My problem is I can't figure out how do I get sub-classes information and use as a part of reflection.

I want to use Reflection to keep it generic functionality for future flexibility.

Java Structure - Class & Fields (field data type in brackets) following in bullets. Setter / Getter methods for each field is present. This is sample representation.

Restaurant
  - RestaurantId (long)
  - RestaurantName (String)
  - RestaurantCity (String)
  - Employee (Employee object)
  - Menu (Menu object)

   Employee
     - EmployeeId (long)
     - EmployeeName (String)

   Menu
     - MenuId (long)
     - MenuItem (String)
     - MenuCategory (String)
     - MenuIngredients (Ingredients object)

       Ingredients
         - IngredientId (long)
         - IngredientName (String)

As we can see I have multiple level of object structure. So Restaurant as the top most object, then within Restaurant there is object of Employee and Menu and within Menu object there is a Ingredients object.

Requirements

I will get class name and field (method name) to set the value. E.g. I will get something like Class = Restaurant & Field Method = setRestaurantCity and value to set in the City. I may also get Class = Menu & Field Method = setMenuItem or let's say Class = Ingredients & Field Method = setIngredientName and respective values for them.

I have provided sample java code with which I am trying to test. As we could see in java code that it works fine for setting value in Restaurant object. However I would like to set the data in another object which is actually two levels down to Restaurant, that is, Restaurant --> Menu --> Ingredients. for valueObject2.

I don't want to use instanceOf as that would need me to verify each and every object. As current structure is not just 2 levels but much more deeper something like going upto 5 levels down with total class objects counting upto 25 of them.

I am looking for some solution where I can match the names of the class and set respective values. So, if I get valueObject3 and respetive Class & Field is sent in future, then the solution should work.

Open for any other solution as well other than Reflection.

Java Code

public static void main(String[] args) {

        try {
            Object valueObject1 = "Mumbai";
            String fromClass1 = "com.test.Restaurant";
            String fromMethod1 = "setRestaurantCity";

            Object valueObject2 = "Bread";
            String fromClass2 = "com.test.Ingredients";
            String fromMethod2 = "setIngredientName";

            Object restnt = new Restaurant();
            Class<? extends Object> clsRestnt = restnt.getClass();
            System.out.println(clsRestnt.getCanonicalName());

            Method[] toRestMethods = clsRestnt.getMethods();

            for (int i = 0; i<toRestMethods.length; i++)
            {
                System.out.println(toRestMethods[i].getName());
                if(toRestMethods[i].getName().equalsIgnoreCase(fromMethod1))
                {
                    System.out.println("Found Method " + toRestMethods[i]);
                    valueObject1 = toRestMethods[i].invoke(restnt, valueObject1);

                }
            }

        } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Help plzz!!!





Aucun commentaire:

Enregistrer un commentaire