diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/inject/Providers.java b/core-common/src/main/java/org/glassfish/jersey/internal/inject/Providers.java index a965574541..be79cd84ca 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/inject/Providers.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/inject/Providers.java @@ -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 @@ -186,16 +186,7 @@ public static Iterable> getAllRankedProviders(InjectionMan for (ServiceHolder provider : providers) { if (!providerMap.containsKey(provider)) { - Set contractTypes = provider.getContractTypes(); - Class implementationClass = provider.getImplementationClass(); - boolean proxyGenerated = true; - for (Type ct : contractTypes) { - if (((Class) ct).isAssignableFrom(implementationClass)) { - proxyGenerated = false; - break; - } - } - Set contracts = proxyGenerated ? contractTypes : null; + Set contracts = isProxyGenerated(contract, provider) ? provider.getContractTypes() : null; providerMap.put(provider, new RankedProvider<>(provider.getInstance(), provider.getRank(), contracts)); } } @@ -311,7 +302,9 @@ private static List> getServiceHolders(InjectionManager inj Annotation... qualifiers) { List> 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; } @@ -370,6 +363,17 @@ private static int getPriority(Class serviceClass) { return Priorities.USER; } + private static Class getImplementationClass(Class contract, ServiceHolder 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 boolean isProxyGenerated(Class contract, ServiceHolder 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