Merge branch '2.4.x'

Closes gh-25988
pull/25997/head
Andy Wilkinson 4 years ago
commit 92dfcc5994

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,11 +17,15 @@
package org.springframework.boot.build.autoconfigure; package org.springframework.boot.build.autoconfigure;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.util.LinkedHashSet;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import org.gradle.api.DefaultTask; import org.gradle.api.DefaultTask;
@ -30,7 +34,10 @@ import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import org.springframework.asm.ClassReader;
import org.springframework.asm.Opcodes;
import org.springframework.core.CollectionFactory; import org.springframework.core.CollectionFactory;
import org.springframework.util.StringUtils;
/** /**
* A {@link Task} for generating metadata describing a project's auto-configuration * A {@link Task} for generating metadata describing a project's auto-configuration
@ -78,12 +85,38 @@ public class AutoConfigurationMetadata extends DefaultTask {
Properties autoConfiguration = CollectionFactory.createSortedProperties(true); Properties autoConfiguration = CollectionFactory.createSortedProperties(true);
Properties springFactories = readSpringFactories( Properties springFactories = readSpringFactories(
new File(this.sourceSet.getOutput().getResourcesDir(), "META-INF/spring.factories")); new File(this.sourceSet.getOutput().getResourcesDir(), "META-INF/spring.factories"));
autoConfiguration.setProperty("autoConfigurationClassNames", String enableAutoConfiguration = springFactories
springFactories.getProperty("org.springframework.boot.autoconfigure.EnableAutoConfiguration")); .getProperty("org.springframework.boot.autoconfigure.EnableAutoConfiguration");
Set<String> classNames = StringUtils.commaDelimitedListToSet(enableAutoConfiguration);
Set<String> publicClassNames = new LinkedHashSet<>();
for (String className : classNames) {
File classFile = findClassFile(className);
if (classFile == null) {
throw new IllegalStateException("Auto-configuration class '" + className + "' not found.");
}
try (InputStream in = new FileInputStream(classFile)) {
int access = new ClassReader(in).getAccess();
if ((access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC) {
publicClassNames.add(className);
}
}
}
autoConfiguration.setProperty("autoConfigurationClassNames", String.join(",", publicClassNames));
autoConfiguration.setProperty("module", getProject().getName()); autoConfiguration.setProperty("module", getProject().getName());
return autoConfiguration; return autoConfiguration;
} }
private File findClassFile(String className) {
String classFileName = className.replace(".", "/") + ".class";
for (File classesDir : this.sourceSet.getOutput().getClassesDirs()) {
File classFile = new File(classesDir, classFileName);
if (classFile.isFile()) {
return classFile;
}
}
return null;
}
private Properties readSpringFactories(File file) throws IOException { private Properties readSpringFactories(File file) throws IOException {
Properties springFactories = new Properties(); Properties springFactories = new Properties();
try (Reader in = new FileReader(file)) { try (Reader in = new FileReader(file)) {

Loading…
Cancel
Save