Skip to content

Commit

Permalink
WELD-1996 Most dependent built-in beans do not have to be stored in
Browse files Browse the repository at this point in the history
CreationalContext
  • Loading branch information
mkouba authored and jharting committed Aug 11, 2015
1 parent 6f9c116 commit 7b89e81
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
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;
}
}

0 comments on commit 7b89e81

Please sign in to comment.