From 8bd592cedd75d6a0094199a9ba73736d86350a8d Mon Sep 17 00:00:00 2001 From: retgal Date: Thu, 22 Feb 2024 16:20:08 +0100 Subject: [PATCH] Fix code smells --- src/main/java/mpo/dayon/assistant/gui/Assistant.java | 9 ++++++++- .../dayon/assistant/network/NetworkAssistantEngine.java | 2 +- src/main/java/mpo/dayon/assisted/gui/Assisted.java | 5 ++++- .../dayon/assisted/network/NetworkAssistedEngine.java | 2 +- .../java/mpo/dayon/common/network/NetworkEngine.java | 8 +++++--- src/main/java/mpo/dayon/common/version/Version.java | 3 +++ 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/java/mpo/dayon/assistant/gui/Assistant.java b/src/main/java/mpo/dayon/assistant/gui/Assistant.java index b7564915..b5f3aa65 100644 --- a/src/main/java/mpo/dayon/assistant/gui/Assistant.java +++ b/src/main/java/mpo/dayon/assistant/gui/Assistant.java @@ -219,7 +219,11 @@ public void actionPerformed(ActionEvent ev) { Log.error("Could not determine public IP", ex); JOptionPane.showMessageDialog(frame, translate("ipAddress.msg2"), translate("ipAddress"), JOptionPane.ERROR_MESSAGE); - } finally { + if (ex instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } + } + finally { frame.setCursor(cursor); } } @@ -609,6 +613,9 @@ public void actionPerformed(ActionEvent ev) { Log.error("Could not obtain token", ex); JOptionPane.showMessageDialog(frame, translate("token.create.error.msg"), translate("token"), JOptionPane.ERROR_MESSAGE); + if (ex instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } } finally { frame.setCursor(cursor); } diff --git a/src/main/java/mpo/dayon/assistant/network/NetworkAssistantEngine.java b/src/main/java/mpo/dayon/assistant/network/NetworkAssistantEngine.java index a4dbdf9b..3a52c0fe 100644 --- a/src/main/java/mpo/dayon/assistant/network/NetworkAssistantEngine.java +++ b/src/main/java/mpo/dayon/assistant/network/NetworkAssistantEngine.java @@ -131,7 +131,7 @@ private void receivingLoop(boolean compatibilityMode) throws NoSuchAlgorithmExce } catch (CertificateEncodingException ex) { Log.error(ex.getMessage()); } catch (ClassNotFoundException e) { - throw new RuntimeException(e); + throw new IllegalArgumentException(e); } finally { closeConnections(); UPnP.closePortTCP(configuration.getPort()); diff --git a/src/main/java/mpo/dayon/assisted/gui/Assisted.java b/src/main/java/mpo/dayon/assisted/gui/Assisted.java index e9ebca24..6c664fd0 100644 --- a/src/main/java/mpo/dayon/assisted/gui/Assisted.java +++ b/src/main/java/mpo/dayon/assisted/gui/Assisted.java @@ -181,8 +181,11 @@ private void applyConnectionSettings(ConnectionSettingsDialog connectionSettings String connectionParams = null; try { connectionParams = resolveToken(tokenServerUrl, token); - } catch (IOException | InterruptedException ie){ + } catch (IOException | InterruptedException ex){ Log.warn("Could not resolve token " + token); + if (ex instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } } Log.debug("Connection params " + connectionParams); newConfiguration = extractConfiguration(connectionParams); diff --git a/src/main/java/mpo/dayon/assisted/network/NetworkAssistedEngine.java b/src/main/java/mpo/dayon/assisted/network/NetworkAssistedEngine.java index 6412465a..b2aaa9c0 100644 --- a/src/main/java/mpo/dayon/assisted/network/NetworkAssistedEngine.java +++ b/src/main/java/mpo/dayon/assisted/network/NetworkAssistedEngine.java @@ -195,7 +195,7 @@ private void receivingLoop() { } catch (IOException ex) { handleIOException(ex); } catch (ClassNotFoundException e) { - throw new RuntimeException(e); + throw new IllegalArgumentException(e); } finally { closeConnections(); fireOnDisconnecting(); diff --git a/src/main/java/mpo/dayon/common/network/NetworkEngine.java b/src/main/java/mpo/dayon/common/network/NetworkEngine.java index cebf8f51..6f397928 100644 --- a/src/main/java/mpo/dayon/common/network/NetworkEngine.java +++ b/src/main/java/mpo/dayon/common/network/NetworkEngine.java @@ -27,6 +27,8 @@ public abstract class NetworkEngine { protected static final String UNSUPPORTED_TYPE = "Unsupported message type [%s]!"; + private final static String CLIPBOARD_DEBUG = "setClipboardContents %s"; + protected NetworkSender sender; // out protected NetworkSender fileSender; // file out @@ -77,18 +79,18 @@ public void sendClipboardFiles(List files, long size, String basePath) { } protected void setClipboardContents(String string, ClipboardOwner clipboardOwner) { - Log.debug("setClipboardContents %s", () -> string); + Log.debug(CLIPBOARD_DEBUG, () -> string); StringSelection stringSelection = new StringSelection(string); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, clipboardOwner); } protected void setClipboardContents(BufferedImage image, ClipboardOwner clipboardOwner) { - Log.debug("setClipboardContents %s", () -> format("%dx%d", image.getWidth(), image.getHeight())); + Log.debug(CLIPBOARD_DEBUG, () -> format("%dx%d", image.getWidth(), image.getHeight())); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new TransferableImage(image), clipboardOwner); } private void setClipboardContents(List files, ClipboardOwner clipboardOwner) { - Log.debug("setClipboardContents %s", () -> String.valueOf(files)); + Log.debug(CLIPBOARD_DEBUG, () -> String.valueOf(files)); TransferableFiles transferableFiles = new TransferableFiles(files); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferableFiles, clipboardOwner); } diff --git a/src/main/java/mpo/dayon/common/version/Version.java b/src/main/java/mpo/dayon/common/version/Version.java index b2730dcb..c8c8dbd7 100644 --- a/src/main/java/mpo/dayon/common/version/Version.java +++ b/src/main/java/mpo/dayon/common/version/Version.java @@ -75,6 +75,9 @@ public String getLatestRelease() { } } catch (IOException | InterruptedException e) { Log.error("Exception", e); + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } } } return latestVersion;