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

ArC: fix StackOverflowError in AutoAddScopeBuildItem #35343

Merged
merged 1 commit into from
Aug 15, 2023
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 @@ -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