All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ocpsoft.rewrite.cdi.CdiServiceLocator Maven / Gradle / Ivy

There is a newer version: 10.0.2.Final
Show newest version
/*
 * Copyright 2011 Lincoln Baxter, III
 * 
 * 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.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
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;
import org.ocpsoft.rewrite.cdi.util.ParameterizedTypeImpl;
import org.ocpsoft.rewrite.cdi.util.WildcardTypeImpl;

/**
 * @author Lincoln Baxter, III
 * @author Christian Kaltepoth
 */
public class CdiServiceLocator extends BeanManagerAware implements ServiceLocator
{

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

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

         BeanManager manager = getBeanManager();

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

         for (Bean bean : beans) {
            result.add((Class) bean.getBeanClass());
         }

      }

      return result;

   }

   /**
    * Builds the correct "required type" including actual type arguments in case of parameterized types.
    * 
    * @see https://github.com/ocpsoft/rewrite/issues/137
    */
   private Type getRequiredType(Class clazz)
   {

      TypeVariable[] typeParameters = clazz.getTypeParameters();

      if (typeParameters.length > 0) {

         Type[] actualTypeParameters = new Type[typeParameters.length];
         for (int i = 0; i < typeParameters.length; i++) {
            actualTypeParameters[i] = new WildcardTypeImpl(new Type[] { Object.class }, new Type[] {});
         }

         return new ParameterizedTypeImpl(clazz, actualTypeParameters, null);

      }

      return clazz;
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy