From b3741409f5b90c06e6fbcd8207aef24c5d47468a Mon Sep 17 00:00:00 2001 From: Matteo Baccan Date: Wed, 14 Apr 2021 12:31:32 +0200 Subject: [PATCH] Code smell reduction (#2554) * Correct use of <> diamond operator * Correct modifiers order * Private constructor for utility class * Correct use of diamond operator * Corrected naming convention * Correct modifier order * Use not synchronized class * Introduced try/resource in stream copy * Removed unused private field * Code reformat Reformat of IOUtils.copy from to stream * Add a space Improved code formatting * Code reformat Only a new space * Code reformat Removed extra spaces --- .../java/brut/directory/AbstractDirectory.java | 18 +++++++++--------- .../src/main/java/brut/directory/DirUtil.java | 4 ++++ .../src/main/java/brut/directory/ZipUtils.java | 4 ++++ .../main/java/brut/util/DataInputDelegate.java | 2 +- brut.j.util/src/main/java/brut/util/Jar.java | 7 ++----- brut.j.util/src/main/java/brut/util/OS.java | 12 ++++++------ .../src/main/java/brut/util/OSDetection.java | 6 +++--- 7 files changed, 29 insertions(+), 24 deletions(-) diff --git a/brut.j.dir/src/main/java/brut/directory/AbstractDirectory.java b/brut.j.dir/src/main/java/brut/directory/AbstractDirectory.java index 94b63384..68110082 100644 --- a/brut.j.dir/src/main/java/brut/directory/AbstractDirectory.java +++ b/brut.j.dir/src/main/java/brut/directory/AbstractDirectory.java @@ -44,7 +44,7 @@ public abstract class AbstractDirectory implements Directory { return mFiles; } if (mFilesRecursive == null) { - mFilesRecursive = new LinkedHashSet(mFiles); + mFilesRecursive = new LinkedHashSet<>(mFiles); for (Map.Entry dir : getAbstractDirs().entrySet()) { for (String path : dir.getValue().getFiles(true)) { mFilesRecursive.add(dir.getKey() + separator + path); @@ -93,7 +93,7 @@ public abstract class AbstractDirectory implements Directory { @Override public Map getDirs(boolean recursive) throws UnsupportedOperationException { - return new LinkedHashMap(getAbstractDirs(recursive)); + return new LinkedHashMap<>(getAbstractDirs(recursive)); } @Override @@ -225,7 +225,7 @@ public abstract class AbstractDirectory implements Directory { return mDirs; } - Map dirs = new LinkedHashMap(mDirs); + Map dirs = new LinkedHashMap<>(mDirs); for (Map.Entry dir : getAbstractDirs().entrySet()) { for (Map.Entry subdir : dir.getValue().getAbstractDirs( true).entrySet()) { @@ -260,15 +260,15 @@ public abstract class AbstractDirectory implements Directory { return new ParsedPath(path.substring(0, pos), path.substring(pos + 1)); } - abstract protected void loadFiles(); - abstract protected void loadDirs(); - abstract protected InputStream getFileInputLocal(String name) + protected abstract void loadFiles(); + protected abstract void loadDirs(); + protected abstract InputStream getFileInputLocal(String name) throws DirectoryException; - abstract protected OutputStream getFileOutputLocal(String name) + protected abstract OutputStream getFileOutputLocal(String name) throws DirectoryException; - abstract protected AbstractDirectory createDirLocal(String name) + protected abstract AbstractDirectory createDirLocal(String name) throws DirectoryException; - abstract protected void removeFileLocal(String name); + protected abstract void removeFileLocal(String name); private class ParsedPath { diff --git a/brut.j.dir/src/main/java/brut/directory/DirUtil.java b/brut.j.dir/src/main/java/brut/directory/DirUtil.java index c7e96e4e..03959e02 100644 --- a/brut.j.dir/src/main/java/brut/directory/DirUtil.java +++ b/brut.j.dir/src/main/java/brut/directory/DirUtil.java @@ -28,6 +28,10 @@ import java.util.logging.Logger; public class DirUtil { private static final Logger LOGGER = Logger.getLogger(""); + private DirUtil() { + // Private constructor for utility class + } + public static void copyToDir(Directory in, Directory out) throws DirectoryException { for (String fileName : in.getFiles(true)) { diff --git a/brut.j.dir/src/main/java/brut/directory/ZipUtils.java b/brut.j.dir/src/main/java/brut/directory/ZipUtils.java index 758b0cc7..864af241 100644 --- a/brut.j.dir/src/main/java/brut/directory/ZipUtils.java +++ b/brut.j.dir/src/main/java/brut/directory/ZipUtils.java @@ -30,6 +30,10 @@ import java.util.zip.ZipOutputStream; public class ZipUtils { private static Collection mDoNotCompress; + + private ZipUtils() { + // Private constructor for utility class + } public static void zipFolders(final File folder, final File zip, final File assets, final Collection doNotCompress) throws BrutException, IOException { diff --git a/brut.j.util/src/main/java/brut/util/DataInputDelegate.java b/brut.j.util/src/main/java/brut/util/DataInputDelegate.java index 4b511961..ea579ee7 100644 --- a/brut.j.util/src/main/java/brut/util/DataInputDelegate.java +++ b/brut.j.util/src/main/java/brut/util/DataInputDelegate.java @@ -19,7 +19,7 @@ package brut.util; import java.io.DataInput; import java.io.IOException; -abstract public class DataInputDelegate implements DataInput { +public abstract class DataInputDelegate implements DataInput { protected final DataInput mDelegate; public DataInputDelegate(DataInput delegate) { diff --git a/brut.j.util/src/main/java/brut/util/Jar.java b/brut.j.util/src/main/java/brut/util/Jar.java index b9d2745e..01f4202a 100644 --- a/brut.j.util/src/main/java/brut/util/Jar.java +++ b/brut.j.util/src/main/java/brut/util/Jar.java @@ -21,14 +21,11 @@ import org.apache.commons.io.IOUtils; import java.io.*; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Set; import java.util.concurrent.ThreadLocalRandom; -abstract public class Jar { - private final static Set mLoaded = new HashSet(); - private final static Map mExtracted = new HashMap(); +public abstract class Jar { + private static final Map mExtracted = new HashMap<>(); public static File getResourceAsFile(String name, Class clazz) throws BrutException { File file = mExtracted.get(name); diff --git a/brut.j.util/src/main/java/brut/util/OS.java b/brut.j.util/src/main/java/brut/util/OS.java index e9b8636f..d29aa15e 100644 --- a/brut.j.util/src/main/java/brut/util/OS.java +++ b/brut.j.util/src/main/java/brut/util/OS.java @@ -72,11 +72,11 @@ public class OS { continue; } try { - InputStream in = new FileInputStream(file); - OutputStream out = new FileOutputStream(destFile); - IOUtils.copy(in, out); - in.close(); - out.close(); + try (InputStream in = new FileInputStream(file)) { + try (OutputStream out = new FileOutputStream(destFile)) { + IOUtils.copy(in, out); + } + } } catch (IOException ex) { throw new BrutException("Could not copy file: " + file, ex); } @@ -173,7 +173,7 @@ public class OS { } static class StreamCollector implements Runnable { - private final StringBuffer buffer = new StringBuffer(); + private final StringBuilder buffer = new StringBuilder(); private final InputStream inputStream; public StreamCollector(InputStream inputStream) { diff --git a/brut.j.util/src/main/java/brut/util/OSDetection.java b/brut.j.util/src/main/java/brut/util/OSDetection.java index 749c77d0..58dc5525 100644 --- a/brut.j.util/src/main/java/brut/util/OSDetection.java +++ b/brut.j.util/src/main/java/brut/util/OSDetection.java @@ -17,8 +17,8 @@ package brut.util; public class OSDetection { - private static String OS = System.getProperty("os.name").toLowerCase(); - private static String Bit = System.getProperty("sun.arch.data.model").toLowerCase(); + private static final String OS = System.getProperty("os.name").toLowerCase(); + private static final String BIT = System.getProperty("sun.arch.data.model").toLowerCase(); public static boolean isWindows() { return (OS.contains("win")); @@ -39,7 +39,7 @@ public class OSDetection { return arch != null && arch.endsWith("64") || wow64Arch != null && wow64Arch.endsWith("64"); } - return Bit.equalsIgnoreCase("64"); + return BIT.equalsIgnoreCase("64"); } public static String returnOS() {