Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved BeanManagerUtils from persistence modules
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
impl/src/main/java/org/jboss/seam/solder/beanManager/BeanManagerUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.jboss.seam.solder.beanManager; | ||
|
||
import javax.enterprise.context.spi.CreationalContext; | ||
import javax.enterprise.inject.spi.Bean; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.enterprise.inject.spi.InjectionTarget; | ||
import java.lang.annotation.Annotation; | ||
|
||
/** | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
*/ | ||
public abstract class BeanManagerUtils | ||
{ | ||
@SuppressWarnings("unchecked") | ||
public static <T> T getContextualInstance(final BeanManager manager, final Class<T> type, Annotation... qualifiers) | ||
{ | ||
T result = null; | ||
Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type, qualifiers)); | ||
if (bean != null) | ||
{ | ||
CreationalContext<T> context = manager.createCreationalContext(bean); | ||
if (context != null) | ||
{ | ||
result = (T) manager.getReference(bean, type, context); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static CreationalContext<Object> injectNonContextualInstance(final BeanManager manager, final Object instance) | ||
{ | ||
if (instance != null) | ||
{ | ||
CreationalContext<Object> creationalContext = manager.createCreationalContext(null); | ||
InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>) manager | ||
.createInjectionTarget(manager.createAnnotatedType(instance.getClass())); | ||
injectionTarget.inject(instance, creationalContext); | ||
return creationalContext; | ||
} | ||
return null; | ||
} | ||
} |