Skip to content

Commit

Permalink
fix code smells. (#104)
Browse files Browse the repository at this point in the history
* 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"
  • Loading branch information
XenoAmess authored Apr 23, 2022
1 parent a4d14cd commit 5150d93
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 51 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
language: java

cache:
directories:
- "$HOME/.m2"

jdk:
- openjdk8
- oraclejdk12
38 changes: 36 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-shade-plugin.version>2.1</maven-shade-plugin.version>
<maven-javadoc-plugin.version>3.1.1</maven-javadoc-plugin.version>
<gson-version>2.2.4</gson-version>
<jackson-version>2.10.0</jackson-version>
</properties>
Expand Down Expand Up @@ -85,8 +88,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -123,6 +126,37 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<quiet>true</quiet>
<encoding>utf-8</encoding>
<docencoding>utf-8</docencoding>
<notimestamp>true</notimestamp>
<links>
<link>https://docs.oracle.com/javase/8/docs/api/</link>
<link>http://docs.oracle.com/javaee/6/api/</link>
</links>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>create-javadoc-jar</id>
<goals>
<goal>javadoc</goal>
<goal>jar</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/org/yinwang/pysonar/$.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ public static String arrayToString(@NotNull Collection<String> strings) {

@NotNull
public static String arrayToSortedStringSet(Collection<String> strings) {
Set<String> sorter = new TreeSet<>();
sorter.addAll(strings);
Set<String> sorter = new TreeSet<>(strings);
return arrayToString(sorter);
}

Expand Down Expand Up @@ -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));
Expand All @@ -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("[.&@%-]", "_");
}

Expand All @@ -300,7 +299,7 @@ public static Collection<String> toStringCollection(@NotNull Collection<Integer>


@NotNull
static public String joinWithSep(@NotNull Collection<String> ls, String sep, @Nullable String start,
public static String joinWithSep(@NotNull Collection<String> ls, String sep, @Nullable String start,
@Nullable String end)
{
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -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();
}
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/org/yinwang/pysonar/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,14 @@ public String makeQname(@NotNull List<Name> 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();
}


Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/yinwang/pysonar/Binding.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/yinwang/pysonar/Outliner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry> getChildren() {
return children;
}


@Override
public void setChildren(List<Entry> children) {
this.children = children;
}
Expand All @@ -180,11 +185,13 @@ public void setChildren(List<Entry> children) {
* An entry with no children.
*/
public static class Leaf extends Entry {
@Override
public boolean isLeaf() {
return true;
}


@Override
public boolean isBranch() {
return false;
}
Expand All @@ -199,17 +206,20 @@ public Leaf(String qname, int start, Binding.Kind kind) {
}


@Override
public boolean hasChildren() {
return false;
}


@Override
@NotNull
public List<Entry> getChildren() {
return new ArrayList<>();
}


@Override
public void setChildren(List<Entry> children) {
throw new UnsupportedOperationException("Leaf nodes cannot have children.");
}
Expand All @@ -219,8 +229,8 @@ public void setChildren(List<Entry> 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.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/yinwang/pysonar/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ List<Name> segmentQname(@NotNull String qname, int start, boolean hasLoc) {
List<Name> result = new ArrayList<>();

for (int i = 0; i < qname.length(); i++) {
String name = "";
StringBuilder name = new StringBuilder();
while (Character.isSpaceChar(qname.charAt(i))) {
i++;
}
Expand All @@ -905,14 +905,14 @@ List<Name> 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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/yinwang/pysonar/Stats.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public String print() {
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, Object> e : contents.entrySet()) {
sb.append("\n- " + e.getKey() + ": " + e.getValue());
sb.append("\n- ").append(e.getKey()).append(": ").append(e.getValue());
}

return sb.toString();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/yinwang/pysonar/ast/FunctionDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(")");
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/yinwang/pysonar/demos/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ private String markup(String path) {
return "";
}

List<Style> styles = new ArrayList<>();
styles.addAll(linker.getStyles(path));
List<Style> styles = new ArrayList<>(linker.getStyles(path));

String styledSource = new StyleApplier(path, source, styles).apply();
String outline = new HtmlOutline(analyzer).generate(path);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/yinwang/pysonar/demos/Style.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public boolean equals(Object o) {
}


@Override
public int compareTo(@NotNull Style other) {
if (this.equals(other)) {
return 0;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/yinwang/pysonar/demos/StyleApplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ void insert() {
if (Analyzer.self.hasOption("debug")) {
switch (style.type) {
case ANCHOR:
buffer.append("<a name='" + style.url + "'");
buffer.append(", id ='" + style.id + "'");
buffer.append("<a name='").append(style.url).append("'");
buffer.append(", id ='").append(style.id).append("'");
if (style.highlight != null && !style.highlight.isEmpty()) {
String ids = $.joinWithSep(style.highlight, "\",\"", "\"", "\"");
buffer.append(", onmouseover='highlight(").append(ids).append(")'");
}
break;
case LINK:
buffer.append("<a href='" + style.url + "'");
buffer.append(", id ='" + style.id + "'");
buffer.append("<a href='").append(style.url).append("'");
buffer.append(", id ='").append(style.id).append("'");
if (style.highlight != null && !style.highlight.isEmpty()) {
String ids = $.joinWithSep(style.highlight, "\",\"", "\"", "\"");
buffer.append(", onmouseover='highlight(").append(ids).append(")'");
Expand All @@ -90,12 +90,12 @@ void insert() {
} else {
switch (style.type) {
case ANCHOR:
buffer.append("<a name='" + style.url + "'");
buffer.append(", xid ='" + style.id + "'");
buffer.append("<a name='").append(style.url).append("'");
buffer.append(", xid ='").append(style.id).append("'");
break;
case LINK:
buffer.append("<a href='" + style.url + "'");
buffer.append(", xid ='" + style.id + "'");
buffer.append("<a href='").append(style.url).append("'");
buffer.append(", xid ='").append(style.id).append("'");
break;
default:
buffer.append("<span class='");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public class FunTypeEqualFunction extends EqualFunction {

@Override
public boolean equals(Object x, Object y) {
if (x instanceof FunType && y instanceof FunType) {
FunType xx = (FunType) x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


public class GenericEqualFunction extends EqualFunction {
@Override
public boolean equals(Object x, Object y) {
return x.equals(y);
}
Expand Down
Loading

0 comments on commit 5150d93

Please sign in to comment.