Skip to content

Commit 0dd2007

Browse files
author
Vladimir Kotal
authored
cleanup Sonar issues in repositories (#3792)
* cleanup Sonar issues * fix style * avoid isLoggable()
1 parent 0ec550c commit 0dd2007

File tree

6 files changed

+51
-70
lines changed

6 files changed

+51
-70
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/BitKeeperHistoryParser.java

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

2020
/*
2121
* Copyright (c) 2017, James Service <[email protected]>.
22-
* Portions Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2323
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
2424
*/
2525
package org.opengrok.indexer.history;
@@ -126,11 +126,9 @@ public void processStream(InputStream input) throws IOException {
126126
if (fields.length == 5) {
127127
renamedFiles.add(fields[4]);
128128
}
129-
} else if (line.startsWith("C ")) {
130-
if (entry != null) {
131-
final String messageLine = line.substring(2);
132-
entry.appendMessage(messageLine);
133-
}
129+
} else if (line.startsWith("C ") && (entry != null)) {
130+
final String messageLine = line.substring(2);
131+
entry.appendMessage(messageLine);
134132
}
135133
}
136134

opengrok-indexer/src/main/java/org/opengrok/indexer/history/BitKeeperTagParser.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
* CDDL HEADER END
1818
*/
1919

20+
/*
21+
* Copyright (c) 2017, James Service <[email protected]>.
22+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
23+
*/
2024
package org.opengrok.indexer.history;
2125

2226
import java.io.BufferedReader;
@@ -46,8 +50,9 @@ public class BitKeeperTagParser implements Executor.StreamHandler {
4650
* Parses dates.
4751
*/
4852
private final SimpleDateFormat dateFormat;
53+
4954
/**
50-
* Store tag entries created by {@link processStream()}.
55+
* Store tag entries created by {@link #processStream(InputStream)}.
5156
*/
5257
private final TreeSet<TagEntry> entries = new TreeSet<>();
5358

@@ -85,7 +90,7 @@ public TreeSet<TagEntry> getEntries() {
8590
public void processStream(InputStream input) throws IOException {
8691
String revision = null;
8792
Date date = null;
88-
String tag = null;
93+
String tag;
8994

9095
final BufferedReader in = new BufferedReader(new InputStreamReader(input));
9196
for (String line = in.readLine(); line != null; line = in.readLine()) {
@@ -97,11 +102,9 @@ public void processStream(InputStream input) throws IOException {
97102
} catch (final Exception e) {
98103
LOGGER.log(Level.SEVERE, "Error: malformed BitKeeper tags output {0}", line);
99104
}
100-
} else if (line.startsWith("T ")) {
101-
if (date != null) {
102-
tag = line.substring(2);
103-
entries.add(new BitKeeperTagEntry(revision, date, tag));
104-
}
105+
} else if (line.startsWith("T ") && (date != null)) {
106+
tag = line.substring(2);
107+
entries.add(new BitKeeperTagEntry(revision, date, tag));
105108
}
106109
}
107110
}

opengrok-indexer/src/main/java/org/opengrok/indexer/history/CVSRepository.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
@@ -36,6 +36,7 @@
3636
import java.util.logging.Level;
3737
import java.util.logging.Logger;
3838

39+
import org.jetbrains.annotations.Nullable;
3940
import org.opengrok.indexer.configuration.CommandTimeoutType;
4041
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4142
import org.opengrok.indexer.logger.LoggerFactory;
@@ -59,7 +60,7 @@ public class CVSRepository extends RCSRepository {
5960
public static final String CMD_FALLBACK = "cvs";
6061

6162
public CVSRepository() {
62-
/**
63+
/*
6364
* This variable is set in the ancestor to TRUE which has a side effect
6465
* that this repository is always marked as working even though it does
6566
* not have the binary available on the system.
@@ -97,24 +98,17 @@ public void setDirectoryName(File directory) {
9798
if (isWorking()) {
9899
File rootFile = new File(getDirectoryName() + File.separatorChar
99100
+ "CVS" + File.separatorChar + "Root");
100-
BufferedReader input;
101101
String root;
102-
try {
103-
input = new BufferedReader(new FileReader(rootFile));
104-
try {
105-
root = input.readLine();
106-
} catch (java.io.IOException e) {
107-
LOGGER.log(Level.WARNING, "failed to load", e);
108-
return;
109-
} finally {
110-
try {
111-
input.close();
112-
} catch (java.io.IOException e) {
113-
LOGGER.log(Level.INFO, "failed to close", e);
114-
}
115-
}
116-
} catch (java.io.FileNotFoundException e) {
117-
LOGGER.log(Level.FINE, "not loading CVS Root file", e);
102+
103+
if (!rootFile.exists()) {
104+
LOGGER.log(Level.FINE, "CVS Root file {0} does not exist", rootFile);
105+
return;
106+
}
107+
108+
try (BufferedReader input = new BufferedReader(new FileReader(rootFile))) {
109+
root = input.readLine();
110+
} catch (IOException e) {
111+
LOGGER.log(Level.WARNING, String.format("failed to read %s", rootFile), e);
118112
return;
119113
}
120114

@@ -229,8 +223,7 @@ public InputStream getHistoryGet(String parent, String basename, String rev) {
229223

230224
ret = new ByteArrayInputStream(out.toByteArray());
231225
} catch (Exception exp) {
232-
LOGGER.log(Level.WARNING,
233-
"Failed to get history: {0}", exp.getClass().toString());
226+
LOGGER.log(Level.WARNING, "Failed to get history", exp);
234227
}
235228

236229
return ret;
@@ -284,6 +277,7 @@ Annotation annotate(File file, String revision) throws IOException {
284277
}
285278

286279
@Override
280+
@Nullable
287281
String determineParent(CommandTimeoutType cmdType) throws IOException {
288282
File rootFile = new File(getDirectoryName() + File.separator + "CVS"
289283
+ File.separator + "Root");
@@ -293,9 +287,7 @@ String determineParent(CommandTimeoutType cmdType) throws IOException {
293287
try (BufferedReader br = new BufferedReader(new FileReader(rootFile))) {
294288
parent = br.readLine();
295289
} catch (IOException ex) {
296-
LOGGER.log(Level.WARNING,
297-
"Failed to read CVS/Root file {0}: {1}",
298-
new Object[]{rootFile, ex.getClass().toString()});
290+
LOGGER.log(Level.WARNING, String.format("Failed to read CVS/Root file %s", rootFile), ex);
299291
}
300292
}
301293

opengrok-indexer/src/main/java/org/opengrok/indexer/history/DirectoryHistoryReader.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ public DirectoryHistoryReader(String path) throws IOException {
130130
try {
131131
cdate = DateTools.stringToDate(doc.get(QueryBuilder.DATE));
132132
} catch (java.text.ParseException ex) {
133-
LOGGER.log(Level.WARNING,
134-
"Could not get date for " + path, ex);
133+
LOGGER.log(Level.WARNING, String.format("Could not get date for %s", path), ex);
135134
cdate = new Date();
136135
}
137136
int ls = rpath.lastIndexOf('/');
@@ -143,8 +142,7 @@ public DirectoryHistoryReader(String path) throws IOException {
143142
File f = new File(srcRoot + rparent, rbase);
144143
hist = HistoryGuru.getInstance().getHistory(f);
145144
} catch (HistoryException e) {
146-
LOGGER.log(Level.WARNING,
147-
"An error occurred while getting history reader", e);
145+
LOGGER.log(Level.WARNING, "An error occurred while getting history reader", e);
148146
}
149147
if (hist == null) {
150148
put(cdate, "", "-", "", rpath);
@@ -170,8 +168,7 @@ public DirectoryHistoryReader(String path) throws IOException {
170168
try {
171169
ireader.close();
172170
} catch (Exception ex) {
173-
LOGGER.log(Level.WARNING,
174-
"An error occurred while closing reader", ex);
171+
LOGGER.log(Level.WARNING, "An error occurred while closing reader", ex);
175172
}
176173
}
177174
}
@@ -204,9 +201,8 @@ private void put(Date date, String revision, String author, String comment, Stri
204201
* and store it into @code currentEntry.
205202
*
206203
* @return true if history entry was successfully generated otherwise false
207-
* @throws IOException
208204
*/
209-
private boolean next() throws IOException {
205+
private boolean next() {
210206
if (diter == null) {
211207
diter = hash.keySet().iterator();
212208
}

opengrok-indexer/src/main/java/org/opengrok/indexer/history/GitRepository.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,20 @@ private HistoryRevResult getHistoryRev(OutputStream out, String fullpath, String
144144
try (org.eclipse.jgit.lib.Repository repository = getJGitRepository(directory.getAbsolutePath())) {
145145
ObjectId commitId = repository.resolve(rev);
146146

147-
// a RevWalk allows to walk over commits based on some filtering that is defined
147+
// A RevWalk allows walking over commits based on some filtering that is defined.
148148
try (RevWalk revWalk = new RevWalk(repository)) {
149149
RevCommit commit = revWalk.parseCommit(commitId);
150150
// and using commit's tree find the path
151151
RevTree tree = commit.getTree();
152152

153-
// now try to find a specific file
153+
// Now try to find a specific file.
154154
try (TreeWalk treeWalk = new TreeWalk(repository)) {
155155
treeWalk.addTree(tree);
156156
treeWalk.setRecursive(true);
157157
treeWalk.setFilter(PathFilter.create(filename));
158158
if (!treeWalk.next()) {
159-
LOGGER.log(Level.FINEST,
160-
String.format("Did not find expected file '%s' in revision %s for repository '%s'",
161-
filename, rev, directory));
159+
LOGGER.log(Level.FINEST, "Did not find expected file ''{0}'' in revision {1} " +
160+
"for repository ''{2}''", new Object[] {filename, rev, directory});
162161
return result;
163162
}
164163

@@ -197,7 +196,7 @@ boolean getHistoryGet(OutputStream out, String parent, String basename, String r
197196
if (!result.success && result.iterations < 1) {
198197
/*
199198
* If we failed to get the contents it might be that the file was
200-
* renamed so we need to find its original name in that revision
199+
* renamed, so we need to find its original name in that revision
201200
* and retry with the original name.
202201
*/
203202
String origPath;
@@ -330,7 +329,7 @@ public Annotation annotate(File file, String revision) throws IOException {
330329

331330
if (annotation.getRevisions().isEmpty() && isHandleRenamedFiles()) {
332331
// The file might have changed its location if it was renamed.
333-
// Try to lookup its original name and get the annotation again.
332+
// Try looking up its original name and get the annotation again.
334333
String origName = findOriginalName(file.getCanonicalPath(), revision);
335334
if (origName != null) {
336335
annotation = getAnnotation(revision, origName, fileName);
@@ -351,8 +350,8 @@ private String getFirstRevision(String filePath) {
351350
if (commit != null) {
352351
revision = commit.getId().getName();
353352
} else {
354-
LOGGER.log(Level.WARNING, String.format("cannot get first revision of '%s' in repository '%s'",
355-
filePath, getDirectoryName()));
353+
LOGGER.log(Level.WARNING, "cannot get first revision of ''{0}'' in repository ''{1}''",
354+
new Object[] {filePath, getDirectoryName()});
356355
}
357356
} catch (IOException | GitAPIException e) {
358357
LOGGER.log(Level.WARNING,
@@ -396,10 +395,6 @@ public boolean fileHasAnnotation(File file) {
396395

397396
@Override
398397
public boolean fileHasHistory(File file) {
399-
// Todo: is there a cheap test for whether Git has history
400-
// available for a file?
401-
// Otherwise, this is harmless, since Git's commands will just
402-
// print nothing if there is no history.
403398
return true;
404399
}
405400

@@ -714,7 +709,8 @@ private void rebuildTagList(File directory) {
714709
String tagName = ref.getName().replace("refs/tags/", "");
715710
commit2Tags.merge(commit, tagName, (oldValue, newValue) -> oldValue + TAGS_SEPARATOR + newValue);
716711
} catch (IOException e) {
717-
LOGGER.log(Level.FINEST, "cannot get tags for \"" + directory.getAbsolutePath() + "\"", e);
712+
LOGGER.log(Level.FINEST,
713+
String.format("cannot get tags for \"%s\"", directory.getAbsolutePath()), e);
718714
}
719715
}
720716

@@ -727,7 +723,7 @@ private void rebuildTagList(File directory) {
727723
}
728724
}
729725
} catch (IOException | GitAPIException e) {
730-
LOGGER.log(Level.WARNING, "cannot get tags for \"" + directory.getAbsolutePath() + "\"", e);
726+
LOGGER.log(Level.WARNING, String.format("cannot get tags for \"%s\"", directory.getAbsolutePath()), e);
731727
// In case of partial success, do not null-out tagList here.
732728
}
733729

opengrok-indexer/src/main/java/org/opengrok/indexer/util/Executor.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,11 @@ public int exec(final boolean reportExceptions, StreamHandler handler) {
240240
stderr = err.getBytes();
241241
} catch (IOException e) {
242242
if (reportExceptions) {
243-
LOGGER.log(Level.SEVERE,
244-
"Failed to read from process: " + cmdList.get(0), e);
243+
LOGGER.log(Level.SEVERE, String.format("Failed to read from process: %s", cmdList.get(0)), e);
245244
}
246245
} catch (InterruptedException e) {
247246
if (reportExceptions) {
248-
LOGGER.log(Level.SEVERE,
249-
"Waiting for process interrupted: " + cmdList.get(0), e);
247+
LOGGER.log(Level.SEVERE, String.format("Waiting for process interrupted: %s", cmdList.get(0)), e);
250248
}
251249
} finally {
252250
// Stop timer thread if the instance exists.
@@ -391,14 +389,12 @@ public void processStream(InputStream input) throws IOException {
391389
}
392390

393391
public static void registerErrorHandler() {
394-
UncaughtExceptionHandler dueh =
395-
Thread.getDefaultUncaughtExceptionHandler();
396-
if (dueh == null) {
392+
UncaughtExceptionHandler exceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
393+
if (exceptionHandler == null) {
397394
LOGGER.log(Level.FINE, "Installing default uncaught exception handler");
398395
Thread.setDefaultUncaughtExceptionHandler((t, e) ->
399-
LOGGER.log(Level.SEVERE, "Uncaught exception in thread "
400-
+ t.getName() + " with ID " + t.getId() + ": "
401-
+ e.getMessage(), e));
396+
LOGGER.log(Level.SEVERE, String.format("Uncaught exception in thread %s with ID %d: %s",
397+
t.getName(), t.getId(), e.getMessage()), e));
402398
}
403399
}
404400

0 commit comments

Comments
 (0)