|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.build.springframework; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileInputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.UncheckedIOException; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.StandardOpenOption; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Properties; |
| 32 | + |
| 33 | +import org.gradle.api.DefaultTask; |
| 34 | +import org.gradle.api.GradleException; |
| 35 | +import org.gradle.api.Task; |
| 36 | +import org.gradle.api.file.DirectoryProperty; |
| 37 | +import org.gradle.api.file.FileCollection; |
| 38 | +import org.gradle.api.file.FileTree; |
| 39 | +import org.gradle.api.tasks.Classpath; |
| 40 | +import org.gradle.api.tasks.InputFiles; |
| 41 | +import org.gradle.api.tasks.OutputDirectory; |
| 42 | +import org.gradle.api.tasks.PathSensitive; |
| 43 | +import org.gradle.api.tasks.PathSensitivity; |
| 44 | +import org.gradle.api.tasks.SkipWhenEmpty; |
| 45 | +import org.gradle.api.tasks.TaskAction; |
| 46 | +import org.gradle.language.base.plugins.LifecycleBasePlugin; |
| 47 | + |
| 48 | +import org.springframework.core.io.support.SpringFactoriesLoader; |
| 49 | +import org.springframework.util.StringUtils; |
| 50 | + |
| 51 | +/** |
| 52 | + * {@link Task} that checks files loaded by {@link SpringFactoriesLoader}. |
| 53 | + * |
| 54 | + * @author Andy Wilkinson |
| 55 | + */ |
| 56 | +public abstract class CheckFactoriesFile extends DefaultTask { |
| 57 | + |
| 58 | + private final String path; |
| 59 | + |
| 60 | + private FileCollection sourceFiles = getProject().getObjects().fileCollection(); |
| 61 | + |
| 62 | + private FileCollection classpath = getProject().getObjects().fileCollection(); |
| 63 | + |
| 64 | + protected CheckFactoriesFile(String path) { |
| 65 | + this.path = path; |
| 66 | + getOutputDirectory().convention(getProject().getLayout().getBuildDirectory().dir(getName())); |
| 67 | + setGroup(LifecycleBasePlugin.VERIFICATION_GROUP); |
| 68 | + } |
| 69 | + |
| 70 | + @InputFiles |
| 71 | + @SkipWhenEmpty |
| 72 | + @PathSensitive(PathSensitivity.RELATIVE) |
| 73 | + public FileTree getSource() { |
| 74 | + return this.sourceFiles.getAsFileTree().matching((filter) -> filter.include(this.path)); |
| 75 | + } |
| 76 | + |
| 77 | + public void setSource(Object source) { |
| 78 | + this.sourceFiles = getProject().getObjects().fileCollection().from(source); |
| 79 | + } |
| 80 | + |
| 81 | + @Classpath |
| 82 | + public FileCollection getClasspath() { |
| 83 | + return this.classpath; |
| 84 | + } |
| 85 | + |
| 86 | + public void setClasspath(Object classpath) { |
| 87 | + this.classpath = getProject().getObjects().fileCollection().from(classpath); |
| 88 | + } |
| 89 | + |
| 90 | + @OutputDirectory |
| 91 | + public abstract DirectoryProperty getOutputDirectory(); |
| 92 | + |
| 93 | + @TaskAction |
| 94 | + void execute() { |
| 95 | + getSource().forEach(this::check); |
| 96 | + } |
| 97 | + |
| 98 | + private void check(File factoriesFile) { |
| 99 | + Properties factories = load(factoriesFile); |
| 100 | + Map<String, List<String>> problems = new LinkedHashMap<>(); |
| 101 | + for (String key : factories.stringPropertyNames()) { |
| 102 | + List<String> values = Arrays |
| 103 | + .asList(StringUtils.commaDelimitedListToStringArray(factories.getProperty(key))); |
| 104 | + for (String value : values) { |
| 105 | + boolean found = find(value); |
| 106 | + if (!found) { |
| 107 | + List<String> problemsForKey = problems.computeIfAbsent(key, (k) -> new ArrayList<>()); |
| 108 | + String binaryName = binaryNameOf(value); |
| 109 | + found = find(binaryName); |
| 110 | + if (found) { |
| 111 | + problemsForKey |
| 112 | + .add("'%s' should be listed using its binary name '%s'".formatted(value, binaryName)); |
| 113 | + } |
| 114 | + else { |
| 115 | + problemsForKey.add("'%s' was not found".formatted(value)); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + List<String> sortedValues = new ArrayList<>(values); |
| 120 | + Collections.sort(sortedValues); |
| 121 | + if (!sortedValues.equals(values)) { |
| 122 | + List<String> problemsForKey = problems.computeIfAbsent(key, (k) -> new ArrayList<>()); |
| 123 | + problemsForKey.add("Entries should be sorted alphabetically"); |
| 124 | + } |
| 125 | + } |
| 126 | + File outputFile = getOutputDirectory().file("failure-report.txt").get().getAsFile(); |
| 127 | + writeReport(factoriesFile, problems, outputFile); |
| 128 | + if (!problems.isEmpty()) { |
| 129 | + throw new GradleException("%s check failed. See '%s' for details".formatted(this.path, outputFile)); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private boolean find(String className) { |
| 134 | + for (File root : this.classpath.getFiles()) { |
| 135 | + String classFilePath = className.replace(".", "/") + ".class"; |
| 136 | + if (new File(root, classFilePath).isFile()) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + } |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + private String binaryNameOf(String className) { |
| 144 | + int lastDotIndex = className.lastIndexOf('.'); |
| 145 | + return className.substring(0, lastDotIndex) + "$" + className.substring(lastDotIndex + 1); |
| 146 | + } |
| 147 | + |
| 148 | + private Properties load(File aotFactories) { |
| 149 | + Properties properties = new Properties(); |
| 150 | + try (FileInputStream input = new FileInputStream(aotFactories)) { |
| 151 | + properties.load(input); |
| 152 | + return properties; |
| 153 | + } |
| 154 | + catch (IOException ex) { |
| 155 | + throw new UncheckedIOException(ex); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private void writeReport(File factoriesFile, Map<String, List<String>> problems, File outputFile) { |
| 160 | + outputFile.getParentFile().mkdirs(); |
| 161 | + StringBuilder report = new StringBuilder(); |
| 162 | + if (!problems.isEmpty()) { |
| 163 | + report.append("Found problems in '%s':%n".formatted(factoriesFile)); |
| 164 | + problems.forEach((key, problemsForKey) -> { |
| 165 | + report.append(" - %s:%n".formatted(key)); |
| 166 | + problemsForKey.forEach((problem) -> report.append(" - %s%n".formatted(problem))); |
| 167 | + }); |
| 168 | + } |
| 169 | + try { |
| 170 | + Files.writeString(outputFile.toPath(), report.toString(), StandardOpenOption.CREATE, |
| 171 | + StandardOpenOption.TRUNCATE_EXISTING); |
| 172 | + } |
| 173 | + catch (IOException ex) { |
| 174 | + throw new UncheckedIOException(ex); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments