Skip to content
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

refactor(): Edits for sonarCloud critical code smells #157

Merged
merged 7 commits into from
Apr 24, 2020
Expand Up @@ -50,26 +50,17 @@ public SpringConfigurationPropertySource(final String name, final IterableConfig
this.configurablePropertyResolver = configurablePropertyResolver;
}

// TODO - this should return an Optional
public static List<SpringConfigurationPropertySource> fromConfigurableEnvironment(ConfigurableEnvironment configurableEnvironment) {
return fromConfigurableEnvironment(configurableEnvironment, true);
}

// TODO - this should return an Optional
public static List<SpringConfigurationPropertySource> fromConfigurableEnvironment(ConfigurableEnvironment configurableEnvironment, boolean ignoreUnknown) {
List<ConfigurationPropertySource> sources = Bds.listOf(ConfigurationPropertySources.get(configurableEnvironment));
return Bds.of(sources).map(it -> {
if (IterableConfigurationPropertySource.class.isAssignableFrom(it.getClass())) {
Object underlying = it.getUnderlyingSource();
if (org.springframework.core.env.PropertySource.class.isAssignableFrom(underlying.getClass())) {
org.springframework.core.env.PropertySource springSource = (org.springframework.core.env.PropertySource) underlying;
return new SpringConfigurationPropertySource(springSource.getName(), (IterableConfigurationPropertySource) it, configurableEnvironment);
} else {
if (ignoreUnknown) {
return null;
} else {
throw new RuntimeException(
new UnknownSpringConfigurationException("Unknown underlying spring configuration source. We may be unable to determine where a property originated. Likely a new property source type should be tested against."));
}
}
return getPropertySource(configurableEnvironment, ignoreUnknown, it);
} else if (RandomValuePropertySource.class.isAssignableFrom(it.getUnderlyingSource().getClass())) {
//We know an underlying random source can't be iterated but we don't care. It can't give a list of known keys.
return null;
Expand All @@ -85,6 +76,22 @@ public static List<SpringConfigurationPropertySource> fromConfigurableEnvironmen

}

// TODO - this should return an Optional
private static SpringConfigurationPropertySource getPropertySource(ConfigurableEnvironment configurableEnvironment, boolean ignoreUnknown, ConfigurationPropertySource configurationPropertySource) {
JakeMathews marked this conversation as resolved.
Show resolved Hide resolved
Object underlying = configurationPropertySource.getUnderlyingSource();
if (org.springframework.core.env.PropertySource.class.isAssignableFrom(underlying.getClass())) {
org.springframework.core.env.PropertySource springSource = (org.springframework.core.env.PropertySource) underlying;
return new SpringConfigurationPropertySource(springSource.getName(), (IterableConfigurationPropertySource) configurationPropertySource, configurableEnvironment);
} else {
if (ignoreUnknown) {
return null;
} else {
throw new RuntimeException(
new UnknownSpringConfigurationException("Unknown underlying spring configuration source. We may be unable to determine where a property originated. Likely a new property source type should be tested against."));
}
}
}

@NotNull
private Optional<ConfigurationPropertyName> toConfigurationName(String key) {
try {
Expand Down
Expand Up @@ -23,8 +23,6 @@
package com.synopsys.integration.detectable;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -49,7 +47,9 @@
//This sample application will an example detectable tool and execute it against the current folder.
public class SingleDetectableApplication {
public static void main(final String[] args) {

/*
Empty main method
*/
crowleySynopsys marked this conversation as resolved.
Show resolved Hide resolved
}

//In this example, we use the Detectable to determine if we can extract and if all necessary pieces are present.
Expand Down
Expand Up @@ -43,16 +43,17 @@ private List<File> findFiles(final File directoryToSearch, final FilenameFilter
return foundFiles;
}
final File[] allFiles = directoryToSearch.listFiles();
if (allFiles != null) {
for (final File file : allFiles) {
final boolean matches = filenameFilter.accept(directoryToSearch, file.getName());
if (matches) {
foundFiles.add(file);
}
if (!matches || findInsideMatchingDirectories) {
if (file.isDirectory() && !Files.isSymbolicLink(file.toPath())) {
foundFiles.addAll(findFiles(file, filenameFilter, depth - 1, findInsideMatchingDirectories));
}
if (allFiles == null) {
return foundFiles;
}
for (final File file : allFiles) {
final boolean matches = filenameFilter.accept(directoryToSearch, file.getName());
if (matches) {
foundFiles.add(file);
}
if (!matches || findInsideMatchingDirectories) {
if (file.isDirectory() && !Files.isSymbolicLink(file.toPath())) {
foundFiles.addAll(findFiles(file, filenameFilter, depth - 1, findInsideMatchingDirectories));
}
}
}
Expand Down
Expand Up @@ -73,15 +73,7 @@ public Optional<GradleReport> parseReport(final File reportFile) {
continue;
}
if (processingMetaData) {
if (line.startsWith(PROJECT_PATH_PREFIX)) {
gradleReport.setProjectSourcePath(line.substring(PROJECT_PATH_PREFIX.length()).trim());
} else if (line.startsWith(PROJECT_GROUP_PREFIX)) {
gradleReport.setProjectGroup(line.substring(PROJECT_GROUP_PREFIX.length()).trim());
} else if (line.startsWith(PROJECT_NAME_PREFIX)) {
gradleReport.setProjectName(line.substring(PROJECT_NAME_PREFIX.length()).trim());
} else if (line.startsWith(PROJECT_VERSION_PREFIX)) {
gradleReport.setProjectVersionName(line.substring(PROJECT_VERSION_PREFIX.length()).trim());
}
setGradleReportInfo(gradleReport, line);
continue;
}

Expand All @@ -103,6 +95,18 @@ public Optional<GradleReport> parseReport(final File reportFile) {
return Optional.ofNullable(gradleReport);
}

private void setGradleReportInfo(GradleReport gradleReport, String line) {
if (line.startsWith(PROJECT_PATH_PREFIX)) {
gradleReport.setProjectSourcePath(line.substring(PROJECT_PATH_PREFIX.length()).trim());
} else if (line.startsWith(PROJECT_GROUP_PREFIX)) {
gradleReport.setProjectGroup(line.substring(PROJECT_GROUP_PREFIX.length()).trim());
} else if (line.startsWith(PROJECT_NAME_PREFIX)) {
gradleReport.setProjectName(line.substring(PROJECT_NAME_PREFIX.length()).trim());
} else if (line.startsWith(PROJECT_VERSION_PREFIX)) {
gradleReport.setProjectVersionName(line.substring(PROJECT_VERSION_PREFIX.length()).trim());
}
}

private void parseConfigurationLines(final List<String> configurationLines, final GradleReport gradleReport) {
if (configurationLines.size() > 1 && isConfigurationHeader(configurationLines)) {
final String header = configurationLines.get(0);
Expand Down
Expand Up @@ -44,8 +44,8 @@

// TODO: Re-write. Some fields could be local variables. Includes many code smells. A component none:Additional_Components:none appears in the graph.
public class MavenCodeLocationPackager {
public static final List<String> indentationStrings = Arrays.asList("+- ", "| ", "\\- ", " ");
public static final List<String> KNOWN_SCOPES = Arrays.asList("compile", "provided", "runtime", "test", "system", "import");
private static final List<String> indentationStrings = Arrays.asList("+- ", "| ", "\\- ", " ");
private static final List<String> KNOWN_SCOPES = Arrays.asList("compile", "provided", "runtime", "test", "system", "import");

private static final Logger logger = LoggerFactory.getLogger(MavenCodeLocationPackager.class);
public static final String ORPHAN_LIST_PARENT_NODE_NAME = "Additional_Components";
Expand Down Expand Up @@ -82,39 +82,15 @@ public List<MavenParseResult> extractCodeLocations(final String sourcePath, fina
level = 0;
for (final String currentLine : mavenOutputText.split(System.lineSeparator())) {
String line = currentLine.trim();
if (!isLineRelevant(line)) {

if (shouldSkipLine(line)) {
continue;
}

line = trimLogLevel(line);
if (StringUtils.isBlank(line)) {
continue;
}
if (isProjectSection(line)) {
parsingProjectSection = true;
continue;
}
if (!parsingProjectSection) {
continue;
}
if (isDependencyTreeUpdates(line)) {
continue;
}

if (parsingProjectSection && currentMavenProject == null) {
// this is the first line of a new code location, the following lines will be the tree of dependencies for this code location
currentGraph = new MutableMapDependencyGraph();
final MavenParseResult mavenProject = createMavenParseResult(sourcePath, line, currentGraph);
if (null != mavenProject && modulesFilter.shouldInclude(mavenProject.getProjectName())) {
logger.trace(String.format("Project: %s", mavenProject.getProjectName()));
this.currentMavenProject = mavenProject;
codeLocations.add(mavenProject);
} else {
logger.trace("Project: unknown");
currentMavenProject = null;
dependencyParentStack.clear();
parsingProjectSection = false;
level = 0;
}
initializeCurrentMavenProject(modulesFilter, sourcePath, line);
continue;
}

Expand All @@ -134,49 +110,91 @@ public List<MavenParseResult> extractCodeLocations(final String sourcePath, fina
continue;
}
if (currentMavenProject != null) {
if (level == 1) {
// a direct dependency, clear the stack and add this as a potential parent for the next line
if (scopeFilter.shouldInclude(dependency.scope)) {
logger.trace(String
.format("Level 1 component %s:%s:%s:%s is in scope; adding it to hierarchy root", dependency.getExternalId().getGroup(), dependency.getExternalId().getName(), dependency.getExternalId().getVersion(),
dependency.scope));
currentGraph.addChildToRoot(dependency);
inOutOfScopeTree = false;
} else {
logger.trace(String.format("Level 1 component %s:%s:%s:%s is a top-level out-of-scope component; entering non-scoped tree", dependency.getExternalId().getGroup(), dependency.getExternalId().getName(),
dependency.getExternalId().getVersion(),
dependency.scope));
inOutOfScopeTree = true;
}
dependencyParentStack.clear();
dependencyParentStack.push(dependency);
} else {
// level should be greater than 1
if (level == previousLevel) {
// a sibling of the previous dependency
dependencyParentStack.pop();
addDependencyIfInScope(currentGraph, orphans, scopeFilter, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
dependencyParentStack.push(dependency);
} else if (level > previousLevel) {
// a child of the previous dependency
addDependencyIfInScope(currentGraph, orphans, scopeFilter, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
dependencyParentStack.push(dependency);
} else {
// a child of a dependency further back than 1 line
for (int i = previousLevel; i >= level; i--) {
dependencyParentStack.pop();
}
addDependencyIfInScope(currentGraph, orphans, scopeFilter, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
dependencyParentStack.push(dependency);
}
}
populateGraphDependencies(scopeFilter, dependency, previousLevel);
}
}
addOrphansToGraph(currentGraph, orphans);

return codeLocations;
}

private boolean shouldSkipLine(String line) {
if (!isLineRelevant(line)) {
return true;
}
line = trimLogLevel(line);
if (StringUtils.isBlank(line)) {
return true;
}
if (isProjectSection(line)) {
parsingProjectSection = true;
return true;
}
if (!parsingProjectSection) {
return true;
}
if (isDependencyTreeUpdates(line)) {
return true;
}
return false;
}

private void initializeCurrentMavenProject(ExcludedIncludedWildcardFilter modulesFilter, String sourcePath, String line) {
// this is the first line of a new code location, the following lines will be the tree of dependencies for this code location
currentGraph = new MutableMapDependencyGraph();
final MavenParseResult mavenProject = createMavenParseResult(sourcePath, line, currentGraph);
if (null != mavenProject && modulesFilter.shouldInclude(mavenProject.getProjectName())) {
logger.trace(String.format("Project: %s", mavenProject.getProjectName()));
this.currentMavenProject = mavenProject;
codeLocations.add(mavenProject);
} else {
logger.trace("Project: unknown");
currentMavenProject = null;
dependencyParentStack.clear();
parsingProjectSection = false;
level = 0;
}
}

private void populateGraphDependencies(ExcludedIncludedWildcardFilter scopeFilter, ScopedDependency dependency, int previousLevel) {
if (level == 1) {
// a direct dependency, clear the stack and add this as a potential parent for the next line
if (scopeFilter.shouldInclude(dependency.scope)) {
logger.trace(String
.format("Level 1 component %s:%s:%s:%s is in scope; adding it to hierarchy root", dependency.getExternalId().getGroup(), dependency.getExternalId().getName(), dependency.getExternalId().getVersion(),
dependency.scope));
currentGraph.addChildToRoot(dependency);
inOutOfScopeTree = false;
} else {
logger.trace(String.format("Level 1 component %s:%s:%s:%s is a top-level out-of-scope component; entering non-scoped tree", dependency.getExternalId().getGroup(), dependency.getExternalId().getName(),
dependency.getExternalId().getVersion(),
dependency.scope));
inOutOfScopeTree = true;
}
dependencyParentStack.clear();
dependencyParentStack.push(dependency);
} else {
// level should be greater than 1
if (level == previousLevel) {
// a sibling of the previous dependency
dependencyParentStack.pop();
addDependencyIfInScope(currentGraph, orphans, scopeFilter, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
dependencyParentStack.push(dependency);
} else if (level > previousLevel) {
// a child of the previous dependency
addDependencyIfInScope(currentGraph, orphans, scopeFilter, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
dependencyParentStack.push(dependency);
} else {
// a child of a dependency further back than 1 line
for (int i = previousLevel; i >= level; i--) {
dependencyParentStack.pop();
}
addDependencyIfInScope(currentGraph, orphans, scopeFilter, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
dependencyParentStack.push(dependency);
}
}
}

private void addOrphansToGraph(final MutableDependencyGraph graph, final List<Dependency> orphans) {
logger.trace(String.format("# orphans: %d", orphans.size()));
if (orphans.size() > 0) {
Expand Down
Expand Up @@ -24,8 +24,8 @@

import java.util.List;

import org.apache.commons.lang3.builder.RecursiveToStringStyle;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.google.gson.annotations.SerializedName;

Expand Down Expand Up @@ -56,6 +56,6 @@ public class NugetContainer {

@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, RecursiveToStringStyle.JSON_STYLE);
return ReflectionToStringBuilder.toString(this, ToStringStyle.JSON_STYLE);
crowleySynopsys marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Up @@ -25,8 +25,8 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.builder.RecursiveToStringStyle;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.google.gson.annotations.SerializedName;

Expand All @@ -42,6 +42,6 @@ public class NugetInspection {

@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, RecursiveToStringStyle.JSON_STYLE);
return ReflectionToStringBuilder.toString(this, ToStringStyle.JSON_STYLE);
}
}
Expand Up @@ -22,8 +22,8 @@
*/
package com.synopsys.integration.detectable.detectables.nuget.model;

import org.apache.commons.lang3.builder.RecursiveToStringStyle;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.google.gson.annotations.SerializedName;

Expand Down Expand Up @@ -71,6 +71,6 @@ public boolean equals(final Object obj) {

@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, RecursiveToStringStyle.JSON_STYLE);
return ReflectionToStringBuilder.toString(this, ToStringStyle.JSON_STYLE);
}
}
Expand Up @@ -24,8 +24,8 @@

import java.util.List;

import org.apache.commons.lang3.builder.RecursiveToStringStyle;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.google.gson.annotations.SerializedName;

Expand All @@ -38,6 +38,6 @@ public class NugetPackageSet {

@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, RecursiveToStringStyle.JSON_STYLE);
return ReflectionToStringBuilder.toString(this, ToStringStyle.JSON_STYLE);
}
}