Skip to content

Apache Lucene open-source search software

build: move forbidden-apis and more to java (#14924)

Dawid Weiss633bb1c68edf
diff --git a/build-tools/build-infra-shadow/build.gradle b/build-tools/build-infra-shadow/build.gradle
index d7768878140..034a060b7cc 100644
--- a/build-tools/build-infra-shadow/build.gradle
+++ b/build-tools/build-infra-shadow/build.gradle
@@ -60,7 +60,8 @@ tasks.matching {
"validateJarChecksums",
"validateJarLicenses",
"collectJarInfos",
- "compileJava",
+ // we do need to compile the sources so that forbidden apis are applied (?)
+ // "compileJava",
"compileTestJava",
"assemble"
]
diff --git a/build-tools/build-infra/build.gradle b/build-tools/build-infra/build.gradle
index a0d0dc79e0c..a462c17de1d 100644
--- a/build-tools/build-infra/build.gradle
+++ b/build-tools/build-infra/build.gradle
@@ -36,6 +36,13 @@ java {
gradlePlugin {
automatedPublishing = false
+
+ plugins {
+ luceneJavaProjectConventions {
+ id = 'lucene.java-projects.conventions'
+ implementationClass = 'org.apache.lucene.gradle.plugins.java.JavaProjectConventionsPlugin'
+ }
+ }
}
// Convert a plugin dependency to a regular dependency so that we can
diff --git a/build-tools/build-infra/src/main/groovy/lucene.java-projects.conventions.gradle b/build-tools/build-infra/src/main/groovy/lucene.java-projects.conventions.gradle
deleted file mode 100644
index 6f0171078df..00000000000
--- a/build-tools/build-infra/src/main/groovy/lucene.java-projects.conventions.gradle
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * 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.
- */
-
-import org.apache.lucene.gradle.plugins.spotless.GoogleJavaFormatPlugin
-import org.apache.lucene.gradle.plugins.java.JavaFolderLayoutPlugin
-import org.apache.lucene.gradle.plugins.java.JavacConfigurationPlugin
-import org.apache.lucene.gradle.plugins.java.JarManifestConfigurationPlugin
-import org.apache.lucene.gradle.plugins.java.TestsAndRandomizationPlugin
-import org.apache.lucene.gradle.plugins.java.TestsBeastingPlugin
-import org.apache.lucene.gradle.plugins.java.AlternativeJdkSupportPlugin
-import org.apache.lucene.gradle.plugins.java.EcjLintPlugin
-
-allprojects { project ->
- if (['src/java', 'src/test'].any {path ->
- project.file(path).exists()
- }) {
- plugins.apply("java-library")
-
- plugins.apply(AlternativeJdkSupportPlugin)
- plugins.apply(JavaFolderLayoutPlugin)
- plugins.apply(JavacConfigurationPlugin)
- plugins.apply(JarManifestConfigurationPlugin)
- plugins.apply(TestsAndRandomizationPlugin)
- plugins.apply(TestsBeastingPlugin)
- plugins.apply("lucene.java.per-project-test-summary")
-
- plugins.apply("lucene.validation.forbidden-apis")
- plugins.apply(EcjLintPlugin)
- plugins.apply(GoogleJavaFormatPlugin)
- }
-}
diff --git a/build-tools/build-infra/src/main/groovy/lucene.validation.forbidden-apis.gradle b/build-tools/build-infra/src/main/groovy/lucene.validation.forbidden-apis.gradle
deleted file mode 100644
index 06e578ea042..00000000000
--- a/build-tools/build-infra/src/main/groovy/lucene.validation.forbidden-apis.gradle
+++ /dev/null
@@ -1,181 +0,0 @@
-/*
- * 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.
- */
-
-// This configures the application of forbidden API rules
-// via https://github.com/policeman-tools/forbidden-apis
-
-project.plugins.apply(deps.plugins.forbiddenapis.get().pluginId)
-
-def forbiddenApiRules = rootProject.layout.projectDirectory.dir("gradle/validation/forbidden-apis")
-
-// This helper method appends signature files based on a set of true
-// dependencies from a given configuration.
-def dynamicSignatures = { configuration, suffix ->
- def resolvedMods = configuration.resolvedConfiguration.resolvedArtifacts
- .collect { a -> a.moduleVersion.id }
- def deps = resolvedMods
- .collect { id ->
- [
- "${id.group}.${id.name}.all.txt",
- "${id.group}.${id.name}.${suffix}.txt",
- ]
- }
- .flatten()
- .sort()
-
- deps += [
- "defaults.all.txt",
- "defaults.${suffix}.txt"
- ]
-
- deps.each { String sig ->
- def signaturesFile = forbiddenApiRules.file(sig)
- if (signaturesFile.asFile.exists()) {
- logger.info("Signature file applied: ${sig}")
- signaturesFiles += files(signaturesFile)
- } else {
- logger.debug("Signature file omitted (does not exist): ${sig}")
- }
- }
-
- // commons-io is special: forbiddenapis has a versioned bundledSignature.
- bundledSignatures += resolvedMods
- .findAll { id -> id.group == 'commons-io' && id.name == 'commons-io' }
- .collect { id -> "${id.name}-unsafe-${id.version}" as String }
-}
-
-// Configure defaults for sourceSets.main
-tasks.matching { it.name ==~ /forbiddenApisMain\d*/ }.configureEach {
- bundledSignatures += [
- 'jdk-unsafe',
- 'jdk-deprecated',
- 'jdk-non-portable',
- 'jdk-reflection',
- 'jdk-system-out',
- ]
-
- suppressAnnotations += [
- "**.SuppressForbidden"
- ]
-
- doFirst dynamicSignatures.curry(configurations.compileClasspath, "lucene")
-}
-
-// Configure defaults for the MR-JAR feature sourceSets by setting java version and ignore missing classes
-// TODO:
-// - Get hold of warning messages, see https://github.com/policeman-tools/forbidden-apis/issues/207
-tasks.matching { it.name ==~ /forbiddenApisMain\d+/ }.configureEach {
- failOnMissingClasses = false
-}
-
-// Configure defaults for sourceSets.test
-tasks.matching {
- it.name in [
- "forbiddenApisTest",
- "forbiddenApisTestFixtures"
- ]
-}.configureEach {
- bundledSignatures += [
- 'jdk-unsafe',
- 'jdk-deprecated',
- 'jdk-non-portable',
- 'jdk-reflection',
- ]
-
- signaturesFiles = files(forbiddenApiRules.file("defaults.tests.txt"))
-
- suppressAnnotations += [
- "**.SuppressForbidden"
- ]
-
- if (it.name == "forbiddenApisTestFixtures") {
- doFirst dynamicSignatures.curry(configurations.testFixturesCompileClasspath, "lucene")
- } else {
- doFirst dynamicSignatures.curry(configurations.testCompileClasspath, "lucene")
- }
-}
-
-// Configure defaults for sourceSets.tools (if present).
-tasks.matching { it.name == "forbiddenApisTools" }.configureEach {
- bundledSignatures += [
- 'jdk-unsafe',
- 'jdk-deprecated',
- 'jdk-non-portable',
- 'jdk-reflection',
- ]
-
- suppressAnnotations += [
- "**.SuppressForbidden"
- ]
-
- doFirst dynamicSignatures.curry(configurations.toolsCompileClasspath, "lucene")
-
- inputs.dir(forbiddenApiRules)
-}
-
-// We rely on resolved configurations to compute the relevant set of rule
-// files for forbiddenApis. Since we don't want to resolve these configurations until
-// the task is executed, we can't really use them as task inputs properly. This is a
-// chicken-and-egg problem.
-//
-// This is the simplest workaround possible: just point at all the rule files and indicate
-// them as inputs. This way if a rule is modified, checks will be reapplied.
-tasks.matching { it.name.startsWith("forbiddenApis") }.configureEach { task ->
- task.inputs.dir(forbiddenApiRules)
-}
-
-// Disable sysout signatures for these projects.
-if (project.name in [
- "missing-doclet",
- "build-infra"
- ]) {
- forbiddenApisMain.bundledSignatures -= [
- 'jdk-non-portable',
- 'jdk-system-out'
- ]
-
- forbiddenApisMain.exclude("**/Checksum*")
- forbiddenApisMain.suppressAnnotations += [
- "**.*SuppressForbidden"
- ]
-}
-
-if (project.name in ["missing-doclet"] || project.path in [
- ":lucene:demo",
- ":lucene:benchmark",
- ":lucene:test-framework"
- ]) {
- forbiddenApisMain.bundledSignatures -= ['jdk-system-out']
-}
-
-// the test-framework module is special in that its main sources are exported for all tests.
-// include the test signature file (defaults.tests.txt) to the main sourceset.
-if (project.path in [
- ":lucene:test-framework"
- ]) {
- forbiddenApisMain.signaturesFiles += files(forbiddenApiRules.file("defaults.tests.txt"))
-}
-
-// apply logging patterns for all modules except Luke.
-if (!(project.path in [":lucene:luke"])) {
- forbiddenApisMain.signaturesFiles += files(forbiddenApiRules.file("defaults.logging.txt"))
-}
-
-// disable forbidden apis for snowball code
-if (project.path == ":lucene:analysis:common") {
- forbiddenApisMain.exclude("org/tartarus/**")
-}
diff --git a/build-tools/build-infra/src/main/groovy/lucene.validation.spotless-groovy.gradle b/build-tools/build-infra/src/main/groovy/lucene.validation.spotless-groovy.gradle
index 361959147d1..18ea7b63cd5 100644
--- a/build-tools/build-infra/src/main/groovy/lucene.validation.spotless-groovy.gradle
+++ b/build-tools/build-infra/src/main/groovy/lucene.validation.spotless-groovy.gradle
@@ -69,6 +69,7 @@ tasks.named("spotlessGradleScripts").configure {
mustRunAfter ":lucene:build-tools:build-infra-shadow:applyGoogleJavaFormat"
mustRunAfter ":lucene:build-tools:build-infra-shadow:pluginUnderTestMetadata"
mustRunAfter ":lucene:build-tools:build-infra-shadow:pluginDescriptors"
+ mustRunAfter ":lucene:build-tools:build-infra-shadow:compileJava"
}
tasks.named("tidy").configure {
diff --git a/build-tools/build-infra/src/main/groovy/lucene.java.per-project-test-summary.gradle b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/SuppressForbidden.java
similarity index 58%
rename from build-tools/build-infra/src/main/groovy/lucene.java.per-project-test-summary.gradle
rename to build-tools/build-infra/src/main/java/org/apache/lucene/gradle/SuppressForbidden.java
index c52b20813ad..b2472eae87b 100644
--- a/build-tools/build-infra/src/main/groovy/lucene.java.per-project-test-summary.gradle
+++ b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/SuppressForbidden.java
@@ -14,21 +14,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+package org.apache.lucene.gradle;
-// Prints per-project test summary.
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
-tasks.withType(Test).configureEach { task ->
- afterSuite { desc, result ->
- if (!desc.parent) {
- if (result.testCount > 0) {
- def components = [
- "test(s)" : result.testCount,
- "failure(s)": result.failedTestCount,
- "skipped" : result.skippedTestCount
- ].findAll { k, v -> v > 0 }.collect { k, v -> "$v $k" }.join(", ")
-
- logger.lifecycle("${task.path} (${result.resultType}): ${components}")
- }
- }
- }
+/**
+ * Forbidden-api suppression for build-infra.
+ *
+ * @see org.apache.lucene.gradle.plugins.java.ApplyForbiddenApisPlugin
+ */
+@Retention(RetentionPolicy.CLASS)
+@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
+public @interface SuppressForbidden {
+ /** A reason for suppressing should always be given. */
+ String reason();
}
diff --git a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/WrapperDownloader.java b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/WrapperDownloader.java
index 0aa3ff2c0e4..f22e57ec454 100644
--- a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/WrapperDownloader.java
+++ b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/WrapperDownloader.java
@@ -21,10 +21,6 @@ import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
@@ -40,9 +36,9 @@ import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
- * Standalone class that can be used to download a gradle-wrapper.jar
+ * Standalone class used to download the {@code gradle-wrapper.jar}.
*
- * <p>Has no dependencies outside of standard java libraries
+ * <p>Ensure this class has no dependencies outside of standard java libraries as it's used direct
*/
public class WrapperDownloader {
public static void main(String[] args) {
@@ -213,7 +209,7 @@ public class WrapperDownloader {
}
}
- @SuppressForbidden(reason = "Correct use of thread.sleep.")
+ @SuppressForbidden(reason = "Valid use of thread.sleep.")
private static void sleep(long millis) throws InterruptedException {
Thread.sleep(millis);
}
@@ -232,11 +228,4 @@ public class WrapperDownloader {
"Could not compute digest of file: " + path + " (" + e.getMessage() + ")");
}
}
-
- @Retention(RetentionPolicy.CLASS)
- @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
- public @interface SuppressForbidden {
- /** A reason for suppressing should always be given. */
- String reason();
- }
}
diff --git a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/globals/RegisterBuildGlobalsPlugin.java b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/globals/RegisterBuildGlobalsPlugin.java
index 1788dd75bce..a0db8e00cbe 100644
--- a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/globals/RegisterBuildGlobalsPlugin.java
+++ b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/globals/RegisterBuildGlobalsPlugin.java
@@ -21,6 +21,7 @@ import com.carrotsearch.gradle.buildinfra.buildoptions.BuildOptionsPlugin;
import com.carrotsearch.randomizedtesting.SeedUtils;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
+import java.util.Locale;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -31,7 +32,7 @@ import org.gradle.api.provider.Provider;
/** Registers global build constants and extensions. */
public class RegisterBuildGlobalsPlugin extends LuceneGradlePlugin {
- private static Pattern VERSION_PATTERN =
+ private static final Pattern VERSION_PATTERN =
Pattern.compile("^(?<baseVersion>(?<majorVersion>\\d+)\\.\\d+\\.\\d+)(-(.+))?");
@Override
@@ -43,9 +44,9 @@ public class RegisterBuildGlobalsPlugin extends LuceneGradlePlugin {
project.setVersion(luceneVersion);
var tstamp = ZonedDateTime.now();
- String buildDate = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(tstamp);
- String buildTime = DateTimeFormatter.ofPattern("HH:mm:ss").format(tstamp);
- String buildYear = DateTimeFormatter.ofPattern("yyyy").format(tstamp);
+ String buildDate = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ROOT).format(tstamp);
+ String buildTime = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ROOT).format(tstamp);
+ String buildYear = DateTimeFormatter.ofPattern("yyyy", Locale.ROOT).format(tstamp);
String baseVersion = getBaseVersion(luceneVersion);
String majorVersion = getMajorVersion(luceneVersion);
@@ -60,7 +61,8 @@ public class RegisterBuildGlobalsPlugin extends LuceneGradlePlugin {
.addOption(
"tests.seed",
"The \"root\" randomization seed for options and test parameters.",
- project.provider(() -> String.format("%08X", new Random().nextLong())));
+ project.provider(
+ () -> String.format(Locale.ROOT, "%08X", new Random().nextLong())));
String rootSeed = rootSeedOption.get();
// We take just the root seed, ignoring any chained sub-seeds.
diff --git a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/ApplyForbiddenApisPlugin.java b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/ApplyForbiddenApisPlugin.java
new file mode 100644
index 00000000000..b8e15e87edf
--- /dev/null
+++ b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/ApplyForbiddenApisPlugin.java
@@ -0,0 +1,293 @@
+/*
+ * 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.lucene.gradle.plugins.java;
+
+import de.thetaphi.forbiddenapis.gradle.CheckForbiddenApis;
+import de.thetaphi.forbiddenapis.gradle.ForbiddenApisPlugin;
+import java.io.File;
+import java.nio.file.Path;
+import java.util.ArrayDeque;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.stream.Collectors;
+import org.apache.lucene.gradle.plugins.LuceneGradlePlugin;
+import org.gradle.api.GradleException;
+import org.gradle.api.Project;
+import org.gradle.api.artifacts.result.DependencyResult;
+import org.gradle.api.artifacts.result.ResolvedComponentResult;
+import org.gradle.api.artifacts.result.ResolvedDependencyResult;
+import org.gradle.api.plugins.JavaPlugin;
+import org.gradle.api.plugins.JavaPluginExtension;
+import org.gradle.api.provider.Provider;
+import org.gradle.api.tasks.SourceSetContainer;
+import org.gradle.api.tasks.TaskCollection;
+import org.gradle.api.tasks.TaskContainer;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * This configures the application of forbidden-API signature files.
+ *
+ * @see "https://github.com/policeman-tools/forbidden-apis"
+ */
+public class ApplyForbiddenApisPlugin extends LuceneGradlePlugin {
+ @Override
+ public void apply(Project project) {
+ requiresAppliedPlugin(project, JavaPlugin.class);
+
+ project.getPlugins().apply(ForbiddenApisPlugin.class);
+
+ JavaPluginExtension javaExt = project.getExtensions().getByType(JavaPluginExtension.class);
+ SourceSetContainer sourceSets = javaExt.getSourceSets();
+
+ Path forbiddenApisDir = gradlePluginResource(project, "validation/forbidden-apis");
+
+ TaskContainer tasks = project.getTasks();
+
+ TaskCollection<CheckForbiddenApis> allForbiddenApisTasks =
+ tasks.withType(CheckForbiddenApis.class);
+
+ // common configuration of all source sets.
+ allForbiddenApisTasks.configureEach(
+ task -> {
+ task.getBundledSignatures()
+ .addAll(
+ List.of("jdk-unsafe", "jdk-deprecated", "jdk-non-portable", "jdk-reflection"));
+
+ task.getSuppressAnnotations().add("**.SuppressForbidden");
+
+ // apply logger restrictions to all modules except Luke.
+ if (!project.getPath().equals(":lucene:luke")) {
+ task.setSignaturesFiles(
+ task.getSignaturesFiles()
+ .plus(
+ project.files(
+ forbiddenApisDir.resolve("non-standard/logging.txt").toFile())));
+ }
+ });
+
+ // Configure defaults for main source sets
+ allForbiddenApisTasks
+ .matching(task -> task.getName().matches("forbiddenApisMain\\d*|forbiddenApisTools"))
+ .configureEach(
+ task -> {
+ task.getBundledSignatures().add("jdk-system-out");
+
+ String ruleGroup =
+ switch (project.getPath()) {
+ case ":lucene:build-tools:build-infra-shadow" -> "main-build-infra";
+ default -> "main";
+ };
+
+ addCompileConfigurationSignatureFiles(
+ project, task, sourceSets, ruleGroup, forbiddenApisDir);
+ });
+
+ // Configure defaults for test source sets
+ allForbiddenApisTasks
+ .matching(task -> task.getName().matches("forbiddenApisTest|forbiddenApisTestFixtures"))
+ .configureEach(
+ task -> {
+ String ruleGroup = "test";
+ addCompileConfigurationSignatureFiles(
+ project, task, sourceSets, ruleGroup, forbiddenApisDir);
+ });
+
+ // Configure defaults for the MR-JAR feature sourceSets: ignore missing classes
+ allForbiddenApisTasks
+ .matching(task -> task.getName().matches("forbiddenApisMain\\d+"))
+ .configureEach(
+ task -> {
+ task.setFailOnMissingClasses(false);
+ });
+
+ // Configure non-standard, per-project stuff.
+ var forbiddenApisMainTask = allForbiddenApisTasks.named("forbiddenApisMain");
+
+ switch (project.getPath()) {
+ case ":lucene:build-tools:missing-doclet", ":lucene:build-tools:build-infra-shadow":
+ forbiddenApisMainTask.configure(
+ task -> {
+ task.getBundledSignatures().removeAll(Set.of("jdk-non-portable", "jdk-system-out"));
+ });
+ break;
+
+ case ":lucene:demo", ":lucene:benchmark", ":lucene:test-framework":
+ forbiddenApisMainTask.configure(
+ task -> {
+ task.getBundledSignatures().remove("jdk-system-out");
+ });
+ break;
+
+ case ":lucene:analysis:common":
+ forbiddenApisMainTask.configure(
+ task -> {
+ task.exclude("org/tartarus/**");
+ });
+ break;
+ }
+ }
+
+ /**
+ * Adds signature files of dependencies derived from the compile configuration associated with the
+ * source set the forbidden apis task was created for.
+ */
+ private void addCompileConfigurationSignatureFiles(
+ Project project,
+ CheckForbiddenApis task,
+ SourceSetContainer sourceSets,
+ String ruleGroup,
+ Path forbiddenApisDir) {
+ if (!task.getName().startsWith("forbiddenApis")) {
+ throw new GradleException(
+ "Expect all forbidden-apis task to be named forbiddenApisXYZ,"
+ + "where XYZ is the sourceSet's compile classpath configuration to inspect: "
+ + task.getName());
+ }
+
+ // Figure out the source set and configuration name.
+ String sourceSetName;
+ {
+ String name = task.getName().replaceAll("^forbiddenApis", "");
+ sourceSetName = name.substring(0, 1).toLowerCase(Locale.ROOT) + name.substring(1);
+ }
+
+ var compileConfigurationName =
+ sourceSets.named(sourceSetName).get().getCompileClasspathConfigurationName();
+
+ var allDependenciesProvider =
+ project
+ .getConfigurations()
+ .named(compileConfigurationName)
+ .flatMap(conf -> conf.getIncoming().getResolutionResult().getRootComponent())
+ .map(ApplyForbiddenApisPlugin::getAllDependencies);
+
+ // collect signature files for all other dependencies.
+ Provider<List<File>> signatureFiles =
+ allDependenciesProvider.map(
+ allDependencies -> {
+ return collectSignatureLocations(ruleGroup, allDependencies).stream()
+ .map(forbiddenApisDir::resolve)
+ .map(Path::toFile)
+ .toList();
+ });
+
+ var allSignatureFiles = project.files(signatureFiles);
+ var existingSignatureFiles = allSignatureFiles.filter(File::exists);
+
+ task.getInputs().files(existingSignatureFiles);
+ task.setSignaturesFiles(task.getSignaturesFiles().plus(existingSignatureFiles));
+
+ addBuiltInSignatures(task, allDependenciesProvider);
+
+ if (task.getLogger().isInfoEnabled()) {
+ task.doFirst(
+ t -> {
+ t.getLogger()
+ .info(
+ "All expected signature file locations for source set '{}', configuration '{}':\n{}",
+ sourceSetName,
+ compileConfigurationName,
+ allSignatureFiles.getFiles().stream()
+ .map(f -> " " + f)
+ .collect(Collectors.joining("\n")));
+
+ t.getLogger()
+ .info(
+ "Existing signature files for source set '{}':\n{}",
+ sourceSetName,
+ allSignatureFiles.getFiles().stream()
+ .filter(File::exists)
+ .map(f -> " " + f)
+ .collect(Collectors.joining("\n")));
+ });
+ }
+ }
+
+ /**
+ * This is hacky; certain signatures are built into forbidden-api checker. Unfortunately, the
+ * task's setBundledSignatures doesn't accept providers and if we try to instantiate the
+ * dependency graph early, it breaks the build... I don't know how to fix this better than by
+ * moving to execution stage.
+ */
+ private static void addBuiltInSignatures(
+ CheckForbiddenApis task,
+ Provider<HashSet<ResolvedDependencyResult>> allDependenciesProvider) {
+ task.doFirst(
+ _ -> {
+ var builtInSignatureProvider =
+ allDependenciesProvider.map(
+ allDependencies -> {
+ return allDependencies.stream()
+ .map(
+ dep -> {
+ return dep.getSelected().getModuleVersion();
+ })
+ .filter(
+ moduleVersion -> {
+ return moduleVersion.getGroup().equals("commons-io")
+ && moduleVersion.getName().equals("commons-io");
+ })
+ .map(
+ moduleVersion ->
+ moduleVersion.getName() + "-unsafe-" + moduleVersion.getVersion())
+ .sorted()
+ .toList();
+ });
+ task.getBundledSignatures().addAll(builtInSignatureProvider.get());
+ });
+ }
+
+ private Set<String> collectSignatureLocations(
+ String ruleGroup, Set<ResolvedDependencyResult> allDependencies) {
+
+ Set<String> signatureLocations = new TreeSet<>();
+ signatureLocations.add("all/defaults.txt");
+ signatureLocations.add(ruleGroup + "/defaults.txt");
+
+ allDependencies.stream()
+ .flatMap(
+ dep -> {
+ var moduleVersion = dep.getSelected().getModuleVersion();
+ var ruleFileName = moduleVersion.getGroup() + "." + moduleVersion.getName() + ".txt";
+ return Set.of("all/" + ruleFileName, ruleGroup + "/" + ruleFileName).stream();
+ })
+ .forEach(signatureLocations::add);
+
+ return signatureLocations;
+ }
+
+ private static @NotNull HashSet<ResolvedDependencyResult> getAllDependencies(
+ ResolvedComponentResult graphRoot) {
+ HashSet<ResolvedDependencyResult> allResolved = new HashSet<>();
+ ArrayDeque<DependencyResult> queue = new ArrayDeque<>(graphRoot.getDependencies());
+
+ while (!queue.isEmpty()) {
+ var dep = queue.removeFirst();
+ if (dep instanceof ResolvedDependencyResult resolvedDep) {
+ if (allResolved.add(resolvedDep)) {
+ queue.addAll(resolvedDep.getSelected().getDependencies());
+ }
+ } else {
+ throw new GradleException("Unresolved dependency, can't apply forbidden APIs: " + dep);
+ }
+ }
+ return allResolved;
+ }
+}
diff --git a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/ErrorReportingTestListener.java b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/ErrorReportingTestListener.java
index a4b1413fc9a..0df8548c915 100644
--- a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/ErrorReportingTestListener.java
+++ b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/ErrorReportingTestListener.java
@@ -153,7 +153,7 @@ public class ErrorReportingTestListener implements TestOutputListener, TestListe
}
}
- private static Pattern SANITIZE = Pattern.compile("[^a-zA-Z .\\-_0-9]+");
+ private static final Pattern SANITIZE = Pattern.compile("[^a-zA-Z .\\-_0-9]+");
public static String getOutputLogName(TestDescriptor suite) {
return SANITIZE.matcher("OUTPUT-" + suite.getName() + ".txt").replaceAll("_");
diff --git a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/JavaProjectConventionsPlugin.java b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/JavaProjectConventionsPlugin.java
new file mode 100644
index 00000000000..fcd6f8a8fff
--- /dev/null
+++ b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/JavaProjectConventionsPlugin.java
@@ -0,0 +1,55 @@
+/*
+ * 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.lucene.gradle.plugins.java;
+
+import java.util.stream.Stream;
+import org.apache.lucene.gradle.plugins.LuceneGradlePlugin;
+import org.apache.lucene.gradle.plugins.spotless.GoogleJavaFormatPlugin;
+import org.gradle.api.Project;
+import org.gradle.api.plugins.JavaLibraryPlugin;
+
+/** Applies java conventions to all projects that seem to be java projects. */
+public class JavaProjectConventionsPlugin extends LuceneGradlePlugin {
+ @Override
+ public void apply(Project project) {
+ applicableToRootProjectOnly(project);
+
+ project.allprojects(
+ p -> {
+ if (Stream.of("src/java", "src/test", "src/tools")
+ .anyMatch(srcDir -> p.file(srcDir).exists())) {
+ applyJavaPlugins(p);
+ }
+ });
+ }
+
+ private void applyJavaPlugins(Project project) {
+ var plugins = project.getPlugins();
+ plugins.apply(JavaLibraryPlugin.class);
+
+ plugins.apply(AlternativeJdkSupportPlugin.class);
+ plugins.apply(JavaFolderLayoutPlugin.class);
+ plugins.apply(JavacConfigurationPlugin.class);
+ plugins.apply(JarManifestConfigurationPlugin.class);
+ plugins.apply(TestsAndRandomizationPlugin.class);
+ plugins.apply(TestsBeastingPlugin.class);
+ plugins.apply(TestsSummaryPlugin.class);
+ plugins.apply(ApplyForbiddenApisPlugin.class);
+ plugins.apply(EcjLintPlugin.class);
+ plugins.apply(GoogleJavaFormatPlugin.class);
+ }
+}
diff --git a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsBeastingPlugin.java b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsBeastingPlugin.java
index 3708c9a9c03..828813b0fc6 100644
--- a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsBeastingPlugin.java
+++ b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsBeastingPlugin.java
@@ -19,6 +19,7 @@ package org.apache.lucene.gradle.plugins.java;
import com.carrotsearch.gradle.buildinfra.buildoptions.BuildOption;
import com.carrotsearch.gradle.buildinfra.buildoptions.BuildOptionValueSource;
import java.util.List;
+import java.util.Locale;
import java.util.Random;
import java.util.stream.IntStream;
import org.apache.lucene.gradle.plugins.LuceneGradlePlugin;
@@ -141,6 +142,7 @@ public class TestsBeastingPlugin extends LuceneGradlePlugin {
test.systemProperty(
"tests.seed",
String.format(
+ Locale.ROOT,
"%08X",
new Random(rootSeed + (idx * 6364136223846793005L))
.nextLong()));
diff --git a/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsSummaryPlugin.java b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsSummaryPlugin.java
new file mode 100644
index 00000000000..f4906b7f1c4
--- /dev/null
+++ b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsSummaryPlugin.java
@@ -0,0 +1,90 @@
+/*
+ * 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.lucene.gradle.plugins.java;
+
+import org.apache.lucene.gradle.plugins.LuceneGradlePlugin;
+import org.gradle.api.Project;
+import org.gradle.api.plugins.JavaPlugin;
+import org.gradle.api.tasks.testing.Test;
+import org.gradle.api.tasks.testing.TestDescriptor;
+import org.gradle.api.tasks.testing.TestListener;
+import org.gradle.api.tasks.testing.TestResult;
+
+/** Prints per-project test summary after each {@link Test} task. */
+public class TestsSummaryPlugin extends LuceneGradlePlugin {
+ @Override
+ public void apply(Project project) {
+ requiresAppliedPlugin(project, JavaPlugin.class);
+
+ project
+ .getTasks()
+ .withType(Test.class)
+ .configureEach(
+ task -> {
+ if (!task.getLogger().isLifecycleEnabled()) {
+ return;
+ }
+
+ task.addTestListener(
+ new TestListener() {
+ @Override
+ public void afterSuite(TestDescriptor suite, TestResult result) {
+ if (suite.getParent() != null) {
+ if (result.getTestCount() > 0) {
+ StringBuilder sb = new StringBuilder();
+ sb.append(task.getPath())
+ .append(" (")
+ .append(result.getResultType().name())
+ .append("): ");
+
+ sb.append(pluralize(result.getTestCount(), "test"));
+ if (result.getFailedTestCount() > 0) {
+ sb.append(", ")
+ .append(pluralize(result.getFailedTestCount(), " failure"));
+ }
+ if (result.getSkippedTestCount() > 0) {
+ sb.append(", ")
+ .append(pluralize(result.getSkippedTestCount(), " skipped"));
+ }
+
+ task.getLogger().lifecycle(sb.toString());
+ }
+ }
+ }
+
+ private static String pluralize(long count, String verb) {
+ return verb + (count == 1 ? "" : "s");
+ }
+
+ @Override
+ public void beforeSuite(TestDescriptor suite) {
+ // empty.
+ }
+
+ @Override
+ public void beforeTest(TestDescriptor testDescriptor) {
+ // empty.
+ }
+
+ @Override
+ public void afterTest(TestDescriptor testDescriptor, TestResult result) {
+ // empty.
+ }
+ });
+ });
+ }
+}
diff --git a/gradle/validation/forbidden-apis/com.carrotsearch.randomizedtesting.randomizedtesting-runner.all.txt b/gradle/validation/forbidden-apis/all/com.carrotsearch.randomizedtesting.randomizedtesting-runner.txt
similarity index 100%
rename from gradle/validation/forbidden-apis/com.carrotsearch.randomizedtesting.randomizedtesting-runner.all.txt
rename to gradle/validation/forbidden-apis/all/com.carrotsearch.randomizedtesting.randomizedtesting-runner.txt
diff --git a/gradle/validation/forbidden-apis/commons-codec.commons-codec.all.txt b/gradle/validation/forbidden-apis/all/commons-codec.commons-codec.txt
similarity index 100%
rename from gradle/validation/forbidden-apis/commons-codec.commons-codec.all.txt
rename to gradle/validation/forbidden-apis/all/commons-codec.commons-codec.txt
diff --git a/gradle/validation/forbidden-apis/defaults.all.txt b/gradle/validation/forbidden-apis/all/defaults.txt
similarity index 100%
rename from gradle/validation/forbidden-apis/defaults.all.txt
rename to gradle/validation/forbidden-apis/all/defaults.txt
diff --git a/gradle/validation/forbidden-apis/junit.junit.lucene.txt b/gradle/validation/forbidden-apis/all/junit.junit.txt
similarity index 100%
rename from gradle/validation/forbidden-apis/junit.junit.lucene.txt
rename to gradle/validation/forbidden-apis/all/junit.junit.txt
diff --git a/gradle/validation/forbidden-apis/defaults.logging.txt b/gradle/validation/forbidden-apis/main-build-infra/defaults.txt
similarity index 78%
rename from gradle/validation/forbidden-apis/defaults.logging.txt
rename to gradle/validation/forbidden-apis/main-build-infra/defaults.txt
index a510df57800..5ab1f873d22 100644
--- a/gradle/validation/forbidden-apis/defaults.logging.txt
+++ b/gradle/validation/forbidden-apis/main-build-infra/defaults.txt
@@ -13,6 +13,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+@defaultMessage Use NIO.2 instead
+java.io.FileInputStream
+java.io.FileOutputStream
+java.io.PrintStream#<init>(java.lang.String,java.lang.String)
+java.io.PrintWriter#<init>(java.lang.String,java.lang.String)
+java.util.Formatter#<init>(java.lang.String,java.lang.String,java.util.Locale)
+java.io.RandomAccessFile
+java.util.jar.JarFile
+java.util.zip.ZipFile
+
@defaultMessage Lucene should never log below Level.WARNING
java.util.logging.Level#CONFIG
java.util.logging.Level#FINE
diff --git a/gradle/validation/forbidden-apis/defaults.lucene.txt b/gradle/validation/forbidden-apis/main/defaults.txt
similarity index 100%
rename from gradle/validation/forbidden-apis/defaults.lucene.txt
rename to gradle/validation/forbidden-apis/main/defaults.txt
diff --git a/gradle/validation/forbidden-apis/non-standard/logging.txt b/gradle/validation/forbidden-apis/non-standard/logging.txt
new file mode 100644
index 00000000000..8082f7db0f9
--- /dev/null
+++ b/gradle/validation/forbidden-apis/non-standard/logging.txt
@@ -0,0 +1,14 @@
+@defaultMessage Lucene should never log below Level.WARNING
+java.util.logging.Level#CONFIG
+java.util.logging.Level#FINE
+java.util.logging.Level#FINER
+java.util.logging.Level#FINEST
+java.util.logging.Level#INFO
+java.util.logging.Logger#config(**)
+java.util.logging.Logger#fine(**)
+java.util.logging.Logger#finer(**)
+java.util.logging.Logger#finest(**)
+java.util.logging.Logger#info(**)
+java.util.logging.Logger#throwing(**)
+java.util.logging.Logger#entering(**)
+java.util.logging.Logger#exiting(**)
diff --git a/gradle/validation/forbidden-apis/defaults.tests.txt b/gradle/validation/forbidden-apis/test/defaults.txt
similarity index 99%
rename from gradle/validation/forbidden-apis/defaults.tests.txt
rename to gradle/validation/forbidden-apis/test/defaults.txt
index bb491804e06..d218f3fe174 100644
--- a/gradle/validation/forbidden-apis/defaults.tests.txt
+++ b/gradle/validation/forbidden-apis/test/defaults.txt
@@ -16,8 +16,9 @@
java.util.Random#<init>() @ Use RandomizedRunner's random() instead
java.lang.Math#random() @ Use RandomizedRunner's random().nextDouble() instead
-# TODO: fix tests that do this!
java.lang.System#currentTimeMillis() @ Don't depend on wall clock times
+
+# TODO: fix tests that do this!
#java.lang.System#nanoTime() @ Don't depend on wall clock times
java.text.Collator#compare(**) @ Use LuceneTestCase.collate instead, which can avoid JDK-8071862
diff --git a/help/forbiddenApis.txt b/help/forbiddenApis.txt
index 10f851924db..86e4e10ff86 100644
--- a/help/forbiddenApis.txt
+++ b/help/forbiddenApis.txt
@@ -3,24 +3,44 @@ Forbidden API rules
Uwe's excellent forbidden API checker is applied as part of 'check'
task. The rules for each project are sourced dynamically based on the
-actual set of dependencies.
+actual set of dependencies of a project.
-If a given project has a dependency on an artifact called "foo.bar:baz"
-then all of these rule files will be applied (all paths relative
-to: gradle/validation/forbidden-apis/).
+Signature files are under gradle/validation/forbidden-apis and are grouped
+into these three main categories:
-defaults.all.txt
-defaults.[project].txt
-foo.bar.baz.all.txt
-foo.bar.baz.[project].txt
+all/
+main/
+test/
+
+and these extra exceptional ones:
+
+main-build-infra/
+non-standard/
+
+Signatures under all/ are applied to all sources, main/ are applied to
+all main source sets and test/ to tests only (and test fixtures, if any).
+main-build-infra is applied to gradle's build-infra project sources (build
+plugins). non-standard/ contains signature files applied selectively
+to just certain projects.
+
+Let's say a project has a test dependency on an artifact called "foo.bar:baz".
+An attempt would be made to load the following signature files:
+
+all/defaults.txt
+test/defaults.txt
+test/foo.bar.baz.txt
+
+From the above, only those files that actually exist are applied. You can
+verify which signature locations are expected and which actually
+exist by running forbiddenApis tasks with info logger:
+
+./gradlew -p lucene/analysis/morfologik forbiddenApis --info
-Note that the "defaults" can't reference any JARs other than Java's
-runtime.
Example
-------
-We'd like to prevent people from using Guava's
+Let's say we'd like to prevent people from using Guava's
com.google.common.base.Charsets class. The rule would be:
@defaultMessage Use java.nio.charset.StandardCharsets instead
@@ -28,7 +48,7 @@ com.google.common.base.Charsets
and we would place this rule in this file:
-gradle/validation/forbidden-apis/com.google.guava.guava.all.txt
+gradle/validation/forbidden-apis/all/com.google.guava.guava.txt
From now on, if *any* module depends on this library, it will
-automatically pick up the rule and enforce it.
+automatically pick up the signature file and enforce it.
diff --git a/lucene/analysis/common/src/tools/java/org/apache/lucene/analysis/standard/GenerateJflexTLDMacros.java b/lucene/analysis/common/src/tools/java/org/apache/lucene/analysis/standard/GenerateJflexTLDMacros.java
index c205a9eea3f..a34cf0fe1ce 100644
--- a/lucene/analysis/common/src/tools/java/org/apache/lucene/analysis/standard/GenerateJflexTLDMacros.java
+++ b/lucene/analysis/common/src/tools/java/org/apache/lucene/analysis/standard/GenerateJflexTLDMacros.java
@@ -20,6 +20,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
+import java.io.PrintStream;
import java.io.Writer;
import java.net.URI;
import java.net.URL;
@@ -39,6 +40,7 @@ import java.util.TimeZone;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collectors;
+import org.apache.lucene.util.SuppressForbidden;
/**
* Generates a file containing JFlex macros to accept valid ASCII TLDs (top level domains), for
@@ -49,11 +51,23 @@ import java.util.stream.Collectors;
* ASCII-only TLDs, including punycode forms of internationalized TLDs (output file cmdline arg #1).
*/
public class GenerateJflexTLDMacros {
+ private static final PrintStream syserr = getSysErr();
+ private static final PrintStream sysout = getSysOut();
+
+ @SuppressForbidden(reason = "Uses System.err, which is fine here.")
+ private static PrintStream getSysErr() {
+ return System.err;
+ }
+
+ @SuppressForbidden(reason = "Uses System.out, which is fine here.")
+ private static PrintStream getSysOut() {
+ return System.err;
+ }
public static void main(String... args) throws Exception {
if (args.length != 3 || args[0].equals("--help") || args[0].equals("-help")) {
- System.err.println("Cmd line params:");
- System.err.println(
+ syserr.println("Cmd line params:");
+ syserr.println(
" java "
+ GenerateJflexTLDMacros.class.getName()
+ "<ZoneFileURL> <JFlexOutputFile> <TLDListFile>");
@@ -132,10 +146,10 @@ public class GenerateJflexTLDMacros {
for (int suffixLength = 0; suffixLength < TLDsBySuffixLength.size(); ++suffixLength) {
int domainsAtThisSuffixLength = TLDsBySuffixLength.get(suffixLength).size();
totalDomains += domainsAtThisSuffixLength;
- System.out.printf(
+ sysout.printf(
Locale.ROOT, "%30s: %4d TLDs%n", getMacroName(suffixLength), domainsAtThisSuffixLength);
}
- System.out.printf(Locale.ROOT, "%30s: %4d TLDs%n", "Total", totalDomains);
+ sysout.printf(Locale.ROOT, "%30s: %4d TLDs%n", "Total", totalDomains);
}
/**
@@ -160,7 +174,7 @@ public class GenerateJflexTLDMacros {
processedTLDsLongestFirst.put(line.toLowerCase(Locale.ROOT), Boolean.FALSE);
}
}
- System.out.println(
+ sysout.println(
"Found "
+ processedTLDsLongestFirst.size()
+ " TLDs in IANA TLD Database at "
diff --git a/lucene/analysis/icu/src/tools/java/org/apache/lucene/analysis/icu/GenerateUTR30DataFiles.java b/lucene/analysis/icu/src/tools/java/org/apache/lucene/analysis/icu/GenerateUTR30DataFiles.java
index bac5fdfaf4d..aae4ae50bbd 100644
--- a/lucene/analysis/icu/src/tools/java/org/apache/lucene/analysis/icu/GenerateUTR30DataFiles.java
+++ b/lucene/analysis/icu/src/tools/java/org/apache/lucene/analysis/icu/GenerateUTR30DataFiles.java
@@ -26,6 +26,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
+import java.io.PrintStream;
import java.io.Writer;
import java.net.URI;
import java.net.URL;
@@ -40,6 +41,7 @@ import java.util.Locale;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.apache.lucene.util.SuppressForbidden;
/**
* Downloads/generates lucene/analysis/icu/src/data/utr30/*.txt for the specified icu release tag.
@@ -55,6 +57,13 @@ import java.util.regex.Pattern;
* </ol>
*/
public class GenerateUTR30DataFiles {
+ private static final PrintStream syserr = getSysErr();
+
+ @SuppressForbidden(reason = "Uses System.err, which is fine here.")
+ private static PrintStream getSysErr() {
+ return System.err;
+ }
+
private static final String ICU_GIT_TAG_URL = "https://raw.githubusercontent.com/unicode-org/icu";
private static final String ICU_DATA_NORM2_PATH = "icu4c/source/data/unidata/norm2";
private static final String NFC_TXT = "nfc.txt";
@@ -80,7 +89,7 @@ public class GenerateUTR30DataFiles {
getNFKCDataFilesFromIcuProject(args[0]);
expandRulesInUTR30DataFiles();
} catch (Throwable t) {
- t.printStackTrace(System.err);
+ t.printStackTrace(syserr);
System.exit(1);
}
}
@@ -128,8 +137,8 @@ public class GenerateUTR30DataFiles {
String rightHandSide = ruleMatcher.group(2).trim();
expandSingleRule(builder, leftHandSide, rightHandSide);
} catch (IllegalArgumentException e) {
- System.err.println("ERROR in " + file.getFileName() + " line #" + lineNum + ":");
- e.printStackTrace(System.err);
+ syserr.println("ERROR in " + file.getFileName() + " line #" + lineNum + ":");
+ e.printStackTrace(syserr);
System.exit(1);
}
modified = true;
@@ -149,7 +158,7 @@ public class GenerateUTR30DataFiles {
}
if (modified) {
- System.err.println("Expanding rules in and overwriting " + file.getFileName());
+ syserr.println("Expanding rules in and overwriting " + file.getFileName());
Files.writeString(file, builder.toString(), StandardCharsets.UTF_8);
}
}
@@ -159,14 +168,14 @@ public class GenerateUTR30DataFiles {
URI icuReleaseTagURI = icuTagsURI.resolve(releaseTag + "/");
URI norm2uri = icuReleaseTagURI.resolve(ICU_DATA_NORM2_PATH + "/");
- System.err.print("Downloading " + NFKC_TXT + " ... ");
+ syserr.print("Downloading " + NFKC_TXT + " ... ");
download(norm2uri.resolve(NFKC_TXT), NFKC_TXT);
- System.err.println("done.");
- System.err.print("Downloading " + NFKC_CF_TXT + " ... ");
+ syserr.println("done.");
+ syserr.print("Downloading " + NFKC_CF_TXT + " ... ");
download(norm2uri.resolve(NFKC_CF_TXT), NFKC_CF_TXT);
- System.err.println("done.");
+ syserr.println("done.");
- System.err.print("Downloading " + NFKC_CF_TXT + " and making diacritic rules one-way ... ");
+ syserr.print("Downloading " + NFKC_CF_TXT + " and making diacritic rules one-way ... ");
URLConnection connection = openConnection(norm2uri.resolve(NFC_TXT).toURL());
try (BufferedReader reader =
new BufferedReader(
@@ -207,7 +216,7 @@ public class GenerateUTR30DataFiles {
writer.write("\n");
}
}
- System.err.println("done.");
+ syserr.println("done.");
}
private static void download(URI uri, String outputFile) throws IOException {
@@ -249,7 +258,7 @@ public class GenerateUTR30DataFiles {
builder.append('>').append(rightHandSide).append("\n");
}
} else {
- System.err.println("ERROR: String '" + it.getString() + "' found in UnicodeSet");
+ syserr.println("ERROR: String '" + it.getString() + "' found in UnicodeSet");
System.exit(1);
}
}
diff --git a/lucene/analysis/icu/src/tools/java/org/apache/lucene/analysis/icu/RBBIRuleCompiler.java b/lucene/analysis/icu/src/tools/java/org/apache/lucene/analysis/icu/RBBIRuleCompiler.java
index b7f322eba64..0d2a60969ba 100644
--- a/lucene/analysis/icu/src/tools/java/org/apache/lucene/analysis/icu/RBBIRuleCompiler.java
+++ b/lucene/analysis/icu/src/tools/java/org/apache/lucene/analysis/icu/RBBIRuleCompiler.java
@@ -24,16 +24,24 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
+import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
+import org.apache.lucene.util.SuppressForbidden;
/**
* Command-line utility to converts RuleBasedBreakIterator (.rbbi) files into binary compiled form
* (.brk).
*/
public class RBBIRuleCompiler {
+ private static final PrintStream syserr = getSysErr();
+
+ @SuppressForbidden(reason = "Uses System.err, which is fine here.")
+ private static PrintStream getSysErr() {
+ return System.err;
+ }
static String getRules(Path ruleFile) throws IOException {
StringBuilder rules = new StringBuilder();
@@ -61,8 +69,7 @@ public class RBBIRuleCompiler {
for (Path file : files) {
Path outputFile = destDir.resolve(file.getFileName().toString().replaceAll("rbbi$", "brk"));
String rules = getRules(file);
- System.err.print(
- "Compiling " + file.getFileName() + " to " + outputFile.getFileName() + ": ");
+ syserr.print("Compiling " + file.getFileName() + " to " + outputFile.getFileName() + ": ");
/*
* if there is a syntax error, compileRules() may succeed. the way to
* check is to try to instantiate from the string. additionally if the
@@ -75,19 +82,19 @@ public class RBBIRuleCompiler {
* do this intentionally, so you don't get a massive stack trace
* instead, get a useful syntax error!
*/
- System.err.println(e.getMessage());
+ syserr.println(e.getMessage());
System.exit(1);
}
try (OutputStream os = Files.newOutputStream(outputFile)) {
RuleBasedBreakIterator.compileRules(rules, os);
}
- System.err.println(Files.size(outputFile) + " bytes.");
+ syserr.println(Files.size(outputFile) + " bytes.");
}
}
public static void main(String[] args) throws Exception {
if (args.length < 2) {
- System.err.println("Usage: RBBIRuleComputer <sourcedir> <destdir>");
+ syserr.println("Usage: RBBIRuleComputer <sourcedir> <destdir>");
System.exit(1);
}
compile(Paths.get(args[0]), Paths.get(args[1]));
diff --git a/lucene/core/src/java24/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.java b/lucene/core/src/java24/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.java
index 2e061ee6f5f..edb2ae12d8e 100644
--- a/lucene/core/src/java24/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.java
+++ b/lucene/core/src/java24/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.java
@@ -25,6 +25,7 @@ import org.apache.lucene.codecs.hnsw.FlatVectorsScorer;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.MemorySegmentAccessInput;
import org.apache.lucene.util.Constants;
+import org.apache.lucene.util.SuppressForbidden;
/** A vectorization provider that leverages the Panama Vector API. */
final class PanamaVectorizationProvider extends VectorizationProvider {
@@ -47,6 +48,11 @@ final class PanamaVectorizationProvider extends VectorizationProvider {
this.vectorUtilSupport = new PanamaVectorUtilSupport();
+ logIncubatorSetup();
+ }
+
+ @SuppressForbidden(reason = "We log at info level here, it's fine.")
+ private void logIncubatorSetup() {
var log = Logger.getLogger(getClass().getName());
log.info(
String.format(
diff --git a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/StrategyTestCase.java b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/StrategyTestCase.java
index 7682ab319bc..2bf08b4db2d 100644
--- a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/StrategyTestCase.java
+++ b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/StrategyTestCase.java
@@ -26,7 +26,6 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
-import java.util.logging.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
@@ -57,8 +56,6 @@ public abstract class StrategyTestCase extends SpatialTestCase {
public static final String QTEST_Cities_Intersects_BBox = "cities-Intersects-BBox.txt";
public static final String QTEST_Simple_Queries_BBox = "simple-Queries-BBox.txt";
- protected Logger log = Logger.getLogger(getClass().getName());
-
protected final SpatialArgsParser argsParser = new SpatialArgsParser();
protected SpatialStrategy strategy;
@@ -66,7 +63,6 @@ public abstract class StrategyTestCase extends SpatialTestCase {
protected void executeQueries(SpatialMatchConcern concern, String... testQueryFile)
throws IOException {
- log.info("testing queried for strategy " + strategy); // nowarn
for (String path : testQueryFile) {
Iterator<SpatialTestQuery> testQueryIterator = getTestQueries(path, ctx);
runTestQueries(testQueryIterator, concern);
diff --git a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestHeatmapFacetCounter.java b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestHeatmapFacetCounter.java
index b3080d6a4ec..edebc495df8 100644
--- a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestHeatmapFacetCounter.java
+++ b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestHeatmapFacetCounter.java
@@ -66,8 +66,8 @@ public class TestHeatmapFacetCounter extends StrategyTestCase {
@After
public void after() {
- log.info(
- "Validated " + cellsValidated + " cells, " + cellValidatedNonZero + " non-zero"); // nowarn
+ System.out.println(
+ "Validated " + cellsValidated + " cells, " + cellValidatedNonZero + " non-zero");
}
@Test
diff --git a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestRandomSpatialOpFuzzyPrefixTree.java b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestRandomSpatialOpFuzzyPrefixTree.java
index d7780430730..d9d65ac60cd 100644
--- a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestRandomSpatialOpFuzzyPrefixTree.java
+++ b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestRandomSpatialOpFuzzyPrefixTree.java
@@ -85,8 +85,6 @@ public class TestRandomSpatialOpFuzzyPrefixTree extends StrategyTestCase {
if (maxLevels == -1 && rarely()) {
((PrefixTreeStrategy) strategy).setPointsOnly(true);
}
-
- log.info("Strategy: " + strategy.toString()); // nowarn
}
private void setupCtx2D(SpatialContext ctx) {