Fix FindBugs warning in PluginLoader

FindBugs says:

  Adding elements of an entry set may fail due to reuse of Map.Entry
  object.

  The entrySet() method is allowed to return a view of the underlying
  Map in which a single Entry object is reused and returned during the
  iteration. As of Java 1.6, both IdentityHashMap and EnumMap did so.

  When iterating through such a Map, the Entry value is only valid
  until you advance to the next iteration. If, for example, you try to
  pass such an entrySet to an addAll method, things will go badly wrong.

Instead of using addAll, explicitly add each entry by iterating over
the map.

Change-Id: I0468060f67c63c877469ec565eb594b960c52c89
This commit is contained in:
David Pursehouse 2014-07-08 16:40:54 +09:00
parent 49098b8717
commit 076e6a36cc

View File

@ -50,6 +50,7 @@ import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -398,6 +399,16 @@ public class PluginLoader implements LifecycleListener {
cleanInBackground();
}
private void addAllEntries(Map<String, File> from,
TreeSet<Entry<String, File>> to) {
Iterator<Entry<String, File>> it = from.entrySet().iterator();
while (it.hasNext()) {
Entry<String,File> entry = it.next();
to.add(new AbstractMap.SimpleImmutableEntry<String, File>(
entry.getKey(), entry.getValue()));
}
}
private TreeSet<Entry<String, File>> jarsFirstSortedPluginsSet(
Map<String, File> activePlugins) {
TreeSet<Entry<String, File>> sortedPlugins =
@ -415,7 +426,8 @@ public class PluginLoader implements LifecycleListener {
}
}
});
sortedPlugins.addAll(activePlugins.entrySet());
addAllEntries(activePlugins, sortedPlugins);
return sortedPlugins;
}