From a46ebcb1c64f2b053b837ad3a5131d6ba21d4d1e Mon Sep 17 00:00:00 2001 From: Roberto Perez Alcolea Date: Tue, 17 Sep 2019 12:25:22 -0700 Subject: [PATCH] Use findByType instead if getByType --- .../recommender/DependencyRecommendationsPlugin.java | 5 +++-- .../recommender/RecommendationStrategyFactory.java | 6 ++++-- .../recommender/provider/RecommendationResolver.java | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/groovy/netflix/nebula/dependency/recommender/DependencyRecommendationsPlugin.java b/src/main/groovy/netflix/nebula/dependency/recommender/DependencyRecommendationsPlugin.java index 1cbaf19..494d91d 100644 --- a/src/main/groovy/netflix/nebula/dependency/recommender/DependencyRecommendationsPlugin.java +++ b/src/main/groovy/netflix/nebula/dependency/recommender/DependencyRecommendationsPlugin.java @@ -42,6 +42,7 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class DependencyRecommendationsPlugin implements Plugin { public static final String NEBULA_RECOMMENDER_BOM = "nebulaRecommenderBom"; @@ -198,7 +199,7 @@ protected String whichStrategy(RecommendationStrategy strategy) { protected void enhanceDependenciesWithRecommender(Project project) { RecommendationResolver resolver = new RecommendationResolver(project); - project.getExtensions().getByType(ExtraPropertiesExtension.class).set("recommend", + Objects.requireNonNull(project.getExtensions().findByType(ExtraPropertiesExtension.class)).set("recommend", new MethodClosure(resolver, "recommend")); } @@ -214,7 +215,7 @@ protected void enhancePublicationsWithBomProducer(Project project) { * @return the recommended version or null */ public String getRecommendedVersionRecursive(Project project, ModuleVersionSelector mvSelector) { - String version = project.getExtensions().getByType(RecommendationProviderContainer.class) + String version = Objects.requireNonNull(project.getExtensions().findByType(RecommendationProviderContainer.class)) .getRecommendedVersion(mvSelector.getGroup(), mvSelector.getName()); if (version != null) return version; diff --git a/src/main/groovy/netflix/nebula/dependency/recommender/RecommendationStrategyFactory.java b/src/main/groovy/netflix/nebula/dependency/recommender/RecommendationStrategyFactory.java index d90b861..5aca794 100644 --- a/src/main/groovy/netflix/nebula/dependency/recommender/RecommendationStrategyFactory.java +++ b/src/main/groovy/netflix/nebula/dependency/recommender/RecommendationStrategyFactory.java @@ -3,6 +3,8 @@ import netflix.nebula.dependency.recommender.provider.RecommendationProviderContainer; import org.gradle.api.Project; +import java.util.Objects; + /** * Creates RecommendationStrategy lazily on demand and caches it. * This is used to allow for scoped recommendationStrategies (e.g. per configuration as in DependencyRecommendationsPlugin) @@ -18,8 +20,8 @@ public RecommendationStrategyFactory(Project project) { public RecommendationStrategy getRecommendationStrategy() { if(recommendationStrategy == null) { try { - RecommendationProviderContainer recommendationProviderContainer = project.getExtensions().getByType(RecommendationProviderContainer.class); - recommendationStrategy = recommendationProviderContainer.getStrategy().getStrategyClass().newInstance(); + RecommendationProviderContainer recommendationProviderContainer = project.getExtensions().findByType(RecommendationProviderContainer.class); + recommendationStrategy = Objects.requireNonNull(recommendationProviderContainer).getStrategy().getStrategyClass().newInstance(); } catch (Exception e) { throw new IllegalStateException(e); } diff --git a/src/main/groovy/netflix/nebula/dependency/recommender/provider/RecommendationResolver.java b/src/main/groovy/netflix/nebula/dependency/recommender/provider/RecommendationResolver.java index 8a0f209..48217af 100644 --- a/src/main/groovy/netflix/nebula/dependency/recommender/provider/RecommendationResolver.java +++ b/src/main/groovy/netflix/nebula/dependency/recommender/provider/RecommendationResolver.java @@ -15,7 +15,7 @@ public String recommend(String dependencyNotation, String recommenderName) throw ModuleVersionIdentifier mvid = ModuleNotationParser.parse(dependencyNotation); String version = mvid.getVersion() != null ? mvid.getVersion() : - project.getExtensions().getByType(RecommendationProviderContainer.class).getByName(recommenderName) + project.getExtensions().findByType(RecommendationProviderContainer.class).getByName(recommenderName) .getVersion(mvid.getGroup(), mvid.getName()); return mvid.getGroup() + ":" + mvid.getName() + ":" + version;