Skip to content

Commit 445a1ce

Browse files
committed
Merge pull request #1584 from zambrovski
* pr/1584: Polish "Add ability to exclude ProjectGenerationConfiguration" Add ability to exclude ProjectGenerationConfiguration Closes gh-1584
2 parents 0db7ed4 + 2945eb1 commit 445a1ce

File tree

8 files changed

+313
-5
lines changed

8 files changed

+313
-5
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2012-2024 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 io.spring.initializr.generator.project;
18+
19+
import java.util.Arrays;
20+
import java.util.Set;
21+
import java.util.function.Predicate;
22+
23+
/**
24+
* Allows to filter {@link ProjectGenerationConfiguration}.
25+
*
26+
* @author Simon Zambrovski
27+
* @author Moritz Halbritter
28+
*/
29+
public interface ProjectGenerationConfigurationTypeFilter extends Predicate<Class<?>> {
30+
31+
/**
32+
* Creates a {@link ProjectGenerationConfigurationTypeFilter} which includes the given
33+
* types.
34+
* @param types the types to include
35+
* @return a {@link ProjectGenerationConfigurationTypeFilter}
36+
*/
37+
static ProjectGenerationConfigurationTypeFilter include(Class<?>... types) {
38+
Set<Class<?>> classes = Set.of(types);
39+
return classes::contains;
40+
}
41+
42+
/**
43+
* Creates a {@link ProjectGenerationConfigurationTypeFilter} which excludes the given
44+
* types.
45+
* @param types the types to exclude
46+
* @return a {@link ProjectGenerationConfigurationTypeFilter}
47+
*/
48+
static ProjectGenerationConfigurationTypeFilter exclude(Class<?>... types) {
49+
Set<Class<?>> classes = Set.of(types);
50+
return (clazz) -> !classes.contains(clazz);
51+
}
52+
53+
/**
54+
* Creates a {@link ProjectGenerationConfigurationTypeFilter} from multiple filters
55+
* which must all match.
56+
* @param filters the filters
57+
* @return a combined {@link ProjectGenerationConfigurationTypeFilter}
58+
*/
59+
static ProjectGenerationConfigurationTypeFilter allMatch(ProjectGenerationConfigurationTypeFilter... filters) {
60+
return allMatch(Arrays.asList(filters));
61+
}
62+
63+
/**
64+
* Creates a {@link ProjectGenerationConfigurationTypeFilter} from multiple filters
65+
* which must all match.
66+
* @param filters the filters
67+
* @return a combined {@link ProjectGenerationConfigurationTypeFilter}
68+
*/
69+
static ProjectGenerationConfigurationTypeFilter allMatch(
70+
Iterable<? extends ProjectGenerationConfigurationTypeFilter> filters) {
71+
return (clazz) -> {
72+
boolean match = true;
73+
for (ProjectGenerationConfigurationTypeFilter filter : filters) {
74+
match &= filter.test(clazz);
75+
}
76+
return match;
77+
};
78+
}
79+
80+
}

initializr-generator/src/main/java/io/spring/initializr/generator/project/ProjectGenerator.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.beans.factory.support.GenericBeanDefinition;
2525
import org.springframework.context.support.GenericApplicationContext;
2626
import org.springframework.core.io.support.SpringFactoriesLoader;
27+
import org.springframework.util.ClassUtils;
2728

2829
/**
2930
* Main entry point for project generation that processes a {@link ProjectDescription} by
@@ -34,6 +35,8 @@
3435
*
3536
* @author Andy Wilkinson
3637
* @author Stephane Nicoll
38+
* @author Simon Zambrovski
39+
* @author Moritz Halbritter
3740
*/
3841
public class ProjectGenerator {
3942

@@ -116,17 +119,45 @@ public <T> T generate(ProjectDescription description, ProjectAssetGenerator<T> p
116119

117120
/**
118121
* Return the {@link ProjectGenerationConfiguration} class names that should be
119-
* considered. By default this method will load candidates using
120-
* {@link SpringFactoriesLoader} with {@link ProjectGenerationConfiguration}.
122+
* considered. By default, this method will load candidates using
123+
* {@link SpringFactoriesLoader} with {@link ProjectGenerationConfiguration} and
124+
* exclude those which are not matched by the
125+
* {@link ProjectGenerationConfigurationTypeFilter}, also loaded by
126+
* {@link SpringFactoriesLoader}.
121127
* @param description the description of the project to generate
122128
* @return a list of candidate configurations
123129
*/
124-
@SuppressWarnings("deprecation")
130+
125131
protected List<String> getCandidateProjectGenerationConfigurations(ProjectDescription description) {
132+
List<String> candidates = getProjectGenerationConfigurationFactoryNames();
133+
ProjectGenerationConfigurationTypeFilter filter = getProjectGenerationConfigurationExclusionFilter();
134+
return candidates.stream().filter((candidate) -> {
135+
Class<?> type = resolveClass(candidate);
136+
return type != null && filter.test(type);
137+
}).toList();
138+
}
139+
140+
private Class<?> resolveClass(String candidate) {
141+
try {
142+
return ClassUtils.forName(candidate, getClass().getClassLoader());
143+
}
144+
catch (ClassNotFoundException ex) {
145+
return null;
146+
}
147+
}
148+
149+
@SuppressWarnings("deprecation")
150+
List<String> getProjectGenerationConfigurationFactoryNames() {
126151
return SpringFactoriesLoader.loadFactoryNames(ProjectGenerationConfiguration.class,
127152
getClass().getClassLoader());
128153
}
129154

155+
ProjectGenerationConfigurationTypeFilter getProjectGenerationConfigurationExclusionFilter() {
156+
List<ProjectGenerationConfigurationTypeFilter> filters = SpringFactoriesLoader
157+
.loadFactories(ProjectGenerationConfigurationTypeFilter.class, getClass().getClassLoader());
158+
return ProjectGenerationConfigurationTypeFilter.allMatch(filters);
159+
}
160+
130161
private void registerProjectDescription(ProjectGenerationContext context, ProjectDescription description) {
131162
context.registerBean(ProjectDescription.class, resolve(description, context));
132163
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2012-2024 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 io.spring.initializr.generator.project;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests for {@link ProjectGenerationConfigurationTypeFilter}.
25+
*
26+
* @author Moritz Halbritter
27+
*/
28+
class ProjectGenerationConfigurationTypeFilterTests {
29+
30+
@Test
31+
void include() {
32+
ProjectGenerationConfigurationTypeFilter filter = ProjectGenerationConfigurationTypeFilter.include(A.class,
33+
B.class);
34+
assertThat(filter).accepts(A.class, B.class);
35+
assertThat(filter).rejects(C.class);
36+
}
37+
38+
@Test
39+
void exclude() {
40+
ProjectGenerationConfigurationTypeFilter filter = ProjectGenerationConfigurationTypeFilter.exclude(A.class,
41+
B.class);
42+
assertThat(filter).rejects(A.class, B.class);
43+
assertThat(filter).accepts(C.class);
44+
}
45+
46+
@Test
47+
void allMatch() {
48+
ProjectGenerationConfigurationTypeFilter filterA = (clazz) -> clazz.equals(A.class);
49+
ProjectGenerationConfigurationTypeFilter filterAorB = (clazz) -> clazz.equals(A.class) || clazz.equals(B.class);
50+
ProjectGenerationConfigurationTypeFilter filterNotC = (clazz) -> !clazz.equals(C.class);
51+
ProjectGenerationConfigurationTypeFilter combined = ProjectGenerationConfigurationTypeFilter.allMatch(filterA,
52+
filterAorB, filterNotC);
53+
assertThat(combined).accepts(A.class);
54+
assertThat(combined).rejects(B.class, C.class);
55+
}
56+
57+
private static final class A {
58+
59+
}
60+
61+
private static final class B {
62+
63+
}
64+
65+
private static final class C {
66+
67+
}
68+
69+
}

initializr-generator/src/test/java/io/spring/initializr/generator/project/ProjectGeneratorTests.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
2727
import io.spring.initializr.generator.project.contributor.TestProjectGenerationConfiguration;
28+
import io.spring.initializr.generator.project.contributor.TestProjectGenerationConfiguration2;
29+
import org.assertj.core.util.Lists;
2830
import org.junit.jupiter.api.Test;
2931
import org.junit.jupiter.api.io.TempDir;
3032
import org.mockito.InOrder;
@@ -181,8 +183,9 @@ void generateCanBeExtendedToFilterProjectContributors(@TempDir Path projectDir)
181183
given(description.getBuildSystem()).willReturn(new MavenBuildSystem());
182184
ProjectGenerator generator = new ProjectGenerator(mockContextInitializr()) {
183185
@Override
184-
protected List<String> getCandidateProjectGenerationConfigurations(ProjectDescription description) {
185-
assertThat(description).isSameAs(description);
186+
protected List<String> getCandidateProjectGenerationConfigurations(
187+
ProjectDescription generatorDescription) {
188+
assertThat(description).isSameAs(generatorDescription);
186189
return Collections.singletonList(TestProjectGenerationConfiguration.class.getName());
187190
}
188191
};
@@ -195,6 +198,37 @@ protected List<String> getCandidateProjectGenerationConfigurations(ProjectDescri
195198
verify(description).getBuildSystem();
196199
}
197200

201+
@Test
202+
void loadAndConstructProjectGenerationTypeExclusionFilter() {
203+
ProjectGenerator generator = new ProjectGenerator(mockContextInitializr());
204+
ProjectGenerationConfigurationTypeFilter filter = generator.getProjectGenerationConfigurationExclusionFilter();
205+
assertThat(filter).isNotNull();
206+
assertThat(filter.test(TestProjectGenerationConfiguration.class)).isFalse();
207+
assertThat(filter.test(TestProjectGenerationConfiguration2.class)).isFalse();
208+
assertThat(filter.test(Integer.class)).isTrue();
209+
}
210+
211+
@Test
212+
void filterProjectContributorsCorrectly() {
213+
ProjectDescription description = mock(ProjectDescription.class);
214+
given(description.getArtifactId()).willReturn("test-custom-contributor");
215+
given(description.getBuildSystem()).willReturn(new MavenBuildSystem());
216+
ProjectGenerator generator = new ProjectGenerator(mockContextInitializr()) {
217+
@Override
218+
List<String> getProjectGenerationConfigurationFactoryNames() {
219+
return Lists.list(TestProjectGenerationConfiguration.class.getName(),
220+
TestProjectGenerationConfiguration2.class.getName());
221+
}
222+
223+
@Override
224+
ProjectGenerationConfigurationTypeFilter getProjectGenerationConfigurationExclusionFilter() {
225+
return (clazz) -> !TestProjectGenerationConfiguration2.class.equals(clazz);
226+
}
227+
};
228+
List<String> candidates = generator.getCandidateProjectGenerationConfigurations(description);
229+
assertThat(candidates).containsOnly(TestProjectGenerationConfiguration.class.getCanonicalName());
230+
}
231+
198232
@SuppressWarnings("unchecked")
199233
private Consumer<ProjectGenerationContext> mockContextInitializr() {
200234
return mock(Consumer.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2012-2020 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 io.spring.initializr.generator.project.contributor;
18+
19+
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
20+
21+
/**
22+
* Test contributor.
23+
*/
24+
@ProjectGenerationConfiguration
25+
public class TestProjectGenerationConfiguration2 {
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2020 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 io.spring.initializr.generator.project.contributor;
18+
19+
import io.spring.initializr.generator.project.ProjectGenerationConfigurationTypeFilter;
20+
21+
public class TestProjectGenerationConfiguration2ExcludingTypeFilter
22+
implements ProjectGenerationConfigurationTypeFilter {
23+
24+
private final ProjectGenerationConfigurationTypeFilter delegate = ProjectGenerationConfigurationTypeFilter
25+
.exclude(TestProjectGenerationConfiguration2.class);
26+
27+
@Override
28+
public boolean test(Class<?> type) {
29+
return this.delegate.test(type);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2020 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 io.spring.initializr.generator.project.contributor;
18+
19+
import io.spring.initializr.generator.project.ProjectGenerationConfigurationTypeFilter;
20+
21+
public class TestProjectGenerationConfigurationExcludingTypeFilter implements ProjectGenerationConfigurationTypeFilter {
22+
23+
private final ProjectGenerationConfigurationTypeFilter delegate = ProjectGenerationConfigurationTypeFilter
24+
.exclude(TestProjectGenerationConfiguration.class);
25+
26+
@Override
27+
public boolean test(Class<?> type) {
28+
return this.delegate.test(type);
29+
}
30+
31+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
io.spring.initializr.generator.project.ProjectGenerationConfigurationTypeFilter=\
2+
io.spring.initializr.generator.project.contributor.TestProjectGenerationConfigurationExcludingTypeFilter,\
3+
io.spring.initializr.generator.project.contributor.TestProjectGenerationConfiguration2ExcludingTypeFilter
4+

0 commit comments

Comments
 (0)