Skip to content

[core] Fix #3349 add source code in PMD output#3354

Closed
gurmsc5 wants to merge 10 commits into
pmd:masterfrom
gurmsc5:master
Closed

[core] Fix #3349 add source code in PMD output#3354
gurmsc5 wants to merge 10 commits into
pmd:masterfrom
gurmsc5:master

Conversation

@gurmsc5

@gurmsc5 gurmsc5 commented Jun 23, 2021

Copy link
Copy Markdown

Describe the PR

  • Configured CSV and XML renderers to include source
    code snippet when running PMD

Related issues

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)

- Configured CSV and XML renderers to include source
code snippet
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/renderers/XMLRenderer.java Outdated
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/renderers/Renderer.java Outdated
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/SourceCode.java
@adangel adangel changed the title Fix #3349 add source code in PMD output [core] Fix #3349 add source code in PMD output Jun 24, 2021
@gurmsc5 gurmsc5 closed this Jun 24, 2021
gurmsc5 added 4 commits June 25, 2021 10:24
- Added XMLVerboseRenderer that can be called
using "-f xml-verbose" which will include code snippet that was
cause of the RuleViolation
@gurmsc5 gurmsc5 reopened this Jul 6, 2021
@gurmsc5 gurmsc5 requested a review from oowekyala July 6, 2021 14:09

@oowekyala oowekyala left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for making changes, this looks better!

Comment thread pmd-core/src/main/java/net/sourceforge/pmd/RuleViolation.java Outdated
@gurmsc5 gurmsc5 requested a review from oowekyala July 12, 2021 13:22
@ghost

ghost commented Jul 12, 2021

Copy link
Copy Markdown
1 Message
📖 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

@oowekyala oowekyala left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@adangel could you take a look? Is this something we should do in pmd 6?

public String getSourceCode(String encoding) {
if ("".equals(getFilename())
|| Files.notExists(Paths.get(getFilename()))) {
return null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The fact that this method may return null should be documented

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fair enough

protected String methodName = "";
protected String variableName = "";

public ParametricRuleViolation(Rule theRule, RuleContext ctx, T node, String message, String snippet) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nobody uses this constructor outside tests, so I don't think your feature works. Actually it looks like it's a real pain to support this as you'll have to change the RuleViolationFactory of every language. This is not a good idea since that has already been removed from pmd 7...

@Override
public String getSourceCode(String encoding) {
try {
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(rawCodeSnippet));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

... The only reasonable way I see to implement this in pmd 6 is to use a FileCodeLoader here and load the filename, then slice the contents of the file. This should not be done eagerly as it sounds really bad for performance.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I used StringCodeLoader here purely for testing purposes used by XMLVerboseRendererTest. Using FileCodeLoader caused an exception during testing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes but then your rawCodeSnippet is not the code snippet, it's the entire source file, since you slice it late. We can't keep all analysed files in memory just for this renderer to work...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I intended my changes in ParametricRuleViolation class to be only used in unit testing. I can create a separate class for unit testing instead.

return sourceCode.getSlice(getBeginLine(), getEndLine());

} catch (RuntimeException e) {
return null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I also wonder how much of a feature that is, if it's so fragile. Why would your code here fail?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I had added this when I was using SourceCode.FileCodeLoader as it was causing an exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[core] Enhance PMD output to include source code snippet similar to CPD

2 participants