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

Enable synthetic beans to be application classes #16984

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,5 @@ Supplier<?> getSupplier() {
RuntimeValue<?> getRuntimeValue() {
return runtimeValue;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void done() {
.params(params)
.defaultBean(defaultBean)
.removable(removable)
.forceApplicationClass(forceApplicationClass)
.build());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public abstract class BeanConfiguratorBase<B extends BeanConfiguratorBase<B, T>,
protected boolean removable;
protected final Map<String, Object> params;
protected Type providerType;
protected boolean forceApplicationClass;

protected BeanConfiguratorBase(DotName implClazz) {
this.implClazz = implClazz;
Expand All @@ -63,6 +64,7 @@ public B read(BeanConfiguratorBase<?, ?> base) {
types.addAll(base.types);
qualifiers.clear();
qualifiers.addAll(base.qualifiers);
forceApplicationClass = base.forceApplicationClass;
scope(base.scope);
if (base.alternativePriority != null) {
alternativePriority(base.alternativePriority);
Expand Down Expand Up @@ -171,6 +173,17 @@ public B unremovable() {
return self();
}

/**
* Forces the bean to be considered an 'application class', so it will be defined in the runtime
* ClassLoader and re-created on each redeployment.
*
* @return self
*/
public B forceApplicationClass() {
this.forceApplicationClass = true;
return self();
}

public B alternativePriority(int priority) {
this.alternativePriority = priority;
return self();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ Collection<Resource> generateSyntheticBean(BeanInfo bean) {
return Collections.emptyList();
}

boolean isApplicationClass = applicationClassPredicate.test(bean.getImplClazz().name());
boolean isApplicationClass = applicationClassPredicate.test(bean.getImplClazz().name())
|| bean.isForceApplicationClass();
ResourceClassOutput classOutput = new ResourceClassOutput(isApplicationClass,
name -> name.equals(generatedName) ? SpecialType.BEAN : null, generateSources);

Expand Down Expand Up @@ -267,7 +268,7 @@ Collection<Resource> generateClassBean(BeanInfo bean, ClassInfo beanClass) {
return Collections.emptyList();
}

boolean isApplicationClass = applicationClassPredicate.test(beanClass.name());
boolean isApplicationClass = applicationClassPredicate.test(beanClass.name()) || bean.isForceApplicationClass();
ResourceClassOutput classOutput = new ResourceClassOutput(isApplicationClass,
name -> name.equals(generatedName) ? SpecialType.BEAN : null, generateSources);

Expand Down Expand Up @@ -373,7 +374,7 @@ Collection<Resource> generateProducerMethodBean(BeanInfo bean, MethodInfo produc
return Collections.emptyList();
}

boolean isApplicationClass = applicationClassPredicate.test(declaringClass.name());
boolean isApplicationClass = applicationClassPredicate.test(declaringClass.name()) || bean.isForceApplicationClass();
ResourceClassOutput classOutput = new ResourceClassOutput(isApplicationClass,
name -> name.equals(generatedName) ? SpecialType.BEAN : null, generateSources);

Expand Down Expand Up @@ -465,7 +466,7 @@ Collection<Resource> generateProducerFieldBean(BeanInfo bean, FieldInfo producer
return Collections.emptyList();
}

boolean isApplicationClass = applicationClassPredicate.test(declaringClass.name());
boolean isApplicationClass = applicationClassPredicate.test(declaringClass.name()) || bean.isForceApplicationClass();
ResourceClassOutput classOutput = new ResourceClassOutput(isApplicationClass,
name -> name.equals(generatedName) ? SpecialType.BEAN : null, generateSources);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class BeanInfo implements InjectionTargetInfo {

private final Map<String, Object> params;

private final boolean forceApplicationClass;

BeanInfo(AnnotationTarget target, BeanDeployment beanDeployment, ScopeInfo scope, Set<Type> types,
Set<AnnotationInstance> qualifiers,
List<Injection> injections, BeanInfo declaringBean, DisposerInfo disposer, Integer alternativePriority,
Expand All @@ -91,7 +93,7 @@ public class BeanInfo implements InjectionTargetInfo {
this(null, null, target, beanDeployment, scope, types, qualifiers, injections, declaringBean, disposer,
alternativePriority,
stereotypes, name, isDefaultBean, null, null,
Collections.emptyMap(), true);
Collections.emptyMap(), true, false);
}

BeanInfo(ClassInfo implClazz, Type providerType, AnnotationTarget target, BeanDeployment beanDeployment, ScopeInfo scope,
Expand All @@ -101,7 +103,7 @@ public class BeanInfo implements InjectionTargetInfo {
List<StereotypeInfo> stereotypes,
String name, boolean isDefaultBean, Consumer<MethodCreator> creatorConsumer,
Consumer<MethodCreator> destroyerConsumer,
Map<String, Object> params, boolean isRemovable) {
Map<String, Object> params, boolean isRemovable, boolean forceApplicationClass) {
this.target = Optional.ofNullable(target);
if (implClazz == null && target != null) {
implClazz = initImplClazz(target, beanDeployment);
Expand Down Expand Up @@ -140,6 +142,7 @@ public class BeanInfo implements InjectionTargetInfo {
this.interceptedMethods = new ConcurrentHashMap<>();
this.decoratedMethods = new ConcurrentHashMap<>();
this.lifecycleInterceptors = new ConcurrentHashMap<>();
this.forceApplicationClass = forceApplicationClass;
}

@Override
Expand Down Expand Up @@ -337,6 +340,10 @@ boolean hasDefaultDestroy() {
}
}

public boolean isForceApplicationClass() {
return forceApplicationClass;
}

/**
*
* @return an ordered list of all interceptors associated with the bean
Expand Down Expand Up @@ -792,6 +799,8 @@ static class Builder {

private boolean removable = true;

private boolean forceApplicationClass;

Builder() {
injections = Collections.emptyList();
stereotypes = Collections.emptyList();
Expand Down Expand Up @@ -890,9 +899,13 @@ Builder removable(boolean val) {
BeanInfo build() {
return new BeanInfo(implClazz, providerType, target, beanDeployment, scope, types, qualifiers, injections,
declaringBean, disposer, alternativePriority, stereotypes, name, isDefaultBean, creatorConsumer,
destroyerConsumer, params, removable);
destroyerConsumer, params, removable, forceApplicationClass);
}

public Builder forceApplicationClass(boolean forceApplicationClass) {
this.forceApplicationClass = forceApplicationClass;
return this;
}
}

}