Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove exports limitation from Module Path Detection #720

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;

Expand Down Expand Up @@ -117,7 +118,7 @@ static void registerPluginProvidedTypes(ScopeInfo defaultScope) {
if (!INJECT_AVAILABLE) {
if (!pluginExists("avaje-module-dependencies.csv")) {
APContext.logNote(
"Unable to detect Avaje Inject in Annotation Processor ClassPath, use the Avaje Inject Maven/Gradle plugin for detecting Inject Plugins from dependencies");
"Unable to detect Avaje Inject in Annotation Processor ClassPath, use the Avaje Inject Maven/Gradle plugin for detecting Inject Plugins from dependencies");
}
return;
}
Expand Down Expand Up @@ -171,28 +172,38 @@ static void scanTheWorld(Collection<String> providedTypes) {
if (!externalMeta.isEmpty()) {
return;
}
var allModules =
APContext.elements().getAllModuleElements().stream()
.filter(m -> !m.getQualifiedName().toString().startsWith("java"))
.filter(m -> !m.getQualifiedName().toString().startsWith("jdk"))
// for whatever reason, compilation breaks if we don't filter out the current module
.filter(m -> m != APContext.getProjectModuleElement())
.collect(toList());

var types = APContext.types();
var spi = APContext.typeElement("io.avaje.inject.spi.AvajeModule").asType();
APContext.elements().getAllModuleElements().stream()
.filter(m -> !m.getQualifiedName().toString().startsWith("java"))
.filter(m -> !m.getQualifiedName().toString().startsWith("jdk"))
// for whatever reason, compilation breaks if we don't filter out the current module
.filter(m -> m != APContext.getProjectModuleElement())
.flatMap(m -> m.getEnclosedElements().stream())
.flatMap(p -> p.getEnclosedElements().stream())
.map(TypeElement.class::cast)
.filter(t -> t.getKind() == ElementKind.CLASS)
.filter(t -> t.getModifiers().contains(Modifier.PUBLIC))
final var checkEnclosing =
allModules.stream()
.flatMap(m -> m.getEnclosedElements().stream())
.flatMap(p -> p.getEnclosedElements().stream())
.map(TypeElement.class::cast)
.filter(t -> t.getKind() == ElementKind.CLASS)
.filter(t -> t.getModifiers().contains(Modifier.PUBLIC));

final var checkDirectives =
allModules.stream()
.flatMap(m -> ElementFilter.providesIn(m.getDirectives()).stream())
.filter(ExternalProvider::isInjectExtension)
.flatMap(p -> p.getImplementations().stream());

Stream.concat(checkEnclosing, checkDirectives)
.filter(t -> t.getInterfaces().stream().anyMatch(i -> types.isAssignable(i, spi)))
.distinct()
.forEach(t -> {
final var provides = new HashSet<String>();
final var requires = new HashSet<String>();
Optional.of(t)
.map(TypeElement::getEnclosedElements)
.map(ElementFilter::methodsIn)
.stream()
.flatMap(List::stream)

ElementFilter.methodsIn(t.getEnclosedElements()).stream()
.map(DependencyMetaPrism::getInstanceOn)
.filter(Objects::nonNull)
.map(MetaData::new)
Expand All @@ -219,4 +230,8 @@ static void scanTheWorld(Collection<String> providedTypes) {
APContext.logNote("No external modules detected");
}
}

private static boolean isInjectExtension(ModuleElement.ProvidesDirective p) {
return "io.avaje.inject.spi.InjectExtension".equals(p.getService().getQualifiedName().toString());
}
}