Skip to content

[core] Improvements and planned updates around PropertySource#1322

Merged
adangel merged 2 commits into
pmd:masterfrom
oowekyala:improve-property-source
Sep 11, 2018
Merged

[core] Improvements and planned updates around PropertySource#1322
adangel merged 2 commits into
pmd:masterfrom
oowekyala:improve-property-source

Conversation

@oowekyala

@oowekyala oowekyala commented Aug 27, 2018

Copy link
Copy Markdown
Member

I was working on making required properties a thing for #973 (the last comments), and it occurred to me that for that to work, PropertySources should provide kind of the same functionality as RuleReferences: track the overridden properties. This was already kind of the case, except it wasn't documented.

This PR brushes up on PropertySource, fixing some inconsistencies, deprecating unneeded API, and making it consistent with RuleReference. The goal for 7.0.0 would be to have RuleReference extend AbstractPropertySource to share the implementation. This can't be done right now as making AbstractDelegateRule extend AbstractPropertySource would break the contract of AbstractDelegateRule by baking in some property overriding facilities. Since AbstractDelegateRule is only used by RuleReference anyway, I think we can remove it without regret come 7.0.0

@oowekyala oowekyala added an:enhancement An improvement on existing features / rules in:pmd-internals Affects PMD's internals labels Aug 27, 2018
@oowekyala oowekyala added this to the 6.7.0 milestone Aug 27, 2018
@ghost

ghost commented Aug 29, 2018

Copy link
Copy Markdown
1 Message
📖 Please check the regression diff report to make sure that everything is expected

Generated by 🚫 Danger

@adangel adangel modified the milestones: 6.7.0, 6.8.0 Sep 1, 2018

@adangel adangel 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.

This is almost ready, sorry for the late review!

I only see these points, before we can merge:

  • reintroduce hasOverriddenProperty(...) as deprecated method just delegating to the new isPropertyOverridden(...) in RuleReference.
  • adding the deprecated classes/methods in the release notes under "API Changes"
  • making getPropertySourceType(...) protected in AbstractPropertySource

The other comments are just thoughts. We probably need to define more in detail, what one can override exactly when defining a rule reference. Like you mentioned, overriding the language doesn't make sense. The examples handling can be simplified (I see a use case for overriding the examples). We also probably shouldn't allow to define new properties on a rule reference...

}

// FIXME should we really allow overriding the language of a rule?
// I don't see any case where this wouldn't just make the rule fail during execution

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.

We probably should throw something like "UnsupportedOperationException" in 7.0.0.
Most likely, you can't reuse a rule for other languages - I think, right now, all our rules are tight to a specific AST.
It could work, if the rule only uses Node and no subclass. For XPath-rules, I don't see, how it can work (unless the AST node names, are the same for two languages...).
So, better we stop here with a exception and get notified that way, when such a real use case pops up.

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.

We could argue similar with setMinimumLanguageVersion / setMaximumLanguageVersion. You could override these properties and effectively disable a rule, but why would someone want to do this, if one can simply remove the rule reference from the ruleset?


@Override
public void addExample(String example) {
// TODO Intuitively, if some examples are overridden (even with empty value), then

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 think, having the possibility to override the example could be used for generating documentation (or displaying documentation in IDE plugins): E.g. someone creates a custom ruleset and wants to provide translated/adapted examples that better fit the project at hand.

I agree, that we should keep the overriding mechanism simple: If a rule reference is provided with examples, then we should remove all original examples and just use the ones provided by the rule reference.

@@ -246,14 +277,16 @@ public void definePropertyDescriptor(PropertyDescriptor<?> propertyDescriptor) t
propertyDescriptors.add(propertyDescriptor);

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.

Does it make sense, to allow defining additional properties on a rule reference? The original rule can't make any sense of a new property...

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.

indeed!

public <T> void setProperty(PropertyDescriptor<T> propertyDescriptor, T value) {
// Only override if different than current value.
if (!isSame(super.getProperty(propertyDescriptor), value)) {
if (!Objects.equals(super.getProperty(propertyDescriptor), value)) {

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 think, this comparison won't work with RegexProperties... (it didn't work with isSame, too).
Actually, it does not hurt so much here - but it will always override the property, even if the pattern is the same.
Any two java.util.regex.Pattern instances are considered not equal.

|| super.hasDescriptor(descriptor);
}

public boolean hasOverriddenProperty(PropertyDescriptor<?> descriptor) {

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.

For compatibility reasons, we'll need to keep hasOverriddenProperty(...) as a deprecated method. It's public...

*
* @return the name
*/
public abstract String getName();

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.

For compatibility reasons, we need to keep getName(), don't we?

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.

Ah, I see, it's in the PropertySource interface now. Good :)

* @return the name
*/
public abstract String getName();
private String getPropertySourceType() {

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.

We probably should make this protected and abstract, so that the AbstractPropertySource doesn't need to know its concrete subclasses... like Rule.

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 support this

@adangel adangel self-assigned this Sep 11, 2018
@oowekyala oowekyala mentioned this pull request Sep 11, 2018
64 tasks
@adangel adangel merged commit faabb2a into pmd:master Sep 11, 2018
adangel added a commit that referenced this pull request Sep 11, 2018
@adangel

adangel commented Sep 11, 2018

Copy link
Copy Markdown
Member

Merged! FYI - I've fixed the points I mentioned.

@Override
public Map<PropertyDescriptor<?>, Object> getOverriddenPropertiesByPropertyDescriptor() {
return propertyValues;
return propertyValues == null ? new HashMap<PropertyDescriptor<?>, Object>() : new HashMap<>(propertyValues);

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.

why not Collections.emptyMap()?

@oowekyala oowekyala deleted the improve-property-source branch January 14, 2019 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

an:enhancement An improvement on existing features / rules in:pmd-internals Affects PMD's internals

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants