Index: trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java	(revision 18665)
@@ -20,4 +20,5 @@
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 
@@ -195,5 +196,5 @@
      * set would have to be checked.
      */
-    private transient Optional<OsmPrimitive> currentHighlight = Optional.empty();
+    private transient OsmPrimitive currentHighlight;
 
     /**
@@ -276,6 +277,4 @@
         determineMapMode(c.isPresent());
 
-        Optional<OsmPrimitive> newHighlight = Optional.empty();
-
         virtualManager.clear();
         if (mode == Mode.MOVE && !dragInProgress() && virtualManager.activateVirtualNodeNearPoint(e.getPoint())) {
@@ -286,5 +285,5 @@
             mv.setNewCursor(SelectActionCursor.virtual_node.cursor(), this);
             // don't highlight anything else if a virtual node will be
-            return repaintIfRequired(newHighlight);
+            return repaintIfRequired(null);
         }
 
@@ -293,13 +292,14 @@
         // return early if there can't be any highlights
         if (!drawTargetHighlight || (mode != Mode.MOVE && mode != Mode.SELECT) || !c.isPresent())
-            return repaintIfRequired(newHighlight);
+            return repaintIfRequired(null);
 
         // CTRL toggles selection, but if while dragging CTRL means merge
         final boolean isToggleMode = platformMenuShortcutKeyMask && !dragInProgress();
-        if (c.isPresent() && (isToggleMode || !c.get().isSelected())) {
+        OsmPrimitive newHighlight = null;
+        if (isToggleMode || !c.get().isSelected()) {
             // only highlight primitives that will change the selection
             // when clicked. I.e. don't highlight selected elements unless
             // we are in toggle mode.
-            newHighlight = c;
+            newHighlight = c.get();
         }
         return repaintIfRequired(newHighlight);
@@ -377,18 +377,21 @@
             ds.clearHighlightedVirtualNodes();
         }
-        if (!currentHighlight.isPresent()) {
+        if (currentHighlight == null) {
             return needsRepaint;
-        } else {
-            currentHighlight.get().setHighlighted(false);
-        }
-        currentHighlight = Optional.empty();
+        }
+        currentHighlight.setHighlighted(false);
+        currentHighlight = null;
         return true;
     }
 
-    private boolean repaintIfRequired(Optional<OsmPrimitive> newHighlight) {
-        if (!drawTargetHighlight || currentHighlight.equals(newHighlight))
+    private boolean repaintIfRequired(OsmPrimitive newHighlight) {
+        if (!drawTargetHighlight || Objects.equals(currentHighlight, newHighlight))
             return false;
-        currentHighlight.ifPresent(osm -> osm.setHighlighted(false));
-        newHighlight.ifPresent(osm -> osm.setHighlighted(true));
+        if (currentHighlight != null) {
+            currentHighlight.setHighlighted(false);
+        }
+        if (newHighlight != null) {
+            newHighlight.setHighlighted(true);
+        }
         currentHighlight = newHighlight;
         return true;
@@ -533,5 +536,5 @@
             if (p != null) {
                 p.setHighlighted(true);
-                currentHighlight = Optional.of(p);
+                currentHighlight = p;
                 needsRepaint = true;
             }
@@ -905,4 +908,7 @@
 
     static void checkCommandForLargeDistance(Command lastCommand) {
+        if (lastCommand == null) {
+            return;
+        }
         final int moveCount = lastCommand.getParticipatingPrimitives().size();
         if (lastCommand instanceof MoveCommand) {
Index: trunk/src/org/openstreetmap/josm/data/oauth/IOAuthAuthorization.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/oauth/IOAuthAuthorization.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/data/oauth/IOAuthAuthorization.java	(revision 18665)
@@ -2,4 +2,5 @@
 package org.openstreetmap.josm.data.oauth;
 
+import java.util.Optional;
 import java.util.function.Consumer;
 
@@ -16,4 +17,4 @@
      * @param scopes The scopes to ask for
      */
-    void authorize(IOAuthParameters parameters, Consumer<IOAuthToken> consumer, Enum<?>... scopes);
+    void authorize(IOAuthParameters parameters, Consumer<Optional<IOAuthToken>> consumer, Enum<?>... scopes);
 }
Index: trunk/src/org/openstreetmap/josm/data/oauth/IOAuthParameters.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/oauth/IOAuthParameters.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/data/oauth/IOAuthParameters.java	(revision 18665)
@@ -91,4 +91,7 @@
     }
 
+    /**
+     * Store the preferences for these parameters
+     */
     void rememberPreferences();
 }
Index: trunk/src/org/openstreetmap/josm/data/oauth/IOAuthToken.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/oauth/IOAuthToken.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/data/oauth/IOAuthToken.java	(revision 18665)
@@ -13,4 +13,5 @@
      * Sign a client
      * @param client The client to sign
+     * @throws OAuthException if the OAuth token type is unknown (AKA we do't know how to handle it)
      */
     void sign(HttpClient client) throws OAuthException;
Index: trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Authorization.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Authorization.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Authorization.java	(revision 18665)
@@ -13,4 +13,5 @@
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.UUID;
 import java.util.function.Consumer;
@@ -45,5 +46,5 @@
 
     @Override
-    public void authorize(IOAuthParameters parameters, Consumer<IOAuthToken> consumer, Enum<?>... scopes) {
+    public void authorize(IOAuthParameters parameters, Consumer<Optional<IOAuthToken>> consumer, Enum<?>... scopes) {
         final String state = UUID.randomUUID().toString();
         final String codeVerifier = UUID.randomUUID().toString(); // Cryptographically random string (ASCII)
@@ -62,8 +63,8 @@
         private final String state;
         private final IOAuthParameters parameters;
-        private final Consumer<IOAuthToken> consumer;
+        private final Consumer<Optional<IOAuthToken>> consumer;
         private final String codeVerifier;
 
-        OAuth20AuthorizationHandler(String state, String codeVerifier, IOAuthParameters parameters, Consumer<IOAuthToken> consumer) {
+        OAuth20AuthorizationHandler(String state, String codeVerifier, IOAuthParameters parameters, Consumer<Optional<IOAuthToken>> consumer) {
             this.state = state;
             this.parameters = parameters;
@@ -98,7 +99,7 @@
                     HttpClient.Response response = tradeCodeForToken.getResponse();
                     OAuth20Token oAuth20Token = new OAuth20Token(parameters, response.getContentReader());
-                    consumer.accept(oAuth20Token);
+                    consumer.accept(Optional.of(oAuth20Token));
                 } catch (IOException | OAuth20Exception e) {
-                    consumer.accept(null);
+                    consumer.accept(Optional.empty());
                     throw new JosmRuntimeException(e);
                 } finally {
Index: trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Token.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Token.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Token.java	(revision 18665)
@@ -20,4 +20,5 @@
 import org.openstreetmap.josm.tools.HttpClient;
 import org.openstreetmap.josm.tools.JosmRuntimeException;
+import org.openstreetmap.josm.tools.Utils;
 
 /**
@@ -81,5 +82,6 @@
     @Override
     public void sign(HttpClient client) throws OAuthException {
-        if (!this.oauthParameters.getApiUrl().contains(client.getURL().getHost())) {
+        if (!Utils.isBlank(this.oauthParameters.getApiUrl())
+                && !this.oauthParameters.getApiUrl().contains(client.getURL().getHost())) {
             String host = URI.create(this.oauthParameters.getAccessTokenUrl()).getHost();
             throw new IllegalArgumentException("Cannot sign URL with token for different host: Expected " + host
Index: trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/FilePaster.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/FilePaster.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/FilePaster.java	(revision 18665)
@@ -4,4 +4,5 @@
 import java.awt.datatransfer.DataFlavor;
 import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
@@ -15,4 +16,5 @@
 import org.openstreetmap.josm.gui.io.importexport.Options;
 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
+import org.openstreetmap.josm.tools.Utils;
 
 /**
@@ -32,10 +34,20 @@
     public boolean importData(TransferSupport support, OsmDataLayer layer, EastNorth pasteAt)
             throws UnsupportedFlavorException, IOException {
-        @SuppressWarnings("unchecked")
-        List<File> files = (List<File>) support.getTransferable().getTransferData(df);
-        OpenFileAction.OpenFileTask task = new OpenFileAction.OpenFileTask(files, null);
-        task.setOptions(Options.RECORD_HISTORY);
-        MainApplication.worker.submit(task);
-        return true;
+        final Object data = support.getTransferable().getTransferData(df);
+        if (data instanceof List) {
+            @SuppressWarnings("unchecked")
+            List<File> files = (List<File>) data;
+            OpenFileAction.OpenFileTask task = new OpenFileAction.OpenFileTask(files, null);
+            task.setOptions(Options.RECORD_HISTORY);
+            MainApplication.worker.submit(task);
+            return true;
+        }
+        // We should never hit this code -- Coverity thinks that it is possible for this to be called with a
+        // StringSelection transferable, which is not currently possible with our code. It *could* be done from
+        // a plugin though.
+        if (data instanceof Closeable) {
+            Utils.close((Closeable) data);
+        }
+        throw new UnsupportedFlavorException(df);
     }
 }
Index: trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/TagTransferPaster.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/TagTransferPaster.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/TagTransferPaster.java	(revision 18665)
@@ -3,4 +3,5 @@
 
 import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.Closeable;
 import java.io.IOException;
 import java.util.Map;
@@ -9,4 +10,5 @@
 
 import org.openstreetmap.josm.gui.datatransfer.data.TagTransferData;
+import org.openstreetmap.josm.tools.Utils;
 
 /**
@@ -25,6 +27,15 @@
     @Override
     protected Map<String, String> getTags(TransferSupport support) throws UnsupportedFlavorException, IOException {
-        TagTransferData data = (TagTransferData) support.getTransferable().getTransferData(df);
-        return data.getTags();
+        final Object data = support.getTransferable().getTransferData(df);
+        if (data instanceof TagTransferData) {
+            return ((TagTransferData) data).getTags();
+        }
+        // We should never hit this code -- Coverity thinks that it is possible for this to be called with a
+        // StringSelection transferable, which is not currently possible with our code. It *could* be done from
+        // a plugin though.
+        if (data instanceof Closeable) {
+            Utils.close((Closeable) data);
+        }
+        throw new UnsupportedFlavorException(df);
     }
 }
Index: trunk/src/org/openstreetmap/josm/gui/dialogs/layer/LayerListTransferHandler.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/layer/LayerListTransferHandler.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/layer/LayerListTransferHandler.java	(revision 18665)
@@ -6,4 +6,5 @@
 import java.awt.datatransfer.Transferable;
 import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.Closeable;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -22,4 +23,5 @@
 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
 import org.openstreetmap.josm.tools.Logging;
+import org.openstreetmap.josm.tools.Utils;
 
 /**
@@ -32,4 +34,6 @@
  */
 public class LayerListTransferHandler extends TransferHandler {
+    private static final long serialVersionUID = -5924044609120394316L;
+
     @Override
     public int getSourceActions(JComponent c) {
@@ -48,5 +52,5 @@
 
     private static boolean onlyDataLayersSelected(LayerListModel tableModel) {
-        return tableModel.getSelectedLayers().stream().allMatch(l -> l instanceof OsmDataLayer);
+        return tableModel.getSelectedLayers().stream().allMatch(OsmDataLayer.class::isInstance);
     }
 
@@ -79,6 +83,18 @@
             LayerListModel tableModel = (LayerListModel) ((JTable) support.getComponent()).getModel();
 
-            LayerTransferable.Data layers = (LayerTransferable.Data) support.getTransferable()
+            final Object data = support.getTransferable()
                     .getTransferData(LayerTransferable.LAYER_DATA);
+            final LayerTransferable.Data layers;
+            if (data instanceof LayerTransferable.Data) {
+                layers = (LayerTransferable.Data) data;
+            } else if (data instanceof Closeable) {
+                // We should never hit this code -- Coverity thinks that it is possible for this to be called with a
+                // StringSelection transferable, which is not currently possible with our code. It *could* be done from
+                // a plugin though.
+                Utils.close((Closeable) data);
+                throw new UnsupportedFlavorException(LayerTransferable.LAYER_DATA);
+            } else {
+                throw new UnsupportedFlavorException(LayerTransferable.LAYER_DATA);
+            }
 
             int dropLocation;
Index: trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java	(revision 18665)
@@ -56,5 +56,5 @@
  * @since 15245 (extracted from {@link ExpressionFactory})
  */
-@SuppressWarnings("UnusedDeclaration")
+@SuppressWarnings({"UnusedDeclaration", "squid:S100"})
 public final class Functions {
 
@@ -313,5 +313,5 @@
      * @param args arguments. First one is used as separator
      * @return assembled string
-     * @see String#join
+     * @see String#join(CharSequence, CharSequence...)
      */
     @NullableArguments
@@ -325,5 +325,5 @@
      * @param values collection of objects
      * @return assembled string
-     * @see String#join
+     * @see String#join(CharSequence, Iterable)
      */
     public static String join_list(final String separator, final List<String> values) {
@@ -739,4 +739,5 @@
      * @see Object#equals(Object)
      */
+    @SuppressWarnings("squid:S1221")
     public static boolean equal(Object a, Object b) {
         if (a.getClass() == b.getClass()) return a.equals(b);
@@ -775,5 +776,5 @@
 
     /**
-     * Obtains the JOSM'key {@link org.openstreetmap.josm.data.Preferences} string for key {@code key},
+     * Obtains the JOSM key {@link org.openstreetmap.josm.data.Preferences} string for key {@code key},
      * and defaults to {@code def} if that is null.
      *
Index: trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java	(revision 18665)
@@ -118,14 +118,18 @@
     public final void initFromPreferences() {
         final String authMethod = OsmApi.getAuthMethod();
-        if ("basic".equals(authMethod)) {
-            rbBasicAuthentication.setSelected(true);
-        } else if ("oauth".equals(authMethod)) {
-            rbOAuth.setSelected(true);
-        } else if ("oauth20".equals(authMethod)) {
-            rbOAuth20.setSelected(true);
-        } else {
-            Logging.warn(tr("Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.",
-                    "osm-server.auth-method", authMethod));
-            rbBasicAuthentication.setSelected(true);
+        switch (authMethod) {
+            case "basic":
+                rbBasicAuthentication.setSelected(true);
+                break;
+            case "oauth":
+                rbOAuth.setSelected(true);
+                break;
+            case "oauth20":
+                rbOAuth20.setSelected(true);
+                break;
+            default:
+                Logging.warn(tr("Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.",
+                        "osm-server.auth-method", authMethod));
+                rbBasicAuthentication.setSelected(true);
         }
         pnlBasicAuthPreferences.initFromPreferences();
Index: trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java	(revision 18665)
@@ -374,5 +374,5 @@
                     // Clean up old token/password
                     OAuthAccessTokenHolder.getInstance().setAccessToken(null);
-                    OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), token);
+                    OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), token.orElse(null));
                     OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance());
                     GuiHelper.runInEDT(OAuthAuthenticationPreferencesPanel.this::refreshView);
Index: trunk/src/org/openstreetmap/josm/io/OsmConnection.java
===================================================================
--- trunk/src/org/openstreetmap/josm/io/OsmConnection.java	(revision 18664)
+++ trunk/src/org/openstreetmap/josm/io/OsmConnection.java	(revision 18665)
@@ -11,4 +11,5 @@
 import java.util.Base64;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
@@ -215,5 +216,5 @@
         }
         CountDownLatch done = new CountDownLatch(1);
-        Consumer<IOAuthToken> consumer = authToken -> {
+        Consumer<Optional<IOAuthToken>> consumer = authToken -> {
                     if (!remoteControlIsRunning) {
                         RemoteControl.stop();
@@ -221,5 +222,5 @@
                     // Clean up old token/password
                     OAuthAccessTokenHolder.getInstance().setAccessToken(null);
-                    OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), authToken);
+                    OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), authToken.orElse(null));
                     OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance());
                     done.countDown();
@@ -229,20 +230,18 @@
                 OsmScopes.read_prefs, OsmScopes.write_prefs,
                 OsmScopes.write_api, OsmScopes.write_notes);
-        synchronized (done) {
-            // Only wait at most 5 minutes
-            int counter = 0;
-            while (done.getCount() >= 0 && counter < 5) {
-                try {
-                    if (done.await(1, TimeUnit.MINUTES)) {
-                        break;
-                    }
-                } catch (InterruptedException e) {
-                    Thread.currentThread().interrupt();
-                    Logging.trace(e);
-                    consumer.accept(null);
-                    throw new MissingOAuthAccessTokenException(e);
+        // Only wait at most 5 minutes
+        int counter = 0;
+        while (done.getCount() >= 0 && counter < 5) {
+            try {
+                if (done.await(1, TimeUnit.MINUTES)) {
+                    break;
                 }
-                counter++;
-            }
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                Logging.trace(e);
+                consumer.accept(null);
+                throw new MissingOAuthAccessTokenException(e);
+            }
+            counter++;
         }
     }
