-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix(#2769): Modify the annotation processor in 2.x to give a warning if a plugin builder attribute does not have a public setter. #3195
base: 2.x
Are you sure you want to change the base?
Conversation
This adds setters to `@PluginBuilderAttribute` fields in setter classes that lack the corresponding public setter.
… plugin builder attribute does not have a public setter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! I added some minor remarks below.
If you haven't done it already, please sign the ASF ICLA.
import javax.tools.JavaFileObject; | ||
import javax.tools.StandardJavaFileManager; | ||
import javax.tools.ToolProvider; | ||
import org.junit.Test; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import org.junit.Test; | |
import org.junit.jupiter.api.Test; |
Please use JUnit 5. Our JUnit 4 tests are being currently rewritten to JUnit 5.
When using JUnit 5, the recommended style is to make all classes and methods package-private instead of public
.
// Get the source files | ||
Path sourceFile = Paths.get(FAKE_PLUGIN_CLASS_PATH); | ||
|
||
assertThat(Files.exists(sourceFile)).isTrue(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertThat(Files.exists(sourceFile)).isTrue(); | |
assertThat(sourceFile).exists(); |
} | ||
|
||
@Test | ||
public void IgnoreWarningWhenSuppressWarningsIsPresent() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public void IgnoreWarningWhenSuppressWarningsIsPresent() { | |
public void ignoreWarningWhenSuppressWarningsIsPresent() { |
// Get the source files | ||
Path sourceFile = Paths.get(FAKE_PLUGIN_CLASS_PATH); | ||
System.out.println(FAKE_PLUGIN_CLASS_PATH); | ||
assertThat(Files.exists(sourceFile)).isTrue(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertThat(Files.exists(sourceFile)).isTrue(); | |
assertThat(sourceFile).exists(); |
boolean followsNamePattern = methodName | ||
.toLowerCase(Locale.ROOT) | ||
.equals(String.format("set%s", fieldName.toLowerCase(Locale.ROOT))) | ||
|| methodName | ||
.toLowerCase(Locale.ROOT) | ||
.equals(String.format("with%s", fieldName.toLowerCase(Locale.ROOT))); | ||
|
||
if (fieldName.toLowerCase(Locale.ROOT).startsWith("is")) { | ||
followsNamePattern = methodName | ||
.toLowerCase(Locale.ROOT) | ||
.equals(String.format( | ||
"set%s", | ||
fieldName | ||
.toLowerCase(Locale.ROOT) | ||
.substring(2))) | ||
|| methodName | ||
.toLowerCase(Locale.ROOT) | ||
.equals(String.format( | ||
"with%s", | ||
fieldName | ||
.toLowerCase(Locale.ROOT) | ||
.substring(2))); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could be stricter here and use a case-sensitive comparison, for example add a helper method that:
- Removes an
is
prefix from the field name. - Capitalizes the first letter.
- Compares with the method name with
set
orwith
removed.
processingEnv | ||
.getMessager() | ||
.printMessage( | ||
Diagnostic.Kind.WARNING, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that ERROR
would be fine here.
Just make sure that the message contains a mention about the @SuppressWarnings
annotation, so third-party Log4j plugin developers don't get crazy if they can not create a setter.
The following commit modifies the annotation processor in 2.x to shell out a warning if the plugin builder attribute does not have a public setter.
A
@SuppressWarnings("log4j.public.setter")
attribute can be used to ignore this compilation warning incase it is a known issue.Checklist
2.x
branch if you are targeting Log4j 2; usemain
otherwise./mvnw verify
succeeds (if it fails due to code formatting issues reported by Spotless, simply run./mvnw spotless:apply
and retry)src/changelog/.2.x.x
directory