Skip to content

Commit 53e96f5

Browse files
author
Vladimir Kotal
authored
Sonar fixes in AnalyzerGuru (#3801)
* clean bunch of Sonar issues * move to issues * use equals() * Revert "use equals()" This reverts commit 608fd91. * join lines
1 parent 6f6b79b commit 53e96f5

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/AnalyzerGuru.java

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.io.Writer;
3434
import java.lang.reflect.InvocationTargetException;
3535
import java.nio.charset.StandardCharsets;
36+
import java.nio.file.Files;
3637
import java.util.ArrayList;
3738
import java.util.Arrays;
3839
import java.util.Collections;
@@ -392,29 +393,31 @@ public static List<AnalyzerFactory> getAnalyzerFactories() {
392393
return Collections.unmodifiableList(factories);
393394
}
394395

396+
private static final String USED_IN_MULTIPLE_MSG = "' used in multiple analyzers";
397+
395398
/**
396399
* Register a {@code FileAnalyzerFactory} instance.
397400
*/
398401
private static void registerAnalyzer(AnalyzerFactory factory) {
399402
for (String name : factory.getFileNames()) {
400403
AnalyzerFactory old = FILE_NAMES.put(name, factory);
401404
assert old == null :
402-
"name '" + name + "' used in multiple analyzers";
405+
"name '" + name + USED_IN_MULTIPLE_MSG;
403406
}
404407
for (String prefix : factory.getPrefixes()) {
405408
AnalyzerFactory old = pre.put(prefix, factory);
406409
assert old == null :
407-
"prefix '" + prefix + "' used in multiple analyzers";
410+
"prefix '" + prefix + USED_IN_MULTIPLE_MSG;
408411
}
409412
for (String suffix : factory.getSuffixes()) {
410413
AnalyzerFactory old = ext.put(suffix, factory);
411414
assert old == null :
412-
"suffix '" + suffix + "' used in multiple analyzers";
415+
"suffix '" + suffix + USED_IN_MULTIPLE_MSG;
413416
}
414417
for (String magic : factory.getMagicStrings()) {
415418
AnalyzerFactory old = magics.put(magic, factory);
416419
assert old == null :
417-
"magic '" + magic + "' used in multiple analyzers";
420+
"magic '" + magic + USED_IN_MULTIPLE_MSG;
418421
}
419422
matchers.addAll(factory.getMatchers());
420423
factories.add(factory);
@@ -596,7 +599,7 @@ public void populateDocument(Document doc, File file, String path, AbstractAnaly
596599
History history;
597600
if ((history = histGuru.getHistory(file)) != null) {
598601
List<HistoryEntry> historyEntries = history.getHistoryEntries(1, 0);
599-
if (historyEntries.size() > 0) {
602+
if (!historyEntries.isEmpty()) {
600603
HistoryEntry histEntry = historyEntries.get(0);
601604
doc.add(new TextField(QueryBuilder.LASTREV, histEntry.getRevision(), Store.YES));
602605
}
@@ -701,7 +704,7 @@ public static void writeDumpedXref(String contextPath,
701704
}
702705
Util.dumpXref(out, xrefTemp, false, contextPath);
703706
} finally {
704-
xrefTemp.delete();
707+
Files.delete(xrefTemp.toPath());
705708
}
706709
}
707710

@@ -867,19 +870,14 @@ private static AnalyzerFactory findFactory(Class<?> factoryClass)
867870
*
868871
* Use if you just want to find file type.
869872
*
870-
*
871873
* @param in The input stream containing the data
872874
* @param file The file name to get the analyzer for
873875
* @return the analyzer factory to use
874-
* @throws java.io.IOException If a problem occurs while reading the data
876+
* @throws java.io.IOException If a problem occurred while reading the data
875877
*/
876-
public static AnalyzerFactory find(InputStream in, String file)
877-
throws IOException {
878+
public static AnalyzerFactory find(InputStream in, String file) throws IOException {
878879
AnalyzerFactory factory = find(file);
879-
// TODO above is not that great, since if 2 analyzers share one extension
880-
// then only the first one registered will own it
881-
// it would be cool if above could return more analyzers and below would
882-
// then decide between them ...
880+
883881
if (factory != null) {
884882
return factory;
885883
}
@@ -1125,8 +1123,7 @@ private static String readOpening(InputStream in, byte[] sig)
11251123
int r;
11261124

11271125
StringBuilder opening = new StringBuilder();
1128-
BufferedReader readr = new BufferedReader(
1129-
new InputStreamReader(in, encoding), OPENING_MAX_CHARS);
1126+
BufferedReader readr = new BufferedReader(new InputStreamReader(in, encoding), OPENING_MAX_CHARS);
11301127
while ((r = readr.read()) != -1) {
11311128
if (++nRead > OPENING_MAX_CHARS) {
11321129
break;
@@ -1158,10 +1155,8 @@ private static String readOpening(InputStream in, byte[] sig)
11581155

11591156
// If the opening starts with "#!", then track so that any
11601157
// trailing whitespace after the hashbang is ignored.
1161-
if (opening.length() == 2) {
1162-
if (opening.charAt(0) == '#' && opening.charAt(1) == '!') {
1163-
postHashbang = true;
1164-
}
1158+
if (opening.length() == 2 && opening.charAt(0) == '#' && opening.charAt(1) == '!') {
1159+
postHashbang = true;
11651160
}
11661161
}
11671162

@@ -1175,25 +1170,24 @@ private static void addCustomizationKey(String k) {
11751170
customizationHashCode = Objects.hash(keys);
11761171
}
11771172

1178-
private static boolean factoriesDifferent(AnalyzerFactory a,
1179-
AnalyzerFactory b) {
1180-
String a_name = null;
1173+
private static boolean factoriesDifferent(AnalyzerFactory a, AnalyzerFactory b) {
1174+
String aName = null;
11811175
if (a != null) {
1182-
a_name = a.getName();
1183-
if (a_name == null) {
1184-
a_name = a.getClass().getSimpleName();
1176+
aName = a.getName();
1177+
if (aName == null) {
1178+
aName = a.getClass().getSimpleName();
11851179
}
11861180
}
1187-
String b_name = null;
1181+
String bName = null;
11881182
if (b != null) {
1189-
b_name = b.getName();
1190-
if (b_name == null) {
1191-
b_name = b.getClass().getSimpleName();
1183+
bName = b.getName();
1184+
if (bName == null) {
1185+
bName = b.getClass().getSimpleName();
11921186
}
11931187
}
1194-
if (a_name == null && b_name == null) {
1188+
if (aName == null && bName == null) {
11951189
return false;
11961190
}
1197-
return a_name == null || !a_name.equals(b_name);
1191+
return aName == null || !aName.equals(bName);
11981192
}
11991193
}

0 commit comments

Comments
 (0)