Skip to content

[java] Groundwork for the upcoming metrics framework #409

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.

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
merged 83 commits into from
Jun 2, 2017

Conversation

oowekyala
Copy link
Member

So we figured with @adangel that I should not wait too much to make a PR for my work on the metrics framework, given the increasing mess that is the history of this branch. In the future I'll do smaller PRs

Note: The framework is absolutely not functional, but should not break anything

Changelog

Changes to the AST

  • Addition of a marker interface that groups ASTConstructorDeclaration and ASTMethodDeclaration together
  • Addition of a QualifiableNode interface:
    • Implemented by FieldDeclaration, MethodDeclaration, ConstructorDeclaration, ClassOrInterfaceDeclaration
    • Adds method QualifiedName getQualifiedName(), which retrieves the qualified name of the node
    • QualifiedName's implementation is nested inside QualifiableNode
    • I added unit tests for Qualifiedname but they will be perfected when I'm sure which specification I need

New stuff

  • New package oom (object oriented metrics) in lang.java, to contain the framework
  • Class oom.Metrics : Façade of the framework
  • Package oom.metrics : will contain the implementations of the metrics (there are already some drafts in there)
  • Package oom.visitor : contains the visitor that will be executed before the rules and related classes. About the visitor:
    • It only triggers if at least one rule of the ruleset has the metrics attribute set to true
    • It fills a data structure implemented by PackageStats, which stores information about the methods and fields of each class. We use QualifiedName to address the contents of the DS and make requests
    • The information stored about methods and fields is represented by Signature classes. More info on signatures in my fork's wiki

@adangel adangel self-assigned this May 28, 2017
@adangel adangel self-requested a review May 28, 2017 17:20
Copy link
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

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

Awesome, great work! I really like seeing the metrics framework evolving!

@@ -201,6 +201,16 @@ public boolean usesTypeResolution(Language language) {
return false;
}

public boolean usesMetrics(Language language) {
Copy link
Member

Choose a reason for hiding this comment

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

Please add for this directly the javadocs. I know, "usesTyperesolution" above doesn't have the docs either, but somewhere we need to start :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Right I'll document both then ;)

MeasuredTotal(12, "Measured total"),
NonMeasuredTotal(13, "Non-measured total"),
TotalPMD(14, "Total PMD");
MetricsVisitor(8, "Metrics visitor"),
Copy link
Member

Choose a reason for hiding this comment

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

Just a minor thing: I think, I wouldn't name this here MetricsVisitor - maybe just Metrics? Since "Visitor" is just an implementation detail...

Copy link
Member Author

Choose a reason for hiding this comment

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

Check

@@ -567,6 +567,10 @@ private void parseSingleRuleNode(RuleSetReferenceId ruleSetReferenceId, RuleSetB
rule.setUsesTypeResolution();
}

if (hasAttributeSetTrue(ruleElement, "metrics")) {
Copy link
Member

Choose a reason for hiding this comment

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

I'll need to lookup myself, but I guess, we need to update somehow the XML schema / DTD. Otherwise every XML editor will complain, that the attribute "metrics" is not allowed and one cannot create a ruleset xml file, with rules, that use metrics ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point! Is this repo the sources for http://pmd.sourceforge.net/ ? In that case I think adding a line to this xsd definition, which is loaded by rulesets, would do the trick

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's one place, were the definition can be downloaded. But the master source is really in the main repo (under pmd-core): https://github.com/pmd/pmd/tree/master/pmd-core/src/main/resources

I'm not sure about whether we could just change the definition and keep the version/namespace the same (given that it is compatible) or whether we should change the namespace...

@@ -567,6 +567,10 @@ private void parseSingleRuleNode(RuleSetReferenceId ruleSetReferenceId, RuleSetB
rule.setUsesTypeResolution();
}

if (hasAttributeSetTrue(ruleElement, "metrics")) {
Copy link
Member

Choose a reason for hiding this comment

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

And of course, we shouldn't forget about documenting this... but that's something for later.

if (isNested()) {
ASTClassOrInterfaceDeclaration parent = this.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
QualifiedName parentQN = parent.getQualifiedName();
return QualifiedName.makeClassOf(parentQN, this.getImage());
Copy link
Member

Choose a reason for hiding this comment

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

Should we cache the qualifiedName in this.qualifiedName here, too? For the other case (not isNested()), we cache it. Since the AST doesn't change, I think, it's safe to cache it here the same way.

Copy link
Member

Choose a reason for hiding this comment

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

That's actually a nice recursive call 👍 Goes up the tree to the outer class. I hope, isNested() returns false, then...

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I forgot to cache it there! Well yes otherwise we'd get a nice NPE :)

*/
public enum OperationMetricKey {

ATFD(new AtfdMetric());
Copy link
Member

Choose a reason for hiding this comment

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

Just wondering: is AFTD both a ClassMetric and a OperationMetric, or is this just an example?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, ATFD is both

}
}

return foreignCalls / callQNames.size();
Copy link
Member

Choose a reason for hiding this comment

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

This look like an integer division. You'll need to cast one number to a double before dividing....


@Override
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder) {
return node.getEndLine() - node.getBeginLine();
Copy link
Member

Choose a reason for hiding this comment

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

This is a very simple method to calculate the lines. It depends on the source code formatting. I don't know about the requirements for this particular metric, but maybe NCSS (non-commenting sourcecode statements) might be an alternative. It of course it a bit more complex to calculate...

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't put much thought into it lol

I guess we could have both metrics, one version that counts with comments and the other not, and set which implementation will be used by rules that use LOC via a rule property.

public enum Role {
GETTER_OR_SETTER, CONSTRUCTOR, METHOD, STATIC;

public static final Role[] ALL = {GETTER_OR_SETTER, CONSTRUCTOR, METHOD, STATIC};
Copy link
Member

Choose a reason for hiding this comment

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

The enum provides already a field values which contains all the values of the enum in an array. So, no need to define ALL manually :)

public enum Visibility {
PUBLIC, PACKAGE, PROTECTED, PRIVATE, UNDEF;

public static final Visibility[] ALL = {PUBLIC, PACKAGE, PROTECTED, PRIVATE};
Copy link
Member

Choose a reason for hiding this comment

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

Same here: you can use Visibility.values instead

oowekyala added 7 commits May 30, 2017 13:34
The creation methods now mostly take the node to describe as parameter. That way, the implementation of the class is less exposed, the creation methods can be overloaded and simplified in the AST nodes' classes.

Only nested class do not do that. That is to keep the recursive call to getQualifiedName which would be much more complicated if it was called from QualifiedName rather than the AST node.
@adangel adangel added an:enhancement An improvement on existing features / rules in:pmd-internals Affects PMD's internals labels May 31, 2017
@adangel adangel added the in:metrics Affects the metrics framework label Jun 2, 2017
adangel added a commit to adangel/pmd that referenced this pull request Jun 2, 2017
@adangel adangel merged commit 455e42b into pmd:master Jun 2, 2017
@adangel adangel added this to the 5.8.0 milestone Jun 2, 2017
@oowekyala oowekyala deleted the metrics-prework branch July 10, 2017 19:12
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:metrics Affects the metrics framework in:pmd-internals Affects PMD's internals
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants