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 — [java] CommentRequired: make property names consistent by snuyanzin · Pull Request #2069 · pmd/pmd

[java] CommentRequired: make property names consistent #2069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
adangel merged 4 commits into pmd:master from snuyanzin:PMD-1683
Dec 20, 2019

Uh oh!

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

Merged

[java] CommentRequired: make property names consistent #2069

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
88ff3f0
[java] CommentRequired: Separate header comment from class comment an…
snuyanzin Oct 15, 2019
6cec748
[java] CommentRequired: Make headerCommentRequirement ignored by defa…
snuyanzin Oct 15, 2019
d8ed0cf
[java] CommentRequired: Deprecated property "headerCommentRequirement"
adangel Dec 17, 2019
3ec8914
[doc] Update release notes, refs #2069, fixes #1683
adangel Dec 17, 2019
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

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

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

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

Diff view
Diff view

There are no files selected for viewing

8 changes: 8 additions & 0 deletions 8 docs/pages/release_notes.md
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ While the language implementation is quite complete, Modelica support is conside
for now. This is to allow us to change the rule API (e.g. the AST classes) slightly and improve
the implementation based on your feedback.

#### Modified Rules

* The Java rule {% rule "java/documentation/CommentRequired" %} (`java-documentation`) has a new property
`classCommentRequirement`. This replaces the now deprecated property `headerCommentRequirement`, since
the name was misleading. (File) header comments are not checked, but class comments are.

### Fixed Issues

* java-codestyle
* [#2167](https://github.com/pmd/pmd/issues/2167): \[java] UnnecessaryLocalBeforeReturn false positive with variable captured by method reference
* java-bestpractices
* [#2149](https://github.com/pmd/pmd/issues/2149): \[java] JUnitAssertionsShouldIncludeMessage - False positive with assertEquals and JUnit5
* java-documentation
* [#1683](https://github.com/pmd/pmd/issues/1683): \[java] CommentRequired property names are inconsistent
* java-performance
* [#2141](https://github.com/pmd/pmd/issues/2141): \[java] StringInstatiation: False negative with String-array access

Expand Down Expand Up @@ -81,6 +88,7 @@ You can identify them with the `@InternalApi` annotation. You'll also get a depr
### External Contributions

* [#2041](https://github.com/pmd/pmd/pull/2041): \[modelica] Initial implementation for PMD - [Anatoly Trosinenko](https://github.com/atrosinenko)
* [#2069](https://github.com/pmd/pmd/pull/2069): \[java] CommentRequired: make property names consistent - [snuyanzin](https://github.com/snuyanzin)

{% endtocmaker %}

16 changes: 9 additions & 7 deletions 16 ...a/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/AbstractCommentRule.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTPackageDeclaration;
import net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode;
import net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessTypeNode;
import net.sourceforge.pmd.lang.java.ast.AbstractJavaNode;
import net.sourceforge.pmd.lang.java.ast.Comment;
import net.sourceforge.pmd.lang.java.ast.CommentUtil;
import net.sourceforge.pmd.lang.java.ast.FormalComment;
Expand Down Expand Up @@ -64,17 +66,15 @@ protected void assignCommentsToDeclarations(ASTCompilationUnit cUnit) {

SortedMap<Integer, Node> itemsByLineNumber = orderedCommentsAndDeclarations(cUnit);
FormalComment lastComment = null;
AbstractJavaAccessNode lastNode = null;
AbstractJavaNode lastNode = null;

for (Entry<Integer, Node> entry : itemsByLineNumber.entrySet()) {
Node value = entry.getValue();

if (value instanceof AbstractJavaAccessNode) {
AbstractJavaAccessNode node = (AbstractJavaAccessNode) value;

if (value instanceof AbstractJavaAccessNode || value instanceof ASTPackageDeclaration) {
AbstractJavaNode node = (AbstractJavaNode) value;
// maybe the last comment is within the last node
if (lastComment != null && isCommentNotWithin(lastComment, lastNode, node)
&& isCommentBefore(lastComment, node)) {
if (lastComment != null && isCommentNotWithin(lastComment, lastNode, value)
&& isCommentBefore(lastComment, value)) {
node.comment(lastComment);
lastComment = null;
}
Expand Down Expand Up @@ -108,6 +108,8 @@ private boolean isCommentBefore(FormalComment n1, Node n2) {
protected SortedMap<Integer, Node> orderedCommentsAndDeclarations(ASTCompilationUnit cUnit) {
SortedMap<Integer, Node> itemsByLineNumber = new TreeMap<>();

addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTPackageDeclaration.class, true));

addDeclarations(itemsByLineNumber, cUnit.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class, true));

addDeclarations(itemsByLineNumber, cUnit.getComments());
Expand Down
50 changes: 42 additions & 8 deletions 50 ...a/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;

import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
Expand All @@ -32,19 +34,21 @@
* @author Brian Remedios
*/
public class CommentRequiredRule extends AbstractCommentRule {
private static final Logger LOG = Logger.getLogger(CommentRequiredRule.class.getName());

// Used to pretty print a message
private static final Map<String, String> DESCRIPTOR_NAME_TO_COMMENT_TYPE = new HashMap<>();


private static final PropertyDescriptor<CommentRequirement> ACCESSOR_CMT_DESCRIPTOR
= requirementPropertyBuilder("accessorCommentRequirement", "Comments on getters and setters\"")
.defaultValue(CommentRequirement.Ignored).build();
private static final PropertyDescriptor<CommentRequirement> OVERRIDE_CMT_DESCRIPTOR
= requirementPropertyBuilder("methodWithOverrideCommentRequirement", "Comments on @Override methods")
.defaultValue(CommentRequirement.Ignored).build();
private static final PropertyDescriptor<CommentRequirement> HEADER_CMT_REQUIREMENT_DESCRIPTOR
= requirementPropertyBuilder("headerCommentRequirement", "Header comments").build();
= requirementPropertyBuilder("headerCommentRequirement", "Deprecated! Header comments. Please use the property \"classCommentRequired\" instead.").build();
private static final PropertyDescriptor<CommentRequirement> CLASS_CMT_REQUIREMENT_DESCRIPTOR
= requirementPropertyBuilder("classCommentRequirement", "Class comments").build();
private static final PropertyDescriptor<CommentRequirement> FIELD_CMT_REQUIREMENT_DESCRIPTOR
= requirementPropertyBuilder("fieldCommentRequirement", "Field comments").build();
private static final PropertyDescriptor<CommentRequirement> PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR
Expand All @@ -60,10 +64,13 @@ public class CommentRequiredRule extends AbstractCommentRule {
= requirementPropertyBuilder("serialPersistentFieldsCommentRequired", "Serial persistent fields comments")
.defaultValue(CommentRequirement.Ignored).build();

/** stores the resolved property values. This is necessary in order to transparently use deprecated properties. */
private final Map<PropertyDescriptor<CommentRequirement>, CommentRequirement> propertyValues = new HashMap<>();

public CommentRequiredRule() {
definePropertyDescriptor(OVERRIDE_CMT_DESCRIPTOR);
definePropertyDescriptor(ACCESSOR_CMT_DESCRIPTOR);
definePropertyDescriptor(CLASS_CMT_REQUIREMENT_DESCRIPTOR);
definePropertyDescriptor(HEADER_CMT_REQUIREMENT_DESCRIPTOR);
definePropertyDescriptor(FIELD_CMT_REQUIREMENT_DESCRIPTOR);
definePropertyDescriptor(PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR);
Expand All @@ -73,10 +80,37 @@ public CommentRequiredRule() {
definePropertyDescriptor(SERIAL_PERSISTENT_FIELDS_CMT_REQUIREMENT_DESCRIPTOR);
}

@Override
public void start(RuleContext ctx) {
propertyValues.put(ACCESSOR_CMT_DESCRIPTOR, getProperty(ACCESSOR_CMT_DESCRIPTOR));
propertyValues.put(OVERRIDE_CMT_DESCRIPTOR, getProperty(OVERRIDE_CMT_DESCRIPTOR));
propertyValues.put(FIELD_CMT_REQUIREMENT_DESCRIPTOR, getProperty(FIELD_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR, getProperty(PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR, getProperty(PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(ENUM_CMT_REQUIREMENT_DESCRIPTOR, getProperty(ENUM_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR,
getProperty(SERIAL_VERSION_UID_CMT_REQUIREMENT_DESCRIPTOR));
propertyValues.put(SERIAL_PERSISTENT_FIELDS_CMT_REQUIREMENT_DESCRIPTOR,
getProperty(SERIAL_PERSISTENT_FIELDS_CMT_REQUIREMENT_DESCRIPTOR));

CommentRequirement headerCommentRequirementValue = getProperty(HEADER_CMT_REQUIREMENT_DESCRIPTOR);
boolean headerCommentRequirementValueOverridden = headerCommentRequirementValue != CommentRequirement.Required;
CommentRequirement classCommentRequirementValue = getProperty(CLASS_CMT_REQUIREMENT_DESCRIPTOR);
boolean classCommentRequirementValueOverridden = classCommentRequirementValue != CommentRequirement.Required;

if (headerCommentRequirementValueOverridden && !classCommentRequirementValueOverridden) {
LOG.warning("Rule CommentRequired uses deprecated property 'headerCommentRequirement'. "
+ "Future versions of PMD will remove support for this property. "
+ "Please use 'classCommentRequirement' instead!");
propertyValues.put(CLASS_CMT_REQUIREMENT_DESCRIPTOR, headerCommentRequirementValue);
} else {
propertyValues.put(CLASS_CMT_REQUIREMENT_DESCRIPTOR, classCommentRequirementValue);
}
}

private void checkCommentMeetsRequirement(Object data, AbstractJavaNode node,
PropertyDescriptor<CommentRequirement> descriptor) {
switch (getProperty(descriptor)) {
switch (propertyValues.get(descriptor)) {
case Ignored:
break;
case Required:
Expand Down Expand Up @@ -109,7 +143,7 @@ private void commentRequiredViolation(Object data, AbstractJavaNode node,

@Override
public Object visit(ASTClassOrInterfaceDeclaration decl, Object data) {
checkCommentMeetsRequirement(data, decl, HEADER_CMT_REQUIREMENT_DESCRIPTOR);
checkCommentMeetsRequirement(data, decl, CLASS_CMT_REQUIREMENT_DESCRIPTOR);
return super.visit(decl, data);
}

Expand Down Expand Up @@ -181,9 +215,9 @@ private boolean isSerialVersionUID(ASTFieldDeclaration field) {
* This field must be initialized with an array of ObjectStreamField objects.
* The modifiers for the field are required to be private, static, and final.
*
* @see <a href="https://docs.oracle.com/javase/7/docs/platform/serialization/spec/serial-arch.html#6250">Oracle docs</a>
* @param field the field, must not be null
* @return true if the field ia a serialPersistentFields variable, otherwise false
* @return true if the field is a serialPersistentFields variable, otherwise false
* @see <a href="https://docs.oracle.com/javase/7/docs/platform/serialization/spec/serial-arch.html#6250">Oracle docs</a>
*/
private boolean isSerialPersistentFields(final ASTFieldDeclaration field) {
return "serialPersistentFields".equals(field.getVariableName())
Expand All @@ -200,7 +234,6 @@ public Object visit(ASTEnumDeclaration decl, Object data) {
return super.visit(decl, data);
}


@Override
public Object visit(ASTCompilationUnit cUnit, Object data) {
assignCommentsToDeclarations(cUnit);
Expand All @@ -211,7 +244,8 @@ private boolean allCommentsAreIgnored() {

return getProperty(OVERRIDE_CMT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(ACCESSOR_CMT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(HEADER_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
&& (getProperty(CLASS_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
|| getProperty(HEADER_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored)
&& getProperty(FIELD_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(PUB_METHOD_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
&& getProperty(PROT_METHOD_CMT_REQUIREMENT_DESCRIPTOR) == CommentRequirement.Ignored
Expand Down
2 changes: 1 addition & 1 deletion 2 pmd-java/src/main/resources/category/java/documentation.xml
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ A rule for the politically correct... we don't want to offend anyone.
class="net.sourceforge.pmd.lang.java.rule.documentation.CommentRequiredRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_documentation.html#commentrequired">
<description>
Denotes whether comments are required (or unwanted) for specific language elements.
Denotes whether javadoc (formal) comments are required (or unwanted) for specific language elements.
</description>
<priority>3</priority>
<example>
Expand Down
9 changes: 9 additions & 0 deletions 9 ...a/src/test/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.junit.Assert.assertNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;
Expand All @@ -23,6 +24,14 @@ public void allCommentTypesIgnored() {
assertNull("By default, the rule should be functional", rule.dysfunctionReason());

List<PropertyDescriptor<?>> propertyDescriptors = getProperties(rule);
// remove deprecated properties
for (Iterator<PropertyDescriptor<?>> it = propertyDescriptors.iterator(); it.hasNext();) {
PropertyDescriptor<?> property = it.next();
if (property.description().startsWith("Deprecated!")) {
it.remove();
}
}

for (PropertyDescriptor<?> property : propertyDescriptors) {
setPropertyValue(rule, property, "Ignored");
}
Expand Down
22 changes: 20 additions & 2 deletions 22 ...c/test/resources/net/sourceforge/pmd/lang/java/rule/documentation/xml/CommentRequired.xml
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Foo {

<test-code>
<description>Missing comments - only class level required.</description>
<rule-property name="headerCommentRequirement">Required</rule-property>
<rule-property name="classCommentRequirement">Required</rule-property>
<rule-property name="fieldCommentRequirement">Ignored</rule-property>
<rule-property name="publicMethodCommentRequirement">Ignored</rule-property>
<rule-property name="protectedMethodCommentRequirement">Ignored</rule-property>
Expand All @@ -61,7 +61,7 @@ public class Foo {

<test-code>
<description>Too many comments - all comments are unwanted.</description>
<rule-property name="headerCommentRequirement">Unwanted</rule-property>
<rule-property name="classCommentRequirement">Unwanted</rule-property>
<rule-property name="fieldCommentRequirement">Unwanted</rule-property>
<rule-property name="publicMethodCommentRequirement">Unwanted</rule-property>
<rule-property name="protectedMethodCommentRequirement">Unwanted</rule-property>
Expand Down Expand Up @@ -505,5 +505,23 @@ public class CommentRequired {
]]></code>
</test-code>

<test-code>
<description>#1683 [java] CommentRequired property names are inconsistent - use deprecated property</description>
<rule-property name="headerCommentRequirement">Unwanted</rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[
/** Unwanted class comment */
public class CommentRequirement {}
]]></code>
</test-code>

<test-code>
<description>#1683 [java] CommentRequired property names are inconsistent - use new property</description>
<rule-property name="classCommentRequirement">Unwanted</rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[
/** Unwanted class comment */
public class CommentRequirement {}
]]></code>
</test-code>
</test-data>
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.