Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void usersUpdate( String attributeToBeChanged, String newAttributeValue, String whereKey ,String whereValue) throws Exception{
- Method getter=getDeclaredMethodIgnoreCase(User.class,"get"+whereKey);
- HashMap<String,User> refToUsersCache= usersCache.getUsersCache(); // The objects are stored in this map as <username,User Object>
- User requiredUser = null;
- for(String user: refToUsersCache.keySet()) { // loop through the hashmap and call its getter to check its "ID" or any attribute
- if (getter.invoke(refToUsersCache.get(user)).equals(whereValue)) // getID() == "5" (in our example)
- requiredUser = refToUsersCache.get(user); // this is the required object
- }
- Method setter=getDeclaredMethodIgnoreCase(User.class,"set"+attributeToBeChanged);
- setter.invoke(requiredUser,newAttributeValue); // change the username = "newValue"
- );
Add Comment
Please, Sign In to add comment