Skip to content

Commit

Permalink
Merge pull request #4095 from pacvz:master
Browse files Browse the repository at this point in the history
[core] CPD: Added begin and end token to XML reports #4095
  • Loading branch information
adangel committed Aug 30, 2022
2 parents 54a2614 + 504d2ad commit 814ee91
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
8 changes: 8 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -6798,6 +6798,14 @@
"contributions": [
"doc"
]
},{
"login": "pacvz",
"name": "pacvz",
"avatar_url": "https://avatars.githubusercontent.com/u/35453365?v=4",
"profile": "https://github.com/pacvz",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
21 changes: 12 additions & 9 deletions docs/pages/pmd/projectdocs/credits.md

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/pages/pmd/userdocs/cpd/cpd_report_formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ Example:
<file path="/home/pmd/source/pmd-core/src/test/java/net/sourceforge/pmd/RuleReferenceTest.java" totalNumberOfTokens="523"/>
<file path="/home/pmd/source/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/JaxenXPathRuleQueryTest.java" totalNumberOfTokens="120"/>
<duplication lines="33" tokens="239">
<file column="29" endcolumn="75" endline="64" line="32"
<file column="29" endcolumn="75" endline="64" line="32" begintoken="2356" endtoken="2594"
path="/home/pmd/source/pmd-core/src/test/java/net/sourceforge/pmd/RuleReferenceTest.java"/>
<file column="37" endcolumn="75" endline="100" line="68"
<file column="37" endcolumn="75" endline="100" line="68" begintoken="5700" endtoken="5938"
path="/home/pmd/source/pmd-core/src/test/java/net/sourceforge/pmd/RuleReferenceTest.java"/>
<codefragment><![CDATA[ public void testOverride() {
final StringProperty PROPERTY1_DESCRIPTOR = new StringProperty("property1", "Test property", null, 0f);
Expand Down Expand Up @@ -143,11 +143,11 @@ Example:
validateOverriddenValues(PROPERTY1_DESCRIPTOR, PROPERTY2_DESCRIPTOR, ruleReference);]]></codefragment>
</duplication>
<duplication lines="16" tokens="110">
<file column="9" endcolumn="28" endline="81" line="66"
<file column="9" endcolumn="28" endline="81" line="66" begintoken="3000" endtoken="3109"
path="/home/pmd/source/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/JaxenXPathRuleQueryTest.java"/>
<file column="9" endcolumn="28" endline="103" line="88"
<file column="9" endcolumn="28" endline="103" line="88" begintoken="3200" endtoken="3309"
path="/home/pmd/source/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/JaxenXPathRuleQueryTest.java"/>
<file column="9" endcolumn="28" endline="125" line="110"
<file column="9" endcolumn="28" endline="125" line="110" begintoken="3400" endtoken="3509"
path="/home/pmd/source/pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/JaxenXPathRuleQueryTest.java"/>
<codefragment><![CDATA[ JaxenXPathRuleQuery query = createQuery(xpath);
List<String> ruleChainVisits = query.getRuleChainVisits();
Expand Down
1 change: 1 addition & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ This is a {{ site.pmd.release_type }} release.
* [#4081](https://github.com/pmd/pmd/pull/4081): \[apex] Remove Jorje leaks outside `ast` package - [@eklimo](https://github.com/eklimo)
* [#4083](https://github.com/pmd/pmd/pull/4083): \[java] UnnecessaryImport false positive for on-demand imports of nested classes (fix for #4082) - [@abyss638](https://github.com/abyss638)
* [#4092](https://github.com/pmd/pmd/pull/4092): \[apex] Implement ApexQualifiableNode for ASTUserEnum - [@aaronhurst-google](https://github.com/aaronhurst-google)
* [#4095](https://github.com/pmd/pmd/pull/4095): \[core] CPD: Added begin and end token to XML reports - [@pacvz](https://github.com/pacvz)
* [#4097](https://github.com/pmd/pmd/pull/4097): \[apex] ApexUnitTestClassShouldHaveAssertsRule: Support new Assert class (Apex v56.0) - [@tprouvot](https://github.com/tprouvot)
* [#4104](https://github.com/pmd/pmd/pull/4104): \[doc] Add MegaLinter in the list of integrations - [@nvuillam](https://github.com/nvuillam)

Expand Down
8 changes: 8 additions & 0 deletions pmd-core/src/main/java/net/sourceforge/pmd/cpd/Mark.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public int getBeginColumn() {
return this.token.getBeginColumn(); // TODO Java 1.8 make optional
}

public int getBeginTokenIndex() {
return this.token.getIndex();
}

public int getEndLine() {
return getBeginLine() + getLineCount() - 1;
}
Expand All @@ -48,6 +52,10 @@ public int getEndColumn() {
return this.endToken == null ? -1 : this.endToken.getEndColumn(); // TODO Java 1.8 make optional
}

public int getEndTokenIndex() {
return this.endToken == null ? -1 : this.endToken.getIndex();
}

public int getLineCount() {
return this.lineCount;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ private Element addFilesToDuplicationElement(Document doc, Element duplication,
if (endCol != -1) {
file.setAttribute("endcolumn", String.valueOf(endCol));
}
final int beginIndex = mark.getBeginTokenIndex();
final int endIndex = mark.getEndTokenIndex();
file.setAttribute("begintoken", String.valueOf(beginIndex));
if (endIndex != -1) {
file.setAttribute("endtoken", String.valueOf(endIndex));
}
duplication.appendChild(file);
}
return duplication;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,38 @@ public void testFilesWithNumberOfTokens() throws IOException, ParserConfiguratio
assertEquals("888", attributes.getNamedItem("totalNumberOfTokens").getNodeValue());
}

@Test
public void testGetDuplicationStartEnd() throws IOException, ParserConfigurationException, SAXException {
TokenEntry.clearImages();
final CPDReportRenderer renderer = new XMLRenderer();
final List<Match> matches = new ArrayList<>();
final String filename = "/var/Foo.java";
final int lineCount = 6;
final String codeFragment = "code\nfragment";
final Mark mark1 = createMark("public", filename, 1, lineCount, codeFragment, 2, 3);
final Mark mark2 = createMark("stuff", filename, 73, lineCount, codeFragment, 4, 5);
final Match match = new Match(75, mark1, mark2);
matches.add(match);
final Map<String, Integer> numberOfTokensPerFile = new HashMap<>();
numberOfTokensPerFile.put(filename, 888);
final CPDReport report = new CPDReport(matches, numberOfTokensPerFile);
final StringWriter writer = new StringWriter();
renderer.render(report, writer);
final String xmlOutput = writer.toString();
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new ByteArrayInputStream(xmlOutput.getBytes(ENCODING)));
final NodeList files = doc.getElementsByTagName("file");
final Node dup_1 = files.item(1);
final NamedNodeMap attrs_1 = dup_1.getAttributes();
assertEquals("0", attrs_1.getNamedItem("begintoken").getNodeValue());
assertEquals("1", attrs_1.getNamedItem("endtoken").getNodeValue());

final Node dup_2 = files.item(2);
final NamedNodeMap attrs_2 = dup_2.getAttributes();
assertEquals("2", attrs_2.getNamedItem("begintoken").getNodeValue());
assertEquals("3", attrs_2.getNamedItem("endtoken").getNodeValue());
}

@Test
public void testRendererEncodedPath() throws IOException {
CPDRenderer renderer = new XMLRenderer();
Expand Down

0 comments on commit 814ee91

Please sign in to comment.