Merge pull request #27 from mkouba/implicit-bean-archives_2

Modify BeanArchiveProcessor so that implicit bean archives are added to
This commit is contained in:
Stuart Douglas
2018-09-20 07:46:06 +10:00
committed by GitHub

View File

@@ -1,11 +1,17 @@
package org.jboss.shamrock.deployment;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget.Kind;
import org.jboss.jandex.CompositeIndex;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
public class BeanArchiveProcessor implements ResourceProcessor {
@@ -15,14 +21,55 @@ public class BeanArchiveProcessor implements ResourceProcessor {
@Override
public void process(ArchiveContext archiveContext, ProcessorContext processorContext) throws Exception {
Set<ApplicationArchive> archives = archiveContext.getAllApplicationArchives();
// The list is not exhaustive - it merely contains all annotations supported by Arc
List<DotName> beanDefiningAnnotations = new ArrayList<>();
beanDefiningAnnotations.add(DotName.createSimple("javax.enterprise.context.Dependent"));
beanDefiningAnnotations.add(DotName.createSimple("javax.enterprise.context.RequestScoped"));
beanDefiningAnnotations.add(DotName.createSimple("javax.enterprise.context.ApplicationScoped"));
beanDefiningAnnotations.add(DotName.createSimple("javax.interceptor.Interceptor"));
// TODO: we could also add @Inject and @Singleton although these are not officialy included
// First find annotations annotated with "meta" bean defining annotations
List<DotName> metaBeanDefiningAnnotations = Collections.singletonList(DotName.createSimple("javax.enterprise.inject.Stereotype"));
for (ApplicationArchive archive : archives) {
for (DotName metaAnnotation : metaBeanDefiningAnnotations) {
Collection<AnnotationInstance> annotations = archive.getIndex().getAnnotations(metaAnnotation);
if (!annotations.isEmpty()) {
for (AnnotationInstance annotationInstance : annotations) {
if (annotationInstance.target().kind() == Kind.CLASS) {
beanDefiningAnnotations.add(annotationInstance.target().asClass().name());
}
}
}
}
}
DotName extensionName = DotName.createSimple("javax.enterprise.inject.spi.Extension");
List<IndexView> indexes = new ArrayList<>();
for(ApplicationArchive archive : archiveContext.getAllApplicationArchives()) {
//TODO: this should not really be in core
if(archive.getChildPath("META-INF/beans.xml") != null) {
indexes.add(archive.getIndex());
} else if(archive.getChildPath("WEB-INF/beans.xml") != null) {
//TODO: how to handle WEB-INF?
indexes.add(archive.getIndex());
for (ApplicationArchive archive : archives) {
IndexView index = archive.getIndex();
// TODO: this should not really be in core
if (archive.getChildPath("META-INF/beans.xml") != null) {
indexes.add(index);
} else if (archive.getChildPath("WEB-INF/beans.xml") != null) {
// TODO: how to handle WEB-INF?
indexes.add(index);
} else {
// Implicit bean archive without beans.xml - contains one or more bean classes with a bean defining annotation and no extension
if (index.getAllKnownImplementors(extensionName).isEmpty()) {
for (DotName beanDefiningAnnotation : beanDefiningAnnotations) {
if (!index.getAnnotations(beanDefiningAnnotation).isEmpty()) {
indexes.add(index);
break;
}
}
}
}
}
beanArchiveIndex.setIndex(CompositeIndex.create(indexes));
@@ -30,7 +77,7 @@ public class BeanArchiveProcessor implements ResourceProcessor {
@Override
public int getPriority() {
//we want this to run early
// we want this to run early
return -1000;
}
}