Skip to content

Commit

Permalink
0.3.8
Browse files Browse the repository at this point in the history
- Modified how exceptions are handled
- Added JetBrains annotations throughout the codebase
  • Loading branch information
Foulest committed Feb 2, 2024
1 parent f9f9580 commit 181f59b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 81 deletions.
43 changes: 17 additions & 26 deletions src/main/java/net/foulest/repairkit/RepairKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ private static void setRepairButtons() {
cleanJunkFiles();
latch.countDown();
} catch (Exception ex) {
MessageUtil.log(Level.WARNING, "Error cleaning junk files.");
ex.printStackTrace();
MessageUtil.printException(ex);
}
});

Expand All @@ -191,8 +190,7 @@ private static void setRepairButtons() {
repairWMIRepository();
latch.countDown();
} catch (Exception ex) {
MessageUtil.log(Level.WARNING, "Error repairing WMI repository.");
ex.printStackTrace();
MessageUtil.printException(ex);
}
});

Expand All @@ -202,8 +200,7 @@ private static void setRepairButtons() {
runServiceTweaks();
latch.countDown();
} catch (Exception ex) {
MessageUtil.log(Level.WARNING, "Error running service tweaks.");
ex.printStackTrace();
MessageUtil.printException(ex);
}
});

Expand All @@ -213,8 +210,7 @@ private static void setRepairButtons() {
removeStockApps();
latch.countDown();
} catch (Exception ex) {
MessageUtil.log(Level.WARNING, "Error removing stock apps.");
ex.printStackTrace();
MessageUtil.printException(ex);
}
});

Expand All @@ -224,8 +220,7 @@ private static void setRepairButtons() {
runRegistryTweaks();
latch.countDown();
} catch (Exception ex) {
MessageUtil.log(Level.WARNING, "Error running registry tweaks.");
ex.printStackTrace();
MessageUtil.printException(ex);
}
});

Expand All @@ -235,8 +230,7 @@ private static void setRepairButtons() {
runSettingsTweaks();
latch.countDown();
} catch (Exception ex) {
MessageUtil.log(Level.WARNING, "Error running settings tweaks.");
ex.printStackTrace();
MessageUtil.printException(ex);
}
});

Expand All @@ -245,8 +239,7 @@ private static void setRepairButtons() {
latch.await();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
MessageUtil.log(Level.WARNING, "Error waiting for tasks to complete.");
ex.printStackTrace();
MessageUtil.printException(ex);
}

// Shut down the executor
Expand All @@ -255,9 +248,11 @@ private static void setRepairButtons() {
// Displays a message dialog
playSound("win.sound.exclamation");
JOptionPane.showMessageDialog(null, "System issues repaired successfully.", "Finished", JOptionPane.QUESTION_MESSAGE);
} catch (Exception ignored) {
} catch (Exception ex) {
MessageUtil.printException(ex);
}
});

buttonRepairs.setBackground(new Color(200, 200, 200));
buttonRepairs.setBounds(5, 30, 310, 35);
addComponents(panelMain, buttonRepairs);
Expand All @@ -284,7 +279,8 @@ private static void setAppButtons() {
} else {
runCommand("start \"\" \"" + fanControlPath + "\"", false);
}
} catch (Exception ignored) {
} catch (Exception ex) {
MessageUtil.printException(ex);
}
});
buttonFanControl.setBackground(new Color(200, 200, 200));
Expand Down Expand Up @@ -471,8 +467,7 @@ private static void deleteSystemPolicies() {
latch.await();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
MessageUtil.log(Level.WARNING, "Error waiting for tasks to complete.");
ex.printStackTrace();
MessageUtil.printException(ex);
}

// Shut down the executor
Expand Down Expand Up @@ -837,8 +832,7 @@ private static void runRegistryTweaks() {
latch.await();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
MessageUtil.log(Level.WARNING, "Error waiting for tasks to complete.");
ex.printStackTrace();
MessageUtil.printException(ex);
}

// Shut down the executor
Expand Down Expand Up @@ -894,8 +888,7 @@ private static void runServiceTweaks() {
latch.await();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
MessageUtil.log(Level.WARNING, "Error waiting for tasks to complete.");
ex.printStackTrace();
MessageUtil.printException(ex);
}

// Shut down the executor
Expand Down Expand Up @@ -994,8 +987,7 @@ private static void removeStockApps() {
latch.await();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
MessageUtil.log(Level.WARNING, "Error waiting for tasks to complete.");
ex.printStackTrace();
MessageUtil.printException(ex);
}

// Shut down the executor
Expand Down Expand Up @@ -1099,8 +1091,7 @@ private static void runSettingsTweaks() {
latch.await();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
MessageUtil.log(Level.WARNING, "Error waiting for tasks to complete.");
ex.printStackTrace();
MessageUtil.printException(ex);
}

// Shut down the executor
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/net/foulest/repairkit/util/CommandUtil.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package net.foulest.repairkit.util;

import lombok.NonNull;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand All @@ -19,7 +20,7 @@ public class CommandUtil {
* @param command Command to run.
* @param async Whether to run the command asynchronously.
*/
public static void runCommand(@NonNull String command, boolean async) {
public static void runCommand(String command, boolean async) {
Runnable commandRunner = () -> {
try {
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", command);
Expand All @@ -29,8 +30,7 @@ public static void runCommand(@NonNull String command, boolean async) {
process.waitFor();
} catch (IOException | InterruptedException ex) {
Thread.currentThread().interrupt();
MessageUtil.log(Level.WARNING, "Failed to run command: " + command);
ex.printStackTrace();
MessageUtil.printException(ex);
}
};

Expand All @@ -49,7 +49,7 @@ public static void runCommand(@NonNull String command, boolean async) {
* @param async Whether to run the command asynchronously.
* @return The output of the command.
*/
public static List<String> getCommandOutput(@NonNull String command, boolean display, boolean async) {
public static @NotNull List<String> getCommandOutput(String command, boolean display, boolean async) {
List<String> output = new ArrayList<>();

runCommand(command, async, line -> {
Expand All @@ -69,8 +69,8 @@ public static List<String> getCommandOutput(@NonNull String command, boolean dis
* @param async Whether to run the command asynchronously.
* @param lineConsumer Consumer to consume the output of the command.
*/
private static void runCommand(@NonNull String command, boolean async,
@NonNull LineConsumer lineConsumer) {
private static void runCommand(String command, boolean async,
LineConsumer lineConsumer) {
Runnable commandRunner = () -> {
try {
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", command);
Expand All @@ -89,8 +89,7 @@ private static void runCommand(@NonNull String command, boolean async,
process.waitFor();
} catch (IOException | InterruptedException ex) {
Thread.currentThread().interrupt();
MessageUtil.log(Level.WARNING, "Failed to run command: " + command);
ex.printStackTrace();
MessageUtil.printException(ex);
}
};

Expand Down
19 changes: 7 additions & 12 deletions src/main/java/net/foulest/repairkit/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.foulest.repairkit.util;

import lombok.NonNull;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -29,7 +27,7 @@ public class FileUtil {
* @param fileZip The file to unzip.
* @param fileDest The destination to unzip the file to.
*/
public static void unzipFile(@NonNull String fileZip, @NonNull String fileDest) {
public static void unzipFile(String fileZip, String fileDest) {
fileZip = fileZip.replace("%temp%", System.getenv("TEMP"));
fileDest = fileDest.replace("%temp%", System.getenv("TEMP"));

Expand Down Expand Up @@ -59,8 +57,7 @@ public static void unzipFile(@NonNull String fileZip, @NonNull String fileDest)
}
}
} catch (IOException ex) {
MessageUtil.log(Level.WARNING, "Failed to unzip file: " + fileZip);
ex.printStackTrace();
MessageUtil.printException(ex);
}
}

Expand All @@ -72,7 +69,7 @@ public static void unzipFile(@NonNull String fileZip, @NonNull String fileDest)
* @param replaceOldFile Whether or not to replace the old file.
*/
@SuppressWarnings("unused")
public static void downloadFile(@NonNull String link, @NonNull String fileName, boolean replaceOldFile) {
public static void downloadFile(String link, String fileName, boolean replaceOldFile) {
DOWNLOAD_EXECUTOR.submit(() -> {
try {
URL url = new URL(link);
Expand All @@ -85,16 +82,15 @@ public static void downloadFile(@NonNull String link, @NonNull String fileName,
try (InputStream ignored = con.getInputStream()) {
// Returns if IP address is blocked.
} catch (IOException ex) {
MessageUtil.log(Level.WARNING, "Failed to download file: " + fileName);
ex.printStackTrace();
MessageUtil.printException(ex);
return;
}

try (InputStream inputStream = new BufferedInputStream(con.getInputStream())) {
saveFile(inputStream, fileName, replaceOldFile);
}
} catch (Exception ex) {
ex.printStackTrace();
MessageUtil.printException(ex);
}
});
}
Expand All @@ -106,7 +102,7 @@ public static void downloadFile(@NonNull String link, @NonNull String fileName,
* @param fileName The name of the file to save.
* @param replaceOldFile Whether or not to replace the old file.
*/
public static void saveFile(@NonNull InputStream input, @NonNull String fileName, boolean replaceOldFile) {
public static void saveFile(InputStream input, String fileName, boolean replaceOldFile) {
Path savedFilePath = Paths.get(String.valueOf(tempDirectory), fileName);

try {
Expand All @@ -124,8 +120,7 @@ public static void saveFile(@NonNull InputStream input, @NonNull String fileName

Files.copy(input, savedFilePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
MessageUtil.log(Level.WARNING, "Failed to save file: " + fileName);
ex.printStackTrace();
MessageUtil.printException(ex);
}
}
}
14 changes: 12 additions & 2 deletions src/main/java/net/foulest/repairkit/util/MessageUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.foulest.repairkit.util;

import lombok.NonNull;
import org.jetbrains.annotations.NotNull;

import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -21,7 +21,17 @@ public final class MessageUtil {
* @param level The level to log the message at.
* @param message The message to log.
*/
public static void log(@NonNull Level level, @NonNull String message) {
public static void log(Level level, String message) {
logger.log(level, message);
}

/**
* Prints an exception's message as a warning to the console.
*
* @param ex The exception to print.
*/
public static void printException(@NotNull Throwable ex) {
logger.log(Level.WARNING, "An error occurred: " + ex.getLocalizedMessage()
+ " (Caused by: " + ex.getCause() + ")");
}
}
16 changes: 7 additions & 9 deletions src/main/java/net/foulest/repairkit/util/RegistryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.sun.jna.Native;
import com.sun.jna.platform.win32.*;
import com.sun.jna.ptr.IntByReference;
import lombok.NonNull;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,7 +16,7 @@ public class RegistryUtil {
* @param hkey Registry key to check.
* @param keyPath Path to the registry key.
*/
private static void createRegistryKeyIfNeeded(@NonNull WinReg.HKEY hkey, @NonNull String keyPath) {
private static void createRegistryKeyIfNeeded(WinReg.HKEY hkey, String keyPath) {
if (!Advapi32Util.registryKeyExists(hkey, keyPath)) {
Advapi32Util.registryCreateKey(hkey, keyPath);
}
Expand All @@ -30,8 +30,7 @@ private static void createRegistryKeyIfNeeded(@NonNull WinReg.HKEY hkey, @NonNul
* @param keyName Name of the registry key.
* @param value Value to set.
*/
public static void setRegistryIntValue(@NonNull WinReg.HKEY hkey, @NonNull String keyPath,
@NonNull String keyName, int value) {
public static void setRegistryIntValue(WinReg.HKEY hkey, String keyPath, String keyName, int value) {
createRegistryKeyIfNeeded(hkey, keyPath);
Advapi32Util.registrySetIntValue(hkey, keyPath, keyName, value);
}
Expand All @@ -44,8 +43,7 @@ public static void setRegistryIntValue(@NonNull WinReg.HKEY hkey, @NonNull Strin
* @param keyName Name of the registry key.
* @param value Value to set.
*/
public static void setRegistryStringValue(@NonNull WinReg.HKEY hkey, @NonNull String keyPath,
@NonNull String keyName, @NonNull String value) {
public static void setRegistryStringValue(WinReg.HKEY hkey, String keyPath, String keyName, String value) {
createRegistryKeyIfNeeded(hkey, keyPath);
Advapi32Util.registrySetStringValue(hkey, keyPath, keyName, value);
}
Expand All @@ -57,7 +55,7 @@ public static void setRegistryStringValue(@NonNull WinReg.HKEY hkey, @NonNull St
* @param keyPath Path to the registry key.
* @param value Value to delete.
*/
public static void deleteRegistryValue(@NonNull WinReg.HKEY hkey, @NonNull String keyPath, @NonNull String value) {
public static void deleteRegistryValue(WinReg.HKEY hkey, String keyPath, String value) {
if (Advapi32Util.registryValueExists(hkey, keyPath, value)) {
Advapi32Util.registryDeleteValue(hkey, keyPath, value);
}
Expand All @@ -69,7 +67,7 @@ public static void deleteRegistryValue(@NonNull WinReg.HKEY hkey, @NonNull Strin
* @param hkey Registry key to check.
* @param keyPath Path to the registry key.
*/
public static void deleteRegistryKey(@NonNull WinReg.HKEY hkey, @NonNull String keyPath) {
public static void deleteRegistryKey(WinReg.HKEY hkey, String keyPath) {
if (Advapi32Util.registryKeyExists(hkey, keyPath)) {
Advapi32Util.registryDeleteKey(hkey, keyPath);
}
Expand All @@ -82,7 +80,7 @@ public static void deleteRegistryKey(@NonNull WinReg.HKEY hkey, @NonNull String
* @param keyPath Path to the registry key.
* @return List of sub keys.
*/
public static List<String> listSubKeys(@NonNull WinReg.HKEY root, @NonNull String keyPath) {
public static @NotNull List<String> listSubKeys(WinReg.HKEY root, String keyPath) {
List<String> subKeysList = new ArrayList<>();
WinReg.HKEYByReference hkeyRef = Advapi32Util.registryGetKey(root, keyPath, WinNT.KEY_READ);

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/net/foulest/repairkit/util/SoundUtil.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.foulest.repairkit.util;

import lombok.NonNull;

import java.awt.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -15,7 +13,7 @@ public class SoundUtil {
*
* @param soundName Name of the sound to play.
*/
public static void playSound(@NonNull String soundName) {
public static void playSound(String soundName) {
Runnable runnable = (Runnable) Toolkit.getDefaultToolkit().getDesktopProperty(soundName);

if (runnable != null) {
Expand Down
Loading

0 comments on commit 181f59b

Please sign in to comment.