public void setGroups(List<String> groups) throws SharedGroupException {
        if (groups == null) {
            this.groups = new LinkedList<>();
        }
        else {
            // Raise an error if the user is trying to remove the item from a shared group
            for (Group group: getSharedGroups()) {
                // Get the display name of the group
                String groupName = group.getSharedDisplayName();
                // Check if the group has been removed from the new groups list
                if (!groups.contains(groupName)) {
                    throw new SharedGroupException("Cannot remove item from shared group");
                }
            }

            // Remove shared groups from the param
            for (Iterator<String> it=groups.iterator(); it.hasNext();) {
                String groupName = it.next();
                try {
                    Group group = GroupManager.getInstance().getGroup(groupName);
                    if (RosterManager.isSharedGroup(group)) {
                        it.remove();
                    }
                } catch (GroupNotFoundException e) {
                    // Check now if there is a group whose display name matches the requested group
                    Collection<Group> groupsWithProp = GroupManager
                            .getInstance()
                            .search(Group.SHARED_ROSTER_DISPLAY_NAME_PROPERTY_KEY, groupName);
                    Iterator<Group> itr = groupsWithProp.iterator();
                    while(itr.hasNext()) {
                        Group group = itr.next();
                        if (RosterManager.isSharedGroup(group)) {
                            it.remove();
                        }
                    }
                }
            }
            this.groups = groups;
        }
    }
Set the current groups for the item.

@param groups The new lists of groups the item belongs to.
@throws org.jivesoftware.openfire.SharedGroupException if trying to remove shared group.
xmppserver/src/main/java/org/jivesoftware/openfire/roster/RosterItem.java