Skip to content

Commit 702d2c8

Browse files
#19 Hoisting RuleSet creation into constructor
Noticed that RuleSet creation can be hoisted into the constructor since it is invariant and always loads the same resource and rule. Hopefully this will improve performance slightly.
1 parent 2aaa49e commit 702d2c8

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@ public class PMDGodClassRuleRunner {
1717

1818
private SourceCodeProcessor sourceCodeProcessor;
1919
private Language java = LanguageRegistry.getLanguage(JavaLanguageModule.NAME);
20+
private RuleSets ruleSets;
2021

2122
public PMDGodClassRuleRunner() {
2223
PMD pmd = new PMD();
2324
sourceCodeProcessor = pmd.getSourceCodeProcessor();
25+
26+
RuleSet ruleSet = new RuleSetLoader().loadFromResource("category/java/design.xml");
27+
Rule godClassRule = ruleSet.getRuleByName("GodClass");
28+
godClassRule.setLanguage(java);
29+
30+
// add your rule to the ruleset
31+
RuleSetFactory ruleSetFactory = new RuleSetFactory();
32+
RuleSet ruleSet2 = ruleSetFactory.createSingleRuleRuleSet(godClassRule);
33+
ruleSets = new RuleSets(ruleSet2);
2434
}
2535

2636
public Optional<GodClass> runGodClassRule(File file) {
@@ -50,15 +60,6 @@ public Optional<GodClass> runPMD(File file) {
5060
}
5161

5262
public Optional<GodClass> runPMD(String sourceCodeFileName, InputStream inputStream) {
53-
RuleSet ruleSet = new RuleSetLoader().loadFromResource("category/java/design.xml");
54-
Rule godClassRule = ruleSet.getRuleByName("GodClass");
55-
godClassRule.setLanguage(java);
56-
57-
// add your rule to the ruleset
58-
RuleSetFactory ruleSetFactory = new RuleSetFactory();
59-
RuleSet ruleSet2 = ruleSetFactory.createSingleRuleRuleSet(godClassRule);
60-
RuleSets ruleSets = new RuleSets(ruleSet2);
61-
6263
GodClass godClass = null;
6364
try {
6465
// Set the javaVersion you are using. (*1)

0 commit comments

Comments
 (0)