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 warning messages of checkstyle #156

Merged
merged 1 commit into from
Aug 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'maven-publish'
id 'signing'
id 'jacoco'
id 'checkstyle'
id 'com.github.kt3k.coveralls' version '2.12.2'
id "me.champeau.jmh" version "0.7.1"
id "com.github.spotbugs" version "5.1.3"
Expand Down Expand Up @@ -56,6 +57,17 @@ spotbugsMain {
}
}

checkstyle {
toolVersion = '10.12.1'
configProperties = [
"org.checkstyle.google.suppressionxpathfilter.config":
"${projectDir}/config/checkstyle/suppressions-xpath.xml",
]
ignoreFailures = false
maxWarnings = 0
checkstyleTest.enabled = false
}

task compileModuleInfoJava(type: JavaCompile, dependsOn: compileJava) {
classpath = files()
source = 'src/main/java9/module-info.java'
Expand Down
383 changes: 383 additions & 0 deletions config/checkstyle/checkstyle.xml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions config/checkstyle/suppressions-xpath.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionXpathFilter Experimental Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2_xpath_experimental.dtd">

<suppressions>
<suppress-xpath files="Keyboard\.java" checks="MemberName" query="//VARIABLE_DEF/IDENT[@text='x']"/>
<suppress-xpath files="Keyboard\.java" checks="MemberName" query="//VARIABLE_DEF/IDENT[@text='y']"/>
<suppress-xpath files="Match\.java" checks="MemberName" query="//VARIABLE_DEF/IDENT[@text='i']"/>
<suppress-xpath files="Match\.java" checks="MemberName" query="//VARIABLE_DEF/IDENT[@text='j']"/>
</suppressions>
42 changes: 24 additions & 18 deletions src/main/java/com/nulabinc/zxcvbn/Scoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Strength mostGuessableMatchSequence(
for (int k = 0; k < n; k++) {
for (Match m : matchesByJ.get(k)) {
if (m.i > 0) {
for (Map.Entry<Integer, Match> entry : optimal.m.get(m.i - 1).entrySet()) {
for (Map.Entry<Integer, Match> entry : optimal.matches.get(m.i - 1).entrySet()) {
int l = entry.getKey();
update(password, m, l + 1, optimal, excludeAdditive);
}
Expand All @@ -67,7 +67,7 @@ public Strength mostGuessableMatchSequence(
}
List<Match> optimalMatchSequence = unwind(n, optimal);
Integer optimalL = optimalMatchSequence.size();
double guesses = password.length() == 0 ? 1 : optimal.g.get(n - 1).get(optimalL);
double guesses = password.length() == 0 ? 1 : optimal.guesses.get(n - 1).get(optimalL);
Strength strength = new Strength();
strength.setPassword(password);
strength.setGuesses(guesses);
Expand All @@ -78,7 +78,6 @@ public Strength mostGuessableMatchSequence(

private void update(
CharSequence password, Match m, int l, Optimal optimal, boolean excludeAdditive) {
int k = m.j;
double pi = new EstimateGuess(this.context, password).exec(m);
if (l > 1) {
pi *= optimal.pi.get(m.i - 1).get(l - 1);
Expand All @@ -96,16 +95,17 @@ private void update(
g = Double.MAX_VALUE;
}
}
for (Map.Entry<Integer, Double> competing : optimal.g.get(k).entrySet()) {
int k = m.j;
for (Map.Entry<Integer, Double> competing : optimal.guesses.get(k).entrySet()) {
if (competing.getKey() > l) {
continue;
}
if (competing.getValue() <= g) {
return;
}
}
optimal.g.get(k).put(l, g);
optimal.m.get(k).put(l, m);
optimal.guesses.get(k).put(l, g);
optimal.matches.get(k).put(l, m);
optimal.pi.get(k).put(l, pi);
}

Expand All @@ -115,10 +115,10 @@ private void bruteforceUpdate(
update(password, m, 1, optimal, excludeAdditive);
for (int i = 1; i <= k; i++) {
m = makeBruteforceMatch(password, i, k);
for (Map.Entry<Integer, Match> entry : optimal.m.get(i - 1).entrySet()) {
for (Map.Entry<Integer, Match> entry : optimal.matches.get(i - 1).entrySet()) {
int l = entry.getKey();
Match last_m = entry.getValue();
if (last_m.pattern != Pattern.Bruteforce) {
Match lastMatch = entry.getValue();
if (lastMatch.pattern != Pattern.Bruteforce) {
update(password, m, l + 1, optimal, excludeAdditive);
}
}
Expand All @@ -131,14 +131,14 @@ private static List<Match> unwind(int n, Optimal optimal) {
if (0 <= k) {
int l = 0;
Double g = Double.POSITIVE_INFINITY;
for (Map.Entry<Integer, Double> candidate : optimal.g.get(k).entrySet()) {
for (Map.Entry<Integer, Double> candidate : optimal.guesses.get(k).entrySet()) {
if (candidate.getValue() < g) {
l = candidate.getKey();
g = candidate.getValue();
}
}
while (k >= 0) {
Match m = optimal.m.get(k).get(l);
Match m = optimal.matches.get(k).get(l);
optimalMatchSequence.add(0, m);
k = m.i - 1;
l--;
Expand All @@ -152,10 +152,16 @@ private static Match makeBruteforceMatch(CharSequence password, int i, int j) {
}

private static long factorial(int n) {
if (n < 2) return 1;
if (n > 19) return JS_NUMBER_MAX;
if (n < 2) {
return 1;
}
if (n > 19) {
return JS_NUMBER_MAX;
}
long f = 1;
for (int i = 2; i <= n; i++) f *= i;
for (int i = 2; i <= n; i++) {
f *= i;
}
return f;
}

Expand All @@ -170,17 +176,17 @@ public int compare(Match m1, Match m2) {

private static class Optimal {

public final List<Map<Integer, Match>> m = new ArrayList<>();
public final List<Map<Integer, Match>> matches = new ArrayList<>();

public final List<Map<Integer, Double>> pi = new ArrayList<>();

public final List<Map<Integer, Double>> g = new ArrayList<>();
public final List<Map<Integer, Double>> guesses = new ArrayList<>();

public Optimal(int n) {
for (int i = 0; i < n; i++) {
m.add(new HashMap<Integer, Match>());
matches.add(new HashMap<Integer, Match>());
pi.add(new HashMap<Integer, Double>());
g.add(new HashMap<Integer, Double>());
guesses.add(new HashMap<Integer, Double>());
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/nulabinc/zxcvbn/TimeEstimates.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static int guessesToScore(double guesses) {
}
if (guesses < 1e10 + DELTA) {
return 3;
} else return 4;
}
return 4;
}

public static String displayTime(final double seconds) {
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/nulabinc/zxcvbn/WipeableString.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public static int parseInt(CharSequence s, int radix) throws NumberFormatExcepti

int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int i = 0;
int len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
Expand All @@ -163,11 +164,12 @@ public static int parseInt(CharSequence s, int radix) throws NumberFormatExcepti
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
} else if (firstChar != '+') {
throw new NumberFormatException("For input string: \"" + s + "\"");

if (len == 1) // Cannot have lone "+" or "-"
throw new NumberFormatException("For input string: \"" + s + "\"");
}
if (len == 1) { // Cannot have lone "+" or "-"
throw new NumberFormatException("For input string: \"" + s + "\"");
}
i++;
}
multmin = limit / radix;
Expand Down Expand Up @@ -202,7 +204,7 @@ public int hashCode() {
// Reproduce the same hash as String
int h = hash;
if (h == 0 && content.length > 0) {
char val[] = content;
char[] val = content;

for (int i = 0; i < content.length; i++) {
h = 31 * h + val[i];
Expand Down Expand Up @@ -243,7 +245,9 @@ public boolean equals(Object obj) {
* not readOnly)
*/
public static void wipeIfPossible(CharSequence text) {
if (text == null) return;
if (text == null) {
return;
}
if (text instanceof WipeableString) {
((WipeableString) text).wipe();
} else if (text instanceof StringBuilder) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/nulabinc/zxcvbn/guesses/BaseGuess.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected Context getContext() {
return context;
}

protected static int nCk(int n, int k) {
protected static int calculateBinomialCoefficient(int n, int k) {
// http://blog.plover.com/math/choose.html
if (k > n) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public int uppercaseVariations(Match match) {
}
int variations = 0;
for (int i = 1; i <= Math.min(upperCount, lowerCount); i++) {
variations += nCk(upperCount + lowerCount, i);
variations += calculateBinomialCoefficient(upperCount + lowerCount, i);
}
lowercaseToken.wipe();
return variations;
Expand Down Expand Up @@ -77,7 +77,7 @@ public int l33tVariations(Match match) {
int minCount = Math.min(originalCount, substitutedCount);
int possibleCombinations = 0;
for (int i = 1; i <= minCount; i++) {
possibleCombinations += nCk(originalCount + substitutedCount, i);
possibleCombinations += calculateBinomialCoefficient(originalCount + substitutedCount, i);
}
totalVariations *= possibleCombinations;
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/nulabinc/zxcvbn/guesses/SpatialGuess.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ private double calculateBaseGuesses(Match match, int startingPositions, double a
for (int i = 2; i <= tokenLength; i++) {
int possibleTurns = Math.min(turns, i - 1);
for (int j = 1; j <= possibleTurns; j++) {
guesses += nCk(i - 1, j - 1) * startingPositions * Math.pow(averageDegree, j);
guesses +=
calculateBinomialCoefficient(i - 1, j - 1)
* startingPositions
* Math.pow(averageDegree, j);
}
}
return guesses;
Expand All @@ -50,7 +53,7 @@ private double calculateShiftedVariations(Match match) {
int shiftedVariations = 0;
int minCount = Math.min(shiftedCount, unshiftedCount);
for (int i = 1; i <= minCount; i++) {
shiftedVariations += nCk(shiftedCount + unshiftedCount, i);
shiftedVariations += calculateBinomialCoefficient(shiftedCount + unshiftedCount, i);
}
return shiftedVariations;
}
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/com/nulabinc/zxcvbn/io/ClasspathResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ public InputStream getInputStream() throws IOException {
* Finally, if even the system ClassLoader could not access resource as stream, return null.
*/
private InputStream getResourceAsStreamWithFallback(String path) {
// 0. try loading the resource from the same artifact as this class
{
InputStream in = getClass().getResourceAsStream(path);
if (in != null) {
return in;
}
} // no exceptions thrown
// Try loading the resource from the same artifact as this class
InputStream in = getClass().getResourceAsStream(path);
if (in != null) {
return in;
}

// 1. try to get resource with thread context ClassLoader
try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream in = this.getResourceAsStream(cl, path);
in = this.getResourceAsStream(cl, path);
if (in != null) {
return in;
}
Expand All @@ -52,7 +50,7 @@ private InputStream getResourceAsStreamWithFallback(String path) {
// 2. try to get resource with this class context ClassLoader
try {
ClassLoader cl = this.getClass().getClassLoader();
InputStream in = this.getResourceAsStream(cl, path);
in = this.getResourceAsStream(cl, path);
if (in != null) {
return in;
}
Expand All @@ -63,7 +61,7 @@ private InputStream getResourceAsStreamWithFallback(String path) {
// 3. try to get resource with this class context ClassLoader
try {
ClassLoader cl = ClassLoader.getSystemClassLoader();
InputStream in = this.getResourceAsStream(cl, path);
in = this.getResourceAsStream(cl, path);
if (in != null) {
return in;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/nulabinc/zxcvbn/matchers/BaseMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.nulabinc.zxcvbn.Context;
import com.nulabinc.zxcvbn.Matcher;
import java.io.Serializable;
import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public abstract class BaseMatcher implements Matcher {

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/nulabinc/zxcvbn/matchers/DateMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.nulabinc.zxcvbn.Context;
import com.nulabinc.zxcvbn.Scoring;
import com.nulabinc.zxcvbn.WipeableString;
import java.util.*;
import java.util.regex.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/*
* a "date" is recognized as:
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/nulabinc/zxcvbn/matchers/Keyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ private Map<Position, String> buildPositionTable(final String layout) {
private static List<String> split(final String str, final SplitMatcher splitMatcher) {
final int len = str.length();
final List<String> list = new ArrayList<>();
int i = 0, start = 0;
int i = 0;
int start = 0;
boolean match = false;
while (i < len) {
if (splitMatcher.match(str.charAt(i))) {
Expand Down Expand Up @@ -208,8 +209,12 @@ public int hashCode() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Position)) return false;
if (this == o) {
return true;
}
if (!(o instanceof Position)) {
return false;
}

final Position position = (Position) o;

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/nulabinc/zxcvbn/matchers/L33tMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import com.nulabinc.zxcvbn.Context;
import com.nulabinc.zxcvbn.WipeableString;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class L33tMatcher extends BaseMatcher {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.*;
import java.util.regex.Pattern;

public class RegexMatcher extends BaseMatcher {

Expand Down