Skip to content

Commit

Permalink
Simplified detection of getter & setter in TypedMember constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
rgoldberg authored and remkop committed Jan 11, 2022
1 parent 8598e7d commit 62d708a
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -11096,10 +11096,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 @@ -11113,11 +11120,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

0 comments on commit 62d708a

Please sign in to comment.