From 1874b7acdb2d8a6ffb4ef83e23751f64b1be29f8 Mon Sep 17 00:00:00 2001 From: camilojc Date: Fri, 19 May 2017 22:45:15 +0100 Subject: [PATCH] Decoupling the MethodTargetResolver from the ApplicationContext Fixes #39 --- .../org/springframework/shell2/JLineShell.java | 4 +--- .../shell2/MethodTargetResolver.java | 7 +++---- .../standard/StandardMethodTargetResolver.java | 9 +++++++-- .../legacy/LegacyMethodTargetResolver.java | 12 +++++++++--- .../legacy/LegacyMethodTargetResolverTest.java | 16 ++++++---------- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/spring-shell2-core/src/main/java/org/springframework/shell2/JLineShell.java b/spring-shell2-core/src/main/java/org/springframework/shell2/JLineShell.java index c1e710f0..4761bbaf 100644 --- a/spring-shell2-core/src/main/java/org/springframework/shell2/JLineShell.java +++ b/spring-shell2-core/src/main/java/org/springframework/shell2/JLineShell.java @@ -44,12 +44,10 @@ import org.jline.terminal.Terminal; import org.jline.utils.AttributedString; import org.jline.utils.AttributedStringBuilder; import org.jline.utils.AttributedStyle; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; import org.springframework.core.MethodParameter; -import org.springframework.shell2.result.TypeHierarchyResultHandler; import org.springframework.stereotype.Component; import org.springframework.util.ReflectionUtils; @@ -95,7 +93,7 @@ public class JLineShell implements Shell { @PostConstruct public void init() throws Exception { for (MethodTargetResolver resolver : applicationContext.getBeansOfType(MethodTargetResolver.class).values()) { - methodTargets.putAll(resolver.resolve(applicationContext)); + methodTargets.putAll(resolver.resolve()); } LineReaderBuilder lineReaderBuilder = LineReaderBuilder.builder() diff --git a/spring-shell2-core/src/main/java/org/springframework/shell2/MethodTargetResolver.java b/spring-shell2-core/src/main/java/org/springframework/shell2/MethodTargetResolver.java index 9c78dd3b..5900a617 100644 --- a/spring-shell2-core/src/main/java/org/springframework/shell2/MethodTargetResolver.java +++ b/spring-shell2-core/src/main/java/org/springframework/shell2/MethodTargetResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,18 +18,17 @@ package org.springframework.shell2; import java.util.Map; -import org.springframework.context.ApplicationContext; - /** * Strategy interface for discovering commands. * * @author Eric Bottard + * @author Camilo Gonzalez */ public interface MethodTargetResolver { /** * Return a mapping from {@literal } to actual behavior. */ - public Map resolve(ApplicationContext context); + public Map resolve(); } diff --git a/spring-shell2-core/src/main/java/org/springframework/shell2/standard/StandardMethodTargetResolver.java b/spring-shell2-core/src/main/java/org/springframework/shell2/standard/StandardMethodTargetResolver.java index 64985358..bb8830e3 100644 --- a/spring-shell2-core/src/main/java/org/springframework/shell2/standard/StandardMethodTargetResolver.java +++ b/spring-shell2-core/src/main/java/org/springframework/shell2/standard/StandardMethodTargetResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.shell2.standard; import java.util.HashMap; import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.shell2.MethodTarget; import org.springframework.shell2.MethodTargetResolver; @@ -31,12 +32,16 @@ import org.springframework.util.ReflectionUtils; * * @author Eric Bottard * @author Florent Biville + * @author Camilo Gonzalez */ @Component public class StandardMethodTargetResolver implements MethodTargetResolver { + @Autowired + private ApplicationContext applicationContext; + @Override - public Map resolve(ApplicationContext applicationContext) { + public Map resolve() { Map methodTargets = new HashMap<>(); Map commandBeans = applicationContext.getBeansWithAnnotation(ShellComponent.class); for (Object bean : commandBeans.values()) { diff --git a/spring-shell2-shell1-adapter/src/main/java/org/springframework/shell2/legacy/LegacyMethodTargetResolver.java b/spring-shell2-shell1-adapter/src/main/java/org/springframework/shell2/legacy/LegacyMethodTargetResolver.java index 2811cb77..265b230b 100644 --- a/spring-shell2-shell1-adapter/src/main/java/org/springframework/shell2/legacy/LegacyMethodTargetResolver.java +++ b/spring-shell2-shell1-adapter/src/main/java/org/springframework/shell2/legacy/LegacyMethodTargetResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.shell2.legacy; import java.util.HashMap; import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.shell.core.CommandMarker; import org.springframework.shell.core.annotation.CliCommand; @@ -30,16 +31,21 @@ import org.springframework.util.ReflectionUtils; /** * A {@link MethodTargetResolver} that discovers methods annotated with {@link CliCommand} on beans * implementing the {@link CommandMarker} marker interface. + * * @author Eric Bottard * @author Florent Biville + * @author Camilo Gonzalez */ @Component public class LegacyMethodTargetResolver implements MethodTargetResolver { + @Autowired + private ApplicationContext applicationContext; + @Override - public Map resolve(ApplicationContext context) { + public Map resolve() { Map methodTargets = new HashMap<>(); - Map beans = context.getBeansOfType(CommandMarker.class); + Map beans = applicationContext.getBeansOfType(CommandMarker.class); for (Object bean : beans.values()) { Class clazz = bean.getClass(); ReflectionUtils.doWithMethods(clazz, method -> { diff --git a/spring-shell2-shell1-adapter/src/test/java/org/springframework/shell2/legacy/LegacyMethodTargetResolverTest.java b/spring-shell2-shell1-adapter/src/test/java/org/springframework/shell2/legacy/LegacyMethodTargetResolverTest.java index 42dec593..0086dcf1 100644 --- a/spring-shell2-shell1-adapter/src/test/java/org/springframework/shell2/legacy/LegacyMethodTargetResolverTest.java +++ b/spring-shell2-shell1-adapter/src/test/java/org/springframework/shell2/legacy/LegacyMethodTargetResolverTest.java @@ -17,10 +17,14 @@ package org.springframework.shell2.legacy; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.data.MapEntry.entry; + +import java.util.Map; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.shell2.MethodTarget; @@ -28,11 +32,6 @@ import org.springframework.shell2.MethodTargetResolver; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.data.MapEntry.entry; - /** * Created by ericbottard on 09/12/15. */ @@ -40,9 +39,6 @@ import static org.assertj.core.data.MapEntry.entry; @ContextConfiguration(classes = LegacyMethodTargetResolverTest.Config.class) public class LegacyMethodTargetResolverTest { - @Autowired - private ApplicationContext applicationContext; - @Autowired private LegacyCommands legacyCommands; @@ -51,7 +47,7 @@ public class LegacyMethodTargetResolverTest { @Test public void findsMethodsAnnotatedWithCliCommand() throws Exception { - Map targets = resolver.resolve(applicationContext); + Map targets = resolver.resolve(); assertThat(targets).contains(entry( "register module",