Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Add PMD for static analysis #102

Merged
merged 12 commits into from
Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
35 changes: 35 additions & 0 deletions config/pmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0"?>

<ruleset name="Basic"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
The Basic ruleset contains a collection of good practices which should be followed.
</description>

<rule ref="category/java/errorprone.xml"> <exclude name="AvoidBranchingStatementAsLastInLoop" /></rule>
<rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor" />
<rule ref="category/java/errorprone.xml/AvoidMultipleUnaryOperators" />
<rule ref="category/java/errorprone.xml/AvoidUsingOctalValues" />
<rule ref="category/java/errorprone.xml/BrokenNullCheck" />
<rule ref="category/java/errorprone.xml/CheckSkipResult" />
<rule ref="category/java/errorprone.xml/ClassCastExceptionWithToArray"/>
<rule ref="category/java/errorprone.xml/DontUseFloatTypeForLoopIndices" />
<rule ref="category/java/errorprone.xml/JumbledIncrementer" />
<rule ref="category/java/errorprone.xml/MisplacedNullCheck" />
<rule ref="category/java/errorprone.xml/OverrideBothEqualsAndHashcode" />
<rule ref="category/java/errorprone.xml/ReturnFromFinallyBlock"/>
<rule ref="category/java/errorprone.xml/UnconditionalIfStatement" />
<rule ref="category/java/multithreading.xml/AvoidThreadGroup" />
<rule ref="category/java/multithreading.xml/DontCallThreadRun" />
<rule ref="category/java/multithreading.xml/DoubleCheckedLocking" />
<rule ref="category/java/bestpractices.xml/AvoidUsingHardCodedIP" />
<rule ref="category/java/bestpractices.xml/CheckResultSet"/>
<rule ref="category/java/codestyle.xml/ExtendsObject" />
<rule ref="category/java/codestyle.xml/ForLoopShouldBeWhileLoop" />
<rule ref="category/java/performance.xml/BigIntegerInstantiation" />
<rule ref="category/java/performance.xml/BooleanInstantiation"/>
<rule ref="category/java/design.xml/CollapsibleIfStatements" />
<rule ref="category/java/design.xml/SimplifiedTernary" />
</ruleset>
martinda marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 20 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<rulesets>
<ruleset>/config/pmd.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
Expand Down Expand Up @@ -255,5 +274,4 @@
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.org.jenkins.custom.jenkins.distribution.service.services.PackagerDownloadService;
import com.org.jenkins.custom.jenkins.distribution.service.util.Util;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.io.ByteArrayInputStream;
import java.util.logging.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
@RequestMapping("api/plugin")
public class PluginController {

private final static Logger LOGGER = Logger.getLogger(PluginController.class.getName());
private final UpdateCenterService updateCenterService;
private transient final static Logger LOGGER = Logger.getLogger(PluginController.class.getName());
private transient final UpdateCenterService updateCenterService;

@Autowired
public PluginController(UpdateCenterService updateCenterService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.json.JSONArray;
import org.json.JSONObject;


@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class PackageConfigGenerator {

private final static UpdateCenterService updateCenterService = new UpdateCenterService();
Expand Down Expand Up @@ -95,9 +95,6 @@ private static JSONObject generateSystemSettings(JSONObject systemSettings) {

private static JSONArray generatePluginList(JSONArray pluginArray) throws Exception {

JSONObject updateCenterJSON = updateCenterService.downloadUpdateCenterJSON();
JSONObject jsonPluginList = updateCenterJSON.getJSONObject("plugins");

JSONArray pluginInfoArray = new JSONArray();
for (int i = 0; i < pluginArray.length(); i++) {
JSONObject pluginObject = pluginArray.getJSONObject(i);
Expand All @@ -107,7 +104,10 @@ private static JSONArray generatePluginList(JSONArray pluginArray) throws Except
String pluginName = pluginNames.next();
pluginInfo.put("groupId", "org.jenkins-ci.plugins");
pluginInfo.put("artifactId", pluginName);
pluginInfo.put("source", new JSONObject().put("version", jsonPluginList.getJSONObject(pluginName).getString("version")));
pluginInfo.put("source", new JSONObject().put("version", updateCenterService.downloadUpdateCenterJSON()
.getJSONObject("plugins")
.getJSONObject(pluginName)
.getString("version")));
pluginInfoArray.put(pluginInfo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.jenkins.tools.warpackager.lib.config.Config;
import io.jenkins.tools.warpackager.lib.impl.Builder;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.yaml.snakeyaml.Yaml;

@Service
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public class PackagerDownloadService {

private final static Logger LOGGER = Logger.getLogger(PackagerDownloadService.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
@Service
public class UpdateCenterService {

private Util util = new Util();
private transient Util util = new Util();

private final static Logger LOGGER = Logger.getLogger(UpdateCenterService.class.getName());
private static final String UPDATE_CENTER_JSON_URL = "https://updates.jenkins.io/current/update-center.actual.json";
private int updateFlag = 0;
private String responseString;
private String updateCenterFilePath = "";
private transient final static Logger LOGGER = Logger.getLogger(UpdateCenterService.class.getName());
private transient static final String UPDATE_CENTER_JSON_URL = "https://updates.jenkins.io/current/update-center.actual.json";
private transient int updateFlag = 0;
private transient String responseString;
private transient String updateCenterFilePath = "";

public JSONObject downloadUpdateCenterJSON() throws Exception {
/*
Expand All @@ -36,14 +36,15 @@ public JSONObject downloadUpdateCenterJSON() throws Exception {
File updateCenterFile = File.createTempFile("update-center", ".json");
updateCenterFilePath = updateCenterFile.getPath();
LOGGER.info("Creating a new file" + updateCenterFile.getPath());
HttpGet get = new HttpGet(UPDATE_CENTER_JSON_URL);
LOGGER.info("Executing Request at " + UPDATE_CENTER_JSON_URL);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(get);
responseString = EntityUtils.toString(response.getEntity());
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(new HttpGet(UPDATE_CENTER_JSON_URL))) {
responseString = EntityUtils.toString(response.getEntity());}
martinda marked this conversation as resolved.
Show resolved Hide resolved
byte[] buf = responseString.getBytes();
Files.write(updateCenterFile.toPath(), buf);
updateFlag = 1;


} else {
responseString = readFileAsString(updateCenterFilePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.logging.Logger;
import org.codehaus.plexus.util.FileUtils;
Expand All @@ -17,7 +18,7 @@ public class Util {
private final static Logger LOGGER = Logger.getLogger(Util.class.getName());

public File getFileFromResources(String fileName) {
ClassLoader classLoader = getClass().getClassLoader();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource(fileName);
if (resource == null) {
throw new IllegalArgumentException("file is not found!");
Expand Down