Skip to content
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

Issue #108: added ForbidAnnotationElementValueCheck #476

Merged
merged 1 commit into from Nov 19, 2016

Conversation

rnveach
Copy link
Contributor

@rnveach rnveach commented Nov 12, 2016

Issue #108

Original code: https://github.com/drozzds/sevntu.checkstyle/commit/cca522efd875dcc913e6169d3c3ff963a09e1cf3
Original discussion: https://groups.google.com/forum/#!msg/sevntu-checkstyle/5MFHVlFTmz8/YBuBK2IonZsJ
Original PR: #265

Only changes are really formatting, field/method renames, and fixed violations.

Numbers from last review:

  1. already shows code sample.
  2. Fixed one, but couldn't really match the other one.
  3. all 3 are there.
  4. Looks like change is there.
  5. Is not static because it calls getSingleElementWithForbiddenValue (new name) which goes against one of the check properties. Want to rename this to isSingleElementAnnotationWithForbiddenValue?
  6. Looks like change was already made
  7. You want me to run regression on this check? What property values do you wish me to use besides empty check?

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 98.248% when pulling 85aeae0 on rnveach:issue_108 into 66e9a0a on sevntu-checkstyle:master.

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

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

Want to rename this to isSingleElementAnnotationWithForbiddenValue?

lets keep as it is, looks ok now.

You want me to run regression on this check?

yes. lets test our ability to remove moratorium

What property values do you wish me to use besides empty check?

  1. forbid - "Test(expected)"
  2. some text of array value of annotation, probably at "SuppresWarning" , it will give some violations for sure.

if (getAnnotationName(ast).equals(annotationName)) {
if (DEFAULT_ELEMENT_NAME.equals(elementName) && isSingleElementAnnotation(ast)) {
log(getSingleElementWithForbiddenValue(ast), MSG_KEY, DEFAULT_ELEMENT_NAME,
annotationName);
Copy link
Member

Choose a reason for hiding this comment

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

please move getSingleElementWithForbiddenValue out of log.
Whole logic is in this method, it should not be hidden.
log line should be similar to next log line, by structure.

* Sets Annotation Name Check property.
*
* @param annotationName
* THe annotation name.
Copy link
Member

Choose a reason for hiding this comment

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

typo.
can we make it single line

* Sets Annotation Element Check property.
*
* @param elementName
* The annotation element name.
Copy link
Member

Choose a reason for hiding this comment

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

please make it single line

* </p>
*
* <pre>
* &#64;Bean(name = AnnotationConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
Copy link
Member

Choose a reason for hiding this comment

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

lets be more practical in examples and do real users problems:
"usage of "expected" parameter in annotation Test"
in web we could find smth else.

* </p>
*
* <pre>
* &#64;Endorsers({"Children", "Unscrupulous dentists"})<br>
Copy link
Member

Choose a reason for hiding this comment

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

example should be close to real life, if nothing come to mind - make example base on SuppressWarning annotation.

Copy link
Contributor Author

@rnveach rnveach left a comment

Choose a reason for hiding this comment

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

@romani What do you think of changing these default values?
As I was working to start regression, I noticed some of them didn't make sense for easy use and found myself having to define these explicitly.

<module name="ForbidAnnotationElementValueCheck" />
<module name="ForbidAnnotationElementValueCheck">
  <property name="annotationName" value="Test" />
  <property name="elementName" value="expected" />
  <property name="forbiddenElementValueRegexp" value=".*" />
</module>
<module name="ForbidAnnotationElementValueCheck">
  <property name="annotationName" value="SuppressWarnings" />
  <property name="elementName" value="value" />
  <property name="forbiddenElementValueRegexp" value=".*unchecked.*" />
</module>

Regression found NPE on:

@javax.annotation.ParametersAreNonnullByDefault
package com.google.common.reflect;
Caused by: java.lang.NullPointerException
    at com.puppycrawl.tools.checkstyle.checks.annotation.ForbidAnnotationElementValueCheck.getAnnotationName(ForbidAnnotationElementValueCheck.java:373)
    at com.puppycrawl.tools.checkstyle.checks.annotation.ForbidAnnotationElementValueCheck.visitToken(ForbidAnnotationElementValueCheck.java:174)

private String annotationName;

/** Forbidden annotation element name. */
private String elementName;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should have default element name be DEFAULT_ELEMENT_NAME?

Copy link
Member

Choose a reason for hiding this comment

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

I do not understamd question. Please change code as you want .

private static final String DEFAULT_ELEMENT_NAME = "value";

/** Precompiled forbidden element value pattern. */
private Pattern forbiddenElementValueRegexp = Pattern.compile("^$");
Copy link
Contributor Author

@rnveach rnveach Nov 17, 2016

Choose a reason for hiding this comment

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

We should have default regexp be a match everything? (IE: .*)
Current expression matches empty value, which shouldn't normally happen.

Copy link
Member

Choose a reason for hiding this comment

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

Check state was far from completed, so please change default to reasonable smth

|| currentNode.getType() == TokenTypes.ANNOTATION_ARRAY_INIT) {
final String elementValue = getSingleElementValue(currentNode);

if (forbiddenElementValueRegexp.matcher(elementValue).matches()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems to make more sense as a find than as a matches.

Copy link
Member

Choose a reason for hiding this comment

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

Yes

final String elementValue = getElementValue(memberValuePair);
final Matcher elementValueMatcher = forbiddenElementValueRegexp.matcher(elementValue);

return getElementName(memberValuePair).equals(elementName) && elementValueMatcher.matches();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same here, find instead of matches.

Copy link
Member

Choose a reason for hiding this comment

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

Yes

@romani
Copy link
Member

romani commented Nov 17, 2016

I would make this Check to catch "Test(expected) " by default.

@rnveach
Copy link
Contributor Author

rnveach commented Nov 19, 2016

All requests were addressed. Default of check was changed.
Will work on regression.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 98.25% when pulling 73df330 on rnveach:issue_108 into d77b072 on sevntu-checkstyle:master.

@rnveach
Copy link
Contributor Author

rnveach commented Nov 19, 2016

@romani romani merged commit 47f16be into sevntu-checkstyle:master Nov 19, 2016
@romani
Copy link
Member

romani commented Nov 19, 2016

awesome work, unfortunately bad code is in checkstyle and sevntu .... even I tried to not make that code appear. Fortunately now it is automated.

@rnveach , please add this Check to confugrations in main project to enforce good practice. I am going to make new release of senvtu now.

@rnveach rnveach deleted the issue_108 branch November 20, 2016 02:52
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.

None yet

3 participants