Skip to content

Commit

Permalink
Fix broken collection assignability check
Browse files Browse the repository at this point in the history
The check that was being done is broken
in Java 21 as that version bring SequencedCollection
which List now implements

Closes: quarkusio#36170
  • Loading branch information
geoand committed Sep 28, 2023
1 parent 26f491b commit ffa805a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import static org.jboss.jandex.Type.Kind.PRIMITIVE;
import static org.jboss.resteasy.reactive.client.impl.RestClientRequestContext.DEFAULT_CONTENT_TYPE_PROP;
import static org.jboss.resteasy.reactive.common.processor.EndpointIndexer.extractProducesConsumesValues;
import static org.jboss.resteasy.reactive.common.processor.JandexUtil.*;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COLLECTION;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COMPLETION_STAGE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.CONSUMES;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.ENCODED;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.FORM_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.MAP;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.MULTI;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.OBJECT;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.PART_TYPE_NAME;
Expand Down Expand Up @@ -2744,28 +2747,11 @@ private void addQueryParamToWebTarget(BytecodeCreator creator, ResultHandle para
}

private boolean isCollection(Type type, IndexView index) {
if (type.kind() == Type.Kind.PRIMITIVE) {
return false;
}
ClassInfo classInfo = index.getClassByName(type.name());
if (classInfo == null) {
return false;
}
return classInfo.interfaceNames().stream().anyMatch(DotName.createSimple(Collection.class.getName())::equals);
return isAssignableFrom(COLLECTION, type.name(), index);
}

private boolean isMap(Type type, IndexView index) {
if (type.kind() == Type.Kind.PRIMITIVE) {
return false;
}
ClassInfo classInfo = index.getClassByName(type.name());
if (classInfo == null) {
return false;
}
if (ResteasyReactiveDotNames.MAP.equals(classInfo.name())) {
return true;
}
return classInfo.interfaceNames().stream().anyMatch(DotName.createSimple(Map.class.getName())::equals);
return isAssignableFrom(MAP, type.name(), index);
}

private void addHeaderParam(BytecodeCreator invoBuilderEnricher, AssignableResultHandle invocationBuilder,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.resteasy.reactive.links.deployment;

import static org.jboss.resteasy.reactive.common.processor.JandexUtil.isAssignableFrom;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COLLECTION;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COMPLETABLE_FUTURE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COMPLETION_STAGE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.MULTI;
Expand All @@ -17,8 +19,6 @@

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.ParameterizedType;
Expand Down Expand Up @@ -138,14 +138,7 @@ private String getAnnotationValue(AnnotationInstance annotationInstance, String
}

private boolean isCollection(Type type, IndexView index) {
if (type.kind() == Type.Kind.PRIMITIVE) {
return false;
}
ClassInfo classInfo = index.getClassByName(type.name());
if (classInfo == null) {
return false;
}
return classInfo.interfaceNames().stream().anyMatch(DotName.createSimple(Collection.class.getName())::equals);
return isAssignableFrom(COLLECTION, type.name(), index);
}

private Type getNonAsyncReturnType(Type returnType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -415,4 +417,42 @@ public static boolean isImplementorOf(IndexView index, ClassInfo info, DotName n
return isImplementorOf(index, superClass, name, additionalIgnoredSuperClasses);
}

public static boolean isAssignableFrom(DotName superType, DotName subType, IndexView index) {
// java.lang.Object is assignable from any type
if (superType.equals(DOTNAME_OBJECT)) {
return true;
}
// type1 is the same as type2
if (superType.equals(subType)) {
return true;
}
// type1 is a superclass
return findSupertypes(subType, index).contains(superType);
}

private static Set<DotName> findSupertypes(DotName name, IndexView index) {
Set<DotName> result = new HashSet<>();

Deque<DotName> workQueue = new ArrayDeque<>();
workQueue.add(name);
while (!workQueue.isEmpty()) {
DotName type = workQueue.poll();
if (result.contains(type)) {
continue;
}
result.add(type);

ClassInfo clazz = index.getClassByName(type);
if (clazz == null) {
continue;
}
if (clazz.superName() != null) {
workQueue.add(clazz.superName());
}
workQueue.addAll(clazz.interfaceNames());
}

return result;
}

}

0 comments on commit ffa805a

Please sign in to comment.