From 2fec59218ec2a9832621dd7e8df3eae77dc9d0e8 Mon Sep 17 00:00:00 2001 From: Gregg Donovan Date: Mon, 19 Aug 2019 12:58:21 -0400 Subject: [PATCH] LOG4J-2676: Backport use of javax.annotation.processing.Messager to release-2.x. Add a test for Bazel support for the PluginProcessor, which does not work on 2.12.1 due to System.out calls, but works with this PR. --- .../plugins/processor/PluginProcessor.java | 8 +-- misc/bazel-test/BUILD.bazel | 50 +++++++++++++++++++ .../core/config/plugins/processor/BUILD.bazel | 5 ++ .../plugins/processor/BazelTestPlugin.java | 33 ++++++++++++ misc/bazel-test/test.sh | 15 ++++++ 5 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 misc/bazel-test/BUILD.bazel create mode 100644 misc/bazel-test/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/BUILD.bazel create mode 100644 misc/bazel-test/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/BazelTestPlugin.java create mode 100755 misc/bazel-test/test.sh diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java index 2f3b53f58cb..023330f42ac 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java @@ -27,6 +27,7 @@ import java.util.Objects; import java.util.Set; import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.Messager; import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; import javax.lang.model.SourceVersion; @@ -67,16 +68,17 @@ public SourceVersion getSupportedSourceVersion() { @Override public boolean process(final Set annotations, final RoundEnvironment roundEnv) { - System.out.println("Processing annotations"); + Messager messager = processingEnv.getMessager(); + messager.printMessage(Kind.NOTE, "Processing Log4j annotations"); try { final Set elements = roundEnv.getElementsAnnotatedWith(Plugin.class); if (elements.isEmpty()) { - System.out.println("No elements to process"); + messager.printMessage(Kind.NOTE, "No elements to process"); return false; } collectPlugins(elements); writeCacheFile(elements.toArray(new Element[elements.size()])); - System.out.println("Annotations processed"); + messager.printMessage(Kind.NOTE, "Annotations processed"); return true; } catch (final IOException e) { e.printStackTrace(); diff --git a/misc/bazel-test/BUILD.bazel b/misc/bazel-test/BUILD.bazel new file mode 100644 index 00000000000..f47f281d00e --- /dev/null +++ b/misc/bazel-test/BUILD.bazel @@ -0,0 +1,50 @@ +java_import( + name = "logging_log4j2_api_jar", + jars = [ + "jars/log4j-api-2.13.0-SNAPSHOT.jar", + ], +) + +java_import( + name = "logging_log4j2_core_jar", + jars = [ + "jars/log4j-core-2.13.0-SNAPSHOT.jar", + ], +) + +java_library( + name = "log4j_api", + visibility = [ + "//visibility:public", + ], + exports = [ + ":logging_log4j2_api_jar", + ], +) + +java_library( + name = "log4j_core", + exported_plugins = [ + ":log4j_core_plugin", + ], + visibility = [ + "//visibility:public", + ], + exports = [ + ":log4j_api", + ":logging_log4j2_core_jar", + ], +) + +java_plugin( + name = "log4j_core_plugin", + generates_api = False, + processor_class = "org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor", + visibility = [ + "//visibility:public", + ], + deps = [ + ":log4j_api", + ":logging_log4j2_core_jar", + ], +) diff --git a/misc/bazel-test/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/BUILD.bazel b/misc/bazel-test/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/BUILD.bazel new file mode 100644 index 00000000000..71ef569e05d --- /dev/null +++ b/misc/bazel-test/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/BUILD.bazel @@ -0,0 +1,5 @@ +java_library( + name = "processor", + srcs = ["BazelTestPlugin.java"], + deps = ["//:log4j_core"], +) diff --git a/misc/bazel-test/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/BazelTestPlugin.java b/misc/bazel-test/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/BazelTestPlugin.java new file mode 100644 index 00000000000..41441ef49ba --- /dev/null +++ b/misc/bazel-test/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/BazelTestPlugin.java @@ -0,0 +1,33 @@ +/* + * 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 org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAliases; + +/** + * Test plugin class for unit tests. + */ +@Plugin(name = "BazelTest", category = "Test") +@PluginAliases({"BazelTest", "BazelTestPlugin"}) +public class BazelTestPlugin { + + @Plugin(name = "Nested", category = "Test") + public static class Nested { + } +} diff --git a/misc/bazel-test/test.sh b/misc/bazel-test/test.sh new file mode 100755 index 00000000000..ffe69c651bb --- /dev/null +++ b/misc/bazel-test/test.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# +# Test Bazel support in log4j2. +# Run this script after `mvn clean package`. + +set -o errexit + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +cd "${DIR}" + +mkdir -p "${DIR}/jars" +cp "${DIR}/../../log4j-api/target/log4j-api-2.13.0-SNAPSHOT.jar" jars/ +cp "${DIR}/../../log4j-core/target/log4j-core-2.13.0-SNAPSHOT.jar" jars/ +bazel build //... +