Skip to content

Commit 3a820b5

Browse files
committed
Refine #1329
Extract method profile checking utility in Utils class
1 parent 56cf581 commit 3a820b5

3 files changed

Lines changed: 23 additions & 16 deletions

File tree

spring-shell-core-autoconfigure/src/main/java/org/springframework/shell/core/autoconfigure/CommandRegistryAutoConfiguration.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,17 @@
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2727
import org.springframework.context.ApplicationContext;
2828
import org.springframework.context.annotation.Bean;
29-
import org.springframework.context.annotation.Profile;
3029
import org.springframework.core.MethodIntrospector;
3130
import org.springframework.core.annotation.AnnotatedElementUtils;
32-
import org.springframework.core.env.Environment;
33-
import org.springframework.core.env.Profiles;
3431
import org.springframework.shell.core.command.Command;
3532
import org.springframework.shell.core.command.CommandRegistry;
3633
import org.springframework.shell.core.command.annotation.support.CommandFactoryBean;
3734
import org.springframework.shell.core.utils.Utils;
3835
import org.springframework.stereotype.Component;
3936
import org.springframework.util.ReflectionUtils;
4037

38+
import static org.springframework.shell.core.utils.Utils.isProfileActive;
39+
4140
@AutoConfiguration
4241
public class CommandRegistryAutoConfiguration {
4342

@@ -79,9 +78,4 @@ private void registerAnnotatedCommands(ApplicationContext applicationContext, Co
7978
}
8079
}
8180

82-
private boolean isProfileActive(Method method, Environment environment) {
83-
Profile profile = AnnotatedElementUtils.findMergedAnnotation(method, Profile.class);
84-
return profile == null || environment.acceptsProfiles(Profiles.of(profile.value()));
85-
}
86-
8781
}

spring-shell-core/src/main/java/org/springframework/shell/core/command/annotation/support/EnableCommandRegistrar.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
import org.springframework.beans.factory.support.RootBeanDefinition;
2525
import org.springframework.context.EnvironmentAware;
2626
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
27-
import org.springframework.context.annotation.Profile;
2827
import org.springframework.core.MethodIntrospector;
2928
import org.springframework.core.annotation.AnnotatedElementUtils;
3029
import org.springframework.core.env.Environment;
31-
import org.springframework.core.env.Profiles;
3230
import org.springframework.core.type.AnnotationMetadata;
3331
import org.springframework.shell.core.ConsoleInputProvider;
3432
import org.springframework.shell.core.SystemShellRunner;
@@ -37,6 +35,8 @@
3735
import org.springframework.shell.core.command.annotation.EnableCommand;
3836
import org.springframework.util.ReflectionUtils;
3937

38+
import static org.springframework.shell.core.utils.Utils.isProfileActive;
39+
4040
/**
4141
* {@link ImportBeanDefinitionRegistrar} for {@link EnableCommand @EnableCommands}.
4242
*
@@ -108,7 +108,7 @@ private void registerCommands(Class<?> candidateClass, BeanDefinitionRegistry re
108108

109109
private void registerAnnotatedMethods(Class<?> candidateClass, BeanDefinitionRegistry registry) {
110110
ReflectionUtils.MethodFilter filter = method -> AnnotatedElementUtils.hasAnnotation(method, Command.class)
111-
&& isProfileActive(method);
111+
&& isProfileActive(method, this.environment);
112112
Set<Method> methods = MethodIntrospector.selectMethods(candidateClass, filter);
113113
for (Method method : methods) {
114114
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
@@ -118,9 +118,4 @@ private void registerAnnotatedMethods(Class<?> candidateClass, BeanDefinitionReg
118118
}
119119
}
120120

121-
private boolean isProfileActive(Method method) {
122-
Profile profile = AnnotatedElementUtils.findMergedAnnotation(method, Profile.class);
123-
return profile == null || environment.acceptsProfiles(Profiles.of(profile.value()));
124-
}
125-
126121
}

spring-shell-core/src/main/java/org/springframework/shell/core/utils/Utils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.shell.core.utils;
1818

19+
import java.lang.reflect.Method;
1920
import java.util.Comparator;
2021
import java.util.HashSet;
2122
import java.util.List;
@@ -25,6 +26,10 @@
2526
import jakarta.validation.Validator;
2627
import jakarta.validation.ValidatorFactory;
2728

29+
import org.springframework.context.annotation.Profile;
30+
import org.springframework.core.annotation.AnnotatedElementUtils;
31+
import org.springframework.core.env.Environment;
32+
import org.springframework.core.env.Profiles;
2833
import org.springframework.shell.core.command.AbstractCommand;
2934
import org.springframework.shell.core.command.Command;
3035
import org.springframework.shell.core.command.CommandContext;
@@ -41,6 +46,19 @@
4146
*/
4247
public class Utils {
4348

49+
/**
50+
* Check if a method is annotated with an active {@link Profile} in the given
51+
* environment.
52+
* @param method the method to check
53+
* @param environment the environment
54+
* @return true if the method has an active profile, false otherwise
55+
* @since 4.0.2
56+
*/
57+
public static boolean isProfileActive(Method method, Environment environment) {
58+
Profile profile = AnnotatedElementUtils.findMergedAnnotation(method, Profile.class);
59+
return profile == null || environment.acceptsProfiles(Profiles.of(profile.value()));
60+
}
61+
4462
/**
4563
* Split a CamelCase class name into separate words. Used to derive command group
4664
* names. For example, "MyCommands" becomes "My Commands".

0 commit comments

Comments
 (0)