-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArC - fix an issue with bridge methods and generics
- Loading branch information
Showing
6 changed files
with
196 additions
and
52 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...ent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/AssignabilityCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.quarkus.arc.processor; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.CompositeIndex; | ||
import org.jboss.jandex.DotName; | ||
import org.jboss.jandex.IndexView; | ||
import org.jboss.jandex.Type; | ||
|
||
final class AssignabilityCheck { | ||
|
||
private final ConcurrentMap<DotName, Set<DotName>> cache; | ||
private final IndexView index; | ||
|
||
public AssignabilityCheck(IndexView beanArchiveIndex, IndexView applicationIndex) { | ||
this.cache = new ConcurrentHashMap<>(); | ||
this.index = applicationIndex != null ? CompositeIndex.create(beanArchiveIndex, applicationIndex) : beanArchiveIndex; | ||
} | ||
|
||
boolean isAssignableFrom(Type type1, Type type2) { | ||
// java.lang.Object is assignable from any type | ||
if (type1.name().equals(DotNames.OBJECT)) { | ||
return true; | ||
} | ||
// type1 is the same as type2 | ||
if (type1.name().equals(type2.name())) { | ||
return true; | ||
} | ||
// type1 is a superclass | ||
return getAssignables(type1.name()).contains(type2.name()); | ||
} | ||
|
||
Set<DotName> getAssignables(DotName name) { | ||
return cache.computeIfAbsent(name, this::findAssignables); | ||
} | ||
|
||
private Set<DotName> findAssignables(DotName name) { | ||
Set<DotName> assignables = new HashSet<>(); | ||
for (ClassInfo subclass : index.getAllKnownSubclasses(name)) { | ||
assignables.add(subclass.name()); | ||
} | ||
for (ClassInfo implementor : index.getAllKnownImplementors(name)) { | ||
assignables.add(implementor.name()); | ||
} | ||
return assignables; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...jects/arc/processor/src/test/java/io/quarkus/arc/processor/SubclassSkipPredicateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.quarkus.arc.processor; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import io.quarkus.arc.processor.Methods.SubclassSkipPredicate; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.DotName; | ||
import org.jboss.jandex.IndexView; | ||
import org.jboss.jandex.MethodInfo; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class SubclassSkipPredicateTest { | ||
|
||
@Test | ||
public void testPredicate() throws IOException { | ||
IndexView index = Basics.index(Base.class, Submarine.class, Long.class, Number.class); | ||
AssignabilityCheck assignabilityCheck = new AssignabilityCheck(index, null); | ||
SubclassSkipPredicate predicate = new SubclassSkipPredicate(assignabilityCheck::isAssignableFrom); | ||
|
||
ClassInfo submarineClass = index.getClassByName(DotName.createSimple(Submarine.class.getName())); | ||
predicate.startProcessing(submarineClass); | ||
|
||
List<MethodInfo> echos = submarineClass.methods().stream().filter(m -> m.name().equals("echo")) | ||
.collect(Collectors.toList()); | ||
assertEquals(2, echos.size()); | ||
assertPredicate(echos, predicate); | ||
|
||
List<MethodInfo> getNames = submarineClass.methods().stream().filter(m -> m.name().equals("getName")) | ||
.collect(Collectors.toList()); | ||
assertEquals(2, getNames.size()); | ||
assertPredicate(getNames, predicate); | ||
|
||
predicate.methodsProcessed(); | ||
} | ||
|
||
private void assertPredicate(List<MethodInfo> methods, SubclassSkipPredicate predicate) { | ||
for (MethodInfo method : methods) { | ||
if (Methods.isBridge(method)) { | ||
// Bridge method with impl | ||
assertTrue(predicate.test(method)); | ||
} else { | ||
assertFalse(predicate.test(method)); | ||
} | ||
} | ||
} | ||
|
||
static class Base<T extends Number, UNUSED> { | ||
|
||
String echo(T payload) { | ||
return payload.toString().toUpperCase(); | ||
} | ||
|
||
T getName() { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
@ApplicationScoped | ||
static class Submarine extends Base<Long, Boolean> { | ||
|
||
@Override | ||
String echo(Long payload) { | ||
return payload.toString(); | ||
} | ||
|
||
@Override | ||
Long getName() { | ||
return 10l; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters