GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [core] CPD: Add total number of tokens to XML reports by maikelsteneker · Pull Request #4021 · pmd/pmd

Conversation

@maikelsteneker

@maikelsteneker maikelsteneker commented Jun 22, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Describe the PR

This PR extends the XML output of CPD. This output produces <duplication> elements for each duplication that is encountered. In addition to this, it now also produces <file> elements for each file with the number of tokens in the file added to this format. For example:

<?xml version="1.0" encoding="UTF-8"?>
<pmd-cpd>
   <file filename="/home/maikel/CR28584/file1.cs" totalNumberOfTokens="102"/>
   <file filename="/home/maikel/CR28584/file2.cs" totalNumberOfTokens="102"/>
   <duplication lines="15" tokens="102">
...

Other output formats currently don't contain this information, but could easily be extended to add it if needed.

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ghost

ghost commented Jun 22, 2022 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
1 Message
📖 Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel changed the title Add total number of tokens to XML reports [core] Add total number of tokens to XML reports Jun 23, 2022
@adangel adangel added the an:enhancement An improvement on existing features / rules label Jun 23, 2022
@oowekyala
oowekyala self-requested a review June 23, 2022 13:35
adangel
adangel reviewed Jun 24, 2022
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I'm not sure, whether it's a good idea to add this additional method to the existing interface CPDRenderer.... wdyt @oowekyala ?

Documentation-wise we should also update the example on https://pmd.github.io/latest/pmd_userdocs_cpd_report_formats.html#xml
Maybe it's even time to add a formal documentation of the format or even a schema?
Add these additional elements to the XML format shouldn't be a problem. E.g. the maven-pmd-plugin would just ignore this additional elements (I hope...).

Could you also describe the use case a bit? What is this information used for? The number of tokens is an indication of how big the file is. Or do you maybe want to calculate later on that e.g. file x contains 20% duplicated code?

The XML format is now rather flat. Should we maybe group the duplication inside a <file> element? That of course would render the whole format incompatible, but we could introduce complete new XML renderer with a different format...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/SimpleRenderer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/VSRenderer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/renderer/CPDRenderer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/renderer/CPDRenderer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CSVRenderer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/test/java/net/sourceforge/pmd/cpd/CSVRendererTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/test/java/net/sourceforge/pmd/cpd/XMLRendererTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@maikelsteneker

maikelsteneker commented Jun 24, 2022

Copy link
Copy Markdown
Contributor Author

Thank you for the comments!

On the use case: you seem to have guessed correctly already :) We're using data from CPD to calculate a percentage of duplicated code in a file/project. Until now, we've done this by calculating the number of lines that contain a duplication and we divide it by the total number of lines. This works reasonably well, but there are obvious examples where the results are unexpected (for example, removing all line breaks from a file means it can only have 0% or 100% duplication). If we can use the number of tokens instead, we'll have a more precise metric for the relative amount of duplication within a file.

On the XML format: my thinking was that adding additional elements would preserve backwards compatibility reasonably well (assuming users don't blindly consume all elements). Restructuring the format would require users to change the way the file is parsed. I think that changing the structure does not result in a worthwhile tradeoff in this case, but I'm not against changing it if you disagree.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala

oowekyala commented Jun 24, 2022

Copy link
Copy Markdown
Member

Thanks for the contribution @maikelsteneker

I'm not sure, whether it's a good idea to add this additional method to the existing interface CPDRenderer.... wdyt @oowekyala ?

This is indeed my main gripe with the code change. I think adding a method parameter is not the right way, if we want to extend the info available to renderers in the future, it would be more flexible to pack all parameters in an object (eg a "CpdReport"). I would rather introduce an interface CpdReportRenderer whose method takes a CpdReport and a Writer, and deprecate the old interface. This way we can add members to CpdReport later without breaking the renderer interface

I wouldn't add an AbstractCpdRenderer either. The renderer interfaces should remain simple and an abstract class hints otherwise. We can use an adapter pattern to convert from the old interface to the new one.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@maikelsteneker
maikelsteneker marked this pull request as draft June 28, 2022 13:07
@maikelsteneker
Add total number of tokens to XML reports
9fc8a56
@maikelsteneker
maikelsteneker force-pushed the report_total_number_of_tokens branch from e7c08e6 to 9fc8a56 Compare June 29, 2022 09:37
@maikelsteneker

maikelsteneker commented Jun 29, 2022

Copy link
Copy Markdown
Contributor Author

@oowekyala Thank you for the feedback!

I think you raised some very good points, so I have reworked my solution to resemble your proposal more closely. That is, I have introduced a new interface, the XML renderer is the only one that is changed as a result and the remaining renderers use an adapter to remain functioning. I guess that in the PMD 7.x branch, all renderers should implement just the latest interface, with the older ones being removed.

@adangel @oowekyala Could you take another look to see if this new implementation is up to your standards? Thanks in advance for your efforts!

oowekyala reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@maikelsteneker
maikelsteneker marked this pull request as ready for review June 29, 2022 10:21
oowekyala
oowekyala reviewed Jun 29, 2022
View reviewed changes

@oowekyala oowekyala left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing these changes, just a couple of minor comments

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDTask.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDReport.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPD.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPD.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

maikelsteneker and others added 2 commits June 30, 2022 11:11
@maikelsteneker
Improve encapsulation of CPD report contents
d544efe
@maikelsteneker @oowekyala
Hide CPDReport constructor
a15758f
Co-authored-by: Clément Fournier <[email protected]>
adangel
adangel reviewed Jun 30, 2022
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes!

From my point of view the only thing we should decide about is: Should we return a List<Matches> instead of Iterator<Matches> in the new CPDReport?

My other comments are some minor things that can also be changed later.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-core/src/test/java/net/sourceforge/pmd/cpd/XMLRendererTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/XMLRenderer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDReport.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDReport.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

maikelsteneker added 2 commits July 1, 2022 10:34
@maikelsteneker
Remove catch from test case
f4dd873
@maikelsteneker
Move sorting to CPDReport class
4d78901
@adangel adangel added this to the 6.48.0 milestone Jul 1, 2022
@adangel
adangel self-requested a review July 18, 2022 19:33
@adangel
Merge branch 'master' into pr-4021
ce6fead
adangel
adangel approved these changes Jul 21, 2022
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updated PR.

I'll change the things I've mentioned and merge this afterwards.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/renderer/CPDRendererAdapter.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDReport.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel added 3 commits July 21, 2022 15:45
@adangel
[core] Internalize methods in CPDConfiguration and CPDRendererAdapter
ee8622e
@adangel
[core] Refactor how CPD Renderers are determined
33bfd00
@adangel
[core] Refactor CPDReport to use a List
94c19d2
@adangel adangel changed the title [core] Add total number of tokens to XML reports [core] CPD: Add total number of tokens to XML reports Jul 21, 2022
@adangel adangel added the in:cpd Affects the copy-paste detector label Jul 21, 2022
adangel added 2 commits July 21, 2022 16:48
@adangel
[doc] Add deprecation notice for CPDRenderer
43a1733
@adangel
[doc] Update release notes (pmd#4021)
4568176
@adangel
adangel merged commit 029b4b2 into pmd:master Jul 21, 2022
@pacvz pacvz mentioned this pull request Aug 17, 2022
[core] CPD: Added begin and end token to XML reports #4095
Merged
4 tasks
@adangel adangel mentioned this pull request Jan 12, 2023
[cpd][core] Add displaying total number of tokens per file #1569
Closed
@adangel adangel mentioned this pull request May 7, 2026
[core] Refactor CPD internals to improve efficiency #6215
Open
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@oowekyala oowekyala oowekyala left review comments
@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

an:enhancement An improvement on existing features / rules in:cpd Affects the copy-paste detector

Projects

None yet

Milestone

6.48.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@maikelsteneker @oowekyala @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.