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

org.apache.deltaspike.core.util.bean.BeanBuilder Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.apache.deltaspike.core.util.bean;

import org.apache.deltaspike.core.api.literal.AnyLiteral;
import org.apache.deltaspike.core.api.literal.DefaultLiteral;
import org.apache.deltaspike.core.util.Annotateds;
import org.apache.deltaspike.core.util.metadata.builder.ContextualLifecycle;
import org.apache.deltaspike.core.util.metadata.builder.DelegatingContextualLifecycle;
import org.apache.deltaspike.core.util.metadata.builder.DummyInjectionTarget;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.inject.Named;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 

* A builder class for creating immutable beans. The builder can create * {@link javax.enterprise.inject.spi.PassivationCapable} beans. *

*

*

* The builder can read from an {@link AnnotatedType} and have any attribute * modified. This class is not thread-safe, but the bean created by calling * {@link #create()} is. *

*

*

* It is advised that a new bean builder is instantiated for each bean created. *

*/ // X TODO: Add tests public class BeanBuilder { private final BeanManager beanManager; private Class beanClass; private String name; private Set qualifiers; private Class scope; private Set> stereotypes; private Set types; private Set injectionPoints; private boolean alternative; private boolean nullable; private ContextualLifecycle beanLifecycle; private boolean passivationCapable; private String id; private String toString; /** * Instantiate a new bean builder. * * @param beanManager the bean manager to use for creating injection targets * and determining if annotations are qualifiers, scopes or * stereotypes. */ public BeanBuilder(BeanManager beanManager) { this.beanManager = beanManager; } /** *

* Read the {@link AnnotatedType}, creating a bean from the class and it's * annotations. *

*

*

* By default the bean lifecycle will wrap the result of calling * {@link BeanManager#createInjectionTarget(AnnotatedType)}. *

*

*

* {@link BeanBuilder} does not support reading members of the class * to create producers or observer methods. *

* * @param type the type to read */ public BeanBuilder readFromType(AnnotatedType type) { this.beanClass = type.getJavaClass(); InjectionTarget injectionTarget; if (!type.getJavaClass().isInterface()) { injectionTarget = beanManager.createInjectionTarget(type); } else { injectionTarget = new DummyInjectionTarget(); } this.beanLifecycle = new DelegatingContextualLifecycle(injectionTarget); this.injectionPoints = injectionTarget.getInjectionPoints(); this.qualifiers = new HashSet(); this.stereotypes = new HashSet>(); this.types = new HashSet(); for (Annotation annotation : type.getAnnotations()) { if (beanManager.isQualifier(annotation.annotationType())) { this.qualifiers.add(annotation); } else if (beanManager.isScope(annotation.annotationType())) { this.scope = annotation.annotationType(); } else if (beanManager.isStereotype(annotation.annotationType())) { this.stereotypes.add(annotation.annotationType()); } if (annotation instanceof Named) { this.name = ((Named) annotation).value(); } if (annotation instanceof Alternative) { this.alternative = true; } } if (this.scope == null) { this.scope = Dependent.class; } for (Class c = type.getJavaClass(); c != Object.class && c != null; c = c.getSuperclass()) { this.types.add(c); } for (Class i : type.getJavaClass().getInterfaces()) { this.types.add(i); } if (qualifiers.isEmpty()) { qualifiers.add(new DefaultLiteral()); } qualifiers.add(new AnyLiteral()); this.id = ImmutableBeanWrapper.class.getName() + ":" + Annotateds.createTypeId(type); return this; } /** *

* Use the bean builder's current state to define the bean. *

* * @return the bean */ public Bean create() { if (!passivationCapable) { return new ImmutableBean(beanClass, name, qualifiers, scope, stereotypes, types, alternative, nullable, injectionPoints, toString, beanLifecycle); } else { return new ImmutablePassivationCapableBean(beanClass, name, qualifiers, scope, stereotypes, types, alternative, nullable, injectionPoints, toString, beanLifecycle, id); } } /** * Qualifiers currently defined for bean creation. * * @return the qualifiers current defined */ public Set getQualifiers() { return qualifiers; } /** * Define the qualifiers used for bean creation. * * @param qualifiers the qualifiers to use */ public BeanBuilder qualifiers(Set qualifiers) { this.qualifiers = qualifiers; return this; } /** * Define the qualifiers used for bean creation. * * @param qualifiers the qualifiers to use */ public BeanBuilder qualifiers(Annotation... qualifiers) { this.qualifiers = new HashSet(Arrays.asList(qualifiers)); return this; } /** * Add to the qualifiers used for bean creation. * * @param qualifier the additional qualifier to use */ public BeanBuilder addQualifier(Annotation qualifier) { this.qualifiers.add(qualifier); return this; } /** * Add to the qualifiers used for bean creation. * * @param qualifiers the additional qualifiers to use */ public BeanBuilder addQualifiers(Annotation... qualifiers) { this.qualifiers.addAll(new HashSet(Arrays.asList(qualifiers))); return this; } /** * Add to the qualifiers used for bean creation. * * @param qualifiers the additional qualifiers to use */ public BeanBuilder addQualifiers(Collection qualifiers) { this.qualifiers.addAll(qualifiers); return this; } /** * Scope currently defined for bean creation. * * @return the scope currently defined */ public Class getScope() { return scope; } /** * Define the scope used for bean creation. * * @param scope the scope to use */ public BeanBuilder scope(Class scope) { this.scope = scope; return this; } /** * Stereotypes currently defined for bean creation. * * @return the stereotypes currently defined */ public Set> getStereotypes() { return stereotypes; } /** * Define the stereotypes used for bean creation. * * @param stereotypes the stereotypes to use */ public BeanBuilder stereotypes(Set> stereotypes) { this.stereotypes = stereotypes; return this; } /** * Type closure currently defined for bean creation. * * @return the type closure currently defined */ public Set getTypes() { return types; } /** * Define the type closure used for bean creation. * * @param types the type closure to use */ public BeanBuilder types(Set types) { this.types = types; return this; } /** * Define the type closure used for bean creation. * * @param types the type closure to use */ public BeanBuilder types(Type... types) { this.types = new HashSet(Arrays.asList(types)); return this; } /** * Add to the type closure used for bean creation. * * @param type additional type to use */ public BeanBuilder addType(Type type) { this.types.add(type); return this; } /** * Add to the type closure used for bean creation. * * @param types the additional types to use */ public BeanBuilder addTypes(Type... types) { this.types.addAll(new HashSet(Arrays.asList(types))); return this; } /** * Add to the type closure used for bean creation. * * @param types the additional types to use */ public BeanBuilder addTypes(Collection types) { this.types.addAll(types); return this; } /** * Whether the created bean will be an alternative. * * @return true if the created bean will be an alternative, * otherwise false */ public boolean isAlternative() { return alternative; } /** * Define that the created bean will (or will not) be an alternative. * * @param alternative true if the created bean should be an * alternative, otherwise false */ public BeanBuilder alternative(boolean alternative) { this.alternative = alternative; return this; } /** * Whether the created bean will be nullable. * * @return true if the created bean will be nullable, otherwise * false */ public boolean isNullable() { return nullable; } /** * Define that the created bean will (or will not) be nullable. * * @param nullable true if the created bean should be nullable, * otherwise false */ public BeanBuilder nullable(boolean nullable) { this.nullable = nullable; return this; } /** * The {@link ContextualLifecycle} currently defined for bean creation. * * @return the bean lifecycle currently defined */ public ContextualLifecycle getBeanLifecycle() { return beanLifecycle; } /** * Define the {@link ContextualLifecycle} used for bean creation. * * @param beanLifecycle the {@link ContextualLifecycle} to use for bean * creation. */ public BeanBuilder beanLifecycle(ContextualLifecycle beanLifecycle) { this.beanLifecycle = beanLifecycle; return this; } /** * The bean class currently defined for bean creation. * * @return the bean class currently defined. */ public Class getBeanClass() { return beanClass; } /** * Define the bean class used for bean creation. * * @param beanClass the bean class to use */ public BeanBuilder beanClass(Class beanClass) { this.beanClass = beanClass; return this; } /** * The bean manager in use. This cannot be changed for this * {@link BeanBuilder}. * * @return the bean manager in use */ public BeanManager getBeanManager() { return beanManager; } /** * The name of the bean currently defined for bean creation. * * @return the name of the bean or null if the bean has no name */ public String getName() { return name; } /** * Define the name of the bean used for bean creation. * * @param name the name of the bean to use or null if the bean * should have no name */ public BeanBuilder name(String name) { this.name = name; return this; } /** * Whether the created bean will be passivation capable. * * @return true if the created bean will be passivation capable, * otherwise false */ public boolean isPassivationCapable() { return passivationCapable; } /** * Define that the created bean will (or will not) be passivation capable. * * @param passivationCapable true if the created bean should be * passivation capable, otherwise false */ public BeanBuilder passivationCapable(boolean passivationCapable) { this.passivationCapable = passivationCapable; return this; } /** * The id currently defined for bean creation. * * @return the id currently defined. */ public String getId() { return id; } /** * Define the id used for bean creation. * * @param id the id to use */ public BeanBuilder id(String id) { this.id = id; return this; } /** * The injection points currently defined for bean creation. * * @return the injection points currently defined. */ public Set getInjectionPoints() { return injectionPoints; } /** * Define the injection points used for bean creation. * * @param injectionPoints the injection points to use */ public BeanBuilder injectionPoints(Set injectionPoints) { this.injectionPoints = injectionPoints; return this; } /** * Define the string used when {@link #toString()} is called on the bean. * * @param toString the string to use */ public BeanBuilder toString(String toString) { this.toString = toString; return this; } /** * The string used when {@link #toString()} is called on the bean. * * @return the string currently defined */ public String getToString() { return toString; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy