Android(Java):泛型与反射

private <T> T getFromList(List list,int id){
  try {
   if(null==list||list.isEmpty()){
    return null;
   }
   T t = null;
   Method method;
   Integer tid;
   for (T temp : (List<T>)list) {
    method = temp.getClass().getMethod("getUid", null);
    tid = (Integer) method.invoke(temp, null);
    if(tid==id){
     t = temp;
     break;
    }
   }
   return t;
  } catch (IllegalArgumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SecurityException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchMethodException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
 }

你可能感兴趣的:(android,泛型,反射)