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 1387] Validate final primitive|String fields w/ Option|Parameters annotations #1711

Merged
merged 4 commits into from
Jun 24, 2022

Conversation

acmcelwee
Copy link
Contributor

Fixes #1387

@acmcelwee acmcelwee force-pushed the issue-1387 branch 2 times, most recently from 787b9c9 to 3d4b67f Compare June 16, 2022 23:54
Comment on lines +229 to +233
boolean isConstantValueField = false;
for (VariableElement variableElement : ElementFilter.fieldsIn(Collections.singletonList(e))) {
isConstantValueField = isConstantValueField || variableElement.getConstantValue() != null;
}
if ((type.getKind().isPrimitive() || typeUtils.isAssignable(type, stringType)) && isConstantValueField) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While I'd love to also apply this compile-time validation to the boxed primitive types, I don't know if there's actually a way to determine if final Integer foo has a value specified in the class definition. getConstantValue() only returns a non-null value if the defined value is non-null and the type is a true primitive type or string.

Comment on lines +254 to +263
String errorFormat = "Constant (final) primitive and String fields like %s cannot be used as %s: compile-time constant inlining may hide new values written to it.";
List<String> expectedValidationErrors = new ArrayList<String>();
for (String type : types) {
String titleized = type.substring(0, 1).toUpperCase() + type.substring(1);
String invalidOptionField = String.format("invalid%s", titleized);
String invalidParamField = String.format("invalid%sParam", titleized);

expectedValidationErrors.add(String.format(errorFormat, invalidOptionField, "@Option"));
expectedValidationErrors.add(String.format(errorFormat, invalidParamField, "@Parameters"));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I also wasn't certain about the java version where these tests get executed, but I demonstrated considerable restraint and wrote this test to only use java 6+ language features.

Comment on lines +229 to +232
boolean isConstantValueField = false;
for (VariableElement variableElement : ElementFilter.fieldsIn(Collections.singletonList(e))) {
isConstantValueField = isConstantValueField || variableElement.getConstantValue() != null;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In reality, the result of ElementFilter.fieldsIn(...) will either have 0 or 1 item in it, but I just expressed it in a more general sense.

@remkop
Copy link
Owner

remkop commented Jun 17, 2022

Thank you for the contribution!
I wanted to reply quickly to say thank you, but I won’t be able to review in detail until after this weekend.

@acmcelwee
Copy link
Contributor Author

No worries! I don't currently need this functionality, but I used this open issue as a prompt for a programming exercise with a candidate I interviewed today. Before I asked someone to try it, I took the time to work through the problem, and I figured the best use of that effort was to contribute back to a tool we use at work.

@acmcelwee
Copy link
Contributor Author

Oh, and BTW, I'll chat with some of the team at work tomorrow to see if anyone has any creative ideas on how to achieve this same functionality for boxed primitive types.

@remkop remkop added this to the 4.7 milestone Jun 19, 2022
@remkop
Copy link
Owner

remkop commented Jun 19, 2022

I mis-clicked and accidentally merged before reviewing. 😅
I won’t be able to review it for a few more days; I hope you’re okay with the delayed feedback. (But I’m sure it’ll be solid stuff anyway.) 😄
Thanks again 🙏

@remkop
Copy link
Owner

remkop commented Jun 20, 2022

Please ignore my previous comment; that comment was intended for a different PR: #1708. 😅

@acmcelwee acmcelwee marked this pull request as ready for review June 20, 2022 21:22
@remkop remkop added type: enhancement ✨ theme: annotation-proc An issue or change related to the annotation processor theme: parser An issue or change related to the parser labels Jun 24, 2022
@remkop remkop merged commit 8682aa3 into remkop:main Jun 24, 2022
remkop added a commit that referenced this pull request Jun 24, 2022
@remkop
Copy link
Owner

remkop commented Jun 24, 2022

Merged. Thank you for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: annotation-proc An issue or change related to the annotation processor theme: parser An issue or change related to the parser type: enhancement ✨
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Annotation processor should validate final primitive and String fields (was: Annotation processor not working)
2 participants