Skip to content

Commit 52b9bea

Browse files
Fixed Sonar issues
1 parent 1d5c481 commit 52b9bea

File tree

7 files changed

+44
-95
lines changed

7 files changed

+44
-95
lines changed

cost-benefit-calculator/src/main/java/org/hjug/cbc/CostBenefitCalculator.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@
2121
@Slf4j
2222
public class CostBenefitCalculator {
2323

24-
public static void main(String[] args) {
25-
CostBenefitCalculator costBenefitCalculator = new CostBenefitCalculator();
26-
String path = "C:\\Code\\myfaces-tobago";
27-
List<RankedDisharmony> rankedDisharmonies = costBenefitCalculator.calculateCostBenefitValues(path);
28-
rankedDisharmonies.sort(Comparator.comparing(RankedDisharmony::getPriority).reversed());
29-
for (RankedDisharmony disharmony : rankedDisharmonies) {
30-
System.out.println("Priority: " + disharmony.getPriority() + "\t path: " + disharmony.getPath());
31-
}
32-
}
33-
34-
3524
public List<RankedDisharmony> calculateCostBenefitValues(String repositoryPath) {
3625

3726
RepositoryLogReader repositoryLogReader = new GitLogReader();
@@ -79,7 +68,7 @@ List<ScmLogInfo> getRankedChangeProneness(RepositoryLogReader repositoryLogReade
7968
}
8069

8170
private List<GodClass> getGodClasses(RepositoryLogReader repositoryLogReader, Repository repository) {
82-
Map<String, ByteArrayOutputStream> filesToScan = null;
71+
Map<String, ByteArrayOutputStream> filesToScan = new HashMap<>();
8372
try {
8473
filesToScan = repositoryLogReader.listRepositoryContentsAtHEAD(repository);
8574
} catch (IOException e) {
@@ -90,8 +79,11 @@ private List<GodClass> getGodClasses(RepositoryLogReader repositoryLogReader, Re
9079
PMDGodClassRuleRunner ruleRunner = new PMDGodClassRuleRunner();
9180

9281
List<GodClass> godClasses = new ArrayList<>();
93-
for (String filePath : filesToScan.keySet()) {
94-
ByteArrayInputStream inputStream = new ByteArrayInputStream(filesToScan.get(filePath).toByteArray());
82+
for (Map.Entry<String, ByteArrayOutputStream> entry : filesToScan.entrySet()) {
83+
String filePath = entry.getKey();
84+
ByteArrayOutputStream value = entry.getValue();
85+
86+
ByteArrayInputStream inputStream = new ByteArrayInputStream(value.toByteArray());
9587
Optional<GodClass> godClassOptional = ruleRunner.runGodClassRule(filePath, inputStream);
9688
godClassOptional.ifPresent(godClasses::add);
9789
}

cost-benefit-calculator/src/main/java/org/hjug/cbc/RankedDisharmony.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public class RankedDisharmony {
2727
private final Instant mostRecentCommitTime;
2828
private final Integer commitCount;
2929

30-
//private final String message;
31-
3230
public RankedDisharmony(GodClass godClass, ScmLogInfo scmLogInfo) {
3331
path = scmLogInfo.getPath();
3432
//from https://stackoverflow.com/questions/1011287/get-file-name-from-a-file-location-in-java

effort-ranker/src/main/java/org/hjug/metrics/GodClassRanker.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
import java.util.Comparator;
44
import java.util.List;
5-
import java.util.function.BiConsumer;
65
import java.util.function.Function;
76
import java.util.function.ObjIntConsumer;
8-
import java.util.function.ObjIntConsumer;
97

108
/**
119
* Created by Wendy on 11/16/2016.

effort-ranker/src/main/java/org/hjug/metrics/PMDGodClassRuleRunner.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Optional<GodClass> runPMD(String sourceCodeFileName, InputStream inputStr
5959

6060
RuleSets ruleSets = new RuleSets(ruleSet);
6161

62-
//List<String> ruleViolationMsgs = new ArrayList<String>();
62+
GodClass godClass = null;
6363
try{
6464
//Set the javaVersion you are using. (*1)
6565
//pmd.setJavaVersion(SourceType.JAVA_16); -- MAY NEED TO SPECIFY THIS...
@@ -74,13 +74,12 @@ public Optional<GodClass> runPMD(String sourceCodeFileName, InputStream inputStr
7474
//write results
7575
if (!ctx.getReport().isEmpty()) {
7676
for (final RuleViolation violation : ctx.getReport()) {
77-
GodClass godClass = new GodClass(sourceCodeFileName, violation.getPackageName(), violation.getDescription());
78-
return Optional.of(godClass);
77+
godClass = new GodClass(sourceCodeFileName, violation.getPackageName(), violation.getDescription());
7978
}
8079
}
81-
} catch (PMDException e) {
82-
//e.printStackTrace();
80+
} catch (PMDException ignore) {
81+
//ignore
8382
}
84-
return Optional.empty();
83+
return Optional.ofNullable(godClass);
8584
}
8685
}

effort-ranker/src/test/java/org/hjug/metrics/GodClassRankerTest.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
*/
1515
public class GodClassRankerTest {
1616

17-
private GodClassRanker godClassRanker = new GodClassRanker();
17+
private final GodClassRanker godClassRanker = new GodClassRanker();
1818

19-
private GodClass attributeHandler = new GodClass("org/hjug/git/AttributeHandler.java", "org.apache.myfaces.tobago.facelets", "null (WMC=79, ATFD=79, TCC=0.027777777777777776)");
20-
private GodClass attributeHandler2 = new GodClass("org/hjug/git/AttributeHandler.java", "org.apache.myfaces.tobago.facelets", "null (WMC=79, ATFD=79, TCC=0.027777777777777776)");
21-
private GodClass sorter = new GodClass("Sorter.java", "org.apache.myfaces.tobago.facelets", " God class (WMC=51, ATFD=25, TCC=0.2)");
22-
private GodClass sorter2 = new GodClass("Sorter2.java", "org.apache.myfaces.tobago.facelets", " God class (WMC=51, ATFD=25, TCC=0.2)");
23-
private GodClass themeImpl = new GodClass("ThemeImpl.java", "org.apache.myfaces.tobago.facelets", "God class (WMC=60, ATFD=16, TCC=0.07816091954022988)");
24-
private GodClass themeImpl2 = new GodClass("ThemeImpl2.java", "org.apache.myfaces.tobago.facelets", "God class (WMC=60, ATFD=16, TCC=0.07816091954022988)");
19+
private final GodClass attributeHandler = new GodClass("org/hjug/git/AttributeHandler.java", "org.apache.myfaces.tobago.facelets", "null (WMC=79, ATFD=79, TCC=0.027777777777777776)");
20+
private final GodClass attributeHandler2 = new GodClass("org/hjug/git/AttributeHandler.java", "org.apache.myfaces.tobago.facelets", "null (WMC=79, ATFD=79, TCC=0.027777777777777776)");
21+
private final GodClass sorter = new GodClass("Sorter.java", "org.apache.myfaces.tobago.facelets", " God class (WMC=51, ATFD=25, TCC=0.2)");
22+
private final GodClass sorter2 = new GodClass("Sorter2.java", "org.apache.myfaces.tobago.facelets", " God class (WMC=51, ATFD=25, TCC=0.2)");
23+
private final GodClass themeImpl = new GodClass("ThemeImpl.java", "org.apache.myfaces.tobago.facelets", "God class (WMC=60, ATFD=16, TCC=0.07816091954022988)");
24+
private final GodClass themeImpl2 = new GodClass("ThemeImpl2.java", "org.apache.myfaces.tobago.facelets", "God class (WMC=60, ATFD=16, TCC=0.07816091954022988)");
2525

2626

27-
private List<GodClass> godClasses = new ArrayList<GodClass>();
27+
private final List<GodClass> godClasses = new ArrayList<>();
2828

2929
@Before
3030
public void setUp() {
@@ -41,13 +41,13 @@ public void testRankGodClasses() {
4141
Assert.assertEquals("Sorter.java", godClasses.get(1).getFileName());
4242
Assert.assertEquals("org/hjug/git/AttributeHandler.java", godClasses.get(2).getFileName());
4343

44-
Assert.assertEquals(godClasses.get(0).getSumOfRanks().longValue(), 5);
45-
Assert.assertEquals(godClasses.get(1).getSumOfRanks().longValue(), 6);
46-
Assert.assertEquals(godClasses.get(2).getSumOfRanks().longValue(), 7);
44+
Assert.assertEquals(5, godClasses.get(0).getSumOfRanks().longValue());
45+
Assert.assertEquals(6, godClasses.get(1).getSumOfRanks().longValue());
46+
Assert.assertEquals(7, godClasses.get(2).getSumOfRanks().longValue());
4747

48-
Assert.assertEquals(godClasses.get(0).getOverallRank().longValue(), 1);
49-
Assert.assertEquals(godClasses.get(1).getOverallRank().longValue(), 2);
50-
Assert.assertEquals(godClasses.get(2).getOverallRank().longValue(), 3);
48+
Assert.assertEquals(1, godClasses.get(0).getOverallRank().longValue());
49+
Assert.assertEquals(2, godClasses.get(1).getOverallRank().longValue());
50+
Assert.assertEquals(3, godClasses.get(2).getOverallRank().longValue());
5151
}
5252

5353
@Test
@@ -59,9 +59,9 @@ public void testWmcRanker() {
5959
Assert.assertEquals("org/hjug/git/AttributeHandler.java", godClasses.get(2).getFileName());
6060

6161

62-
Assert.assertEquals(godClasses.get(0).getWmcRank().longValue(), 1);
63-
Assert.assertEquals(godClasses.get(1).getWmcRank().longValue(), 2);
64-
Assert.assertEquals(godClasses.get(2).getWmcRank().longValue(), 3);
62+
Assert.assertEquals(1, godClasses.get(0).getWmcRank().longValue());
63+
Assert.assertEquals(2, godClasses.get(1).getWmcRank().longValue());
64+
Assert.assertEquals(3, godClasses.get(2).getWmcRank().longValue());
6565
}
6666

6767
@Test

graph-data-generator/src/main/java/org/hjug/gdg/GraphDataGenerator.java

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,13 @@
33
import org.hjug.cbc.CostBenefitCalculator;
44
import org.hjug.cbc.RankedDisharmony;
55

6-
import java.io.IOException;
7-
import java.nio.charset.StandardCharsets;
8-
import java.nio.file.Files;
9-
import java.nio.file.Paths;
106
import java.util.ArrayList;
117
import java.util.List;
128

139
public class GraphDataGenerator {
1410

15-
private CostBenefitCalculator costBenefitCalculator = new CostBenefitCalculator();
16-
private List<RankedDisharmony> calculateCostBenefitValues = new ArrayList<>();
17-
18-
public static void main(String[] args) throws IOException {
19-
String path = "C:\\Code\\myfaces-tobago";
20-
GraphDataGenerator graphDataGenerator = new GraphDataGenerator();
21-
22-
List<String> output = new ArrayList<>();
23-
output.add(graphDataGenerator.generateJavaScript(path));
24-
25-
Files.write(Paths.get("chart.html"), output, StandardCharsets.UTF_8);
26-
}
27-
28-
public String generateJavaScript(String repositoryPath) {
29-
String open = "<html>\n" +
30-
" <head>\n" +
31-
" <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n" +
32-
" <script type=\"text/javascript\">\n";
33-
34-
String scriptStart = getScriptStart();
35-
36-
String middle = generateBubbleChartData(repositoryPath);
37-
38-
String scriptEnd =
39-
getScriptEnd();
40-
String endHead = "</script>" +
41-
" </head>\n" +
42-
" <body>\n" ;
43-
String chartDiv = getChartDiv();
44-
45-
String close =
46-
" </body>\n" +
47-
"</html>";
48-
49-
return open + scriptStart + middle + scriptEnd + endHead + chartDiv + close;
50-
}
11+
private final CostBenefitCalculator costBenefitCalculator = new CostBenefitCalculator();
12+
private final List<RankedDisharmony> calculateCostBenefitValues = new ArrayList<>();
5113

5214
public String getScriptStart() {
5315
return
@@ -77,9 +39,9 @@ public String getScriptEnd() {
7739
" }\n";
7840
}
7941

80-
public String getChartDiv() {
42+
/* public String getChartDiv() {
8143
return " <div id=\"series_chart_div\" style=\"width: 900px; height: 500px;\"></div>\n";
82-
}
44+
}*/
8345

8446

8547
public String generateBubbleChartData(String repositoryPath) {

refactor-first-maven-plugin/src/main/java/org/hjug/mavenreport/RefactorFirstMavenReport.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ protected void executeReport(Locale locale) throws MavenReportException {
112112

113113
SinkEventAttributeSet divAttrs = new SinkEventAttributeSet();
114114
divAttrs.addAttribute( SinkEventAttributes.ID, "series_chart_div" );
115-
mainSink.unknown( "div", new Object[]{new Integer( HtmlMarkup.TAG_TYPE_START )}, divAttrs );
116-
mainSink.unknown( "div", new Object[]{new Integer( HtmlMarkup.TAG_TYPE_END )}, null );
115+
mainSink.unknown( "div", new Object[]{HtmlMarkup.TAG_TYPE_START}, divAttrs );
116+
mainSink.unknown( "div", new Object[]{HtmlMarkup.TAG_TYPE_END}, null );
117117

118118
SinkEventAttributes tableAttributes = new SinkEventAttributeSet();
119119
tableAttributes.addAttribute(SinkEventAttributes.BORDER, "5px");
@@ -260,35 +260,35 @@ protected void executeReport(Locale locale) throws MavenReportException {
260260
* @See https://maven.apache.org/doxia/developers/sink.html#How_to_inject_javascript_code_into_HTML
261261
*/
262262
private void generateChart(GraphDataGenerator graphDataGenerator, Sink mainSink) {
263+
263264
SinkEventAttributeSet googleChartImport = new SinkEventAttributeSet();
264265
googleChartImport.addAttribute( SinkEventAttributes.TYPE, "text/javascript" );
265266
googleChartImport.addAttribute( SinkEventAttributes.SRC, "https://www.gstatic.com/charts/loader.js" );
266267

267-
mainSink.unknown( "script", new Object[]{new Integer( HtmlMarkup.TAG_TYPE_START )}, googleChartImport);
268-
mainSink.unknown( "script", new Object[]{new Integer( HtmlMarkup.TAG_TYPE_END )}, null);
268+
String script = "script";
269+
mainSink.unknown(script, new Object[]{HtmlMarkup.TAG_TYPE_START}, googleChartImport);
270+
mainSink.unknown(script, new Object[]{HtmlMarkup.TAG_TYPE_END}, null);
269271
String scriptStart = graphDataGenerator.getScriptStart();
270272
String bubbleChartData = graphDataGenerator.generateBubbleChartData(project.getBasedir().getPath());
271273
String scriptEnd = graphDataGenerator.getScriptEnd();
272274

273275
String javascriptCode = scriptStart + bubbleChartData + scriptEnd;
274276

275-
String reportOutputDirectory = project.getReporting().getOutputDirectory();
277+
String reportOutputDirectory = project.getModel().getReporting().getOutputDirectory();
276278
File reportOutputDir = new File(reportOutputDirectory);
277279
if(!reportOutputDir.exists()) {
278-
System.out.println("Creating report directory");
279280
reportOutputDir.mkdirs();
280281
}
281282
String pathname = reportOutputDirectory + File.separator + "gchart.js";
282-
System.out.println("Path: " + pathname);
283283

284-
File script = new File(pathname);
284+
File scriptFile = new File(pathname);
285285
try {
286-
System.out.println("File created: " + script.createNewFile());
286+
scriptFile.createNewFile();
287287
} catch (IOException e) {
288288
e.printStackTrace();
289289
}
290290

291-
try(BufferedWriter writer = new BufferedWriter(new FileWriter(script))) {
291+
try(BufferedWriter writer = new BufferedWriter(new FileWriter(scriptFile))) {
292292
writer.write(javascriptCode);
293293
} catch (IOException e) {
294294
e.printStackTrace();
@@ -298,7 +298,7 @@ private void generateChart(GraphDataGenerator graphDataGenerator, Sink mainSink)
298298
javascript.addAttribute( SinkEventAttributes.TYPE, "text/javascript");
299299
javascript.addAttribute( SinkEventAttributes.SRC, "./gchart.js");
300300

301-
mainSink.unknown( "script", new Object[]{new Integer( HtmlMarkup.TAG_TYPE_START )}, javascript );
302-
mainSink.unknown( "script", new Object[]{new Integer( HtmlMarkup.TAG_TYPE_END )}, null );
301+
mainSink.unknown(script, new Object[]{HtmlMarkup.TAG_TYPE_START }, javascript );
302+
mainSink.unknown(script, new Object[]{HtmlMarkup.TAG_TYPE_END}, null );
303303
}
304304
}

0 commit comments

Comments
 (0)