Drop jcommander

- Fixes #395
This commit is contained in:
Janne Valkealahti
2022-04-29 15:55:21 +01:00
parent d16cda75f3
commit 4e625feb03
15 changed files with 0 additions and 630 deletions

13
pom.xml
View File

@@ -23,7 +23,6 @@
<properties>
<jline.version>3.21.0</jline.version>
<jcommander.version>1.81</jcommander.version>
<antlr-st4.version>4.3.1</antlr-st4.version>
<jimfs.version>1.2</jimfs.version>
</properties>
@@ -33,7 +32,6 @@
<module>spring-shell-core-test-support</module>
<module>spring-shell-standard</module>
<module>spring-shell-standard-commands</module>
<module>spring-shell-jcommander-adapter</module>
<module>spring-shell-table</module>
<module>spring-shell-docs</module>
<module>spring-shell-dependencies</module>
@@ -64,11 +62,6 @@
<artifactId>spring-shell-standard-commands</artifactId>
<version>2.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-jcommander-adapter</artifactId>
<version>2.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-starter</artifactId>
@@ -100,12 +93,6 @@
<version>${jline.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>${jcommander.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>ST4</artifactId>

View File

@@ -26,16 +26,6 @@
<artifactId>spring-shell-standard-commands</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-jcommander-adapter</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -1,39 +0,0 @@
/*
* Copyright 2017-2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.boot;
import com.beust.jcommander.JCommander;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.shell.jcommander.JCommanderParameterResolver;
import org.springframework.context.annotation.Bean;
/**
* Registers JCommanderParameterResolver and supporting beans as appropriate.
*
* @author Eric Bottard
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ JCommander.class, JCommanderParameterResolver.class })
public class JCommanderParameterResolverAutoConfiguration {
@Bean
public JCommanderParameterResolver jCommanderParameterResolver() {
return new JCommanderParameterResolver();
}
}

View File

@@ -38,16 +38,6 @@
<artifactId>spring-shell-standard-commands</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-jcommander-adapter</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-table</artifactId>

View File

@@ -1,40 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-shell-jcommander-adapter</artifactId>
<name>Spring Shell JCommander Adapter</name>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-parent</artifactId>
<version>2.1.0-SNAPSHOT</version>
</parent>
<description>Adapter classes to allow JCommander type annotations and parsing via Spring Shell 2</description>
<dependencies>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-core</artifactId>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,158 +0,0 @@
/*
* Copyright 2015-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.jcommander;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.metadata.BeanDescriptor;
import com.beust.jcommander.DynamicParameter;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.ParametersDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.shell.CompletionContext;
import org.springframework.shell.CompletionProposal;
import org.springframework.shell.ParameterDescription;
import org.springframework.shell.ParameterResolver;
import org.springframework.shell.Utils;
import org.springframework.shell.ValueResult;
import org.springframework.util.ReflectionUtils;
import static org.springframework.shell.Utils.unCamelify;
/**
* Provides integration with JCommander.
*
* @author Eric Bottard
* @author Josh Long
*/
public class JCommanderParameterResolver implements ParameterResolver {
private final static Logger log = LoggerFactory.getLogger(JCommanderParameterResolver.class);
private static final Collection<Class<? extends Annotation>> JCOMMANDER_ANNOTATIONS = Arrays.asList(Parameter.class,
DynamicParameter.class, ParametersDelegate.class);
private Validator validator = Utils.defaultValidator();
@Autowired(required = false)
public void setValidatorFactory(ValidatorFactory validatorFactory) {
this.validator = validatorFactory.getValidator();
}
@Override
public boolean supports(MethodParameter parameter) {
AtomicBoolean isSupported = new AtomicBoolean(false);
Class<?> parameterType = parameter.getParameterType();
log.debug("isLegalReflectiveAccess before");
ReflectionUtils.doWithFields(parameterType, field -> {
ReflectionUtils.makeAccessible(field);
boolean hasAnnotation = Arrays.stream(field.getAnnotations())
.map(Annotation::annotationType)
.anyMatch(JCOMMANDER_ANNOTATIONS::contains);
isSupported.compareAndSet(false, hasAnnotation);
log.debug("isLegalReflectiveAccess fields {}", hasAnnotation);
});
ReflectionUtils.doWithMethods(parameterType, method -> {
ReflectionUtils.makeAccessible(method);
boolean hasAnnotation = Arrays.stream(method.getAnnotations())
.map(Annotation::annotationType)
.anyMatch(Parameter.class::equals);
isSupported.compareAndSet(false, hasAnnotation);
log.debug("isLegalReflectiveAccess methods {}", hasAnnotation);
});
log.debug("isLegalReflectiveAccess supports {}", isSupported.get());
return isSupported.get();
}
@Override
public ValueResult resolve(MethodParameter methodParameter, List<String> words) {
JCommander jCommander = createJCommander(methodParameter);
jCommander.parse(words.toArray(new String[words.size()]));
return new ValueResult(methodParameter, jCommander.getObjects().get(0));
}
private JCommander createJCommander(MethodParameter methodParameter) {
Object pojo = BeanUtils.instantiateClass(methodParameter.getParameterType());
return new JCommander(pojo);
}
@Override
public Stream<ParameterDescription> describe(MethodParameter parameter) {
JCommander jCommander = createJCommander(parameter);
Stream<com.beust.jcommander.ParameterDescription> jCommanderDescriptions = streamAllJCommanderDescriptions(
jCommander);
BeanDescriptor constraintsForClass = validator.getConstraintsForClass(parameter.getParameterType());
return jCommanderDescriptions
.map(j -> new ParameterDescription(parameter,
unCamelify(j.getParameterized().getType().getSimpleName()))
.keys(Arrays.asList(j.getParameter().names()))
.help(j.getDescription())
.mandatoryKey(!j.equals(jCommander.getMainParameterValue()))
// Not ideal as this does not take reverse-conversion into account, but just toString()
.defaultValue(j.getDefault() == null ? "" : String.valueOf(j.getDefault()))
.elementDescriptor(
constraintsForClass.getConstraintsForProperty(j.getParameterized().getName())));
}
/**
* Return <em>all</em> JCommander parameter descriptions, including the "main" parameter
* if present.
*/
private Stream<com.beust.jcommander.ParameterDescription> streamAllJCommanderDescriptions(JCommander jCommander) {
return Stream.concat(
jCommander.getParameters().stream(),
jCommander.getMainParameterValue() != null ? Stream.of(jCommander.getMainParameterValue()) : Stream.empty());
}
@Override
public List<CompletionProposal> complete(MethodParameter parameter, CompletionContext context) {
JCommander jCommander = createJCommander(parameter);
List<String> words = context.getWords();
try {
jCommander.parseWithoutValidation(words.toArray(new String[words.size()]));
}
catch (ParameterException ignored) {
// Exception here certainly means current buffer is not parseable in full.
// Better to bail out now.
return Collections.emptyList();
}
return streamAllJCommanderDescriptions(jCommander)
.filter(p -> !p.isAssigned())
.flatMap(p -> Arrays.stream(p.getParameter().names()))
.map(CompletionProposal::new)
.collect(Collectors.toList());
}
}

View File

@@ -1,22 +0,0 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Provides integration with JCommander.
*
* @author Eric Bottard
*/
package org.springframework.shell.jcommander;

View File

@@ -1,18 +0,0 @@
[
{
"name":"com.beust.jcommander.JCommander"
},
{
"name":"com.beust.jcommander.converters.IntegerConverter",
"queryAllDeclaredConstructors":true,
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]
},
{
"name":"com.beust.jcommander.validators.NoValidator",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"com.beust.jcommander.validators.NoValueValidator",
"methods":[{"name":"<init>","parameterTypes":[] }]
}
]

View File

@@ -1,64 +0,0 @@
/*
* Copyright 2015 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.jcommander;
import java.util.ArrayList;
import java.util.List;
import com.beust.jcommander.Parameter;
/**
* A POJO with fields annotated with JCommander annotations.
*
* @author Eric Bottard
* @see MyLordCommands#genesis(FieldCollins)
*/
public class FieldCollins {
@Parameter(names = {"--name", "-n"}, description = "what's in a name?")
private String name;
@Parameter(names = "-level")
private int level = 3;
@Parameter(description = "rest")
private List<String> rest = new ArrayList<>();
public List<String> getRest() {
return rest;
}
public void setRest(List<String> rest) {
this.rest = rest;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}

View File

@@ -1,117 +0,0 @@
/*
* Copyright 2015-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.jcommander;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import org.springframework.core.MethodParameter;
import org.springframework.shell.CompletionContext;
import org.springframework.shell.CompletionProposal;
import org.springframework.shell.ParameterDescription;
import org.springframework.shell.Utils;
import org.springframework.util.ReflectionUtils;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit test for {@link JCommanderParameterResolver}.
*
* @author Eric Bottard
* @author Florent Biville
*/
public class JCommanderParameterResolverTest {
private static final Method COMMAND_METHOD = ReflectionUtils.findMethod(MyLordCommands.class, "genesis", FieldCollins.class);
private JCommanderParameterResolver resolver = new JCommanderParameterResolver();
@Test
@DisabledOnJre({JRE.JAVA_17})
public void testSupportsJCommanderPojos() throws Exception {
assertThat(resolver.supports(Utils.createMethodParameter(COMMAND_METHOD, 0))).isEqualTo(true);
}
@Test
@DisabledOnJre({JRE.JAVA_17})
public void testDoesNotSupportsNonJCommanderPojos() throws Exception {
Method method = ReflectionUtils.findMethod(MyLordCommands.class, "apocalypse", String.class);
assertThat(resolver.supports(Utils.createMethodParameter(method, 0))).isFalse();
}
@Test
public void testPojoValuesAreCorrectlySet() {
MethodParameter methodParameter = Utils.createMethodParameter(COMMAND_METHOD, 0);
FieldCollins resolved = (FieldCollins) resolver
.resolve(methodParameter, asList("--name foo -level 2 something-else yet-something-else".split(" ")))
.resolvedValue();
assertThat(resolved.getName()).isEqualTo("foo");
assertThat(resolved.getLevel()).isEqualTo(2);
assertThat(resolved.getRest()).containsOnlyOnce("something-else", "yet-something-else");
}
@Test
public void testDescribe() {
MethodParameter methodParameter = Utils.createMethodParameter(COMMAND_METHOD, 0);
Stream<ParameterDescription> desciptions = resolver.describe(methodParameter);
ParameterDescription name = new ParameterDescription(methodParameter, "string")
.keys(Arrays.asList("--name", "-n"))
.help("what's in a name?")
.defaultValue("");
ParameterDescription level = new ParameterDescription(methodParameter, "int")
.keys(singletonList("-level"))
.defaultValue("3");
ParameterDescription rest = new ParameterDescription(methodParameter, "list")
.defaultValue("[]")
.mandatoryKey(false)
.help("rest");
assertThat(desciptions).contains(name, level, rest);
}
@Test
public void testCanComplete() {
MethodParameter methodParameter = Utils.createMethodParameter(COMMAND_METHOD, 0);
CompletionContext context = new CompletionContext(Collections.emptyList(), 0, 0);
Stream<String> proposals = resolver.complete(methodParameter, context).stream().map(CompletionProposal::value);
assertThat(proposals).containsExactly("--name", "-n", "-level");
context = new CompletionContext(Arrays.asList("-n", "foo"), 0, 0);
proposals = resolver.complete(methodParameter, context).stream().map(CompletionProposal::value);
assertThat(proposals).containsExactly("-level");
}
@Test
public void testCannotComplete() {
MethodParameter methodParameter = Utils.createMethodParameter(COMMAND_METHOD, 0);
CompletionContext context = new CompletionContext(Arrays.asList("--name"), 0, 0);
Stream<String> proposals = resolver.complete(methodParameter, context).stream().map(CompletionProposal::value);
assertThat(proposals).isEmpty();
}
}

View File

@@ -1,40 +0,0 @@
/*
* Copyright 2015 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.jcommander;
/**
* An hypothetical command class, with one method using JCommander args, and the other not.
*
* @author Eric Bottard
*/
public class MyLordCommands {
/**
* This method should be supported.
*/
public void genesis(FieldCollins fieldCollins) {
}
/**
* This method is not.
*/
public void apocalypse(String param) {
}
}

View File

@@ -39,10 +39,6 @@
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-starter</artifactId>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
</dependency>
</dependencies>
<profiles>

View File

@@ -1,55 +0,0 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.samples.jcommander;
import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.Min;
import com.beust.jcommander.Parameter;
/**
* An example straight from the JCommander documentation.
*
* @author Eric Bottard
* @author Cédric Beust
*/
public class Args {
@Parameter
private List<String> parameters = new ArrayList<>();
@Min(3)
@Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
private Integer verbose = 1;
@Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
private String groups;
@Parameter(names = "-debug", description = "Debug mode")
private boolean debug = false;
@Override
public String toString() {
return "Args{" +
"parameters=" + parameters +
", verbose=" + verbose +
", groups='" + groups + '\'' +
", debug=" + debug +
'}';
}
}

View File

@@ -1,36 +0,0 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.samples.jcommander;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;
import javax.validation.Valid;
/**
* A class with JCommander commands.
*
* @author Eric Bottard
*/
@ShellComponent
public class JCommanderCommands {
@ShellMethod("Bind parameters to JCommander POJO.")
public String jcommander(@ShellOption(optOut = true) @Valid Args args) {
return "You said " + args;
}
}

View File

@@ -30,10 +30,6 @@
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-standard-commands</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-jcommander-adapter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-table</artifactId>