Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package javax.enterprise.inject.spi;
  

Helper class for injecting and calling lifecycle callbacks unmanaged instances for use by framework and library integrators.

 Unmanaged<Foo> unmanagedFoo = new Unmanaged<Foo>(Foo.class);
 UnmanagedInstance<Foo> fooInstance = unmanagedFoo.newInstance();
 Foo foo = fooInstance.produce().inject().postConstruct().get();
 ... // Use the foo instance
 fooInstance.preDestroy().dispose();
 

An instance of this class can be safely held for the lifetime of the application.

Unmanaged.UnmanagedInstances created by this class are not suitable for sharing between threads.

Author(s):
Pete Muir
Since:
1.1
 
 public class Unmanaged<T> {
 
     private final InjectionTarget<T> injectionTarget;
     private final BeanManager beanManager;

    
Create an injector for the given class
 
     public Unmanaged(BeanManager managerClass<T> clazz) {
         this. = manager;
         AnnotatedType<T> type = manager.createAnnotatedType(clazz);
         this. = manager.getInjectionTargetFactory(type).createInjectionTarget(null);
     }

    
Create an injector for the given class, using the current bean manager
 
     public Unmanaged(Class<T> clazz) {
         this(CDI.current().getBeanManager(), clazz);
     }
 
     public UnmanagedInstance<T> newInstance() {
         return new UnmanagedInstance<T>();
     }

    
Represents a non-contextual instance.

See also:
Unmanaged
 
     public static class UnmanagedInstance<T> {
 
         private final CreationalContext<T> ctx;
         private final InjectionTarget<T> injectionTarget;
         private T instance;
         private boolean disposed = false;
 
         private UnmanagedInstance(BeanManager beanManagerInjectionTarget<T> injectionTarget) {
             this. = injectionTarget;
             this. = beanManager.createCreationalContext(null);
         }

        
Get the instance
 
         public T get() {
             return ;
         }

        
Create the instance

Throws:
java.lang.IllegalStateException if produce() is called on an already produced instance
java.lang.IllegalStateException if produce() is called on an instance that has already been disposed
 
         public UnmanagedInstance<T> produce() {
             if (this. != null) {
                 throw new IllegalStateException("Trying to call produce() on already constructed instance");
             }
             if () {
                 throw new IllegalStateException("Trying to call produce() on an already disposed instance");
             }
             this. = .produce();
             return this;
         }

        
Inject the instance

Throws:
java.lang.IllegalStateException if inject() is called before produce() is called
java.lang.IllegalStateException if inject() is called on an instance that has already been disposed
        public UnmanagedInstance<T> inject() {
            if (this. == null) {
                throw new IllegalStateException("Trying to call inject() before produce() was called");
            }
            if () {
                throw new IllegalStateException("Trying to call inject() on already disposed instance");
            }
            .inject();
            return this;
        }

        
Call the

Throws:
java.lang.IllegalStateException if postConstruct() is called before produce() is called
java.lang.IllegalStateException if postConstruct() is called on an instance that has already been disposed
PostConstruct:
callback
        public UnmanagedInstance<T> postConstruct() {
            if (this. == null) {
                throw new IllegalStateException("Trying to call postConstruct() before produce() was called");
            }
            if () {
                throw new IllegalStateException("Trying to call postConstruct() on already disposed instance");
            }
            .postConstruct();
            return this;
        }

        
Call the

Throws:
java.lang.IllegalStateException if preDestroy() is called before produce() is called
java.lang.IllegalStateException if preDestroy() is called on an instance that has already been disposed
PreDestroy:
callback
        public UnmanagedInstance<T> preDestroy() {
            if (this. == null) {
                throw new IllegalStateException("Trying to call preDestroy() before produce() was called");
            }
            if () {
                throw new IllegalStateException("Trying to call preDestroy() on already disposed instance");
            }
            .preDestroy();
            return this;
        }

        
Dispose of the instance, doing any necessary cleanup

Throws:
java.lang.IllegalStateException if dispose() is called before produce() is called
java.lang.IllegalStateException if dispose() is called on an instance that has already been disposed
        public UnmanagedInstance<T> dispose() {
            if (this. == null) {
                throw new IllegalStateException("Trying to call dispose() before produce() was called");
            }
            if () {
                throw new IllegalStateException("Trying to call dispose() on already disposed instance");
            }
            .dispose();
            .release();
            return this;
        }
    }
New to GrepCode? Check out our FAQ X