Skip to content

Commit cfc3361

Browse files
committed
fix: resolve static analysis issues in code
Fix various static analysis warnings including - Use string format placeholders in log messages - Fix keyword import ordering - Improve generic type usage - Optimize lambda expressions - Apply consistent code style These changes improve code quality and maintainability without changing functionality.
1 parent 251b84a commit cfc3361

File tree

57 files changed

+228
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+228
-243
lines changed

jabgui/src/main/java/org/jabref/Launcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static void initLogging(String[] args) {
105105

106106
// We must configure logging as soon as possible, which is why we cannot wait for the usual
107107
// argument parsing workflow to parse logging options e.g. --debug
108-
boolean isDebugEnabled = Arrays.stream(args).anyMatch(arg -> "--debug".equalsIgnoreCase(arg));
108+
boolean isDebugEnabled = Arrays.stream(args).anyMatch("--debug"::equalsIgnoreCase);
109109

110110
// addLogToDisk
111111
// We cannot use `Injector.instantiateModelOrService(BuildInfo.class).version` here, because this initializes logging

jabgui/src/main/java/org/jabref/gui/autocompleter/PersonNameStringConverter.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public PersonNameStringConverter(AutoCompletePreferences preferences) {
2828
autoCompFF = false;
2929
autoCompLF = true;
3030
break;
31-
default:
3231
case BOTH:
32+
default:
3333
autoCompFF = true;
3434
autoCompLF = true;
3535
break;
@@ -42,24 +42,20 @@ public PersonNameStringConverter(AutoCompletePreferences preferences) {
4242
public String toString(Author author) {
4343
if (autoCompLF) {
4444
switch (autoCompleteFirstNameMode) {
45-
case ONLY_ABBREVIATED:
45+
case ONLY_ABBREVIATED, BOTH:
4646
return author.getFamilyGiven(true);
4747
case ONLY_FULL:
4848
return author.getFamilyGiven(false);
49-
case BOTH:
50-
return author.getFamilyGiven(true);
5149
default:
5250
break;
5351
}
5452
}
5553
if (autoCompFF) {
5654
switch (autoCompleteFirstNameMode) {
57-
case ONLY_ABBREVIATED:
55+
case ONLY_ABBREVIATED, BOTH:
5856
return author.getGivenFamily(true);
5957
case ONLY_FULL:
6058
return author.getGivenFamily(false);
61-
case BOTH:
62-
return author.getGivenFamily(true);
6359
default:
6460
break;
6561
}

jabgui/src/main/java/org/jabref/gui/commonfxcontrols/CitationKeyPatternSuggestionCell.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ public CitationKeyPatternSuggestionTextField(List<String> citationKeyPatterns) {
7474
}
7575

7676
private void setListener() {
77-
textProperty().addListener((observable, oldValue, newValue) -> {
77+
textProperty().addListener((_, _, _) -> {
7878
String enteredText = getText();
7979
if (enteredText == null || enteredText.isEmpty()) {
8080
suggestionsList.hide();
8181
} else {
8282
List<String> filteredEntries = citationKeyPatterns.stream()
8383
.filter(e -> e.toLowerCase().contains(enteredText.toLowerCase()))
84-
.collect(Collectors.toList());
84+
.toList();
8585

8686
if (!filteredEntries.isEmpty()) {
8787
populatePopup(filteredEntries);
@@ -96,9 +96,7 @@ private void setListener() {
9696
}
9797
});
9898

99-
focusedProperty().addListener((observable, oldValue, newValue) -> {
100-
suggestionsList.hide();
101-
});
99+
focusedProperty().addListener((_, _, _) -> suggestionsList.hide());
102100
}
103101

104102
private void populatePopup(List<String> searchResult) {

jabgui/src/main/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private List<Node> createRowButtons(SortCriterionViewModel criterionViewModel) {
136136
private void clearCriterionRow(int row) {
137137
List<Node> criterionRow = sortCriterionList.getChildren().stream()
138138
.filter(item -> GridPane.getRowIndex(item) == row)
139-
.collect(Collectors.toList());
139+
.toList();
140140
sortCriterionList.getChildren().removeAll(criterionRow);
141141

142142
sortCriterionList.getChildren().stream()

jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckDialog.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,10 @@ public void initialize() {
8787
message.message().getFirst().equals(viewModel.selectedEntryTypeProperty().get())
8888
);
8989

90-
viewModel.selectedEntryTypeProperty().addListener((obs, oldValue, newValue) -> {
91-
filteredData.setPredicate(message ->
92-
message.message().getFirst().equals(newValue)
93-
);
94-
});
90+
viewModel.selectedEntryTypeProperty().addListener((_, _, newValue) ->
91+
filteredData.setPredicate(message ->
92+
message.message().getFirst().equals(newValue)
93+
));
9594

9695
tableView.setItems(filteredData);
9796

jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckDialogViewModel.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545

4646
public class ConsistencyCheckDialogViewModel extends AbstractViewModel {
4747

48-
private final Logger LOGGER = LoggerFactory.getLogger(ConsistencyCheckDialogViewModel.class);
48+
private static final Logger LOGGER = LoggerFactory.getLogger(ConsistencyCheckDialogViewModel.class);
49+
private static final int EXTRA_COLUMNS_COUNT = 2;
4950

5051
private final BibliographyConsistencyCheck.Result result;
5152
private final DialogService dialogService;
@@ -54,7 +55,6 @@ public class ConsistencyCheckDialogViewModel extends AbstractViewModel {
5455

5556
private final List<Field> allReportedFields;
5657
private final int columnCount;
57-
private final int EXTRA_COLUMNS_COUNT = 2;
5858
private final ObservableList<ConsistencyMessage> tableData = FXCollections.observableArrayList();
5959
private final StringProperty selectedEntryType = new SimpleStringProperty();
6060

@@ -85,9 +85,7 @@ public StringProperty selectedEntryTypeProperty() {
8585

8686
public List<String> getEntryTypes() {
8787
List<String> entryTypes = new ArrayList<>();
88-
result.entryTypeToResultMap().forEach((entrySet, entryTypeResult) -> {
89-
entryTypes.add(entrySet.toString());
90-
});
88+
result.entryTypeToResultMap().forEach((entrySet, _) -> entryTypes.add(entrySet.toString()));
9189
return entryTypes;
9290
}
9391

@@ -96,7 +94,7 @@ public ObservableList<ConsistencyMessage> getTableData() {
9694
}
9795

9896
public Set<String> getColumnNames() {
99-
Set<String> result = new LinkedHashSet<>(columnCount + EXTRA_COLUMNS_COUNT);
97+
Set<String> result = LinkedHashSet.newLinkedHashSet(columnCount + EXTRA_COLUMNS_COUNT);
10098
result.add("Entry Type");
10199
result.add("CitationKey");
102100
allReportedFields.forEach(field-> result.add(field.getDisplayName().trim()));
@@ -124,12 +122,12 @@ private void writeMapEntry(Map.Entry<EntryType, BibliographyConsistencyCheck.Ent
124122
BibliographyConsistencyCheck.EntryTypeResult entries = mapEntry.getValue();
125123
SequencedCollection<BibEntry> bibEntries = entries.sortedEntries();
126124

127-
bibEntries.forEach(Unchecked.consumer(bibEntry -> {
128-
writeBibEntry(bibEntry, entryType, requiredFields, optionalFields);
129-
}));
125+
bibEntries.forEach(Unchecked.consumer(bibEntry ->
126+
writeBibEntry(bibEntry, entryType, requiredFields, optionalFields)
127+
));
130128
}
131129

132-
private void writeBibEntry(BibEntry bibEntry, String entryType, Set<Field> requiredFields, Set<Field> optionalFields) throws IOException {
130+
private void writeBibEntry(BibEntry bibEntry, String entryType, Set<Field> requiredFields, Set<Field> optionalFields) {
133131
List<String> theRecord = getFindingsAsList(bibEntry, entryType, requiredFields, optionalFields);
134132
List<String> message = new ArrayList<>();
135133
for (String s: theRecord) {
@@ -143,7 +141,7 @@ private List<String> getFindingsAsList(BibEntry bibEntry, String entryType, Set<
143141
List<String> result = new ArrayList<>(columnCount + EXTRA_COLUMNS_COUNT);
144142
result.add(entryType);
145143
result.add(bibEntry.getCitationKey().orElse(""));
146-
allReportedFields.forEach(field -> {
144+
allReportedFields.forEach(field ->
147145
result.add(bibEntry.getField(field).map(_ -> {
148146
if (requiredFields.contains(field)) {
149147
return ConsistencySymbol.REQUIRED_FIELD_AT_ENTRY_TYPE_CELL_ENTRY.getText();
@@ -152,8 +150,8 @@ private List<String> getFindingsAsList(BibEntry bibEntry, String entryType, Set<
152150
} else {
153151
return ConsistencySymbol.UNKNOWN_FIELD_AT_ENTRY_TYPE_CELL_ENTRY.getText();
154152
}
155-
}).orElse(ConsistencySymbol.UNSET_FIELD_AT_ENTRY_TYPE_CELL_ENTRY.getText()));
156-
});
153+
}).orElse(ConsistencySymbol.UNSET_FIELD_AT_ENTRY_TYPE_CELL_ENTRY.getText()))
154+
);
157155
return result;
158156
}
159157

jabgui/src/main/java/org/jabref/gui/edit/EditAction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public void execute() {
7272
}
7373
} else if ((focusOwner instanceof CodeArea) || (focusOwner instanceof WebView)) {
7474
LOGGER.debug("Ignoring request in CodeArea or WebView");
75-
return;
7675
} else {
7776
LOGGER.debug("Else: {}", focusOwner.getClass().getSimpleName());
7877
// Not sure what is selected -> copy/paste/cut selected entries except for Preview and CodeArea

jabgui/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ private Optional<Path> askForSavePath() {
194194
// Workaround for linux systems not adding file extension
195195
if (!savePath.getFileName().toString().toLowerCase().endsWith(".bib")) {
196196
savePath = Path.of(savePath.toString() + ".bib");
197-
if (!Files.notExists(savePath)) {
198-
if (!dialogService.showConfirmationDialogAndWait(Localization.lang("Overwrite file"), Localization.lang("'%0' exists. Overwrite file?", savePath.getFileName()))) {
197+
if (!Files.notExists(savePath) && !dialogService.showConfirmationDialogAndWait(Localization.lang("Overwrite file"),
198+
Localization.lang("'%0' exists. Overwrite file?", savePath.getFileName()))) {
199199
return Optional.empty();
200200
}
201-
}
201+
202202
selectedPath = Optional.of(savePath);
203203
}
204204
}
@@ -246,7 +246,7 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) {
246246
dialogService.notify(Localization.lang("Library saved"));
247247
return success;
248248
} catch (SaveException ex) {
249-
LOGGER.error("A problem occurred when trying to save the file %s".formatted(targetPath), ex);
249+
LOGGER.error("A problem occurred when trying to save the file {}", targetPath, ex);
250250
dialogService.showErrorDialogAndWait(Localization.lang("Save library"), Localization.lang("Could not save file."), ex);
251251
return false;
252252
} finally {
@@ -307,7 +307,8 @@ private void saveWithDifferentEncoding(Path file, boolean selectedOnly, Charset
307307
.filter(buttonType -> buttonType.equals(tryDifferentEncoding))
308308
.isPresent();
309309
if (saveWithDifferentEncoding) {
310-
Optional<Charset> newEncoding = dialogService.showChoiceDialogAndWait(Localization.lang("Save library"), Localization.lang("Select new encoding"), Localization.lang("Save library"), encoding, Encodings.getCharsets());
310+
Optional<Charset> newEncoding = dialogService.showChoiceDialogAndWait(Localization.lang("Save library"), Localization.lang("Select new encoding"),
311+
Localization.lang("Save library"), encoding, Encodings.getCharsets());
311312
if (newEncoding.isPresent()) {
312313
// Make sure to remember which encoding we used.
313314
libraryTab.getBibDatabaseContext().getMetaData().setEncoding(newEncoding.get(), ChangePropagation.DO_NOT_POST_EVENT);

jabgui/src/main/java/org/jabref/gui/externalfiles/ChainedFilters.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ChainedFilters implements DirectoryStream.Filter<Path> {
1717

1818
private DirectoryStream.Filter<Path>[] filters;
1919

20+
@SafeVarargs
2021
public ChainedFilters(DirectoryStream.Filter<Path>... filters) {
2122
this.filters = filters;
2223
}

jabgui/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesCrawler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.nio.file.DirectoryStream;
56
import java.nio.file.DirectoryStream.Filter;
67
import java.nio.file.Files;
78
import java.nio.file.Path;
@@ -91,7 +92,8 @@ FileNodeViewModel searchDirectory(Path directory, UnlinkedPDFFileFilter unlinked
9192
// 2. GitIgnoreFilter
9293
ChainedFilters filters = new ChainedFilters(unlinkedPDFFileFilter, new GitIgnoreFileFilter(directory));
9394
Map<Boolean, List<Path>> directoryAndFilePartition;
94-
try (Stream<Path> filesStream = StreamSupport.stream(Files.newDirectoryStream(directory, filters).spliterator(), false)) {
95+
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(directory, filters);
96+
Stream<Path> filesStream = StreamSupport.stream(dirStream.spliterator(), false)) {
9597
directoryAndFilePartition = filesStream.collect(Collectors.partitioningBy(Files::isDirectory));
9698
} catch (IOException e) {
9799
LOGGER.error("Error while searching files", e);
@@ -136,7 +138,7 @@ FileNodeViewModel searchDirectory(Path directory, UnlinkedPDFFileFilter unlinked
136138
// create and add FileNodeViewModel to the FileNodeViewModel for the current directory
137139
fileNodeViewModelForCurrentDirectory.getChildren().addAll(resultingFiles.stream()
138140
.map(FileNodeViewModel::new)
139-
.collect(Collectors.toList()));
141+
.toList());
140142

141143
return fileNodeViewModelForCurrentDirectory;
142144
}

0 commit comments

Comments
 (0)