Skip to content

Commit

Permalink
Use findByType instead if getByType
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed Sep 17, 2019
1 parent bcaed70 commit a46ebcb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {
public static final String NEBULA_RECOMMENDER_BOM = "nebulaRecommenderBom";
Expand Down Expand Up @@ -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"));
}

Expand All @@ -214,7 +215,7 @@ protected void enhancePublicationsWithBomProducer(Project project) {
* @return the recommended version or <code>null</code>
*/
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a46ebcb

Please sign in to comment.