dimanche 9 avril 2017

Java reflection: how get value of Field as Arraylist<>

I have a Menu object.

this has an Arrraylist<MenuItemImpl> to "mItems" name. this is hide.

(MenuItemImpl) is a hide & protected class. See class (here)

Now ,how i can get this arraylist.

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    { 

      List<Field> list = getAllFields(menu);
      for(Field f: list)
      {
          if(f.getName().equals("mItems"))
           {
             f.setAccessible(true);
             return f.get( /* here */ );   // <<--- I use new Arraylist<Object> , but get exception
           }
       }
   }

public static List<Field> getAllFields(Object obj)
    {
        List<Field> res = new ArrayList<>();
        res.addAll(Arrays.asList(obj.getClass().getDeclaredFields()));
    if (obj.getClass().getSuperclass() != null)
    {
        res.addAll(Arrays.asList(obj.getClass().getSuperclass().getDeclaredFields()));
    }

    return res;
}

in f.get() I use new Arraylist<Object> , but get exception

Please help me.Tanks





Aucun commentaire:

Enregistrer un commentaire