Skip to content
Browse files

Handle multi value on method setters

Signed-off-by: Alin Dreghiciu <adreghiciu@gmail.com>
  • Loading branch information...
1 parent 8ced1f7 commit 670d36d49fcb07b06e70b13b6dec9d7093c584b4 @adreghiciu adreghiciu committed
View
6 ...him/src/main/java/org/eclipse/tesla/shell/gshell/internal/GShellShimActionPreparator.java
@@ -84,6 +84,8 @@ protected CommandDescriptor getCommandDescriptor( final Action action )
{
options.add(
optionDescriptor( option, resourceBundle, field.getName() )
+ .setMultiValued( field.getType().isArray()
+ || Collection.class.isAssignableFrom( field.getType() ) )
.setInjector( new ActionFieldInjector( commandAction, field ) )
);
}
@@ -96,6 +98,7 @@ protected CommandDescriptor getCommandDescriptor( final Action action )
{
options.add(
optionDescriptor( option, resourceBundle, method.getName() )
+ .setMultiValued( true )
.setInjector( new ActionMethodInjector( commandAction, method ) )
);
}
@@ -158,8 +161,7 @@ private ArgumentDescriptor argumentDescriptor( final Argument argument,
return argumentDescriptor( argument )
.setName( method.getName() )
.loadDescription( resourceBundle, method.getName() )
- .setMultiValued( method.getParameterTypes()[0].isArray()
- || Collection.class.isAssignableFrom( method.getParameterTypes()[0] ) );
+ .setMultiValued( true );
}
private ArgumentDescriptor argumentDescriptor( final Argument argument )
View
39 ...gi-support/src/main/java/org/eclipse/tesla/shell/preparator/AbstractActionPreparator.java
@@ -413,16 +413,49 @@ protected Object convert( final Action action,
final boolean multiValue )
throws Exception
{
- if ( toType == String.class )
+ if ( value == null )
{
- return value != null ? value.toString() : null;
+ return null;
+ }
+ if ( toType == String.class && !multiValue )
+ {
+ return value.toString();
}
Object toConvert = value;
if ( multiValue && !( toConvert.getClass().isArray() || toConvert instanceof Collection ) )
{
toConvert = new Object[]{ toConvert };
}
- return new DefaultConverter( action.getClass().getClassLoader() ).convert( toConvert, toType );
+ final DefaultConverter converter = new DefaultConverter( action.getClass().getClassLoader() );
+ try
+ {
+ return converter.convert( toConvert, toType );
+ }
+ catch ( Exception e )
+ {
+ if ( !multiValue )
+ {
+ throw e;
+ }
+ final List<Object> converted = new ArrayList<Object>();
+ if ( toConvert.getClass().isArray() )
+ {
+ for ( final Object item : (Object[]) toConvert )
+ {
+ converted.add( convert( action, item, toType, false ) );
+ }
+ return converted;
+ }
+ else if ( toConvert instanceof Collection )
+ {
+ for ( final Object item : (Collection) toConvert )
+ {
+ converted.add( convert( action, item, toType, false ) );
+ }
+ return converted;
+ }
+ throw e;
+ }
}
}
View
26 ...l-osgi-support/src/main/java/org/eclipse/tesla/shell/preparator/ActionMethodInjector.java
@@ -10,6 +10,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
+import java.util.Collection;
/**
* A {@link Method} {@link ActionInjector}.
@@ -62,7 +63,30 @@ public void set( final Object value )
method.setAccessible( true );
try
{
- method.invoke( instance, value );
+ if ( value != null
+ && value instanceof Collection
+ && !( method.getParameterTypes()[0].isArray()
+ || Collection.class.isAssignableFrom( method.getParameterTypes()[0] ) ) )
+ {
+ for ( final Object item : (Collection) value )
+ {
+ method.invoke( instance, item );
+ }
+ }
+ else if ( value != null
+ && value.getClass().isArray()
+ && !( method.getParameterTypes()[0].isArray()
+ || Collection.class.isAssignableFrom( method.getParameterTypes()[0] ) ) )
+ {
+ for ( final Object item : (Object[]) value )
+ {
+ method.invoke( instance, item );
+ }
+ }
+ else
+ {
+ method.invoke( instance, value );
+ }
}
catch ( InvocationTargetException e )
{

0 comments on commit 670d36d

Please sign in to comment.
Something went wrong with that request. Please try again.