Skip to content

Commit

Permalink
WELD-2745 Restore proper getReference behavior from 5.1.0.Final and r…
Browse files Browse the repository at this point in the history
…epair CreationalContextImpl destruction and releasing logic

Signed-off-by: Laird Nelson <[email protected]>
  • Loading branch information
ljnelson authored and manovotn committed Jul 11, 2023
1 parent 2a7eb8e commit 42204fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jakarta.enterprise.context.spi.Contextual;
import jakarta.enterprise.context.spi.CreationalContext;
Expand Down Expand Up @@ -60,6 +62,9 @@ public class CreationalContextImpl<T> implements CreationalContext<T>, WeldCreat

private final CreationalContextImpl<?> parentCreationalContext;

// Precondition for access when non-null: synchronized (dependentInstances)
private transient Set<ContextualInstance<?>> destroyed;

private transient List<ResourceReference<?>> resourceReferences;

private transient boolean constructorInterceptionSuppressed;
Expand Down Expand Up @@ -126,9 +131,12 @@ public void release() {
public void release(Contextual<T> contextual, T instance) {
synchronized (dependentInstances) {
for (ContextualInstance<?> dependentInstance : dependentInstances) {
// do not destroy contextual again, since it's just being destroyed
if (contextual == null || !(dependentInstance.getContextual().equals(contextual))) {
destroy(dependentInstance);
} else {
// do not destroy contextual again, since it's just being destroyed, but make sure its dependencies
// are destroyed
release(dependentInstance);
}
}
}
Expand All @@ -139,8 +147,29 @@ public void release(Contextual<T> contextual, T instance) {
}
}

private static <T> void destroy(ContextualInstance<T> beanInstance) {
beanInstance.getContextual().destroy(beanInstance.getInstance(), beanInstance.getCreationalContext());
private <T> void destroy(ContextualInstance<T> beanInstance) {
// Precondition: synchronized (dependentInstances)
if (this.destroyed == null) {
this.destroyed = new HashSet<>();
}
if (this.destroyed.add(beanInstance)) {
beanInstance.getContextual().destroy(beanInstance.getInstance(), beanInstance.getCreationalContext());
}
}

private <T> void release(ContextualInstance<T> beanInstance) {
// Precondition: synchronized (dependentInstances)
if (this.destroyed == null) {
this.destroyed = new HashSet<>();
}
if (this.destroyed.add(beanInstance)) {
CreationalContext<T> cc = beanInstance.getCreationalContext();
if (cc instanceof CreationalContextImpl<?>) {
((CreationalContextImpl<T>) cc).release(beanInstance.getContextual(), beanInstance.getInstance());
} else {
cc.release();
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,7 @@ private Context internalGetContext(Class<? extends Annotation> scopeType) {
}

public Object getReference(Bean<?> bean, Type requestedType, CreationalContext<?> creationalContext, boolean noProxy) {
return getReference(bean, requestedType, creationalContext, noProxy, true);
}

private Object getReference(Bean<?> bean, Type requestedType, CreationalContext<?> creationalContext, boolean noProxy, boolean createChildCc) {
if (creationalContext instanceof CreationalContextImpl<?> && createChildCc) {
if (creationalContext instanceof CreationalContextImpl<?>) {
creationalContext = ((CreationalContextImpl<?>) creationalContext).getCreationalContext(bean);
}
if (!noProxy && isProxyRequired(bean)) {
Expand Down Expand Up @@ -704,7 +700,7 @@ public Object getReference(Bean<?> bean, Type requestedType, CreationalContext<?
// Ensure that there is no injection point associated
final ThreadLocalStackReference<InjectionPoint> stack = currentInjectionPoint.push(EmptyInjectionPoint.INSTANCE);
try {
return getReference(bean, requestedType, creationalContext, false, false);
return getReference(bean, requestedType, creationalContext, false);
} finally {
stack.pop();
}
Expand Down

0 comments on commit 42204fe

Please sign in to comment.