Moving the project to a multi-level maven project to allow for separation of the adapter from the core of the project
Fixes #28
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.shell2.jcommander;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import com.beust.jcommander.DynamicParameter;
|
||||
import com.beust.jcommander.JCommander;
|
||||
import com.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.ParametersDelegate;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.shell2.CompletionContext;
|
||||
import org.springframework.shell2.CompletionProposal;
|
||||
import org.springframework.shell2.ParameterDescription;
|
||||
import org.springframework.shell2.ParameterResolver;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Provides integration with JCommander.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class JCommanderParameterResolver implements ParameterResolver {
|
||||
|
||||
private static final Collection<Class<? extends Annotation>> JCOMMANDER_ANNOTATIONS =
|
||||
Arrays.asList(Parameter.class, DynamicParameter.class, ParametersDelegate.class);
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter parameter) {
|
||||
AtomicBoolean isSupported = new AtomicBoolean(false);
|
||||
|
||||
Class<?> parameterType = parameter.getParameterType();
|
||||
ReflectionUtils.doWithFields(parameterType, field -> {
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
boolean hasAnnotation = Arrays.stream(field.getAnnotations())
|
||||
.map(Annotation::annotationType)
|
||||
.anyMatch(JCOMMANDER_ANNOTATIONS::contains);
|
||||
isSupported.compareAndSet(false, 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);
|
||||
});
|
||||
return isSupported.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolve(MethodParameter methodParameter, List<String> words) {
|
||||
Object pojo = BeanUtils.instantiateClass(methodParameter.getParameterType());
|
||||
JCommander jCommander = new JCommander();
|
||||
jCommander.addObject(pojo);
|
||||
jCommander.setAcceptUnknownOptions(true);
|
||||
jCommander.parse(words.toArray(new String[words.size()]));
|
||||
return pojo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParameterDescription describe(MethodParameter parameter) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CompletionProposal> complete(MethodParameter parameter, CompletionContext context) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.shell2.jcommander;
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.shell2.jcommander;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
|
||||
/**
|
||||
* Created by ericbottard on 15/12/15.
|
||||
*/
|
||||
public class FieldCollins {
|
||||
|
||||
@Parameter(names = "--name")
|
||||
private String name;
|
||||
|
||||
@Parameter(names = "-level")
|
||||
private int level;
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.shell2.jcommander;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.shell2.Utils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Created by ericbottard on 15/12/15.
|
||||
*/
|
||||
public class JCommanderParameterResolverTest {
|
||||
|
||||
private static final Method COMMAND_METHOD = ReflectionUtils.findMethod(MyLordCommands.class, "genesis", FieldCollins.class);
|
||||
|
||||
private JCommanderParameterResolver resolver = new JCommanderParameterResolver();
|
||||
|
||||
@Test
|
||||
public void testSupportsJCommanderPojos() throws Exception {
|
||||
assertThat(resolver.supports(Utils.createMethodParameter(COMMAND_METHOD, 0))).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
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(" ")));
|
||||
|
||||
assertThat(resolved.getName()).isEqualTo("foo");
|
||||
assertThat(resolved.getLevel()).isEqualTo(2);
|
||||
assertThat(resolved.getRest()).containsOnlyOnce("something-else", "yet-something-else");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.shell2.jcommander;
|
||||
|
||||
/**
|
||||
* Created by ericbottard on 15/12/15.
|
||||
*/
|
||||
public class MyLordCommands {
|
||||
|
||||
public void genesis(FieldCollins fieldCollins) {
|
||||
|
||||
}
|
||||
|
||||
public void apocalypse(String param) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user