Guest User

Untitled

a guest
Jan 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public static void usersUpdate( String attributeToBeChanged, String newAttributeValue, String whereKey ,String whereValue) throws Exception{
  2.  
  3.  
  4. Method getter=getDeclaredMethodIgnoreCase(User.class,"get"+whereKey);
  5.  
  6. HashMap<String,User> refToUsersCache= usersCache.getUsersCache(); // The objects are stored in this map as <username,User Object>
  7. User requiredUser = null;
  8.  
  9. for(String user: refToUsersCache.keySet()) { // loop through the hashmap and call its getter to check its "ID" or any attribute
  10. if (getter.invoke(refToUsersCache.get(user)).equals(whereValue)) // getID() == "5" (in our example)
  11. requiredUser = refToUsersCache.get(user); // this is the required object
  12. }
  13.  
  14. Method setter=getDeclaredMethodIgnoreCase(User.class,"set"+attributeToBeChanged);
  15. setter.invoke(requiredUser,newAttributeValue); // change the username = "newValue"
  16.  
  17. );
Add Comment
Please, Sign In to add comment