org.gradle.initialization.GradleApiSpecAggregator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2016 the original author or authors.
*
* Licensed 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.gradle.initialization;
import com.google.common.collect.ImmutableSet;
import org.gradle.initialization.GradleApiSpecProvider.Spec;
import org.gradle.internal.reflect.Instantiator;
import org.gradle.internal.service.DefaultServiceLocator;
import java.util.List;
import java.util.Set;
class GradleApiSpecAggregator {
private final ClassLoader classLoader;
private final Instantiator instantiator;
public GradleApiSpecAggregator(ClassLoader classLoader, Instantiator instantiator) {
this.classLoader = classLoader;
this.instantiator = instantiator;
}
public Spec aggregate() {
List> providers = providers();
if (providers.isEmpty()) {
return emptySpec();
}
if (providers.size() == 1) {
return specFrom(providers.get(0));
}
return mergeSpecsOf(providers);
}
private GradleApiSpecProvider.SpecAdapter emptySpec() {
return new GradleApiSpecProvider.SpecAdapter();
}
private Spec specFrom(Class extends GradleApiSpecProvider> provider) {
return instantiate(provider).get();
}
private Spec mergeSpecsOf(List> providers) {
final ImmutableSet.Builder> exportedClasses = ImmutableSet.builder();
final ImmutableSet.Builder exportedPackages = ImmutableSet.builder();
final ImmutableSet.Builder exportedResources = ImmutableSet.builder();
final ImmutableSet.Builder exportedResourcePrefixes = ImmutableSet.builder();
final ImmutableSet.Builder unexportedPackages = ImmutableSet.builder();
for (Class extends GradleApiSpecProvider> provider : providers) {
Spec spec = specFrom(provider);
exportedClasses.addAll(spec.getExportedClasses());
exportedPackages.addAll(spec.getExportedPackages());
exportedResources.addAll(spec.getExportedResources());
exportedResourcePrefixes.addAll(spec.getExportedResourcePrefixes());
unexportedPackages.addAll(spec.getUnexportedPackages());
}
return new DefaultSpec(
exportedClasses.build(),
exportedPackages.build(),
exportedResources.build(),
exportedResourcePrefixes.build(),
unexportedPackages.build()
);
}
private List> providers() {
return new DefaultServiceLocator(classLoader).implementationsOf(GradleApiSpecProvider.class);
}
private GradleApiSpecProvider instantiate(Class extends GradleApiSpecProvider> providerClass) {
return instantiator.newInstance(providerClass);
}
static class DefaultSpec implements Spec {
private final ImmutableSet> exportedClasses;
private final ImmutableSet exportedPackages;
private final ImmutableSet exportedResources;
private final ImmutableSet exportedResourcePrefixes;
private final ImmutableSet unexportedPackages;
DefaultSpec(
ImmutableSet> exportedClasses,
ImmutableSet exportedPackages,
ImmutableSet exportedResources,
ImmutableSet exportedResourcePrefixes,
ImmutableSet unexportedPackages
) {
this.exportedClasses = exportedClasses;
this.exportedPackages = exportedPackages;
this.exportedResources = exportedResources;
this.exportedResourcePrefixes = exportedResourcePrefixes;
this.unexportedPackages = unexportedPackages;
}
@Override
public Set> getExportedClasses() {
return exportedClasses;
}
@Override
public Set getExportedPackages() {
return exportedPackages;
}
@Override
public Set getExportedResources() {
return exportedResources;
}
@Override
public Set getExportedResourcePrefixes() {
return exportedResourcePrefixes;
}
@Override
public ImmutableSet getUnexportedPackages() {
return unexportedPackages;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy