Skip to content

Commit

Permalink
Fix priority ordering (eclipse-ee4j#4103)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Supol <[email protected]>
  • Loading branch information
jansupol authored Apr 23, 2019
1 parent 895c293 commit a62d1ff
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -186,16 +186,7 @@ public static <T> Iterable<RankedProvider<T>> getAllRankedProviders(InjectionMan

for (ServiceHolder<T> provider : providers) {
if (!providerMap.containsKey(provider)) {
Set<Type> contractTypes = provider.getContractTypes();
Class<?> implementationClass = provider.getImplementationClass();
boolean proxyGenerated = true;
for (Type ct : contractTypes) {
if (((Class<?>) ct).isAssignableFrom(implementationClass)) {
proxyGenerated = false;
break;
}
}
Set<Type> contracts = proxyGenerated ? contractTypes : null;
Set<Type> contracts = isProxyGenerated(contract, provider) ? provider.getContractTypes() : null;
providerMap.put(provider, new RankedProvider<>(provider.getInstance(), provider.getRank(), contracts));
}
}
Expand Down Expand Up @@ -311,7 +302,9 @@ private static <T> List<ServiceHolder<T>> getServiceHolders(InjectionManager inj
Annotation... qualifiers) {

List<ServiceHolder<T>> serviceHolders = injectionManager.getAllServiceHolders(contract, qualifiers);
serviceHolders.sort((o1, o2) -> objectComparator.compare(o1.getImplementationClass(), o2.getImplementationClass()));
serviceHolders.sort((o1, o2) -> objectComparator.compare(
getImplementationClass(contract, o1), getImplementationClass(contract, o2))
);
return serviceHolders;
}

Expand Down Expand Up @@ -370,6 +363,17 @@ private static int getPriority(Class<?> serviceClass) {
return Priorities.USER;
}

private static <T> Class<T> getImplementationClass(Class<T> contract, ServiceHolder<T> serviceHolder) {
return isProxyGenerated(contract, serviceHolder)
? serviceHolder.getContractTypes().stream().filter(a -> Class.class.isInstance(a))
.map(a -> (Class) a).reduce(contract, (a, b) -> a.isAssignableFrom(b) ? b : a)
: serviceHolder.getImplementationClass();
}

private static <T> boolean isProxyGenerated(Class<T> contract, ServiceHolder<T> serviceHolder) {
return !contract.isAssignableFrom(serviceHolder.getImplementationClass());
}

/**
* Returns provider contracts recognized by Jersey that are implemented by the {@code clazz}.
* Recognized provider contracts include all JAX-RS providers as well as all Jersey SPI
Expand Down

0 comments on commit a62d1ff

Please sign in to comment.