Skip to content

Issue 543#2252

Closed
phisad wants to merge 17 commits intospotbugs:masterfrom
phisad:issue-543
Closed

Issue 543#2252
phisad wants to merge 17 commits intospotbugs:masterfrom
phisad:issue-543

Conversation

@phisad
Copy link
Copy Markdown

@phisad phisad commented Nov 9, 2022

Hello all,

I added a simple AnnotationMatcher that matches classes annotated with that annotation name.
For example, use in an excludeFilter.xml as follows

<Match>
<Annotation name="org.immutables.value.Generated" />
</Match>

to ignore classes generated by the Immutable framework.

Request For Comments
The main issue was to add the java class annotation names of the analysed classes to the PackageMemberAnnotation for later use during exclude file filtering. For this I simply changed the BugInstance.add() method as this seemed to me a central point to get this information and avoiding to re-write all the detectors. For better performance I access the IAnalysisCache.

Please suggest a better place for this, if necessary, because I do not know the codebase too well.

The PR includes:

  • a new AnnotationMatcher for excludeFilter.xml files; added an AnnotationMatcherTest
  • add AnnotationType to findbugsfilter.xsd
  • add a javaAnnotationNames attribute to PackageMemberAnnotation
  • added addJavaAnnotationNames() method to BugInstance as a central point to get the java annotations that a class is annotated with
  • a comma-separated "classjas" attribute is now written to XML when writeXml() is called on ClassAnnotation, MethodAnnotation or FieldAnnotation; a test case is added to BugInstanceTest
  • adjust SAXBugCollectionHandler to handle the new "Annotation" matcher and "classjas" attribute; a test case added to SAXBugCollectionHandlerTest

Make sure these boxes are checked before submitting your PR -- thank you!

  • Added an entry into CHANGELOG.md if you have changed SpotBugs code

@ThrawnCA
Copy link
Copy Markdown
Contributor

Build is failing due to a formatting error.

@phisad
Copy link
Copy Markdown
Author

phisad commented Nov 16, 2022

Build is failing due to a formatting error.

I ran spotlessApply on spotbugs-spotbugs and spotbugs-test and hope it is fixed now.

@phisad
Copy link
Copy Markdown
Author

phisad commented Nov 18, 2022

Fixed the test and run spotlessApply and test

@phisad
Copy link
Copy Markdown
Author

phisad commented Nov 25, 2022

btw. any comments on the changes to BugInstance.add() ?

@phisad
Copy link
Copy Markdown
Author

phisad commented Nov 30, 2022

Thanks for the valuable comments. I applied the requested changes.

}

if (!getJavaAnnotationNames().isEmpty()) {
attributeList.addAttribute("classjas", // similar to classname, short for classJavaAnnotations
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.

@phisad is 'classjas' just made up naming? Seems odd even from change log. Why not just 'classAnnotations'? We already know its java, that is redundant. At least then it follows the idea you have there and is more clear on name without new jargon. Then you probably don't need to describe why its named that way...

Copy link
Copy Markdown
Author

@phisad phisad Jan 13, 2023

Choose a reason for hiding this comment

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

Thanks for the comment! I was also thinking about "classAnnotations" but felt that it might be confused with the ClassAnnotation from spotbugs. But I think you are right, that a user of the XML file will not know that the term "classAnnotation" is also otherwise used. If you say that this is clearer, then I will certainly adjust it to "classannotation" (to comply with classname which is also not camelcase).

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.

Good point, thanks for calling that out, that certainly would cause unnecessary confusion. how about 'classAnnotationNames' which is more in line with what its loading there?

Copy link
Copy Markdown
Author

@phisad phisad Jan 30, 2023

Choose a reason for hiding this comment

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

OK, I changed the attribute name in the XMLs from classjas to classAnnotationNames

</annotation>
<attribute name="name" type="string">
<annotation>
<documentation>The exact or regex match pattern for a java class annotation name. If the name starts with the ~ character the rest of attribute content is interpreted as a Java regular expression.</documentation>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"the rest of attribute content" -> "the rest of the attribute content

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.

Adjusted. Note that this is a copy paste error (that still exists in the other documentation tags as well).

// map annotation entry type to dotted class name, for example
// Lorg/immutables/value/Generated; -> org.immutables.value.Generated
String annotationType = ae.getAnnotationType().substring(1); // remove starting L
annotationType = annotationType.replace("/", ".");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These two lines could be combined.

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.

Inlined the replace operations.

CHANGELOG.md Outdated

#### Security
### Added
* New simple name-based AnnotationMatcher for exclude files (now bug annotations store the class java annotations in an attribute called `classjas`). For example, use like <Match><Annotation name="org.immutables.value.Generated" /></Match> in an excludeFilter.xml to ignore classes generated by the Immutable framework. This ignores all class, method or field bugs in classes with that annotation.
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.

@phisad Please fix the classjas part here too, please double check to make sure all is where needed then I think we will be good.

@ThrawnCA
Copy link
Copy Markdown
Contributor

Changelog needs to be deconflicted and merged.

@phisad
Copy link
Copy Markdown
Author

phisad commented Feb 10, 2023

Rebased on upstream/master. Adjusted CHANGELOG.

@hazendaz
Copy link
Copy Markdown
Member

@phisad Fix fix formatting issues GH-actions raised.

The following files had format violations:
src/main/java/edu/umd/cs/findbugs/BugInstance.java
@@ -2113,7 +2113,7 @@
············List·javaAnnotationNames·=·Arrays.asList(annotationEntries).stream().map((AnnotationEntry·ae)·->·{
················//·map·annotation·entry·type·to·dotted·class·name,·for·example
················//·Lorg/immutables/value/Generated;·->·org.immutables.value.Generated
-················String·annotationType·=·ae.getAnnotationType().substring(1).replace("/",·".").replace(";",·"");·
+················String·annotationType·=·ae.getAnnotationType().substring(1).replace("/",·".").replace(";",·"");
················return·annotationType;
············}).collect(Collectors.toList());
············pma.setJavaAnnotationNames(javaAnnotationNames);
Run './gradlew :spotbugs:spotlessApply' to fix these violations.

@phisad
Copy link
Copy Markdown
Author

phisad commented Feb 27, 2023

Did run './gradlew :spotbugs:spotlessApply' to fix the violations.

@phisad
Copy link
Copy Markdown
Author

phisad commented Mar 29, 2023

@hazendaz Does it look fine now?


<element name="Annotation" type="fb:AnnotationType">
<annotation>
<documentation>This element matches warnings associated with a particular class that is annoted with a
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.

@phisad See a spelling mistake here, should 'annoted' be 'annotated'?

@hazendaz
Copy link
Copy Markdown
Member

@phisad Overall good, just one spelling mistake I found, can you do a double check across this?

@ThrawnCA Look good to you now as well?

@hazendaz hazendaz self-assigned this Mar 29, 2023
@cpfeiffer cpfeiffer mentioned this pull request Mar 31, 2023
@cpfeiffer
Copy link
Copy Markdown
Contributor

Supserseded by #2395

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.

4 participants