Skip to content

Java: Add support for Annotation types stub generation#8695

Merged
atorralba merged 11 commits into
github:mainfrom
atorralba:atorralba/stub-generator-annotation-types
Oct 3, 2022
Merged

Java: Add support for Annotation types stub generation#8695
atorralba merged 11 commits into
github:mainfrom
atorralba:atorralba/stub-generator-annotation-types

Conversation

@atorralba
Copy link
Copy Markdown
Contributor

Adds support for Annotation types in our Java stub generator.

Due to default values for Annotation methods not being currently supported, the stubs generated may need manual correction if the original types have default values, e.g. https://github.com/spring-projects/spring-framework/blob/main/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java#L85.

@atorralba atorralba requested a review from a team as a code owner April 7, 2022 13:27
@github-actions github-actions Bot added the Java label Apr 7, 2022
@atorralba atorralba added the no-change-note-required This PR does not need a change note label Apr 7, 2022
Copy link
Copy Markdown
Contributor

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

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

Hopefully the following comments are helpful, and this is not too intrusive.
I am not a member of the project so feel free to consider these only as suggestions.

Comment thread java/ql/src/utils/stub-generator/Stubs.qll Outdated
Comment thread java/ql/src/utils/stub-generator/Stubs.qll Outdated
Comment thread java/ql/src/utils/stub-generator/Stubs.qll Outdated
Comment thread java/ql/src/utils/stub-generator/Stubs.qll
Comment thread java/ql/src/utils/stub-generator/Stubs.qll Outdated
Comment thread java/ql/src/utils/stub-generator/Stubs.qll Outdated
Comment thread java/ql/src/utils/stub-generator/Stubs.qll Outdated
Copy link
Copy Markdown
Contributor

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

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

Thanks a lot for considering the feedback!
There might be a few remaining issues.

Regarding annotation default values: There might not be any additional work needed because they seem to be included when an annotation is used (at least the last time I checked this), see also #6275.

Comment thread java/ql/src/utils/stub-generator/Stubs.qll Outdated
Comment thread java/ql/src/utils/stub-generator/Stubs.qll Outdated
Comment thread java/ql/src/utils/stub-generator/Stubs.qll Outdated
Comment thread java/ql/src/utils/stub-generator/Stubs.qll
@atorralba atorralba force-pushed the atorralba/stub-generator-annotation-types branch from 1f1e494 to fdf6510 Compare April 20, 2022 07:53
Copy link
Copy Markdown
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

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

Should there be a test for this? There's a similar query in C#, we added a not so human readable test in https://github.com/github/codeql/tree/main/csharp/ql/test/query-tests/Stubs/Minimal. It might be worth considering for Java too.

@atorralba atorralba force-pushed the atorralba/stub-generator-annotation-types branch from fdf6510 to 7687c8c Compare September 2, 2022 08:51
@atorralba
Copy link
Copy Markdown
Contributor Author

Should there be a test for this?

Good point @tamasvajk, thanks! I had to create a .jar file to test the stub generation, since our usual stubs are source files, which are ignored by the stub generator.

See 7687c8c for details, let me know if something doesn't add up.

tamasvajk
tamasvajk previously approved these changes Sep 13, 2022
Copy link
Copy Markdown
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me. I added a question regarding stable ordering of elements, feel free to ignore it as it's not specific to annotations.

}

private string stubAnnotations() {
result = concat(stubAnnotation(this.(AnnotationType).getAnAnnotation()) + "\n")
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.

Do we need a stable order for the annotations? I see that we don't order the members in stubMembers either, but we do order by all concats in C#. Just wondering.

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.

This would seem wise, ordering by their fully-qualified names for example

Comment on lines +113 to +114
result = this.(AnnotationType).getAnAnnotation().getAValue().getType() or
result = this.(AnnotationType).getAnAnnotation().getAValue().(ArrayInit).getAnInit().getType()
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.

Note that getAValue was recently deprecated because it's usually not useful -- use getAnArrayValue(_) instead, which unpacks array members


language[monotonicAggregates]
private string stubAnnotation(Annotation a) {
if exists(a.getAValue())
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.

getValue(_)

@atorralba atorralba force-pushed the atorralba/stub-generator-annotation-types branch from 7687c8c to f860ae8 Compare October 3, 2022 08:38
@atorralba
Copy link
Copy Markdown
Contributor Author

atorralba commented Oct 3, 2022

Thanks for the review @tamasvajk @smowton and sorry for the delay. Comments addressed in f860ae8.

@atorralba atorralba merged commit 9942dff into github:main Oct 3, 2022
@atorralba atorralba deleted the atorralba/stub-generator-annotation-types branch October 3, 2022 10:54
Comment on lines +39 to +43
result = concat(stubAnnotation(this.(AnnotationType).getAnAnnotation()) + "\n")
result =
concat(Annotation an |
this.(AnnotationType).getAnAnnotation() = an
|
stubAnnotation(an), "\n" order by an.getType().getQualifiedName()
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.

This is actually a behavior change: Previously it would add a \n after each annotation. Now it only adds it between annotations but the last one does not have a trailing \n (see also test output changes).

When the last annotation has values (and therefore ends with )) that still seems to produce valid Java code, but I assume when an annotation has no values this produces malformed Java code:

@MyAnnotationpublic void m() {
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @Marcono1234, good catch. I addressed this in #10662.

atorralba referenced this pull request in atorralba/codeql Oct 3, 2022
Add line break after all stubbed annotations to avoid malformed code

See https://github.com/github/codeql/pull/8695\#discussion_r985674245
atorralba referenced this pull request in atorralba/codeql Oct 3, 2022
Add line break after all stubbed annotations to avoid malformed code

See https://github.com/github/codeql/pull/8695\#discussion_r985674245
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Java no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants