mirror of https://github.com/apache/maven.git
[MNG-8220] Fix loading DI-powered beans from extensions (#1683)
This commit is contained in:
parent
acec540547
commit
40fe1dc167
|
@ -55,13 +55,16 @@ import org.codehaus.plexus.PlexusContainer;
|
|||
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
|
||||
|
||||
@Named
|
||||
class SisuDiBridgeModule extends AbstractModule {
|
||||
public class SisuDiBridgeModule extends AbstractModule {
|
||||
|
||||
InjectorImpl injector;
|
||||
final Set<String> loaded = new HashSet<>();
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
Provider<PlexusContainer> containerProvider = getProvider(PlexusContainer.class);
|
||||
|
||||
InjectorImpl injector = new InjectorImpl() {
|
||||
injector = new InjectorImpl() {
|
||||
@Override
|
||||
public <Q> Supplier<Q> getCompiledBinding(Key<Q> key) {
|
||||
Set<Binding<Q>> res = getBindings(key);
|
||||
|
@ -142,19 +145,45 @@ class SisuDiBridgeModule extends AbstractModule {
|
|||
});
|
||||
injector.bindInstance(Injector.class, injector);
|
||||
bind(Injector.class).toInstance(injector);
|
||||
bind(SisuDiBridgeModule.class).toInstance(this);
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
if (classLoader == null) {
|
||||
classLoader = getClass().getClassLoader();
|
||||
}
|
||||
loadFromClassLoader(classLoader);
|
||||
injector.getBindings().keySet().stream()
|
||||
.filter(k -> k.getQualifier() != null)
|
||||
.sorted(Comparator.comparing(k -> k.getRawType().getName()))
|
||||
.distinct()
|
||||
.forEach(key -> {
|
||||
Class<?> clazz = key.getRawType();
|
||||
Class<Object> itf = (clazz.isInterface()
|
||||
? null
|
||||
: (Class<Object>) (clazz.getInterfaces().length > 0 ? clazz.getInterfaces()[0] : clazz));
|
||||
if (itf != null) {
|
||||
AnnotatedBindingBuilder<Object> binder = bind(itf);
|
||||
if (key.getQualifier() instanceof String s && !s.isEmpty()) {
|
||||
binder.annotatedWith(Names.named(s));
|
||||
} else if (key.getQualifier() instanceof Annotation a) {
|
||||
binder.annotatedWith(a);
|
||||
}
|
||||
binder.toProvider(() -> injector.getInstance(clazz));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void loadFromClassLoader(ClassLoader classLoader) {
|
||||
try {
|
||||
for (Iterator<URL> it = classLoader
|
||||
.getResources("META-INF/maven/org.apache.maven.api.di.Inject")
|
||||
.asIterator();
|
||||
it.hasNext(); ) {
|
||||
URL url = it.next();
|
||||
if (loaded.add(url.toExternalForm())) {
|
||||
List<String> lines;
|
||||
try (InputStream is = url.openStream();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
|
||||
BufferedReader reader =
|
||||
new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
|
||||
lines = reader.lines()
|
||||
.map(String::trim)
|
||||
.filter(s -> !s.isEmpty() && !s.startsWith("#"))
|
||||
|
@ -170,28 +199,9 @@ class SisuDiBridgeModule extends AbstractModule {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new MavenException(e);
|
||||
}
|
||||
injector.getBindings().keySet().stream()
|
||||
.filter(k -> k.getQualifier() != null)
|
||||
.sorted(Comparator.comparing(k -> k.getRawType().getName()))
|
||||
.distinct()
|
||||
.forEach(key -> {
|
||||
Class<?> clazz = key.getRawType();
|
||||
Class<Object> itf = (clazz.isInterface()
|
||||
? null
|
||||
: (Class<Object>) (clazz.getInterfaces().length > 0 ? clazz.getInterfaces()[0] : clazz));
|
||||
if (itf != null) {
|
||||
AnnotatedBindingBuilder<Object> binder = bind(itf);
|
||||
if (key.getQualifier() instanceof String s) {
|
||||
binder.annotatedWith(Names.named(s));
|
||||
} else if (key.getQualifier() instanceof Annotation a) {
|
||||
binder.annotatedWith(a);
|
||||
}
|
||||
binder.toProvider(() -> injector.getInstance(clazz));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -138,7 +138,8 @@ public class SessionScope implements Scope, org.apache.maven.di.Scope {
|
|||
} else {
|
||||
for (Annotation a : superType.getAnnotations()) {
|
||||
Class<? extends Annotation> annotationType = a.annotationType();
|
||||
if ("org.eclipse.sisu.Typed".equals(annotationType.getName())
|
||||
if ("org.apache.maven.api.di.Typed".equals(annotationType.getName())
|
||||
|| "org.eclipse.sisu.Typed".equals(annotationType.getName())
|
||||
|| "javax.enterprise.inject.Typed".equals(annotationType.getName())
|
||||
|| "jakarta.enterprise.inject.Typed".equals(annotationType.getName())) {
|
||||
try {
|
||||
|
|
|
@ -128,8 +128,15 @@ public abstract class Key<T> {
|
|||
* and prepended qualifier display string if this key has a qualifier.
|
||||
*/
|
||||
public String getDisplayString() {
|
||||
return (qualifier != null ? Utils.getDisplayString(qualifier) + " " : "")
|
||||
+ ReflectionUtils.getDisplayName(type);
|
||||
return (qualifier != null ? getQualifierDisplayString() + " " : "") + ReflectionUtils.getDisplayName(type);
|
||||
}
|
||||
|
||||
private String getQualifierDisplayString() {
|
||||
if (qualifier instanceof String s) {
|
||||
return s.isEmpty() ? "@Named" : "@Named(\"" + s + "\")";
|
||||
}
|
||||
String s = Utils.getDisplayString(qualifier);
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -155,6 +162,6 @@ public abstract class Key<T> {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return (qualifier != null ? qualifier + " " : "") + type.getTypeName();
|
||||
return getDisplayString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -137,6 +137,7 @@ public class InjectorImpl implements Injector {
|
|||
current.add(key);
|
||||
throw new DIException("Circular references: " + current);
|
||||
}
|
||||
try {
|
||||
doBindImplicit(key, binding);
|
||||
Class<?> cls = key.getRawType().getSuperclass();
|
||||
while (cls != Object.class && cls != null) {
|
||||
|
@ -144,8 +145,10 @@ public class InjectorImpl implements Injector {
|
|||
doBindImplicit(key, binding);
|
||||
cls = cls.getSuperclass();
|
||||
}
|
||||
current.remove(key);
|
||||
return this;
|
||||
} finally {
|
||||
current.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
protected <U> Injector bind(Key<U> key, Binding<U> b) {
|
||||
|
|
|
@ -95,6 +95,7 @@ import org.apache.maven.execution.scope.internal.MojoExecutionScope;
|
|||
import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule;
|
||||
import org.apache.maven.extension.internal.CoreExports;
|
||||
import org.apache.maven.extension.internal.CoreExtensionEntry;
|
||||
import org.apache.maven.internal.impl.SisuDiBridgeModule;
|
||||
import org.apache.maven.jline.JLineMessageBuilderFactory;
|
||||
import org.apache.maven.jline.MessageUtils;
|
||||
import org.apache.maven.lifecycle.LifecycleExecutionException;
|
||||
|
@ -715,6 +716,7 @@ public class MavenCli {
|
|||
new SessionScopeModule(container.lookup(SessionScope.class)),
|
||||
new MojoExecutionScopeModule(container.lookup(MojoExecutionScope.class)),
|
||||
new ExtensionConfigurationModule(extension, extensionSource));
|
||||
container.lookup(SisuDiBridgeModule.class).loadFromClassLoader(extension.getClassRealm());
|
||||
}
|
||||
|
||||
customizeContainer(container);
|
||||
|
|
Loading…
Reference in New Issue