Skip to content

Commit

Permalink
ArC: fix StackOverflowError in AutoAddScopeBuildItem
Browse files Browse the repository at this point in the history
- if multiple conditions are used.
  • Loading branch information
mkouba committed Aug 14, 2023
1 parent 42cd3ea commit 1786eff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,19 @@ public interface MatchPredicate {

/**
* @param clazz
* @param annotations
* @param annotations The current set of (possibly transformed) annotations
* @param index
* @return {@code true} if the input arguments match the predicate,
* {@code false} otherwise
*/
boolean test(ClassInfo clazz, Collection<AnnotationInstance> annotations, IndexView index);

default MatchPredicate and(MatchPredicate other) {
MatchPredicate previous = this;
return new MatchPredicate() {
@Override
public boolean test(ClassInfo clazz, Collection<AnnotationInstance> annotations, IndexView index) {
return test(clazz, annotations, index) && other.test(clazz, annotations, index);
return previous.test(clazz, annotations, index) && other.test(clazz, annotations, index);
}
};
}
Expand Down Expand Up @@ -140,6 +141,8 @@ public Builder unremovable() {

/**
* Set a custom predicate.
* <p>
* The previous predicate (if any) is replaced.
*
* @param predicate
* @return self
Expand Down Expand Up @@ -286,7 +289,13 @@ public Builder scopeAlreadyAdded(BiConsumer<DotName, String> consumer) {
return this;
}

private Builder and(MatchPredicate other) {
/**
* The final predicate is a short-circuiting logical AND of the previous predicate (if any) and this condition.
*
* @param other
* @return self
*/
public Builder and(MatchPredicate other) {
if (matchPredicate == null) {
matchPredicate = other;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import org.jboss.jandex.DotName;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -31,9 +33,11 @@ public class AutoScopeBuildItemTest {
b.addBuildStep(new BuildStep() {
@Override
public void execute(BuildContext context) {
context.produce(AutoAddScopeBuildItem.builder().match((clazz, annotations, index) -> {
return clazz.name().toString().equals(SimpleBean.class.getName());
}).defaultScope(BuiltinScope.DEPENDENT)
context.produce(AutoAddScopeBuildItem.builder()
.match((clazz, annotations, index) -> {
return clazz.name().toString().equals(SimpleBean.class.getName());
})
.defaultScope(BuiltinScope.DEPENDENT)
.scopeAlreadyAdded((scope, reason) -> {
// We can't pass the state directly to AutoScopeBuildItemTest because it's loaded by a different classloader
Logger.getLogger("AutoScopeBuildItemTest").info(scope + ":" + reason);
Expand All @@ -43,17 +47,30 @@ public void execute(BuildContext context) {
b.addBuildStep(new BuildStep() {
@Override
public void execute(BuildContext context) {
context.produce(AutoAddScopeBuildItem.builder().match((clazz, annotations, index) -> {
return clazz.name().toString().equals(SimpleBean.class.getName());
}).defaultScope(BuiltinScope.SINGLETON).priority(10).reason("Foo!").build());
context.produce(AutoAddScopeBuildItem.builder()
.match((clazz, annotations, index) -> {
return clazz.name().toString().equals(SimpleBean.class.getName());
})
.anyMethodMatches(m -> m.hasAnnotation(PostConstruct.class))
.isAnnotatedWith(DotName.createSimple(Named.class))
.defaultScope(BuiltinScope.SINGLETON)
.priority(10)
.reason("Foo!")
.build());
}
}).produces(AutoAddScopeBuildItem.class).build();
b.addBuildStep(new BuildStep() {
@Override
public void execute(BuildContext context) {
context.produce(AutoAddScopeBuildItem.builder().match((clazz, annotations, index) -> {
return clazz.name().toString().equals(NotABean.class.getName());
}).defaultScope(BuiltinScope.SINGLETON).unremovable().build());
context.produce(AutoAddScopeBuildItem.builder()
.match((clazz, annotations, index) -> {
return clazz.name().toString().equals(NotABean.class.getName());
})
.containsAnnotations(DotName.createSimple(PostConstruct.class))
.and((clazz, annotations, index) -> annotations.isEmpty())
.defaultScope(BuiltinScope.SINGLETON)
.unremovable()
.build());
}
}).produces(AutoAddScopeBuildItem.class).build();
}).setLogRecordPredicate(log -> "AutoScopeBuildItemTest".equals(log.getLoggerName()))
Expand All @@ -76,6 +93,7 @@ public void testBeans() {
assertEquals(notABean1.ping(), Arc.container().instance(NotABean.class).get().ping());
}

@Named
static class SimpleBean {

private String id;
Expand Down

0 comments on commit 1786eff

Please sign in to comment.