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

org.infinispan.cdi.common.util.BeanBuilder Maven / Gradle / Ivy

package org.infinispan.cdi.common.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Alternative;
import jakarta.enterprise.inject.spi.AnnotatedType;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.InjectionPoint;
import jakarta.enterprise.inject.spi.InjectionTarget;
import jakarta.enterprise.inject.spi.PassivationCapable;
import jakarta.inject.Named;

/**
 * 

* A builder class for creating immutable beans. The builder can create * {@link PassivationCapable} beans, using * {@link Annotateds#createTypeId(AnnotatedType)} to generate the id. *

*

*

* 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. *

* * @author Stuart Douglas * @author Pete Muir * @see ImmutableBean * @see ImmutablePassivationCapableBean */ 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; 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; this.types = new HashSet(); } /** *

* 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>(); 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(DefaultLiteral.INSTANCE); } qualifiers.add(AnyLiteral.INSTANCE); this.id = ImmutableBean.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, beanLifecycle, toString); } else { return new ImmutablePassivationCapableBean(id, beanClass, name, qualifiers, scope, stereotypes, types, alternative, nullable, injectionPoints, beanLifecycle, toString); } } /** * 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 = Arrays2.asSet(qualifiers); return this; } /** * Add to the qualifiers used for bean creation. * * @param qualifiers 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(Arrays2.asSet(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 = Arrays2.asSet(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(Arrays2.asSet(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 nullable 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