Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified detection of getter & setter in TypedMember constructor #1518

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -11091,10 +11091,17 @@ private TypedMember(Method method, IScope scope, CommandSpec spec) {
accessible.setAccessible(true);
name = propertyName(method.getName());
Class<?>[] parameterTypes = method.getParameterTypes();
boolean isGetter = parameterTypes.length == 0 && method.getReturnType() != Void.TYPE && method.getReturnType() != Void.class;
boolean isSetter = parameterTypes.length > 0;
if (isSetter == isGetter) { throw new InitializationException("Invalid method, must be either getter or setter: " + method); }
if (isGetter) {
if (parameterTypes.length > 0) {
// accepts arguments, so must be a setter
typeInfo = createTypeInfo(parameterTypes[0], method.getGenericParameterTypes()[0]);
MethodBinding binding = new MethodBinding(scope, method, spec);
getter = binding; setter = binding;
initialValueState = InitialValueState.UNAVAILABLE; // arg is setter method;
} else if (method.getReturnType() == Void.TYPE || method.getReturnType() == Void.class) {
// neither accepts arguments, nor returns non-void, so cannot be a setter or a getter, respectively
throw new InitializationException("Invalid method, must be either getter or setter: " + method);
} else {
// does not accept arguments, but returns non-void, so is a getter
typeInfo = createTypeInfo(method.getReturnType(), method.getGenericReturnType());
if (ObjectScope.isProxyClass(scope)) {
Object proxy = ObjectScope.tryGet(scope);
Expand All @@ -11108,11 +11115,6 @@ private TypedMember(Method method, IScope scope, CommandSpec spec) {
getter = binding; setter = binding;
}
initialValueState = InitialValueState.POSTPONED; // the initial value can be obtained from the getter
} else {
typeInfo = createTypeInfo(parameterTypes[0], method.getGenericParameterTypes()[0]);
MethodBinding binding = new MethodBinding(scope, method, spec);
getter = binding; setter = binding;
initialValueState = InitialValueState.UNAVAILABLE; // arg is setter method;
}
}
TypedMember(MethodParam param, IScope scope) {
Expand Down