Decoupling the MethodTargetResolver from the ApplicationContext
Fixes #39
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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 <command keyword(s)>} to actual behavior.
|
||||
*/
|
||||
public Map<String, MethodTarget> resolve(ApplicationContext context);
|
||||
public Map<String, MethodTarget> resolve();
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String, MethodTarget> resolve(ApplicationContext applicationContext) {
|
||||
public Map<String, MethodTarget> resolve() {
|
||||
Map<String, MethodTarget> methodTargets = new HashMap<>();
|
||||
Map<String, Object> commandBeans = applicationContext.getBeansWithAnnotation(ShellComponent.class);
|
||||
for (Object bean : commandBeans.values()) {
|
||||
|
||||
@@ -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<String, MethodTarget> resolve(ApplicationContext context) {
|
||||
public Map<String, MethodTarget> resolve() {
|
||||
Map<String, MethodTarget> methodTargets = new HashMap<>();
|
||||
Map<String, CommandMarker> beans = context.getBeansOfType(CommandMarker.class);
|
||||
Map<String, CommandMarker> beans = applicationContext.getBeansOfType(CommandMarker.class);
|
||||
for (Object bean : beans.values()) {
|
||||
Class<?> clazz = bean.getClass();
|
||||
ReflectionUtils.doWithMethods(clazz, method -> {
|
||||
|
||||
@@ -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<String, MethodTarget> targets = resolver.resolve(applicationContext);
|
||||
Map<String, MethodTarget> targets = resolver.resolve();
|
||||
|
||||
assertThat(targets).contains(entry(
|
||||
"register module",
|
||||
|
||||
Reference in New Issue
Block a user