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 - distinguish "bean archive" and "additional" index #11980

Merged
merged 1 commit into from
Sep 9, 2020
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 @@ -127,6 +127,7 @@ AdditionalBeanBuildItem quarkusApplication(CombinedIndexBuildItem combinedIndexB
public ContextRegistrationPhaseBuildItem initialize(
ArcConfig arcConfig,
BeanArchiveIndexBuildItem beanArchiveIndex,
CombinedIndexBuildItem combinedIndex,
ApplicationArchivesBuildItem applicationArchivesBuildItem,
List<AnnotationsTransformerBuildItem> annotationTransformers,
List<InjectionPointTransformerBuildItem> injectionPointTransformers,
Expand Down Expand Up @@ -193,7 +194,8 @@ public void transform(TransformationContext transformationContext) {
}
}
});
builder.setIndex(index);
builder.setBeanArchiveIndex(index);
builder.setApplicationIndex(combinedIndex.getIndex());
List<BeanDefiningAnnotation> beanDefiningAnnotations = additionalBeanDefiningAnnotations.stream()
.map((s) -> new BeanDefiningAnnotation(s.getName(), s.getDefaultScope())).collect(Collectors.toList());
beanDefiningAnnotations.add(new BeanDefiningAnnotation(ADDITIONAL_BEAN, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ public Builder containsAnnotations(DotName... annotationNames) {
* The final predicate is a short-circuiting logical AND of the previous predicate (if any) and this condition.
*
* @param interfaceName
* @param index
* @return self
*/
public Builder implementsInterface(DotName interfaceName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected BeanConfigurator<T> self() {
*/
public void done() {
if (consumed.compareAndSet(false, true)) {
ClassInfo implClass = getClassByName(beanDeployment.getIndex(), Objects.requireNonNull(implClazz));
ClassInfo implClass = getClassByName(beanDeployment.getBeanArchiveIndex(), Objects.requireNonNull(implClazz));
if (implClass == null) {
throw new IllegalStateException("Unable to find the bean class in the index: " + implClazz);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public class BeanDeployment {

private final BuildContextImpl buildContext;

private final IndexView index;
private final IndexView beanArchiveIndex;

private final IndexView applicationIndex;

private final Map<DotName, ClassInfo> qualifiers;

Expand Down Expand Up @@ -108,15 +110,16 @@ public class BeanDeployment {

private final List<Predicate<ClassInfo>> excludeTypes;

BeanDeployment(IndexView index, BuildContextImpl buildContext, BeanProcessor.Builder builder) {
BeanDeployment(BuildContextImpl buildContext, BeanProcessor.Builder builder) {
this.buildContext = buildContext;
Set<BeanDefiningAnnotation> beanDefiningAnnotations = new HashSet<>();
if (builder.additionalBeanDefiningAnnotations != null) {
beanDefiningAnnotations.addAll(builder.additionalBeanDefiningAnnotations);
}
this.beanDefiningAnnotations = beanDefiningAnnotations;
this.resourceAnnotations = new HashSet<>(builder.resourceAnnotations);
this.index = index;
this.beanArchiveIndex = builder.beanArchiveIndex;
this.applicationIndex = builder.applicationIndex;
this.annotationStore = new AnnotationStore(initAndSort(builder.annotationTransformers, buildContext), buildContext);
if (buildContext != null) {
buildContext.putInternal(Key.ANNOTATION_STORE.asString(), annotationStore);
Expand All @@ -130,16 +133,16 @@ public class BeanDeployment {

this.customContexts = new ConcurrentHashMap<>();

this.qualifiers = findQualifiers(index);
this.repeatingQualifierAnnotations = findContainerAnnotations(qualifiers, index);
this.qualifiers = findQualifiers(this.beanArchiveIndex);
this.repeatingQualifierAnnotations = findContainerAnnotations(qualifiers, this.beanArchiveIndex);
buildContextPut(Key.QUALIFIERS.asString(), Collections.unmodifiableMap(qualifiers));

this.interceptorBindings = findInterceptorBindings(index);
this.interceptorBindings = findInterceptorBindings(this.beanArchiveIndex);
this.nonBindingFields = new HashMap<>();
for (InterceptorBindingRegistrar registrar : builder.additionalInterceptorBindingRegistrars) {
for (Map.Entry<DotName, Set<String>> bindingEntry : registrar.registerAdditionalBindings().entrySet()) {
DotName dotName = bindingEntry.getKey();
ClassInfo classInfo = getClassByName(index, dotName);
ClassInfo classInfo = getClassByName(this.beanArchiveIndex, dotName);
if (classInfo != null) {
if (bindingEntry.getValue() != null) {
nonBindingFields.put(dotName, bindingEntry.getValue());
Expand All @@ -150,11 +153,12 @@ public class BeanDeployment {
}
buildContextPut(Key.INTERCEPTOR_BINDINGS.asString(), Collections.unmodifiableMap(interceptorBindings));

this.stereotypes = findStereotypes(index, interceptorBindings, beanDefiningAnnotations, customContexts,
this.stereotypes = findStereotypes(this.beanArchiveIndex, interceptorBindings, beanDefiningAnnotations, customContexts,
builder.additionalStereotypes, annotationStore);
buildContextPut(Key.STEREOTYPES.asString(), Collections.unmodifiableMap(stereotypes));

this.transitiveInterceptorBindings = findTransitiveInterceptorBindigs(interceptorBindings.keySet(), index,
this.transitiveInterceptorBindings = findTransitiveInterceptorBindigs(interceptorBindings.keySet(),
this.beanArchiveIndex,
new HashMap<>(), interceptorBindings, annotationStore);

this.injectionPoints = new CopyOnWriteArrayList<>();
Expand Down Expand Up @@ -356,8 +360,28 @@ public Collection<InterceptorInfo> getInterceptors() {
return interceptors;
}

public IndexView getIndex() {
return index;
/**
* This index was used to discover components (beans, interceptors, qualifiers, etc.) and during type-safe resolution.
*
* @return the bean archive index
*/
public IndexView getBeanArchiveIndex() {
return beanArchiveIndex;
}

/**
* This index is optional and is used to discover types during type-safe resolution.
* <p>
* Some types may not be part of the bean archive index but are still needed during type-safe resolution.
*
* @return the application index or {@code null}
*/
public IndexView getApplicationIndex() {
return applicationIndex;
}

boolean hasApplicationIndex() {
return applicationIndex != null;
}

BeanResolver getBeanResolver() {
Expand Down Expand Up @@ -631,7 +655,7 @@ private List<BeanInfo> findBeans(Collection<DotName> beanDefiningAnnotations, Li
.map(Entry::getKey)
.collect(Collectors.toList());

for (ClassInfo beanClass : index.getKnownClasses()) {
for (ClassInfo beanClass : beanArchiveIndex.getKnownClasses()) {

if (Modifier.isInterface(beanClass.flags()) || Modifier.isAbstract(beanClass.flags())
// Replace with ClassInfo#isAnnotation() and ClassInfo#isEnum() when using Jandex 2.1.4+
Expand Down Expand Up @@ -769,7 +793,7 @@ private List<BeanInfo> findBeans(Collection<DotName> beanDefiningAnnotations, Li
Type superType = aClass.superClassType();
aClass = superType != null && !superType.name().equals(DotNames.OBJECT)
&& CLASS_TYPES.contains(superType.kind())
? getClassByName(index, superType.name())
? getClassByName(beanArchiveIndex, superType.name())
: null;
}
for (FieldInfo field : beanClass.fields()) {
Expand Down Expand Up @@ -1013,7 +1037,7 @@ static void processErrors(List<Throwable> errors) {

private List<InterceptorInfo> findInterceptors(List<InjectionPointInfo> injectionPoints) {
Set<ClassInfo> interceptorClasses = new HashSet<>();
for (AnnotationInstance annotation : index.getAnnotations(DotNames.INTERCEPTOR)) {
for (AnnotationInstance annotation : beanArchiveIndex.getAnnotations(DotNames.INTERCEPTOR)) {
if (Kind.CLASS.equals(annotation.target().kind())) {
interceptorClasses.add(annotation.target().asClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ protected void implementDestroy(BeanInfo bean, ClassCreator beanCreator, String
// PreDestroy callbacks
List<MethodInfo> preDestroyCallbacks = Beans.getCallbacks(bean.getTarget().get().asClass(),
DotNames.PRE_DESTROY,
bean.getDeployment().getIndex());
bean.getDeployment().getBeanArchiveIndex());
for (MethodInfo callback : preDestroyCallbacks) {
if (Modifier.isPrivate(callback.flags())) {
privateMembers.add(isApplicationClass, String.format("@PreDestroy callback %s#%s()",
Expand Down Expand Up @@ -1483,7 +1483,7 @@ void implementCreateForClassBean(ClassOutput classOutput, ClassCreator beanCreat
if (!bean.isInterceptor()) {
List<MethodInfo> postConstructCallbacks = Beans.getCallbacks(bean.getTarget().get().asClass(),
DotNames.POST_CONSTRUCT,
bean.getDeployment().getIndex());
bean.getDeployment().getBeanArchiveIndex());
for (MethodInfo callback : postConstructCallbacks) {
if (isReflectionFallbackNeeded(callback, targetPackage)) {
if (Modifier.isPrivate(callback.flags())) {
Expand Down Expand Up @@ -1531,7 +1531,7 @@ protected void implementGet(BeanInfo bean, ClassCreator beanCreator, String prov
canBeOptimized = bean.getLifecycleInterceptors(InterceptionType.PRE_DESTROY).isEmpty()
&& Beans.getCallbacks(bean.getTarget().get().asClass(),
DotNames.PRE_DESTROY,
bean.getDeployment().getIndex()).isEmpty();
bean.getDeployment().getBeanArchiveIndex()).isEmpty();
} else if (bean.isProducerMethod() || bean.isProducerField()) {
canBeOptimized = bean.getDisposer() == null;
}
Expand Down Expand Up @@ -1817,7 +1817,7 @@ static ResultHandle collectInjectionPointAnnotations(ClassOutput classOutput, Cl
.readStaticField(FieldDescriptor.of(InjectLiteral.class, "INSTANCE", InjectLiteral.class));
} else {
// Create annotation literal if needed
ClassInfo literalClass = getClassByName(beanDeployment.getIndex(), annotation.name());
ClassInfo literalClass = getClassByName(beanDeployment.getBeanArchiveIndex(), annotation.name());
annotationHandle = annotationLiterals.process(constructor,
classOutput, literalClass, annotation,
Types.getPackageName(beanCreator.getClassName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ boolean hasDefaultDestroy() {
}
if (isClassBean()) {
return getLifecycleInterceptors(InterceptionType.PRE_DESTROY).isEmpty()
&& Beans.getCallbacks(target.get().asClass(), DotNames.PRE_DESTROY, beanDeployment.getIndex()).isEmpty();
&& Beans.getCallbacks(target.get().asClass(), DotNames.PRE_DESTROY, beanDeployment.getBeanArchiveIndex())
.isEmpty();
} else {
return disposer == null && destroyerConsumer == null;
}
Expand Down Expand Up @@ -452,7 +453,7 @@ private void addClassLevelBindings(ClassInfo classInfo, Collection<AnnotationIns
&& bindings.stream().noneMatch(e -> e.name().equals(a.name())))
.forEach(a -> bindings.add(a));
if (classInfo.superClassType() != null && !classInfo.superClassType().name().equals(DotNames.OBJECT)) {
ClassInfo superClass = getClassByName(beanDeployment.getIndex(), classInfo.superName());
ClassInfo superClass = getClassByName(beanDeployment.getBeanArchiveIndex(), classInfo.superName());
if (superClass != null) {
addClassLevelBindings(superClass, bindings);
}
Expand Down Expand Up @@ -536,9 +537,9 @@ private ClassInfo initImplClazz(AnnotationTarget target, BeanDeployment beanDepl
case CLASS:
return target.asClass();
case FIELD:
return getClassByName(beanDeployment.getIndex(), target.asField().type());
return getClassByName(beanDeployment.getBeanArchiveIndex(), target.asField().type());
case METHOD:
return getClassByName(beanDeployment.getIndex(), target.asMethod().returnType());
return getClassByName(beanDeployment.getBeanArchiveIndex(), target.asMethod().returnType());
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ private BeanProcessor(Builder builder) {

// Initialize all build processors
buildContext = new BuildContextImpl();
buildContext.putInternal(Key.INDEX.asString(), builder.index);
buildContext.putInternal(Key.INDEX.asString(), builder.beanArchiveIndex);

this.beanRegistrars = initAndSort(builder.beanRegistrars, buildContext);
this.observerRegistrars = initAndSort(builder.observerRegistrars, buildContext);
this.contextRegistrars = initAndSort(builder.contextRegistrars, buildContext);
this.beanDeploymentValidators = initAndSort(builder.beanDeploymentValidators, buildContext);
this.beanDeployment = new BeanDeployment(builder.index, buildContext, builder);
this.beanDeployment = new BeanDeployment(buildContext, builder);

// Make it configurable if we find that the set of annotations needs to grow
this.injectionPointAnnotationsPredicate = annotationName -> !annotationName.equals(DotNames.DEPRECATED);
Expand Down Expand Up @@ -247,7 +247,8 @@ public static class Builder {

String name = DEFAULT_NAME;

IndexView index;
IndexView beanArchiveIndex;
IndexView applicationIndex;

Collection<BeanDefiningAnnotation> additionalBeanDefiningAnnotations = Collections.emptySet();
Map<DotName, Collection<AnnotationInstance>> additionalStereotypes = Collections.emptyMap();
Expand Down Expand Up @@ -290,8 +291,28 @@ public Builder setName(String name) {
return this;
}

public Builder setIndex(IndexView index) {
this.index = index;
/**
* Set the bean archive index. This index is mandatory and is used to discover components (beans, interceptors,
* qualifiers, etc.) and during type-safe resolution.
*
* @param beanArchiveIndex
* @return self
*/
public Builder setBeanArchiveIndex(IndexView beanArchiveIndex) {
this.beanArchiveIndex = beanArchiveIndex;
return this;
}

/**
* Set the application index. This index is optional and is also used to discover types during type-safe resolution.
* <p>
* Some types may not be part of the bean archive index but are still needed during type-safe resolution.
*
* @param applicationIndex
* @return self
*/
public Builder setApplicationIndex(IndexView applicationIndex) {
this.applicationIndex = applicationIndex;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import io.quarkus.arc.processor.InjectionPointInfo.TypeAndQualifiers;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -27,8 +26,7 @@
import org.jboss.jandex.WildcardType;

/**
*
* @author Martin Kouba
* Implements type-safe resolution rules.
*/
class BeanResolver {

Expand All @@ -45,14 +43,20 @@ public BeanResolver(BeanDeployment beanDeployment) {
this.assignableFromMap = new ConcurrentHashMap<>();
this.assignableFromMapFunction = name -> {
Set<DotName> assignables = new HashSet<>();
Collection<ClassInfo> subclasses = beanDeployment.getIndex().getAllKnownSubclasses(name);
for (ClassInfo subclass : subclasses) {
for (ClassInfo subclass : beanDeployment.getBeanArchiveIndex().getAllKnownSubclasses(name)) {
assignables.add(subclass.name());
}
Collection<ClassInfo> implementors = beanDeployment.getIndex().getAllKnownImplementors(name);
for (ClassInfo implementor : implementors) {
for (ClassInfo implementor : beanDeployment.getBeanArchiveIndex().getAllKnownImplementors(name)) {
assignables.add(implementor.name());
}
if (beanDeployment.hasApplicationIndex()) {
for (ClassInfo subclass : beanDeployment.getApplicationIndex().getAllKnownSubclasses(name)) {
assignables.add(subclass.name());
}
for (ClassInfo implementor : beanDeployment.getApplicationIndex().getAllKnownImplementors(name)) {
assignables.add(implementor.name());
}
}
return assignables;
};
this.resolved = new ConcurrentHashMap<>();
Expand Down
Loading