package org.jboss.shamrock.deployment;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.jboss.jandex.CompositeIndex;
import org.jboss.jandex.IndexView;

public class BeanArchiveProcessor implements ResourceProcessor {

    @Inject
    private BeanArchiveIndex beanArchiveIndex;

    @Override
    public void process(ArchiveContext archiveContext, ProcessorContext processorContext) throws Exception {
        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());
            }
        }
        beanArchiveIndex.setIndex(CompositeIndex.create(indexes));
    }

    @Override
    public int getPriority() {
        //we want this to run early
        return -1000;
    }
}

@@ -82,43 +82,215 @@
 til.
-List;%0A%0Aimport javax.inject.Inject;%0A
+Collection;%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0Aimport javax.inject.Inject;%0A%0Aimport org.jboss.jandex.AnnotationInstance;%0Aimport org.jboss.jandex.AnnotationTarget.Kind;
 %0Aimp
@@ -326,16 +326,49 @@
 eIndex;%0A
+import org.jboss.jandex.DotName;%0A
 import o
@@ -648,58 +648,970 @@
 n %7B%0A
-        List%3CIndexView%3E indexes = new ArrayList%3C%3E(
+%0A        Set%3CApplicationArchive%3E archives = archiveContext.getAllApplicationArchives();%0A%0A        // The list is not exhaustive - it merely contains all annotations supported by Arc%0A        List%3CDotName%3E beanDefiningAnnotations = new ArrayList%3C%3E();%0A        beanDefiningAnnotations.add(DotName.createSimple(%22javax.enterprise.context.Dependent%22));%0A        beanDefiningAnnotations.add(DotName.createSimple(%22javax.enterprise.context.RequestScoped%22));%0A        beanDefiningAnnotations.add(DotName.createSimple(%22javax.enterprise.context.ApplicationScoped%22));%0A        beanDefiningAnnotations.add(DotName.createSimple(%22javax.interceptor.Interceptor%22));%0A        // TODO: we could also add @Inject and @Singleton although these are not officialy included%0A%0A        // First find annotations annotated with %22meta%22 bean defining annotations%0A        List%3CDotName%3E metaBeanDefiningAnnotations = Collections.singletonList(DotName.createSimple(%22javax.enterprise.inject.Stereotype%22)
 );%0A 
@@ -1620,16 +1620,17 @@
      for
+ 
 (Applica
@@ -1662,46 +1662,852 @@
 hive
-Context.getAllApplicationArchives()) %7B
+s) %7B%0A            for (DotName metaAnnotation : metaBeanDefiningAnnotations) %7B%0A                Collection%3CAnnotationInstance%3E annotations = archive.getIndex().getAnnotations(metaAnnotation);%0A                if (!annotations.isEmpty()) %7B%0A                    for (AnnotationInstance annotationInstance : annotations) %7B%0A                        if (annotationInstance.target().kind() == Kind.CLASS) %7B%0A                            beanDefiningAnnotations.add(annotationInstance.target().asClass().name());%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        DotName extensionName = DotName.createSimple(%22javax.enterprise.inject.spi.Extension%22);%0A%0A        List%3CIndexView%3E indexes = new ArrayList%3C%3E();%0A%0A        for (ApplicationArchive archive : archives) %7B%0A%0A            IndexView index = archive.getIndex();%0A
 %0A   
@@ -2513,24 +2513,25 @@
           //
+ 
 TODO: this s
@@ -2572,16 +2572,17 @@
       if
+ 
 (archive
@@ -2656,34 +2656,21 @@
 xes.add(
-archive.getI
+i
 ndex
-()
 );%0A     
@@ -2685,16 +2685,17 @@
  else if
+ 
 (archive
@@ -2758,16 +2758,17 @@
       //
+ 
 TODO: ho
@@ -2820,28 +2820,579 @@
 add(
-archive.getIndex());
+index);%0A            %7D else %7B%0A                // Implicit bean archive without beans.xml - contains one or more bean classes with a bean defining annotation and no extension%0A                if (index.getAllKnownImplementors(extensionName).isEmpty()) %7B%0A                    for (DotName beanDefiningAnnotation : beanDefiningAnnotations) %7B%0A                        if (!index.getAnnotations(beanDefiningAnnotation).isEmpty()) %7B%0A                            indexes.add(index);%0A                            break;%0A                        %7D%0A                    %7D%0A                %7D
 %0A   
@@ -3541,16 +3541,17 @@
       //
+ 
 we want 

Modify BeanArchiveProcessor so that implicit bean archives are added to the index