Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix the warnings from SpotBugs #139

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id 'jacoco'
id 'com.github.kt3k.coveralls' version '2.12.0'
id "me.champeau.jmh" version "0.6.6"
id "com.github.spotbugs" version "5.1.2"
}

group = 'com.nulab-inc'
Expand All @@ -31,6 +32,19 @@ dependencies {
jmh group: 'org.ow2.asm', name: 'asm', version: '9.3'
}

spotbugs {
ignoreFailures = false
effort = 'max'
reportLevel = 'high'
}

spotbugsMain {
reports {
xml.enabled = false
html.enabled = true
}
}

task compileModuleInfoJava(type: JavaCompile, dependsOn: compileJava) {
classpath = files()
source = 'src/main/java9/module-info.java'
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/nulabinc/zxcvbn/Scoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.nulabinc.zxcvbn.matchers.Match;
import com.nulabinc.zxcvbn.matchers.MatchFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
Expand Down Expand Up @@ -48,12 +49,7 @@ public Strength mostGuessableMatchSequence(CharSequence password, List<Match> ma
matchesByJ.get(m.j).add(m);
}
for(List<Match> lst : matchesByJ) {
Collections.sort(lst, new Comparator<Match>() {
@Override
public int compare(Match m1, Match m2) {
return m1.i - m2.i;
}
});
Collections.sort(lst, new MatchComparator());
}
final Optimal optimal = new Optimal(n);
for (int k = 0; k < n; k++) {
Expand Down Expand Up @@ -162,6 +158,15 @@ private static long factorial(int n) {
return f;
}

private static class MatchComparator implements Comparator<Match>, Serializable {
private static final long serialVersionUID = 1L;

@Override
public int compare(Match m1, Match m2) {
return m1.i - m2.i;
}
}

private static class Optimal {

public final List<Map<Integer, Match>> m = new ArrayList<>();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/nulabinc/zxcvbn/Strength.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.nulabinc.zxcvbn.matchers.Match;

import java.util.ArrayList;
import java.util.List;

public class Strength {
Expand All @@ -17,6 +18,7 @@ public class Strength {
private long calcTime;

public Strength() {
this.sequence = new ArrayList<>();
}

public CharSequence getPassword() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/nulabinc/zxcvbn/Zxcvbn.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class Zxcvbn {

Expand Down Expand Up @@ -35,7 +36,7 @@ public Strength measure(CharSequence password, List<String> sanitizedInputs) {
if (sanitizedInputs != null && !sanitizedInputs.isEmpty()) {
lowerSanitizedInputs = new ArrayList<>(sanitizedInputs.size());
for (String sanitizedInput : sanitizedInputs) {
lowerSanitizedInputs.add(sanitizedInput.toLowerCase());
lowerSanitizedInputs.add(sanitizedInput.toLowerCase(Locale.getDefault()));
}
} else {
lowerSanitizedInputs = Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.IOException;
import java.io.InputStream;

public class ClasspathResource implements Resource {
public final class ClasspathResource implements Resource {

private final String path;

Expand Down
27 changes: 16 additions & 11 deletions src/main/java/com/nulabinc/zxcvbn/matchers/BaseMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.nulabinc.zxcvbn.Matcher;
import com.nulabinc.zxcvbn.WipeableString;

import java.io.Serializable;
import java.util.*;

public abstract class BaseMatcher implements Matcher {
Expand All @@ -19,17 +20,7 @@ protected Context getContext() {
}

protected List<Match> sorted(List<Match> matches) {
Collections.sort(matches, new Comparator<Match>() {
@Override
public int compare(Match o1, Match o2) {
int c = o1.i - o2.i;
if (c != 0) {
return c;
} else {
return (o1.j - o2.j);
}
}
});
Collections.sort(matches, new MatchComparator());
return matches;
}

Expand All @@ -48,4 +39,18 @@ protected CharSequence translate(CharSequence string, Map<Character, Character>
return result;
}

private static class MatchComparator implements Comparator<Match>, Serializable {
private static final long serialVersionUID = 1L;

@Override
public int compare(Match o1, Match o2) {
int c = o1.i - o2.i;
if (c != 0) {
return c;
} else {
return (o1.j - o2.j);
}
}
}

}
4 changes: 2 additions & 2 deletions src/main/java/com/nulabinc/zxcvbn/matchers/Keyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private Map<Position, String> buildPositionTable(final String layout) {
final int xUnit = tokenSize + 1;

for (String token : tokens) {
assert token.length() == tokenSize : String.format("token [%s] length mismatch:\n%s", token, layout);
assert token.length() == tokenSize : String.format("token [%s] length mismatch:%n%s", token, layout);
}

int y = 1;
Expand All @@ -131,7 +131,7 @@ private Map<Position, String> buildPositionTable(final String layout) {
int index = line.indexOf(token) - slant;
int x = index / xUnit;
final int remainder = index % xUnit;
assert remainder == 0 : String.format("unexpected x offset [%d] for %s in:\n%s", x, token, layout);
assert remainder == 0 : String.format("unexpected x offset [%d] for %s in:%n%s", x, token, layout);
positionTable.put(Position.of(x, y), token);
}

Expand Down