/*
 * Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
 * 
 * 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.ocpsoft.rewrite.cdi;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.enterprise.inject.Any;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.util.AnnotationLiteral;

import org.ocpsoft.common.spi.ServiceLocator;
import org.ocpsoft.rewrite.cdi.manager.BeanManagerAware;

/**
 * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
 * @author <a href="mailto:christian@kaltepoth.de">Christian Kaltepoth</a>
 */
public class CdiServiceLocator extends BeanManagerAware implements ServiceLocator
{

   @Override
   @SuppressWarnings("unchecked")
   public <T> Collection<Class<T>> locate(final Class<T> type)
   {
      List<Class<T>> result = new ArrayList<Class<T>>();

      // the BeanManager may be not available during Rewrite startup
      if (isBeanManagerAvailable()) {

         BeanManager manager = getBeanManager();

         Set<Bean<?>> beans = manager.getBeans(type, new Annotation[] { new AnnotationLiteral<Any>() {
            private static final long serialVersionUID = -1896831901770051851L;
         } });

         for (Bean<?> bean : beans) {
            result.add((Class<T>) bean.getBeanClass());
         }

      }

      return result;

   }

}

@@ -714,24 +714,94 @@
 Annotation;%0A
+import java.lang.reflect.Type;%0Aimport java.lang.reflect.TypeVariable;%0A
 import java.
@@ -812,24 +812,24 @@
 .ArrayList;%0A
-
 import java.
@@ -1167,16 +1167,75 @@
 erAware;
+%0Aimport org.ocpsoft.rewrite.cdi.util.ParameterizedTypeImpl;
 %0A%0A/**%0A *
@@ -1850,20 +1850,37 @@
 etBeans(
+getRequiredType(
 type
+)
 , new An
@@ -2162,10 +2162,665 @@
 %0A%0A   %7D%0A%0A
+   /**%0A    * Builds the correct %22required type%22 including actual type arguments in case of parameterized types.%0A    * %0A    * @see https://github.com/ocpsoft/rewrite/issues/137%0A    */%0A   private Type getRequiredType(Class%3C?%3E clazz)%0A   %7B%0A%0A      TypeVariable%3C?%3E%5B%5D typeParameters = clazz.getTypeParameters();%0A%0A      if (typeParameters.length %3E 0) %7B%0A%0A         Type%5B%5D actualTypeParameters = new Type%5BtypeParameters.length%5D;%0A         for (int i = 0; i %3C typeParameters.length; i++) %7B%0A            actualTypeParameters%5Bi%5D = Object.class;%0A         %7D%0A%0A         return new ParameterizedTypeImpl(clazz, actualTypeParameters, null);%0A%0A      %7D%0A%0A      return clazz;%0A   %7D%0A%0A
 %7D%0A

Rewrite CDI integration doesn't work with OpenWebBeans (#137)