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

juzu.impl.inject.BeanDescriptor Maven / Gradle / Ivy

/*
 * Copyright 2013 eXo Platform SAS
 *
 * 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 juzu.impl.inject;

import juzu.Scope;
import juzu.impl.common.Tools;
import juzu.impl.inject.spi.Injector;

import javax.inject.Provider;
import javax.inject.Qualifier;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Describes a bean registered in an IOC container.
 * 

* The {@link #declaredType} type is the mandatory type exposed by the bean. The {@link #implementationType} type can be * optionally provided to specify the implementation type of the bean. When the implementation type implements the * {@link javax.inject.Provider} interface, the implementation.... * * @author Julien Viet */ public abstract class BeanDescriptor { /** The bean declared type. */ private final Class declaredType; /** The bean scope. */ private final Scope scope; /** The bean qualifiers. */ private final List qualifiers; /** The bean implementation type. */ private final Class implementationType; private BeanDescriptor( Class declaredType, Scope scope, List qualifiers, Class implementationType) throws NullPointerException, IllegalArgumentException { if (declaredType == null) { throw new NullPointerException("No null declared type accepted"); } if (qualifiers != null) { for (Annotation qualifier : qualifiers) { if (qualifier.annotationType().getAnnotation(Qualifier.class) == null) { throw new IllegalArgumentException("Qualifier annotation " + qualifier + " is not annotated with @Qualifier"); } } } // this.declaredType = declaredType; this.scope = scope; this.qualifiers = qualifiers; this.implementationType = implementationType; } public static BeanDescriptor createFromProviderType( final Class declaredType, Scope scope, List qualifiers, final Class> implementationType) throws NullPointerException, IllegalArgumentException { return new BeanDescriptor(declaredType, scope, qualifiers, implementationType) { @Override public void bind(Injector builder) { // Bind provider builder.declareProvider( declaredType, getScope(), getQualifiers(), implementationType); } }; } public static BeanDescriptor createFromProvider( final Class declaredType, Scope scope, List qualifiers, final Provider implementationType) throws NullPointerException, IllegalArgumentException { return new BeanDescriptor(declaredType, scope, qualifiers, null) { @Override public void bind(Injector builder) { // Bind provider builder.bindProvider( declaredType, getScope(), getQualifiers(), implementationType); } }; } public static BeanDescriptor createFromImpl( final Class declaredType, Scope scope, List qualifiers, final Class implementationType) throws NullPointerException, IllegalArgumentException { return new BeanDescriptor(declaredType, scope, qualifiers, implementationType) { @Override public void bind(Injector builder) { // Bean implementation declaration builder.declareBean( declaredType, getScope(), getQualifiers(), implementationType); } }; } public static BeanDescriptor createFromBean( final Class declaredType, Scope scope, List qualifiers) throws NullPointerException, IllegalArgumentException { // Direct declaration return new BeanDescriptor(declaredType, scope, qualifiers, null) { @Override public void bind(Injector builder) { builder.declareBean(declaredType, getScope(), getQualifiers(), null); } }; } public Class getDeclaredType() { return declaredType; } public Scope getScope() { return scope; } public List getQualifiers() { return qualifiers; } public Class getImplementationType() { return implementationType; } public abstract void bind(Injector builder); /** * Utils method that happens the qualifiers provided by the {@link javax.inject.Provider} get method to the list * of existing qualifiers * * @param qualifiers the original qualifiers * @param providerClass the provided class * @return the annotations */ public static Iterable appendProvidedQualifiers(Iterable qualifiers, Class providerClass) { try { Method getMethod = providerClass.getMethod("get"); Annotation[] annotations = getMethod.getAnnotations(); return appendQualifiers(qualifiers, annotations); } catch (NoSuchMethodException e) { // ? return qualifiers; } } /** * Utils method that happens the qualifiers of the bean class to the list * of existing qualifiers * * @param qualifiers the original qualifiers * @param beanClass the bean class * @return the annotations */ public static Iterable appendQualifiers(Iterable qualifiers, Class beanClass) { return appendQualifiers(qualifiers, beanClass.getAnnotations()); } /** * Utils method that happens the qualifiers of the bean class to the list * of existing qualifiers * * @param qualifiers the original qualifiers * @param annotations the the annotations * @return the annotations */ private static Iterable appendQualifiers(Iterable qualifiers, Annotation[] annotations) { List next = null; for (Annotation annotation : annotations) { if (annotation.annotationType().isAnnotationPresent(Qualifier.class)) { if (next == null) { next = new ArrayList(); if (qualifiers != null) { Tools.addAll(next, qualifiers); } } next.add(annotation); } } return next != null ? next : qualifiers; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy