|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.boot.build.autoconfigure;
|
18 | 18 |
|
19 | 19 | import java.io.File;
|
| 20 | +import java.io.FileInputStream; |
20 | 21 | import java.io.FileReader;
|
21 | 22 | import java.io.FileWriter;
|
22 | 23 | import java.io.IOException;
|
| 24 | +import java.io.InputStream; |
23 | 25 | import java.io.Reader;
|
| 26 | +import java.util.LinkedHashSet; |
24 | 27 | import java.util.Properties;
|
| 28 | +import java.util.Set; |
25 | 29 | import java.util.concurrent.Callable;
|
26 | 30 |
|
27 | 31 | import org.gradle.api.DefaultTask;
|
|
30 | 34 | import org.gradle.api.tasks.SourceSet;
|
31 | 35 | import org.gradle.api.tasks.TaskAction;
|
32 | 36 |
|
| 37 | +import org.springframework.asm.ClassReader; |
| 38 | +import org.springframework.asm.Opcodes; |
33 | 39 | import org.springframework.core.CollectionFactory;
|
| 40 | +import org.springframework.util.StringUtils; |
34 | 41 |
|
35 | 42 | /**
|
36 | 43 | * A {@link Task} for generating metadata describing a project's auto-configuration
|
@@ -78,12 +85,38 @@ private Properties readAutoConfiguration() throws IOException {
|
78 | 85 | Properties autoConfiguration = CollectionFactory.createSortedProperties(true);
|
79 | 86 | Properties springFactories = readSpringFactories(
|
80 | 87 | new File(this.sourceSet.getOutput().getResourcesDir(), "META-INF/spring.factories"));
|
81 |
| - autoConfiguration.setProperty("autoConfigurationClassNames", |
82 |
| - springFactories.getProperty("org.springframework.boot.autoconfigure.EnableAutoConfiguration")); |
| 88 | + String enableAutoConfiguration = springFactories |
| 89 | + .getProperty("org.springframework.boot.autoconfigure.EnableAutoConfiguration"); |
| 90 | + Set<String> classNames = StringUtils.commaDelimitedListToSet(enableAutoConfiguration); |
| 91 | + Set<String> publicClassNames = new LinkedHashSet<>(); |
| 92 | + for (String className : classNames) { |
| 93 | + File classFile = findClassFile(className); |
| 94 | + if (classFile == null) { |
| 95 | + throw new IllegalStateException("Auto-configuration class '" + className + "' not found."); |
| 96 | + } |
| 97 | + try (InputStream in = new FileInputStream(classFile)) { |
| 98 | + int access = new ClassReader(in).getAccess(); |
| 99 | + if ((access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC) { |
| 100 | + publicClassNames.add(className); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + autoConfiguration.setProperty("autoConfigurationClassNames", String.join(",", publicClassNames)); |
83 | 105 | autoConfiguration.setProperty("module", getProject().getName());
|
84 | 106 | return autoConfiguration;
|
85 | 107 | }
|
86 | 108 |
|
| 109 | + private File findClassFile(String className) { |
| 110 | + String classFileName = className.replace(".", "/") + ".class"; |
| 111 | + for (File classesDir : this.sourceSet.getOutput().getClassesDirs()) { |
| 112 | + File classFile = new File(classesDir, classFileName); |
| 113 | + if (classFile.isFile()) { |
| 114 | + return classFile; |
| 115 | + } |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
87 | 120 | private Properties readSpringFactories(File file) throws IOException {
|
88 | 121 | Properties springFactories = new Properties();
|
89 | 122 | try (Reader in = new FileReader(file)) {
|
|
0 commit comments