Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WELD-1996 Most dependent built-in beans do not have to be stored in CreationalContext #1106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -92,6 +92,11 @@ public Class<T> getType() {
return type;
}

public boolean isDependentContextOptimizationAllowed() {
// By default, all dependent built-in beans do not have to be stored in a CreationalContext
return Dependent.class.equals(getScope());
}

protected static class BuiltInBeanAttributes<T> extends ImmutableBeanAttributes<T> {

private static final Set<Annotation> DEFAULT_QUALIFIERS = Arrays2.asSet(DefaultLiteral.INSTANCE, AnyLiteral.INSTANCE);
Expand Down
Expand Up @@ -66,4 +66,9 @@ protected Type getDefaultType() {
return INSTANCE_TYPE;
}

@Override
public boolean isDependentContextOptimizationAllowed() {
return false;
}

}
Expand Up @@ -33,6 +33,7 @@

import org.jboss.weld.bean.AbstractProducerBean;
import org.jboss.weld.bean.ManagedBean;
import org.jboss.weld.bean.builtin.AbstractBuiltInBean;
import org.jboss.weld.context.DependentContext;
import org.jboss.weld.context.SerializableContextualInstanceImpl;
import org.jboss.weld.context.WeldCreationalContext;
Expand Down Expand Up @@ -79,7 +80,7 @@ public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContex
protected <T> void addDependentInstance(T instance, Contextual<T> contextual, WeldCreationalContext<T> creationalContext) {
// by this we are making sure that the dependent instance has no transitive dependency with @PreDestroy / disposal method
if (creationalContext.getDependentInstances().isEmpty()) {
if (contextual instanceof ManagedBean<?> && ! isInterceptorOrDecorator(contextual)) {
if (contextual instanceof ManagedBean<?> && !isInterceptorOrDecorator(contextual)) {
ManagedBean<?> managedBean = (ManagedBean<?>) contextual;
if (managedBean.getProducer() instanceof BasicInjectionTarget<?>) {
BasicInjectionTarget<?> injectionTarget = (BasicInjectionTarget<?>) managedBean.getProducer();
Expand All @@ -101,6 +102,10 @@ protected <T> void addDependentInstance(T instance, Contextual<T> contextual, We
}
}
}
if (isOptimizableBuiltInBean(contextual)) {
// Most built-in dependent beans do not have to be stored
return;
}
}

// Only add the dependent instance if none of the conditions above is met
Expand Down Expand Up @@ -128,4 +133,12 @@ public Class<? extends Annotation> getScope() {
public void destroy(Contextual<?> contextual) {
throw new UnsupportedOperationException();
}

private boolean isOptimizableBuiltInBean(Contextual<?> contextual) {
if (contextual instanceof AbstractBuiltInBean<?>) {
AbstractBuiltInBean<?> abstractBuiltInBean = (AbstractBuiltInBean<?>) contextual;
return abstractBuiltInBean.isDependentContextOptimizationAllowed();
}
return false;
}
}