-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn if plugin attribute has no public setter (#3195)
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")` annotation can be used to ignore this compilation warning in case it is a known issue.
- Loading branch information
1 parent
be2d145
commit e1715dc
Showing
10 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...org/apache/logging/log4j/core/config/plugins/processor/FakePluginPublicSetter.java.source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.logging.log4j.core.config.plugins.processor; | ||
|
||
import java.io.Serializable; | ||
import org.apache.logging.log4j.core.Layout; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.apache.logging.log4j.core.config.Configuration; | ||
import org.apache.logging.log4j.core.config.Node; | ||
import org.apache.logging.log4j.core.config.plugins.Plugin; | ||
import org.apache.logging.log4j.core.config.plugins.PluginAliases; | ||
import org.apache.logging.log4j.core.config.plugins.PluginAttribute; | ||
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; | ||
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; | ||
import org.apache.logging.log4j.core.config.plugins.PluginElement; | ||
import org.apache.logging.log4j.core.config.plugins.PluginFactory; | ||
import org.apache.logging.log4j.core.config.plugins.PluginLoggerContext; | ||
import org.apache.logging.log4j.core.config.plugins.PluginNode; | ||
import org.apache.logging.log4j.core.config.plugins.PluginValue; | ||
|
||
/** | ||
* Test plugin class for unit tests. | ||
*/ | ||
@Plugin(name = "Fake", category = "Test") | ||
@PluginAliases({"AnotherFake", "StillFake"}) | ||
public class FakePluginPublicSetter { | ||
|
||
@Plugin(name = "Nested", category = "Test") | ||
public static class Nested {} | ||
|
||
@PluginFactory | ||
public static FakePluginPublicSetter newPlugin( | ||
@PluginAttribute("attribute") int attribute, | ||
@PluginElement("layout") Layout<? extends Serializable> layout, | ||
@PluginConfiguration Configuration config, | ||
@PluginNode Node node, | ||
@PluginLoggerContext LoggerContext loggerContext, | ||
@PluginValue("value") String value) { | ||
return null; | ||
} | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder implements org.apache.logging.log4j.core.util.Builder<FakePluginPublicSetter> { | ||
|
||
@PluginBuilderAttribute | ||
private int attribute; | ||
|
||
@PluginBuilderAttribute | ||
@SuppressWarnings("log4j.public.setter") | ||
private int attributeWithoutPublicSetterButWithSuppressAnnotation; | ||
|
||
@PluginElement("layout") | ||
private Layout<? extends Serializable> layout; | ||
|
||
@PluginConfiguration | ||
private Configuration config; | ||
|
||
@PluginNode | ||
private Node node; | ||
|
||
@PluginLoggerContext | ||
private LoggerContext loggerContext; | ||
|
||
@PluginValue("value") | ||
private String value; | ||
|
||
@Override | ||
public FakePluginPublicSetter build() { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...g/apache/logging/log4j/core/config/plugins/processor/PluginProcessorPublicSetterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.logging.log4j.core.config.plugins.processor; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.stream.Collectors; | ||
import javax.tools.Diagnostic; | ||
import javax.tools.DiagnosticCollector; | ||
import javax.tools.JavaCompiler; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.StandardJavaFileManager; | ||
import javax.tools.ToolProvider; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class PluginProcessorPublicSetterTest { | ||
|
||
private static final String FAKE_PLUGIN_CLASS_PATH = | ||
"src/test/java/org/apache/logging/log4j/core/config/plugins/processor/" + FakePlugin.class.getSimpleName() | ||
+ "PublicSetter.java.source"; | ||
|
||
private File createdFile; | ||
private DiagnosticCollector<JavaFileObject> diagnosticCollector; | ||
private List<Diagnostic<? extends JavaFileObject>> errorDiagnostics; | ||
|
||
@BeforeEach | ||
void setup() { | ||
// Instantiate the tooling | ||
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); | ||
diagnosticCollector = new DiagnosticCollector<>(); | ||
final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, Locale.ROOT, UTF_8); | ||
|
||
// Get the source files | ||
Path sourceFile = Paths.get(FAKE_PLUGIN_CLASS_PATH); | ||
|
||
assertThat(sourceFile).exists(); | ||
|
||
final File orig = sourceFile.toFile(); | ||
createdFile = new File(orig.getParentFile(), "FakePluginPublicSetter.java"); | ||
assertDoesNotThrow(() -> FileUtils.copyFile(orig, createdFile)); | ||
|
||
// get compilation units | ||
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(createdFile); | ||
|
||
JavaCompiler.CompilationTask task = compiler.getTask( | ||
null, | ||
fileManager, | ||
diagnosticCollector, | ||
Arrays.asList("-proc:only", "-processor", PluginProcessor.class.getName()), | ||
null, | ||
compilationUnits); | ||
task.call(); | ||
|
||
errorDiagnostics = diagnosticCollector.getDiagnostics().stream() | ||
.filter(diagnostic -> diagnostic.getKind() == Diagnostic.Kind.ERROR) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
assertDoesNotThrow(() -> FileUtils.delete(createdFile)); | ||
File pluginsDatFile = Paths.get("Log4j2Plugins.dat").toFile(); | ||
if (pluginsDatFile.exists()) { | ||
pluginsDatFile.delete(); | ||
} | ||
} | ||
|
||
@Test | ||
void warnWhenPluginBuilderAttributeLacksPublicSetter() { | ||
|
||
assertThat(errorDiagnostics).anyMatch(errorMessage -> errorMessage | ||
.getMessage(Locale.ROOT) | ||
.contains("The field `attribute` does not have a public setter")); | ||
} | ||
|
||
@Test | ||
void ignoreWarningWhenSuppressWarningsIsPresent() { | ||
assertThat(errorDiagnostics) | ||
.allMatch( | ||
errorMessage -> !errorMessage | ||
.getMessage(Locale.ROOT) | ||
.contains( | ||
"The field `attributeWithoutPublicSetterButWithSuppressAnnotation` does not have a public setter")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.