org.jboss.weld.inject.WeldInstance Maven / Gradle / Ivy
Show all versions of weld-servlet-shaded Show documentation
/*
* JBoss, Home of Professional Open Source
* Copyright 2016, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.inject;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Comparator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.AmbiguousResolutionException;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.UnsatisfiedResolutionException;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Prioritized;
import javax.enterprise.util.TypeLiteral;
/**
* Represents an enhanced version of {@link Instance}.
*
*
* In the following example we filter out beans which are not {@link Dependent} then sort the beans by priority and use the handler whose bean has the highest
* priority (according to {@link #getPriorityComparator()}) to obtain the hello string. Note that contextual references for beans with lower priority are not
* created at all.
*
*
*
* @ApplicationScoped
* class Hello {
*
* @Inject
* WeldInstance<HelloProvider> instance;
*
* String hello() {
* HelloProvider helloProvider = instance.handlersStream().filter(h -> h.getBean().getScope().equals(Dependent.class))
* .sorted(instance.getPriorityComparator()).findFirst().map(Handler::get).orElse(null);
* if (helloProvider != null)
* return helloProvider.getHello();
* return "No hello provider found!";
* }
* }
*
*
* @author Martin Kouba
* @seeIssue WELD-2204
* @param
*/
public interface WeldInstance extends Instance {
/**
* Obtains an initialized contextual reference handler for the bean that has the required type and required qualifiers and is eligible for injection.
*
*
* The contextual reference is obtained lazily, i.e. when first needed.
*
*
* @return a new handler
* @throws UnsatisfiedResolutionException
* @throws AmbiguousResolutionException
*/
Handler getHandler();
/**
* Allows to iterate over contextual reference handlers for all the beans that have the required type and required qualifiers and are eligible
* for injection.
*
*
* Note that the returned {@link Iterable} is stateless and so each {@link Iterable#iterator()} produces a new set of handlers.
*
*
* @return a new iterable
*/
Iterable> handlers();
/**
*
* @return a new stream of contextual reference handlers
*/
default Stream> handlersStream() {
return StreamSupport.stream(handlers().spliterator(), false);
}
/**
* The returned comparator sorts handlers by priority in descending order.
*
* - A class-based bean whose annotated type has {@code javax.annotation.Priority} has the priority of value {@code javax.annotation.Priority#value()}
* - A custom bean which implements {@link Prioritized} has the priority of value {@link Prioritized#getPriority()}
* - Any other bean has the priority of value 0
*
*
* @return a comparator instance
*/
Comparator> getPriorityComparator();
@Override
WeldInstance select(Annotation... qualifiers);
@Override
WeldInstance select(Class subtype, Annotation... qualifiers);
@Override
WeldInstance select(TypeLiteral subtype, Annotation... qualifiers);
/**
*
* Obtains a child {@code Instance} for the given required type and additional required qualifiers.
* Must be invoked on {@code Instance} where T is {@link java.lang.Object}.
*
*
* @param the required type
* @param subtype a {@link java.lang.reflect.Type} representing the required type
* @param qualifiers the additional required qualifiers
* @return the child Instance
* @throws IllegalArgumentException if passed two instances of the same non repeating qualifier type, or an instance of an
* annotation that is not a qualifier type
* @throws IllegalStateException if the container is already shutdown
* @throws IllegalStateException if invoked on {@code Instance} where T is of any other type than {@link java.lang.Object}
*/
WeldInstance select(Type subtype, Annotation... qualifiers);
/**
* This interface represents a contextual reference handler.
*
* Allows to inspect the metadata of the relevant bean and also to destroy the underlying contextual instance.
*
*
* @author Martin Kouba
* @param
*/
interface Handler extends AutoCloseable {
/**
* The contextual reference is obtained lazily, i.e. when first needed.
*
* @return the contextual reference
* @see Instance#get()
* @throws IllegalStateException If the producing {@link WeldInstance} does not exist
*/
T get();
/**
*
* @return the bean metadata
*/
Bean> getBean();
/**
* Destroy the contextual instance.
*
* It's a no-op if:
*
* - called multiple times
* - if the producing {@link WeldInstance} does not exist
* - if the handler does not hold a contextual reference, i.e. {@link #get()} was never called
*
*
* @see Instance#destroy(Object)
*/
void destroy();
/**
* Delegates to {@link #destroy()}.
*/
@Override
void close();
}
}