Skip to content

Commit 311d83f

Browse files
author
Vladimir Kotal
authored
address some issues reported by Sonar (#3778)
* address some issues reported by Sonar * bump year
1 parent ad16cec commit 311d83f

File tree

6 files changed

+53
-57
lines changed

6 files changed

+53
-57
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/framework/PluginClassLoader.java

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

2020
/*
21-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.framework;
@@ -67,6 +67,8 @@ public class PluginClassLoader extends ClassLoader {
6767
"sun"
6868
};
6969

70+
private static final String CLASS_SUFFIX = ".class";
71+
7072
private final File directory;
7173

7274
public PluginClassLoader(File directory) {
@@ -86,9 +88,9 @@ private Class<?> loadClassFromJar(String classname) throws ClassNotFoundExceptio
8688
for (File f : jars) {
8789
try (JarFile jar = new JarFile(f)) {
8890
// jar files always use / separator
89-
String filename = classname.replace('.', '/') + ".class";
91+
String filename = classname.replace('.', '/') + CLASS_SUFFIX;
9092
JarEntry entry = (JarEntry) jar.getEntry(filename);
91-
if (entry != null && entry.getName().endsWith(".class")) {
93+
if (entry != null && entry.getName().endsWith(CLASS_SUFFIX)) {
9294
try (InputStream is = jar.getInputStream(entry)) {
9395
byte[] bytes = loadBytes(is);
9496
Class<?> c = defineClass(classname, bytes, 0, bytes.length);
@@ -111,7 +113,7 @@ private Class<?> loadClassFromJar(String classname) throws ClassNotFoundExceptio
111113

112114
private Class<?> loadClassFromFile(String classname) throws ClassNotFoundException {
113115
try {
114-
String filename = classname.replace('.', File.separatorChar) + ".class";
116+
String filename = classname.replace('.', File.separatorChar) + CLASS_SUFFIX;
115117
File f = new File(directory, filename);
116118
try (FileInputStream in = new FileInputStream(f)) {
117119
byte[] bytes = loadBytes(in);

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,16 @@ public Annotation annotate(File file, String rev) throws IOException {
170170
if (hist != null && annotation != null) {
171171
Set<String> revs = annotation.getRevisions();
172172
int revsMatched = 0;
173-
// !!! cannot do this because of not matching rev ids (keys)
174-
// first is the most recent one, so we need the position of "rev"
175-
// until the end of the list
176-
//if (hent.indexOf(rev)>0) {
177-
// hent = hent.subList(hent.indexOf(rev), hent.size());
178-
//}
179173
for (HistoryEntry he : hist.getHistoryEntries()) {
180-
String hist_rev = he.getRevision();
181-
String short_rev = repo.getRevisionForAnnotate(hist_rev);
182-
if (revs.contains(short_rev)) {
183-
annotation.addDesc(short_rev, "changeset: " + he.getRevision()
174+
String histRev = he.getRevision();
175+
String shortRev = repo.getRevisionForAnnotate(histRev);
176+
if (revs.contains(shortRev)) {
177+
annotation.addDesc(shortRev, "changeset: " + he.getRevision()
184178
+ "\nsummary: " + he.getMessage() + "\nuser: "
185179
+ he.getAuthor() + "\ndate: " + he.getDate());
186-
// History entries are coming from recent to older,
187-
// file version should be from oldest to newer.
188-
annotation.addFileVersion(short_rev, revs.size() - revsMatched);
180+
// History entries are coming from recent to older,
181+
// file version should be from oldest to newer.
182+
annotation.addFileVersion(shortRev, revs.size() - revsMatched);
189183
revsMatched++;
190184
}
191185
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public class SubversionRepository extends Repository {
6969
*/
7070
public static final String CMD_FALLBACK = "svn";
7171

72+
private static final String XML_OPTION = "--xml";
73+
private static final String NON_INTERACT_OPTION = "--non-interactive";
74+
7275
private static final String URLattr = "url";
7376

7477
protected String reposPath;
@@ -112,7 +115,7 @@ private Document getInfoDocument() {
112115

113116
cmd.add(RepoCommand);
114117
cmd.add("info");
115-
cmd.add("--xml");
118+
cmd.add(XML_OPTION);
116119
File directory = new File(getDirectoryName());
117120

118121
Executor executor = new Executor(cmd, directory,
@@ -205,9 +208,9 @@ Executor getHistoryLogExecutor(final File file, String sinceRevision,
205208
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
206209
cmd.add(RepoCommand);
207210
cmd.add("log");
208-
cmd.add("--non-interactive");
211+
cmd.add(NON_INTERACT_OPTION);
209212
cmd.addAll(getAuthCommandLineParams());
210-
cmd.add("--xml");
213+
cmd.add(XML_OPTION);
211214
cmd.add("-v");
212215
if (numEntries > 0) {
213216
cmd.add("-l" + numEntries);
@@ -247,7 +250,7 @@ boolean getHistoryGet(OutputStream out, String parent, String basename, String r
247250
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
248251
cmd.add(RepoCommand);
249252
cmd.add("cat");
250-
cmd.add("--non-interactive");
253+
cmd.add(NON_INTERACT_OPTION);
251254
cmd.addAll(getAuthCommandLineParams());
252255
cmd.add("-r");
253256
cmd.add(rev);
@@ -304,8 +307,8 @@ public Annotation annotate(File file, String revision) throws IOException {
304307
argv.add(RepoCommand);
305308
argv.add("annotate");
306309
argv.addAll(getAuthCommandLineParams());
307-
argv.add("--non-interactive");
308-
argv.add("--xml");
310+
argv.add(NON_INTERACT_OPTION);
311+
argv.add(XML_OPTION);
309312
if (revision != null) {
310313
argv.add("-r");
311314
argv.add(revision);
@@ -409,7 +412,7 @@ public String determineCurrentVersion(CommandTimeoutType cmdType) throws IOExcep
409412
History hist = getHistory(new File(getDirectoryName()), null, 1, cmdType);
410413
if (hist != null) {
411414
List<HistoryEntry> hlist = hist.getHistoryEntries();
412-
if (hlist != null && hlist.size() > 0) {
415+
if (hlist != null && !hlist.isEmpty()) {
413416
HistoryEntry he = hlist.get(0);
414417
curVersion = format(he.getDate()) + " " +
415418
he.getRevision() + " " + he.getAuthor() + " " +

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,9 @@ public static void main(String[] argv) {
167167
try {
168168
argv = parseOptions(argv);
169169

170-
if (webappURI != null) {
171-
if (!HostUtil.isReachable(webappURI, CONNECT_TIMEOUT)) {
172-
System.err.println(webappURI + " is not reachable.");
173-
System.exit(1);
174-
}
170+
if (webappURI != null && !HostUtil.isReachable(webappURI, CONNECT_TIMEOUT)) {
171+
System.err.println(webappURI + " is not reachable.");
172+
System.exit(1);
175173
}
176174

177175
/*
@@ -268,9 +266,8 @@ public static void main(String[] argv) {
268266
if (!IndexCheck.check(cfg, subFilesList)) {
269267
System.err.printf("Index check failed%n");
270268
System.err.print("You might want to remove " +
271-
(subFilesList.size() > 0 ?
272-
"data for projects " + String.join(",", subFilesList) : "all data") +
273-
" under the data root and reindex\n");
269+
(!subFilesList.isEmpty() ? "data for projects " + String.join(",", subFilesList) :
270+
"all data") + " under the data root and reindex\n");
274271
System.exit(1);
275272
}
276273

@@ -453,7 +450,7 @@ public static String[] parseOptions(String[] argv) throws ParseException {
453450

454451
optParser = OptionParser.execute(parser -> {
455452
parser.setPrologue(
456-
String.format("\nUsage: java -jar %s [options] [subDir1 [...]]\n", program));
453+
String.format("\nUsage: java -jar %s [options] [subDir1 [...]]%n", program));
457454

458455
parser.on(HELP_OPT_3, HELP_OPT_2, HELP_OPT_1, "=[mode]",
459456
"With no mode specified, display this usage summary. Or specify a mode:",

opengrok-indexer/src/main/java/org/opengrok/indexer/web/SearchHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public SearchHelper prepareExec(SortedSet<String> projects) {
293293
Set<String> invalidProjects = projects.stream().
294294
filter(proj -> (Project.getByName(proj) == null)).
295295
collect(Collectors.toSet());
296-
if (invalidProjects.size() > 0) {
296+
if (!invalidProjects.isEmpty()) {
297297
errorMsg = "Project list contains invalid projects: " +
298298
String.join(", ", invalidProjects);
299299
return this;
@@ -303,7 +303,7 @@ public SearchHelper prepareExec(SortedSet<String> projects) {
303303
map(Project::getByName).
304304
filter(proj -> !proj.isIndexed()).
305305
collect(Collectors.toSet());
306-
if (notIndexedProjects.size() > 0) {
306+
if (!notIndexedProjects.isEmpty()) {
307307
errorMsg = "Some of the projects to be searched are not indexed yet: " +
308308
String.join(", ", notIndexedProjects.stream().
309309
map(Project::getName).

opengrok-indexer/src/test/java/org/opengrok/indexer/history/GitRepositoryTest.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private void checkCurrentVersion(File root, int timestamp, String commitId, Stri
107107
}
108108

109109
@Test
110-
public void testDetermineCurrentVersionWithEmptyRepository() throws Exception {
110+
void testDetermineCurrentVersionWithEmptyRepository() throws Exception {
111111
File emptyGitDir = new File(repository.getSourceRoot(), "gitEmpty");
112112
try (Git git = Git.init().setDirectory(emptyGitDir).call()) {
113113
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(git.getRepository().getWorkTree());
@@ -119,7 +119,7 @@ public void testDetermineCurrentVersionWithEmptyRepository() throws Exception {
119119
}
120120

121121
@Test
122-
public void testDetermineCurrentVersionOfKnownRepository() throws Exception {
122+
void testDetermineCurrentVersionOfKnownRepository() throws Exception {
123123
File root = new File(repository.getSourceRoot(), "git");
124124
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
125125
assertNotNull(gitrepo);
@@ -130,7 +130,7 @@ public void testDetermineCurrentVersionOfKnownRepository() throws Exception {
130130
}
131131

132132
@Test
133-
public void testDetermineCurrentVersionAfterChange() throws Exception {
133+
void testDetermineCurrentVersionAfterChange() throws Exception {
134134
File root = new File(repository.getSourceRoot(), "git");
135135
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
136136
assertNotNull(gitrepo);
@@ -174,7 +174,7 @@ public void testDetermineCurrentVersionAfterChange() throws Exception {
174174
}
175175

176176
@Test
177-
public void testDetermineBranchBasic() throws Exception {
177+
void testDetermineBranchBasic() throws Exception {
178178
// First check branch of known repository.
179179
File root = new File(repository.getSourceRoot(), "git");
180180
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
@@ -184,7 +184,7 @@ public void testDetermineBranchBasic() throws Exception {
184184
}
185185

186186
@Test
187-
public void testDetermineBranchAfterChange() throws Exception {
187+
void testDetermineBranchAfterChange() throws Exception {
188188
// Clone the test repository and create new branch there.
189189
// Clone under source root to avoid problems with prohibited symlinks.
190190
File root = new File(repository.getSourceRoot(), "git");
@@ -246,15 +246,15 @@ void testGetHistoryInBranch() throws Exception {
246246
}
247247

248248
@Test
249-
public void testDetermineParentEmpty() throws Exception {
249+
void testDetermineParentEmpty() throws Exception {
250250
File root = new File(repository.getSourceRoot(), "git");
251251
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
252252
String parent = gitrepo.determineParent();
253253
assertNull(parent);
254254
}
255255

256256
@Test
257-
public void testDetermineParent() throws Exception {
257+
void testDetermineParent() throws Exception {
258258
File root = new File(repository.getSourceRoot(), "git");
259259
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
260260
String parent;
@@ -284,7 +284,7 @@ public void testDetermineParent() throws Exception {
284284
* Test of fileHasAnnotation method, of class GitRepository.
285285
*/
286286
@Test
287-
public void fileHasAnnotation() {
287+
void fileHasAnnotation() {
288288
boolean result = instance.fileHasAnnotation(null);
289289
assertTrue(result);
290290
}
@@ -293,7 +293,7 @@ public void fileHasAnnotation() {
293293
* Test of fileHasHistory method, of class GitRepository.
294294
*/
295295
@Test
296-
public void fileHasHistory() {
296+
void fileHasHistory() {
297297
boolean result = instance.fileHasHistory(null);
298298
assertTrue(result);
299299
}
@@ -309,7 +309,7 @@ public void fileHasHistory() {
309309
* </pre>
310310
*/
311311
@Test
312-
public void testRenamedFiles() throws Exception {
312+
void testRenamedFiles() throws Exception {
313313
String[][] tests = new String[][] {
314314
{Paths.get("moved2", "renamed2.c").toString(), "84599b3c", Paths.get("moved2", "renamed2.c").toString()},
315315
{Paths.get("moved2", "renamed2.c").toString(), "67dfbe26", Paths.get("moved", "renamed2.c").toString()},
@@ -342,7 +342,7 @@ private void testAnnotationOfFile(GitRepository gitrepo, File file, String revis
342342
}
343343

344344
@Test
345-
public void testAnnotationOfRenamedFileWithHandlingOff() throws Exception {
345+
void testAnnotationOfRenamedFileWithHandlingOff() throws Exception {
346346
String[] revisions = {"84599b3c"};
347347
Set<String> revSet = new HashSet<>();
348348
Collections.addAll(revSet, revisions);
@@ -355,7 +355,7 @@ public void testAnnotationOfRenamedFileWithHandlingOff() throws Exception {
355355
}
356356

357357
@Test
358-
public void testAnnotationOfRenamedFileWithHandlingOn() throws Exception {
358+
void testAnnotationOfRenamedFileWithHandlingOn() throws Exception {
359359
String[] revisions = {"1086eaf5", "ce4c98ec"};
360360
Set<String> revSet = new HashSet<>();
361361
Collections.addAll(revSet, revisions);
@@ -368,7 +368,7 @@ public void testAnnotationOfRenamedFileWithHandlingOn() throws Exception {
368368
}
369369

370370
@Test
371-
public void testAnnotationOfRenamedFilePastWithHandlingOn() throws Exception {
371+
void testAnnotationOfRenamedFilePastWithHandlingOn() throws Exception {
372372
String[] revisions = {"1086eaf5", "ce4c98ec"};
373373
Set<String> revSet = new HashSet<>();
374374
Collections.addAll(revSet, revisions);
@@ -381,7 +381,7 @@ public void testAnnotationOfRenamedFilePastWithHandlingOn() throws Exception {
381381
}
382382

383383
@Test
384-
public void testInvalidRenamedFiles() {
384+
void testInvalidRenamedFiles() {
385385
String[][] tests = new String[][] {
386386
{"", "67dfbe26"},
387387
{"moved/renamed2.c", ""},
@@ -408,7 +408,7 @@ public void testInvalidRenamedFiles() {
408408
* @see #testRenamedFiles for git repo structure info
409409
*/
410410
@Test
411-
public void testGetRenamedFileContent() throws Exception {
411+
void testGetRenamedFileContent() throws Exception {
412412
String old_content
413413
= "#include <stdio.h>\n"
414414
+ "#include <stdlib.h>\n"
@@ -483,7 +483,7 @@ public void testGetRenamedFileContent() throws Exception {
483483
* @see #testRenamedFiles for git repo structure info
484484
*/
485485
@Test
486-
public void testGetHistoryForNonExistentRenamed() throws Exception {
486+
void testGetHistoryForNonExistentRenamed() throws Exception {
487487
final List<String[]> tests = Arrays.asList(
488488
new String[] {Paths.get("moved", "renamed2.c").toString(), "84599b3c"},
489489

@@ -520,7 +520,7 @@ private void runRenamedTest(String fname, String cset, String content) throws Ex
520520

521521
@ParameterizedTest
522522
@ValueSource(booleans = {true, false})
523-
public void testHistory(boolean renamedHandling) throws Exception {
523+
void testHistory(boolean renamedHandling) throws Exception {
524524
RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(renamedHandling);
525525
File root = new File(repository.getSourceRoot(), "git");
526526
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
@@ -597,7 +597,7 @@ public void testHistory(boolean renamedHandling) throws Exception {
597597
}
598598

599599
@Test
600-
public void testSingleHistory() throws Exception {
600+
void testSingleHistory() throws Exception {
601601
RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(false);
602602
File root = new File(repository.getSourceRoot(), "git");
603603
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
@@ -611,7 +611,7 @@ public void testSingleHistory() throws Exception {
611611
}
612612

613613
@Test
614-
public void testRenamedSingleHistory() throws Exception {
614+
void testRenamedSingleHistory() throws Exception {
615615
RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(true);
616616
File root = new File(repository.getSourceRoot(), "git");
617617
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
@@ -689,7 +689,7 @@ void testGetHistorySinceTillRevRev() throws Exception {
689689
}
690690

691691
@Test
692-
public void testBuildTagListEmpty() throws Exception {
692+
void testBuildTagListEmpty() throws Exception {
693693
File root = new File(repository.getSourceRoot(), "git");
694694
// Clone under source root to avoid problems with prohibited symlinks.
695695
File localPath = new File(repository.getSourceRoot(), "testBuildTagListEmpty");
@@ -710,7 +710,7 @@ public void testBuildTagListEmpty() throws Exception {
710710
}
711711

712712
@Test
713-
public void testBuildTagListMultipleTags() throws Exception {
713+
void testBuildTagListMultipleTags() throws Exception {
714714
File root = new File(repository.getSourceRoot(), "git");
715715
// Clone under source root to avoid problems with prohibited symlinks.
716716
File localPath = new File(repository.getSourceRoot(), "testBuildTagListMultipleTags");
@@ -750,7 +750,7 @@ public void testBuildTagListMultipleTags() throws Exception {
750750
}
751751

752752
@Test
753-
public void testBuildTagListNotHead() throws Exception {
753+
void testBuildTagListNotHead() throws Exception {
754754
File root = new File(repository.getSourceRoot(), "git");
755755
// Clone under source root to avoid problems with prohibited symlinks.
756756
File localPath = new File(repository.getSourceRoot(), "testBuildTagListNotHead");

0 commit comments

Comments
 (0)