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

Fix priority ordering #4103

Merged
merged 1 commit into from
Apr 23, 2019
Merged
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
@@ -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