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

IAnnotatedElementProvider to Access to ArgSpec/OptionSpec setter/getter field/method #2328

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 23 additions & 2 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6009,6 +6009,19 @@ public interface IScoped {
* @return {@link IScope} instance */
IScope getScope();
}

/**
* Provides access to the underlying {@link AnnotatedElement}.
* @since 4.8.0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a @since 4.8.0 javadoc tag here please?

*/
public interface IAnnotatedElementProvider {

/**
* @return {@link AnnotatedElement} used for reflection - field, method, ...
*/
AnnotatedElement getAnnotatedElement();

}

/** Customizable getter for obtaining the current value of an option or positional parameter.
* When an option or positional parameter is matched on the command line, its getter or setter is invoked to capture the value.
Expand Down Expand Up @@ -12093,7 +12106,7 @@ private static UnmatchedArgsBinding buildUnmatchedForMember(final IAnnotatedElem
}
}

static class FieldBinding implements IGetter, ISetter, IScoped {
static class FieldBinding implements IGetter, ISetter, IScoped, IAnnotatedElementProvider {
private final IScope scope;
private final Field field;
FieldBinding(Object scope, Field field) { this(ObjectScope.asScope(scope), field); }
Expand Down Expand Up @@ -12128,8 +12141,12 @@ public String toString() {
return String.format("%s(%s %s.%s)", getClass().getSimpleName(), field.getType().getName(),
field.getDeclaringClass().getName(), field.getName());
}
@Override
public AnnotatedElement getAnnotatedElement() {
return field;
}
}
static class MethodBinding implements IGetter, ISetter, IScoped {
static class MethodBinding implements IGetter, ISetter, IScoped, IAnnotatedElementProvider {
private final IScope scope;
private final Method method;
private final CommandSpec spec;
Expand Down Expand Up @@ -12166,6 +12183,10 @@ private ParameterException createParameterException(Object value, Throwable t) {
public String toString() {
return String.format("%s(%s)", getClass().getSimpleName(), method);
}
@Override
public AnnotatedElement getAnnotatedElement() {
return method;
}
}
private static class PicocliInvocationHandler implements InvocationHandler {
final Map<String, Object> map = new HashMap<String, Object>();
Expand Down
95 changes: 95 additions & 0 deletions src/test/java/picocli/IAnnotatedElementProviderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2017 Remko Popma

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 picocli;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Model.IAnnotatedElementProvider;
import picocli.CommandLine.Model.OptionSpec;
import picocli.CommandLine.Option;

/**
* Tests for {@link IAnnotatedElementProvider}.
* @since 4.8.0
*/
public class IAnnotatedElementProviderTest {

@Before public void setUp() { System.clearProperty("picocli.trace"); }
@After public void tearDown() { System.clearProperty("picocli.trace"); }

@Command(name = "annotated-element-provider-test")
static class IAnnotatedElementProviderTestCommand {
@Option(names = "-a") int a;
@Option(names = "-b") long b;
@Option(names = "-c")
void setC(long c) {

}
}

@Test
public void testAnnotatedElementAccess() throws Exception {
IAnnotatedElementProviderTestCommand command = new IAnnotatedElementProviderTestCommand();
CommandLine commandLine = new CommandLine(command);
CommandSpec spec = commandLine.getCommandSpec();
for (OptionSpec option: spec.options()) {
org.junit.Assert.assertTrue(option.setter() instanceof IAnnotatedElementProvider);
org.junit.Assert.assertTrue(option.getter() instanceof IAnnotatedElementProvider);

String optionName = option.names()[0];
AnnotatedElement setterAnnotatedElement = ((IAnnotatedElementProvider) option.setter()).getAnnotatedElement();
AnnotatedElement getterAnnotatedElement = ((IAnnotatedElementProvider) option.getter()).getAnnotatedElement();
switch (optionName) {
case "-a":
case "-b":
org.junit.Assert.assertTrue(setterAnnotatedElement instanceof Field);
org.junit.Assert.assertTrue(getterAnnotatedElement instanceof Field);

Field setterField = (Field) setterAnnotatedElement;
org.junit.Assert.assertEquals(IAnnotatedElementProviderTestCommand.class, setterField.getDeclaringClass());
org.junit.Assert.assertEquals(optionName.substring(1), setterField.getName());

Field getterField = (Field) getterAnnotatedElement;
org.junit.Assert.assertEquals(IAnnotatedElementProviderTestCommand.class, getterField.getDeclaringClass());
org.junit.Assert.assertEquals(optionName.substring(1), getterField.getName());
break;
case "-c":
org.junit.Assert.assertTrue(setterAnnotatedElement instanceof Method);
org.junit.Assert.assertTrue(getterAnnotatedElement instanceof Method);

Method setterMethod = (Method) setterAnnotatedElement;
org.junit.Assert.assertEquals(IAnnotatedElementProviderTestCommand.class, setterMethod.getDeclaringClass());
org.junit.Assert.assertEquals("setC", setterMethod.getName());

Method getterMethod = (Method) getterAnnotatedElement;
org.junit.Assert.assertEquals(IAnnotatedElementProviderTestCommand.class, getterMethod.getDeclaringClass());
org.junit.Assert.assertEquals("setC", getterMethod.getName());
break;
default:
org.junit.Assert.fail("Unexpected option: " + optionName);
}
}
}

}