Skip to content

address some issues reported by Sonar #3778

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

Merged
merged 2 commits into from
Nov 23, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

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

private static final String CLASS_SUFFIX = ".class";

private final File directory;

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

private Class<?> loadClassFromFile(String classname) throws ClassNotFoundException {
try {
String filename = classname.replace('.', File.separatorChar) + ".class";
String filename = classname.replace('.', File.separatorChar) + CLASS_SUFFIX;
File f = new File(directory, filename);
try (FileInputStream in = new FileInputStream(f)) {
byte[] bytes = loadBytes(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,16 @@ public Annotation annotate(File file, String rev) throws IOException {
if (hist != null && annotation != null) {
Set<String> revs = annotation.getRevisions();
int revsMatched = 0;
// !!! cannot do this because of not matching rev ids (keys)
// first is the most recent one, so we need the position of "rev"
// until the end of the list
//if (hent.indexOf(rev)>0) {
// hent = hent.subList(hent.indexOf(rev), hent.size());
//}
for (HistoryEntry he : hist.getHistoryEntries()) {
String hist_rev = he.getRevision();
String short_rev = repo.getRevisionForAnnotate(hist_rev);
if (revs.contains(short_rev)) {
annotation.addDesc(short_rev, "changeset: " + he.getRevision()
String histRev = he.getRevision();
String shortRev = repo.getRevisionForAnnotate(histRev);
if (revs.contains(shortRev)) {
annotation.addDesc(shortRev, "changeset: " + he.getRevision()
+ "\nsummary: " + he.getMessage() + "\nuser: "
+ he.getAuthor() + "\ndate: " + he.getDate());
// History entries are coming from recent to older,
// file version should be from oldest to newer.
annotation.addFileVersion(short_rev, revs.size() - revsMatched);
// History entries are coming from recent to older,
// file version should be from oldest to newer.
annotation.addFileVersion(shortRev, revs.size() - revsMatched);
revsMatched++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public class SubversionRepository extends Repository {
*/
public static final String CMD_FALLBACK = "svn";

private static final String XML_OPTION = "--xml";
private static final String NON_INTERACT_OPTION = "--non-interactive";

private static final String URLattr = "url";

protected String reposPath;
Expand Down Expand Up @@ -112,7 +115,7 @@ private Document getInfoDocument() {

cmd.add(RepoCommand);
cmd.add("info");
cmd.add("--xml");
cmd.add(XML_OPTION);
File directory = new File(getDirectoryName());

Executor executor = new Executor(cmd, directory,
Expand Down Expand Up @@ -205,9 +208,9 @@ Executor getHistoryLogExecutor(final File file, String sinceRevision,
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
cmd.add(RepoCommand);
cmd.add("log");
cmd.add("--non-interactive");
cmd.add(NON_INTERACT_OPTION);
cmd.addAll(getAuthCommandLineParams());
cmd.add("--xml");
cmd.add(XML_OPTION);
cmd.add("-v");
if (numEntries > 0) {
cmd.add("-l" + numEntries);
Expand Down Expand Up @@ -247,7 +250,7 @@ boolean getHistoryGet(OutputStream out, String parent, String basename, String r
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
cmd.add(RepoCommand);
cmd.add("cat");
cmd.add("--non-interactive");
cmd.add(NON_INTERACT_OPTION);
cmd.addAll(getAuthCommandLineParams());
cmd.add("-r");
cmd.add(rev);
Expand Down Expand Up @@ -304,8 +307,8 @@ public Annotation annotate(File file, String revision) throws IOException {
argv.add(RepoCommand);
argv.add("annotate");
argv.addAll(getAuthCommandLineParams());
argv.add("--non-interactive");
argv.add("--xml");
argv.add(NON_INTERACT_OPTION);
argv.add(XML_OPTION);
if (revision != null) {
argv.add("-r");
argv.add(revision);
Expand Down Expand Up @@ -409,7 +412,7 @@ public String determineCurrentVersion(CommandTimeoutType cmdType) throws IOExcep
History hist = getHistory(new File(getDirectoryName()), null, 1, cmdType);
if (hist != null) {
List<HistoryEntry> hlist = hist.getHistoryEntries();
if (hlist != null && hlist.size() > 0) {
if (hlist != null && !hlist.isEmpty()) {
HistoryEntry he = hlist.get(0);
curVersion = format(he.getDate()) + " " +
he.getRevision() + " " + he.getAuthor() + " " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,9 @@ public static void main(String[] argv) {
try {
argv = parseOptions(argv);

if (webappURI != null) {
if (!HostUtil.isReachable(webappURI, CONNECT_TIMEOUT)) {
System.err.println(webappURI + " is not reachable.");
System.exit(1);
}
if (webappURI != null && !HostUtil.isReachable(webappURI, CONNECT_TIMEOUT)) {
System.err.println(webappURI + " is not reachable.");
System.exit(1);
}

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

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

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

parser.on(HELP_OPT_3, HELP_OPT_2, HELP_OPT_1, "=[mode]",
"With no mode specified, display this usage summary. Or specify a mode:",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public SearchHelper prepareExec(SortedSet<String> projects) {
Set<String> invalidProjects = projects.stream().
filter(proj -> (Project.getByName(proj) == null)).
collect(Collectors.toSet());
if (invalidProjects.size() > 0) {
if (!invalidProjects.isEmpty()) {
errorMsg = "Project list contains invalid projects: " +
String.join(", ", invalidProjects);
return this;
Expand All @@ -303,7 +303,7 @@ public SearchHelper prepareExec(SortedSet<String> projects) {
map(Project::getByName).
filter(proj -> !proj.isIndexed()).
collect(Collectors.toSet());
if (notIndexedProjects.size() > 0) {
if (!notIndexedProjects.isEmpty()) {
errorMsg = "Some of the projects to be searched are not indexed yet: " +
String.join(", ", notIndexedProjects.stream().
map(Project::getName).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void checkCurrentVersion(File root, int timestamp, String commitId, Stri
}

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

@Test
public void testDetermineCurrentVersionOfKnownRepository() throws Exception {
void testDetermineCurrentVersionOfKnownRepository() throws Exception {
File root = new File(repository.getSourceRoot(), "git");
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
assertNotNull(gitrepo);
Expand All @@ -130,7 +130,7 @@ public void testDetermineCurrentVersionOfKnownRepository() throws Exception {
}

@Test
public void testDetermineCurrentVersionAfterChange() throws Exception {
void testDetermineCurrentVersionAfterChange() throws Exception {
File root = new File(repository.getSourceRoot(), "git");
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
assertNotNull(gitrepo);
Expand Down Expand Up @@ -174,7 +174,7 @@ public void testDetermineCurrentVersionAfterChange() throws Exception {
}

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

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

@Test
public void testDetermineParentEmpty() throws Exception {
void testDetermineParentEmpty() throws Exception {
File root = new File(repository.getSourceRoot(), "git");
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
String parent = gitrepo.determineParent();
assertNull(parent);
}

@Test
public void testDetermineParent() throws Exception {
void testDetermineParent() throws Exception {
File root = new File(repository.getSourceRoot(), "git");
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
String parent;
Expand Down Expand Up @@ -284,7 +284,7 @@ public void testDetermineParent() throws Exception {
* Test of fileHasAnnotation method, of class GitRepository.
*/
@Test
public void fileHasAnnotation() {
void fileHasAnnotation() {
boolean result = instance.fileHasAnnotation(null);
assertTrue(result);
}
Expand All @@ -293,7 +293,7 @@ public void fileHasAnnotation() {
* Test of fileHasHistory method, of class GitRepository.
*/
@Test
public void fileHasHistory() {
void fileHasHistory() {
boolean result = instance.fileHasHistory(null);
assertTrue(result);
}
Expand All @@ -309,7 +309,7 @@ public void fileHasHistory() {
* </pre>
*/
@Test
public void testRenamedFiles() throws Exception {
void testRenamedFiles() throws Exception {
String[][] tests = new String[][] {
{Paths.get("moved2", "renamed2.c").toString(), "84599b3c", Paths.get("moved2", "renamed2.c").toString()},
{Paths.get("moved2", "renamed2.c").toString(), "67dfbe26", Paths.get("moved", "renamed2.c").toString()},
Expand Down Expand Up @@ -342,7 +342,7 @@ private void testAnnotationOfFile(GitRepository gitrepo, File file, String revis
}

@Test
public void testAnnotationOfRenamedFileWithHandlingOff() throws Exception {
void testAnnotationOfRenamedFileWithHandlingOff() throws Exception {
String[] revisions = {"84599b3c"};
Set<String> revSet = new HashSet<>();
Collections.addAll(revSet, revisions);
Expand All @@ -355,7 +355,7 @@ public void testAnnotationOfRenamedFileWithHandlingOff() throws Exception {
}

@Test
public void testAnnotationOfRenamedFileWithHandlingOn() throws Exception {
void testAnnotationOfRenamedFileWithHandlingOn() throws Exception {
String[] revisions = {"1086eaf5", "ce4c98ec"};
Set<String> revSet = new HashSet<>();
Collections.addAll(revSet, revisions);
Expand All @@ -368,7 +368,7 @@ public void testAnnotationOfRenamedFileWithHandlingOn() throws Exception {
}

@Test
public void testAnnotationOfRenamedFilePastWithHandlingOn() throws Exception {
void testAnnotationOfRenamedFilePastWithHandlingOn() throws Exception {
String[] revisions = {"1086eaf5", "ce4c98ec"};
Set<String> revSet = new HashSet<>();
Collections.addAll(revSet, revisions);
Expand All @@ -381,7 +381,7 @@ public void testAnnotationOfRenamedFilePastWithHandlingOn() throws Exception {
}

@Test
public void testInvalidRenamedFiles() {
void testInvalidRenamedFiles() {
String[][] tests = new String[][] {
{"", "67dfbe26"},
{"moved/renamed2.c", ""},
Expand All @@ -408,7 +408,7 @@ public void testInvalidRenamedFiles() {
* @see #testRenamedFiles for git repo structure info
*/
@Test
public void testGetRenamedFileContent() throws Exception {
void testGetRenamedFileContent() throws Exception {
String old_content
= "#include <stdio.h>\n"
+ "#include <stdlib.h>\n"
Expand Down Expand Up @@ -483,7 +483,7 @@ public void testGetRenamedFileContent() throws Exception {
* @see #testRenamedFiles for git repo structure info
*/
@Test
public void testGetHistoryForNonExistentRenamed() throws Exception {
void testGetHistoryForNonExistentRenamed() throws Exception {
final List<String[]> tests = Arrays.asList(
new String[] {Paths.get("moved", "renamed2.c").toString(), "84599b3c"},

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

@ParameterizedTest
@ValueSource(booleans = {true, false})
public void testHistory(boolean renamedHandling) throws Exception {
void testHistory(boolean renamedHandling) throws Exception {
RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(renamedHandling);
File root = new File(repository.getSourceRoot(), "git");
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
Expand Down Expand Up @@ -597,7 +597,7 @@ public void testHistory(boolean renamedHandling) throws Exception {
}

@Test
public void testSingleHistory() throws Exception {
void testSingleHistory() throws Exception {
RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(false);
File root = new File(repository.getSourceRoot(), "git");
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
Expand All @@ -611,7 +611,7 @@ public void testSingleHistory() throws Exception {
}

@Test
public void testRenamedSingleHistory() throws Exception {
void testRenamedSingleHistory() throws Exception {
RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(true);
File root = new File(repository.getSourceRoot(), "git");
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
Expand Down Expand Up @@ -689,7 +689,7 @@ void testGetHistorySinceTillRevRev() throws Exception {
}

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

@Test
public void testBuildTagListMultipleTags() throws Exception {
void testBuildTagListMultipleTags() throws Exception {
File root = new File(repository.getSourceRoot(), "git");
// Clone under source root to avoid problems with prohibited symlinks.
File localPath = new File(repository.getSourceRoot(), "testBuildTagListMultipleTags");
Expand Down Expand Up @@ -750,7 +750,7 @@ public void testBuildTagListMultipleTags() throws Exception {
}

@Test
public void testBuildTagListNotHead() throws Exception {
void testBuildTagListNotHead() throws Exception {
File root = new File(repository.getSourceRoot(), "git");
// Clone under source root to avoid problems with prohibited symlinks.
File localPath = new File(repository.getSourceRoot(), "testBuildTagListNotHead");
Expand Down