简体   繁体   中英

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

oh, I get my answer.

for(Field f: list)
      {
          if(f.getName().equals("mItems"))
           {
             f.setAccessible(true);
             return f.get(menu);
           }
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM