From 65dd6c9777fa56eff7c983d2860b2ed6f2dfb8d9 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 30 Apr 2015 11:43:54 +0200 Subject: [PATCH] Fix RosterEntry.setNames(String) also mark the method synchornized, to prevent race conditions with the name. Fixes SMACK-662. --- .../org/jivesoftware/smack/roster/RosterEntry.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterEntry.java b/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterEntry.java index 2a387f13b..e73fdad8d 100644 --- a/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterEntry.java +++ b/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterEntry.java @@ -94,7 +94,7 @@ public final class RosterEntry extends Manager { * @throws XMPPErrorException * @throws NoResponseException */ - public void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException { + public synchronized void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException { // Do nothing if the name hasn't changed. if (name != null && name.equals(this.name)) { return; @@ -102,7 +102,10 @@ public final class RosterEntry extends Manager { RosterPacket packet = new RosterPacket(); packet.setType(IQ.Type.set); - packet.addRosterItem(toRosterItem(this)); + + // Create a new roster item with the current RosterEntry and the *new* name. Note that we can't set the name of + // RosterEntry right away, as otherwise the updated event wont get fired, because equalsDeep would return true. + packet.addRosterItem(toRosterItem(this, name)); connection().createPacketCollectorAndSend(packet).nextResultOrThrow(); // We have received a result response to the IQ set, the name was successfully changed @@ -246,7 +249,11 @@ public final class RosterEntry extends Manager { } static RosterPacket.Item toRosterItem(RosterEntry entry) { - RosterPacket.Item item = new RosterPacket.Item(entry.getUser(), entry.getName()); + return toRosterItem(entry, entry.getName()); + } + + private static RosterPacket.Item toRosterItem(RosterEntry entry, String name) { + RosterPacket.Item item = new RosterPacket.Item(entry.getUser(), name); item.setItemType(entry.getType()); item.setItemStatus(entry.getStatus()); // Set the correct group names for the item.