From 5150d9323012b2457e95fb877b93b8c718368ffb Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Sat, 23 Apr 2022 08:09:42 +0800 Subject: [PATCH] fix code smells. (#104) * add missing @Override s. * array declaration beautify. * loop beautify. * fix error in javadoc. * replace addAll() to constructor. * use StringBuilder insteadof += * StringBuilder chain append. * add cache to travis. add openjdk8 to travis. * add javadoc-plugin to build in pom. * "static public" to "public static" --- .travis.yml | 6 +++ pom.xml | 38 +++++++++++++++- src/main/java/org/yinwang/pysonar/$.java | 15 +++---- .../java/org/yinwang/pysonar/Analyzer.java | 32 ++++++------- .../java/org/yinwang/pysonar/Binding.java | 1 + .../java/org/yinwang/pysonar/Outliner.java | 14 +++++- src/main/java/org/yinwang/pysonar/Parser.java | 6 +-- src/main/java/org/yinwang/pysonar/Stats.java | 2 +- .../org/yinwang/pysonar/ast/FunctionDef.java | 4 +- .../java/org/yinwang/pysonar/demos/Demo.java | 3 +- .../java/org/yinwang/pysonar/demos/Style.java | 1 + .../yinwang/pysonar/demos/StyleApplier.java | 16 +++---- .../pysonar/hash/FunTypeEqualFunction.java | 1 + .../pysonar/hash/GenericEqualFunction.java | 1 + .../org/yinwang/pysonar/hash/MyHashMap.java | 45 +++++++++++++++++-- .../org/yinwang/pysonar/hash/MyHashSet.java | 7 +++ .../org/yinwang/pysonar/types/UnionType.java | 6 +-- 17 files changed, 147 insertions(+), 51 deletions(-) diff --git a/.travis.yml b/.travis.yml index bd91a952..b136692c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,9 @@ language: java + +cache: + directories: + - "$HOME/.m2" + jdk: + - openjdk8 - oraclejdk12 diff --git a/pom.xml b/pom.xml index ea38f056..9a89999c 100644 --- a/pom.xml +++ b/pom.xml @@ -13,8 +13,11 @@ UTF-8 UTF-8 + 1.8 + 1.8 3.1 2.1 + 3.1.1 2.2.4 2.10.0 @@ -85,8 +88,8 @@ maven-compiler-plugin ${maven-compiler-plugin.version} - 1.8 - 1.8 + ${maven.compiler.source} + ${maven.compiler.target} @@ -123,6 +126,37 @@ + + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + ${maven.compiler.source} + true + utf-8 + utf-8 + true + + https://docs.oracle.com/javase/8/docs/api/ + http://docs.oracle.com/javaee/6/api/ + + + + true + true + + + + + + create-javadoc-jar + + javadoc + jar + + package + + + diff --git a/src/main/java/org/yinwang/pysonar/$.java b/src/main/java/org/yinwang/pysonar/$.java index e8a6cf18..a0e0fcfe 100644 --- a/src/main/java/org/yinwang/pysonar/$.java +++ b/src/main/java/org/yinwang/pysonar/$.java @@ -130,8 +130,7 @@ public static String arrayToString(@NotNull Collection strings) { @NotNull public static String arrayToSortedStringSet(Collection strings) { - Set sorter = new TreeSet<>(); - sorter.addAll(strings); + Set sorter = new TreeSet<>(strings); return arrayToString(sorter); } @@ -270,7 +269,7 @@ public static String getContentHash(byte[] fileContents) { algorithm.reset(); algorithm.update(fileContents); - byte messageDigest[] = algorithm.digest(); + byte[] messageDigest = algorithm.digest(); StringBuilder sb = new StringBuilder(); for (byte aMessageDigest : messageDigest) { sb.append(String.format("%02x", 0xFF & aMessageDigest)); @@ -279,7 +278,7 @@ public static String getContentHash(byte[] fileContents) { } - static public String escapeQname(@NotNull String s) { + public static String escapeQname(@NotNull String s) { return s.replaceAll("[.&@%-]", "_"); } @@ -300,7 +299,7 @@ public static Collection toStringCollection(@NotNull Collection @NotNull - static public String joinWithSep(@NotNull Collection ls, String sep, @Nullable String start, + public static String joinWithSep(@NotNull Collection ls, String sep, @Nullable String start, @Nullable String end) { StringBuilder sb = new StringBuilder(); @@ -581,11 +580,11 @@ public static String getGCStats() { StringBuilder sb = new StringBuilder(); sb.append(banner("memory stats")); - sb.append("\n- total collections: " + totalGC); - sb.append("\n- total collection time: " + formatTime(gcTime)); + sb.append("\n- total collections: ").append(totalGC); + sb.append("\n- total collection time: ").append(formatTime(gcTime)); Runtime runtime = Runtime.getRuntime(); - sb.append("\n- total memory: " + $.printMem(runtime.totalMemory())); + sb.append("\n- total memory: ").append($.printMem(runtime.totalMemory())); return sb.toString(); } diff --git a/src/main/java/org/yinwang/pysonar/Analyzer.java b/src/main/java/org/yinwang/pysonar/Analyzer.java index e56d9354..ebcf8a44 100644 --- a/src/main/java/org/yinwang/pysonar/Analyzer.java +++ b/src/main/java/org/yinwang/pysonar/Analyzer.java @@ -337,14 +337,14 @@ public String makeQname(@NotNull List names) { return ""; } - String ret = ""; + StringBuilder ret = new StringBuilder(); for (int i = 0; i < names.size() - 1; i++) { - ret += names.get(i).id + "."; + ret.append(names.get(i).id).append("."); } - ret += names.get(names.size() - 1).id; - return ret; + ret.append(names.get(names.size() - 1).id); + return ret.toString(); } @@ -563,13 +563,13 @@ public void applyUncalled() { @NotNull public String getAnalysisSummary() { StringBuilder sb = new StringBuilder(); - sb.append("\n" + $.banner("analysis summary")); + sb.append("\n").append($.banner("analysis summary")); String duration = $.formatTime(System.currentTimeMillis() - stats.getInt("startTime")); - sb.append("\n- total time: " + duration); - sb.append("\n- modules loaded: " + loadedFiles.size()); - sb.append("\n- semantic problems: " + semanticErrors.size()); - sb.append("\n- failed to parse: " + failedToParse.size()); + sb.append("\n- total time: ").append(duration); + sb.append("\n- modules loaded: ").append(loadedFiles.size()); + sb.append("\n- semantic problems: ").append(semanticErrors.size()); + sb.append("\n- failed to parse: ").append(failedToParse.size()); // calculate number of defs, refs, xrefs int nDef = 0, nXRef = 0; @@ -578,16 +578,16 @@ public String getAnalysisSummary() { nXRef += b.refs.size(); } - sb.append("\n- number of definitions: " + nDef); - sb.append("\n- number of cross references: " + nXRef); - sb.append("\n- number of references: " + references.size()); + sb.append("\n- number of definitions: ").append(nDef); + sb.append("\n- number of cross references: ").append(nXRef); + sb.append("\n- number of references: ").append(references.size()); long nResolved = resolved.size(); long nUnresolved = unresolved.size(); - sb.append("\n- resolved names: " + nResolved); - sb.append("\n- unresolved names: " + nUnresolved); - sb.append("\n- name resolve rate: " + $.percent(nResolved, nResolved + nUnresolved)); - sb.append("\n" + $.getGCStats()); + sb.append("\n- resolved names: ").append(nResolved); + sb.append("\n- unresolved names: ").append(nUnresolved); + sb.append("\n- name resolve rate: ").append($.percent(nResolved, nResolved + nUnresolved)); + sb.append("\n").append($.getGCStats()); return sb.toString(); } diff --git a/src/main/java/org/yinwang/pysonar/Binding.java b/src/main/java/org/yinwang/pysonar/Binding.java index c470d8cb..cdbfc6a8 100644 --- a/src/main/java/org/yinwang/pysonar/Binding.java +++ b/src/main/java/org/yinwang/pysonar/Binding.java @@ -211,6 +211,7 @@ public boolean isURL() { /** * Bindings can be sorted by their location for outlining purposes. */ + @Override public int compareTo(@NotNull Object o) { if (start == ((Binding) o).start) { return end - ((Binding) o).end; diff --git a/src/main/java/org/yinwang/pysonar/Outliner.java b/src/main/java/org/yinwang/pysonar/Outliner.java index 0043324d..b1c4bfdb 100644 --- a/src/main/java/org/yinwang/pysonar/Outliner.java +++ b/src/main/java/org/yinwang/pysonar/Outliner.java @@ -150,26 +150,31 @@ public Branch(String qname, int start, Binding.Kind kind) { } + @Override public boolean isLeaf() { return false; } + @Override public boolean isBranch() { return true; } + @Override public boolean hasChildren() { return children != null && !children.isEmpty(); } + @Override public List getChildren() { return children; } + @Override public void setChildren(List children) { this.children = children; } @@ -180,11 +185,13 @@ public void setChildren(List children) { * An entry with no children. */ public static class Leaf extends Entry { + @Override public boolean isLeaf() { return true; } + @Override public boolean isBranch() { return false; } @@ -199,17 +206,20 @@ public Leaf(String qname, int start, Binding.Kind kind) { } + @Override public boolean hasChildren() { return false; } + @Override @NotNull public List getChildren() { return new ArrayList<>(); } + @Override public void setChildren(List children) { throw new UnsupportedOperationException("Leaf nodes cannot have children."); } @@ -219,8 +229,8 @@ public void setChildren(List children) { /** * Create an outline for a file in the index. * - * @param scope the file scope - * @param path the file for which to build the outline + * @param idx the file scope + * @param abspath the file for which to build the outline * @return a list of entries constituting the file outline. * Returns an empty list if the analyzer hasn't analyzed that path. */ diff --git a/src/main/java/org/yinwang/pysonar/Parser.java b/src/main/java/org/yinwang/pysonar/Parser.java index 8c284e15..444debd4 100644 --- a/src/main/java/org/yinwang/pysonar/Parser.java +++ b/src/main/java/org/yinwang/pysonar/Parser.java @@ -894,7 +894,7 @@ List segmentQname(@NotNull String qname, int start, boolean hasLoc) { List result = new ArrayList<>(); for (int i = 0; i < qname.length(); i++) { - String name = ""; + StringBuilder name = new StringBuilder(); while (Character.isSpaceChar(qname.charAt(i))) { i++; } @@ -905,14 +905,14 @@ List segmentQname(@NotNull String qname, int start, boolean hasLoc) { qname.charAt(i) == '*') && qname.charAt(i) != '.') { - name += qname.charAt(i); + name.append(qname.charAt(i)); i++; } int nameStop = i; int nstart = hasLoc ? start + nameStart : -1; int nstop = hasLoc ? start + nameStop : -1; - result.add(new Name(name, file, nstart, nstop, 0, 0)); + result.add(new Name(name.toString(), file, nstart, nstop, 0, 0)); } return result; diff --git a/src/main/java/org/yinwang/pysonar/Stats.java b/src/main/java/org/yinwang/pysonar/Stats.java index d18343fc..7e1022a7 100644 --- a/src/main/java/org/yinwang/pysonar/Stats.java +++ b/src/main/java/org/yinwang/pysonar/Stats.java @@ -43,7 +43,7 @@ public String print() { StringBuilder sb = new StringBuilder(); for (Map.Entry e : contents.entrySet()) { - sb.append("\n- " + e.getKey() + ": " + e.getValue()); + sb.append("\n- ").append(e.getKey()).append(": ").append(e.getValue()); } return sb.toString(); diff --git a/src/main/java/org/yinwang/pysonar/ast/FunctionDef.java b/src/main/java/org/yinwang/pysonar/ast/FunctionDef.java index 233aa7e3..b729d4ee 100644 --- a/src/main/java/org/yinwang/pysonar/ast/FunctionDef.java +++ b/src/main/java/org/yinwang/pysonar/ast/FunctionDef.java @@ -61,14 +61,14 @@ public String getArgumentExpr() { argExpr.append(", "); } first = false; - argExpr.append("*" + vararg.toDisplay()); + argExpr.append("*").append(vararg.toDisplay()); } if (kwarg != null) { if (!first) { argExpr.append(", "); } - argExpr.append("**" + kwarg.toDisplay()); + argExpr.append("**").append(kwarg.toDisplay()); } argExpr.append(")"); diff --git a/src/main/java/org/yinwang/pysonar/demos/Demo.java b/src/main/java/org/yinwang/pysonar/demos/Demo.java index 4450a4ae..95b044d0 100644 --- a/src/main/java/org/yinwang/pysonar/demos/Demo.java +++ b/src/main/java/org/yinwang/pysonar/demos/Demo.java @@ -108,8 +108,7 @@ private String markup(String path) { return ""; } - List