[main] Warn when @PluginBuilderAttribute field lacks public setter (#3195 port) - #4156
[main] Warn when @PluginBuilderAttribute field lacks public setter (#3195 port)#4156vpelikh wants to merge 7 commits into
@PluginBuilderAttribute field lacks public setter (#3195 port)#4156Conversation
The annotation processor now validates that every @PluginBuilderAttributevfield in a plugin builder class has an accessible public setter method (setXxx or withXxx). If no setter is found, a compilation ERROR is emitted. Use @SuppressWarnings("log4j.public.setter") on the field to suppress. Added @SuppressWarnings to known false positives in: - SocketPerformancePreferences (3 fields: setters return void, not builder) - StringMatchFilter (field 'text' has setter named 'setMatchString') - Rfc5424Layout (field 'enterpriseNumber' has no setter at all) Signed-off-by: Vasily Pelikh <2010720+vpelikh@users.noreply.github.com>
@PluginBuilderAttribute field lacks public setter (#3195 port)@PluginBuilderAttribute field lacks public setter (#3195 port)
|
However, your new processor works so well that it actually caught a pre-existing violation in main! When building locally, the compile fails in log4j-jdbc because JdbcAppender is missing public setters: Could you please update |
|
@vpelikh, Also, I noticed Spotless formatting on 3 PR as well. When you push your updates, please make sure to run |
The annotation processor requires @PluginBuilderAttribute setters to return a type assignable to the enclosing builder class. Changed setImmediateFail() and setReconnectIntervalMillis() from void to B, following the same pattern as all other setters in the Builder class. Fixes compilation errors caught by the new annotation processor validation: - The field immediateFail does not have a public setter. - The field reconnectIntervalMillis does not have a public setter. Signed-off-by: Vasily Pelikh <2010720+vpelikh@users.noreply.github.com>
Fixes formatting violations caught by spotless:check: - PluginProcessor: line wrapping, duplicate import removal - PluginProcessorPublicSetterTest: line wrapping consistency Signed-off-by: Vasily Pelikh <2010720+vpelikh@users.noreply.github.com>
552a04f to
a38185a
Compare
|
Thank you for the review, @ramanathan1504 ! Great catch – you're absolutely right, the new validation uncovered real issues in I've just pushed two commits that:
The build should be green now. Please take another look when you have a moment – happy to adjust anything else if needed. |
|
@ramanathan1504 gentle ping, PR ready for re-review. Thanks! |
Noted!. I will Try to close within a week. Thanks |
|
@vpelikh both my earlier points are fixed. Nice catch on the One blocker: the new test leaves Also worth a look: adding |
- Simplify @SupportedAnnotationTypes — removed both @PluginBuilderAttribute FQNs from the annotation type declaration. Now only @plugin is declared; @PluginBuilderAttribute (both 3.x and 2.x variants) is resolved via Elements.getTypeElement() inside process(), matching the 2.x approach. The FQNs live in a BUILDER_ATTRIBUTE_ANNOTATIONS constant, consistent with internal/Annotations.java which already tracks both. - Fix test cleanup — replaced stale Log4j2Plugins.dat cleanup (a 2.x artifact name that doesn't exist in 3.x) with @tempdir and file manager output routing so generated files (Log4jPlugins.java, service file) go to a temp directory managed by JUnit instead of leaking into the module directory.
The setter name mismatch is resolved by PR apache#4153 which renames setMatchString() to setText() to match the field name. Once that merges, the @SuppressWarnings becomes unnecessary.
|
@ramanathan1504, thanks for review! Addresed your comments. |
This ports the annotation processor validation from #3195 (2.x) to
main(3.x).What it does
The
PluginProcessorannotation processor now validates that every@PluginBuilderAttributefield in a plugin builder class has a publicsetXxx()orwithXxx()method that:publicIf no such setter is found, a compilation ERROR is emitted:
Suppressing false positives
Use
@SuppressWarnings("log4j.public.setter")on the field to suppress the warning for legitimate cases (e.g., fields set via constructor or reflection).Files changed
PluginProcessor.java— Added setter validation logic inprocessBuilderAttributeFields()PluginProcessorPublicSetterTest.java— Test with 3 cases: public setter OK, missing setter → error, suppressed → no errorFakePluginPublicSetter.java.source— Test plugin for the above testSocketPerformancePreferences.java— Added@SuppressWarningsto 3 fields (setters returnvoid, not builder type)StringMatchFilter.java— Added@SuppressWarnings(setMatchStringname doesn't match fieldtext)Rfc5424Layout.java— Added@SuppressWarnings(fieldenterpriseNumberhas no setter)Note
Unlike the 2.x version, the 3.x
PluginProcessoruses@SupportedAnnotationTypes({"org.apache.logging.log4j.plugins.Plugin", "org.apache.logging.log4j.plugins.PluginBuilderAttribute", "org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute"})and iterates by FQN, because 3.x has two@PluginBuilderAttributevariants.