From 9f2da7561af1f1ad4f69aa3056b19b27c8c0c7f0 Mon Sep 17 00:00:00 2001 From: Jiaqi Guo Date: Wed, 7 Feb 2018 15:11:31 -0800 Subject: [PATCH] Format with Google style --- .../jcli/ArgumentProcessorFactory.java | 48 ++-- .../cyclopsgroup/jcli/AutoCompletable.java | 33 ++- .../cyclopsgroup/jcli/ValidationResult.java | 157 +++++------ .../jcli/annotation/Argument.java | 27 +- .../org/cyclopsgroup/jcli/annotation/Cli.java | 39 ++- .../jcli/annotation/MultiValue.java | 39 ++- .../cyclopsgroup/jcli/annotation/Option.java | 55 ++-- .../jcli/annotation/package-info.java | 2 +- .../jcli/example/UserControlAction.java | 35 ++- .../jcli/impl/DefaultBeanProcessor.java | 98 +++---- .../jcli/impl/DefaultHelpPrinter.java | 60 ++--- .../jcli/impl/MultiValueReference.java | 66 ++--- .../cyclopsgroup/jcli/impl/OptionHelp.java | 81 +++--- .../jcli/impl/ParsingContextBuilder.java | 160 ++++++------ .../jcli/impl/SingleValueReference.java | 31 +-- .../jcli/jline/ArgumentsInspector.java | 217 +++++++-------- .../jcli/jline/ArgumentsInspectorState.java | 43 ++- .../cyclopsgroup/jcli/jline/package-info.java | 2 +- .../org/cyclopsgroup/jcli/package-info.java | 2 +- .../org/cyclopsgroup/jcli/spi/Argument.java | 19 +- .../java/org/cyclopsgroup/jcli/spi/Cli.java | 27 +- .../jcli/spi/CommandLineBuilder.java | 145 +++++----- .../jcli/spi/CommandLineParser.java | 15 +- .../org/cyclopsgroup/jcli/spi/Option.java | 81 +++--- .../cyclopsgroup/jcli/spi/ParsingContext.java | 47 ++-- .../cyclopsgroup/jcli/spi/package-info.java | 2 +- .../org/cyclopsgroup/jcli/GnuParserTest.java | 229 ++++++++-------- .../cyclopsgroup/jcli/MultiActionTest.java | 26 +- .../jcli/WithMultiValueOption.java | 37 ++- .../cyclopsgroup/jcli/WithSimpleArgument.java | 35 ++- .../org/cyclopsgroup/jcli/WithoutCli.java | 37 ++- .../jcli/example/UserControlTest.java | 27 +- .../impl/DefaultArgumentProcessorTest.java | 72 +++-- .../jcli/jline/ArgumentsInspectorTest.java | 56 ++-- .../jcli/jline/CliCompletorTest.java | 247 ++++++++---------- 35 files changed, 1065 insertions(+), 1232 deletions(-) diff --git a/src/main/java/org/cyclopsgroup/jcli/ArgumentProcessorFactory.java b/src/main/java/org/cyclopsgroup/jcli/ArgumentProcessorFactory.java index 3fc713c..58e6b09 100644 --- a/src/main/java/org/cyclopsgroup/jcli/ArgumentProcessorFactory.java +++ b/src/main/java/org/cyclopsgroup/jcli/ArgumentProcessorFactory.java @@ -10,30 +10,30 @@ * * @author Jiaqi Guo */ -public abstract class ArgumentProcessorFactory -{ - /** - * @return Instance of ArgumentProcessorFactory. The implementation is determined by {@link ServiceLoader} - */ - static ArgumentProcessorFactory getInstance() - { - Iterator factories = ServiceLoader.load( ArgumentProcessorFactory.class ).iterator(); - if ( factories.hasNext() ) - { - return factories.next(); - } - throw new AssertionError( "Can't find an implementation of " + ArgumentProcessorFactory.class.getName() - + " from service loader" ); +public abstract class ArgumentProcessorFactory { + /** + * @return Instance of ArgumentProcessorFactory. The implementation is determined by + * {@link ServiceLoader} + */ + static ArgumentProcessorFactory getInstance() { + Iterator factories = + ServiceLoader.load(ArgumentProcessorFactory.class).iterator(); + if (factories.hasNext()) { + return factories.next(); } + throw new AssertionError("Can't find an implementation of " + + ArgumentProcessorFactory.class.getName() + " from service loader"); + } - /** - * Create new instance of {@link ArgumentProcessor}. The implementation of factory needs to implement this method to - * create customized argument processor - * - * @param Type of bean to process - * @param beanType Type of bean to process - * @param parser Command line parser that is aware of command line syntax - * @return Instance of argument processor implementation - */ - protected abstract ArgumentProcessor newProcessor( Class beanType, CommandLineParser parser ); + /** + * Create new instance of {@link ArgumentProcessor}. The implementation of factory needs to + * implement this method to create customized argument processor + * + * @param Type of bean to process + * @param beanType Type of bean to process + * @param parser Command line parser that is aware of command line syntax + * @return Instance of argument processor implementation + */ + protected abstract ArgumentProcessor newProcessor(Class beanType, + CommandLineParser parser); } diff --git a/src/main/java/org/cyclopsgroup/jcli/AutoCompletable.java b/src/main/java/org/cyclopsgroup/jcli/AutoCompletable.java index 9cfad7c..8cf7023 100644 --- a/src/main/java/org/cyclopsgroup/jcli/AutoCompletable.java +++ b/src/main/java/org/cyclopsgroup/jcli/AutoCompletable.java @@ -7,22 +7,21 @@ * * @author Jiaqi Guo */ -public interface AutoCompletable -{ - /** - * Suggest candidates for an option with given partial input - * - * @param optionName Name of option - * @param partialOption Given partial input - * @return List of candidates or NULL if it can't figure out - */ - List suggestOption( String optionName, String partialOption ); +public interface AutoCompletable { + /** + * Suggest candidates for an option with given partial input + * + * @param optionName Name of option + * @param partialOption Given partial input + * @return List of candidates or NULL if it can't figure out + */ + List suggestOption(String optionName, String partialOption); - /** - * Suggest candidates for argument with given partial input - * - * @param partialArgument Partial argument input - * @return List of candidates or NULL if it can't figure out - */ - List suggestArgument( String partialArgument ); + /** + * Suggest candidates for argument with given partial input + * + * @param partialArgument Partial argument input + * @return List of candidates or NULL if it can't figure out + */ + List suggestArgument(String partialArgument); } diff --git a/src/main/java/org/cyclopsgroup/jcli/ValidationResult.java b/src/main/java/org/cyclopsgroup/jcli/ValidationResult.java index 576bbd4..a49f5ad 100644 --- a/src/main/java/org/cyclopsgroup/jcli/ValidationResult.java +++ b/src/main/java/org/cyclopsgroup/jcli/ValidationResult.java @@ -9,111 +9,94 @@ /** * Argument validation result coming from {@link ArgumentProcessor#validate(String[])} */ -public final class ValidationResult -{ - /** - * A violation indicating required argument is missing - */ - public static final class ArgumentMissing - extends Violation - { - } - - /** - * Type of violation where a required option is missing - */ - public static final class OptionMissing - extends Violation - { - private final String optionName; +public final class ValidationResult { + /** + * A violation indicating required argument is missing + */ + public static final class ArgumentMissing extends Violation { + } - /** - * @param optionName Name of missing option - */ - public OptionMissing( String optionName ) - { - Validate.notNull( optionName, "Name of missing option can't be NULL" ); - this.optionName = optionName; - } - - /** - * Get name of missing option - * - * @return optionName Name of option missed - */ - public String getOptionName() - { - return optionName; - } - } + /** + * Type of violation where a required option is missing + */ + public static final class OptionMissing extends Violation { + private final String optionName; /** - * Violation where an unexpected option value is found + * @param optionName Name of missing option */ - public static final class UnexpectedOption - extends Violation - { - private final String optionName; - - /** - * @param optionName Name of unexpected option - */ - public UnexpectedOption( String optionName ) - { - Validate.notNull( optionName, "Name of missing option can't be NULL" ); - this.optionName = optionName; - } - - /** - * Get name of missing option - * - * @return optionName Name of option missed - */ - public String getOptionName() - { - return optionName; - } + public OptionMissing(String optionName) { + Validate.notNull(optionName, "Name of missing option can't be NULL"); + this.optionName = optionName; } /** - * A validation violation + * Get name of missing option + * + * @return optionName Name of option missed */ - public static abstract class Violation - { - Violation() - { - } + public String getOptionName() { + return optionName; } + } - private final List violations = new ArrayList(); + /** + * Violation where an unexpected option value is found + */ + public static final class UnexpectedOption extends Violation { + private final String optionName; /** - * Add a new violation to validation result - * - * @param Type of validation violation - * @param violation Violation + * @param optionName Name of unexpected option */ - public void addViolation( T violation ) - { - violations.add( violation ); + public UnexpectedOption(String optionName) { + Validate.notNull(optionName, "Name of missing option can't be NULL"); + this.optionName = optionName; } /** - * Get list of violations in result + * Get name of missing option * - * @return List of violations in result + * @return optionName Name of option missed */ - public List getViolations() - { - return Collections.unmodifiableList( violations ); - + public String getOptionName() { + return optionName; } + } - /** - * @return True if there is not violation - */ - public boolean isValid() - { - return violations.isEmpty(); - } + /** + * A validation violation + */ + public static abstract class Violation { + Violation() {} + } + + private final List violations = new ArrayList(); + + /** + * Add a new violation to validation result + * + * @param Type of validation violation + * @param violation Violation + */ + public void addViolation(T violation) { + violations.add(violation); + } + + /** + * Get list of violations in result + * + * @return List of violations in result + */ + public List getViolations() { + return Collections.unmodifiableList(violations); + + } + + /** + * @return True if there is not violation + */ + public boolean isValid() { + return violations.isEmpty(); + } } diff --git a/src/main/java/org/cyclopsgroup/jcli/annotation/Argument.java b/src/main/java/org/cyclopsgroup/jcli/annotation/Argument.java index 758ce8d..dc67ebc 100644 --- a/src/main/java/org/cyclopsgroup/jcli/annotation/Argument.java +++ b/src/main/java/org/cyclopsgroup/jcli/annotation/Argument.java @@ -7,23 +7,22 @@ import java.lang.annotation.Target; /** - * This annotation marks a property as non-option argument or arguments. Type of this property can be array, List or - * single value. + * This annotation marks a property as non-option argument or arguments. Type of this property can + * be array, List or single value. * * @author Jiaqi Guo */ @Documented -@Target( { ElementType.METHOD, ElementType.FIELD } ) -@Retention( RetentionPolicy.RUNTIME ) -public @interface Argument -{ - /** - * @return String description of argument which will be displayed in usage - */ - String description() default ""; +@Target({ElementType.METHOD, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Argument { + /** + * @return String description of argument which will be displayed in usage + */ + String description() default ""; - /** - * @return Name of argument displayed in usage - */ - String displayName() default "arg"; + /** + * @return Name of argument displayed in usage + */ + String displayName() default "arg"; } diff --git a/src/main/java/org/cyclopsgroup/jcli/annotation/Cli.java b/src/main/java/org/cyclopsgroup/jcli/annotation/Cli.java index c183a84..9a892e7 100644 --- a/src/main/java/org/cyclopsgroup/jcli/annotation/Cli.java +++ b/src/main/java/org/cyclopsgroup/jcli/annotation/Cli.java @@ -12,27 +12,26 @@ * @author Jiaqi Guo */ @Documented -@Target( ElementType.TYPE ) -@Retention( RetentionPolicy.RUNTIME ) -public @interface Cli -{ - /** - * @return String description of command - */ - String description() default ""; +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface Cli { + /** + * @return String description of command + */ + String description() default ""; - /** - * @return Name of command - */ - String name(); + /** + * @return Name of command + */ + String name(); - /** - * @return Note displayed as footer - */ - String note() default ""; + /** + * @return Note displayed as footer + */ + String note() default ""; - /** - * @return True if unexpected option or argument is expected to cause error - */ - boolean restrict() default true; + /** + * @return True if unexpected option or argument is expected to cause error + */ + boolean restrict() default true; } diff --git a/src/main/java/org/cyclopsgroup/jcli/annotation/MultiValue.java b/src/main/java/org/cyclopsgroup/jcli/annotation/MultiValue.java index 1874a86..58b0403 100644 --- a/src/main/java/org/cyclopsgroup/jcli/annotation/MultiValue.java +++ b/src/main/java/org/cyclopsgroup/jcli/annotation/MultiValue.java @@ -13,27 +13,26 @@ * @author Jiaqi Guo */ @Documented -@Target( { ElementType.METHOD, ElementType.FIELD } ) -@Retention( RetentionPolicy.RUNTIME ) -public @interface MultiValue -{ - /** - * @return Type of list that contains values - */ - Class listType() default ArrayList.class; +@Target({ElementType.METHOD, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface MultiValue { + /** + * @return Type of list that contains values + */ + Class listType() default ArrayList.class; - /** - * @return Max number of values in list. By default it's -1 that means unlimited - */ - int maxValues() default -1; + /** + * @return Max number of values in list. By default it's -1 that means unlimited + */ + int maxValues() default -1; - /** - * @return Minimal number of values in list. By default it's 0 - */ - int minValues() default 0; + /** + * @return Minimal number of values in list. By default it's 0 + */ + int minValues() default 0; - /** - * @return Type of value in list - */ - Class valueType() default String.class; + /** + * @return Type of value in list + */ + Class valueType() default String.class; } diff --git a/src/main/java/org/cyclopsgroup/jcli/annotation/Option.java b/src/main/java/org/cyclopsgroup/jcli/annotation/Option.java index abcaed6..fc9f6b3 100644 --- a/src/main/java/org/cyclopsgroup/jcli/annotation/Option.java +++ b/src/main/java/org/cyclopsgroup/jcli/annotation/Option.java @@ -12,37 +12,36 @@ * @author Jiaqi Guo */ @Documented -@Target( { ElementType.METHOD, ElementType.FIELD } ) -@Retention( RetentionPolicy.RUNTIME ) -public @interface Option -{ - /** - * @return Default value of option - */ - String defaultValue() default ""; +@Target({ElementType.METHOD, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Option { + /** + * @return Default value of option + */ + String defaultValue() default ""; - /** - * @return String description of option which is displayed in usage - */ - String description() default ""; + /** + * @return String description of option which is displayed in usage + */ + String description() default ""; - /** - * @return Display name of option - */ - String displayName() default "value"; + /** + * @return Display name of option + */ + String displayName() default "value"; - /** - * @return Long option name specified with double dash - */ - String longName() default ""; + /** + * @return Long option name specified with double dash + */ + String longName() default ""; - /** - * @return Short option name specified with single dash - */ - String name(); + /** + * @return Short option name specified with single dash + */ + String name(); - /** - * @return True if option has to be specified explicitly - */ - boolean required() default false; + /** + * @return True if option has to be specified explicitly + */ + boolean required() default false; } diff --git a/src/main/java/org/cyclopsgroup/jcli/annotation/package-info.java b/src/main/java/org/cyclopsgroup/jcli/annotation/package-info.java index 3f87a05..8ab84d4 100644 --- a/src/main/java/org/cyclopsgroup/jcli/annotation/package-info.java +++ b/src/main/java/org/cyclopsgroup/jcli/annotation/package-info.java @@ -1,4 +1,4 @@ /** * Annotations that defines command line syntax */ -package org.cyclopsgroup.jcli.annotation; \ No newline at end of file +package org.cyclopsgroup.jcli.annotation; diff --git a/src/main/java/org/cyclopsgroup/jcli/example/UserControlAction.java b/src/main/java/org/cyclopsgroup/jcli/example/UserControlAction.java index 5d286e4..65440a3 100644 --- a/src/main/java/org/cyclopsgroup/jcli/example/UserControlAction.java +++ b/src/main/java/org/cyclopsgroup/jcli/example/UserControlAction.java @@ -5,22 +5,21 @@ * * @author Jiaqi Guo */ -public enum UserControlAction -{ - /** - * Add something - */ - ADD, - /** - * Delete something - */ - DELETE, - /** - * Change something - */ - EDIT, - /** - * Display something - */ - DISPLAY; +public enum UserControlAction { + /** + * Add something + */ + ADD, + /** + * Delete something + */ + DELETE, + /** + * Change something + */ + EDIT, + /** + * Display something + */ + DISPLAY; } diff --git a/src/main/java/org/cyclopsgroup/jcli/impl/DefaultBeanProcessor.java b/src/main/java/org/cyclopsgroup/jcli/impl/DefaultBeanProcessor.java index 2a0b927..5d8dc60 100644 --- a/src/main/java/org/cyclopsgroup/jcli/impl/DefaultBeanProcessor.java +++ b/src/main/java/org/cyclopsgroup/jcli/impl/DefaultBeanProcessor.java @@ -8,60 +8,48 @@ import org.cyclopsgroup.jcli.spi.CommandLine; import org.cyclopsgroup.jcli.spi.CommandLineParser; -class DefaultBeanProcessor -{ - static void process( AnnotationParsingContext context, List arguments, T bean, - CommandLineParser parser ) - { - CommandLine cli = parser.parse( arguments, context ); - Map> multiValues = new HashMap>(); - for ( CommandLine.OptionValue ov : cli.getOptionValues() ) - { - Reference ref = context.lookupReference( ov.name, !ov.shortName ); - if ( ref == null ) - { - throw new AssertionError( "Option " + ov.name + " doesn't exist" ); - } - if ( ref instanceof SingleValueReference ) - { - ( (SingleValueReference) ref ).setValue( bean, ov.value ); - continue; - } - String optionName; - if ( ov.shortName ) - { - optionName = ov.name; - } - else - { - optionName = context.optionWithLongName( ov.name ).getName(); - } - List values = multiValues.get( optionName ); - if ( values == null ) - { - values = new ArrayList(); - multiValues.put( optionName, values ); - } - values.add( ov.value ); - } - for ( Map.Entry> entry : multiValues.entrySet() ) - { - MultiValueReference ref = (MultiValueReference) context.lookupReference( entry.getKey(), false ); - ref.setValues( bean, entry.getValue() ); - } - Reference ref = context.lookupReference( DefaultArgumentProcessor.ARGUMENT_REFERNCE_NAME, false ); - if ( ref == null ) - { - return; - } - if ( ref instanceof MultiValueReference ) - { - ( (MultiValueReference) ref ).setValues( bean, cli.getArguments() ); - } - else - { - String value = cli.getArguments().isEmpty() ? null : cli.getArguments().get( 0 ); - ( (SingleValueReference) ref ).setValue( bean, value ); - } +class DefaultBeanProcessor { + static void process(AnnotationParsingContext context, List arguments, T bean, + CommandLineParser parser) { + CommandLine cli = parser.parse(arguments, context); + Map> multiValues = new HashMap>(); + for (CommandLine.OptionValue ov : cli.getOptionValues()) { + Reference ref = context.lookupReference(ov.name, !ov.shortName); + if (ref == null) { + throw new AssertionError("Option " + ov.name + " doesn't exist"); + } + if (ref instanceof SingleValueReference) { + ((SingleValueReference) ref).setValue(bean, ov.value); + continue; + } + String optionName; + if (ov.shortName) { + optionName = ov.name; + } else { + optionName = context.optionWithLongName(ov.name).getName(); + } + List values = multiValues.get(optionName); + if (values == null) { + values = new ArrayList(); + multiValues.put(optionName, values); + } + values.add(ov.value); } + for (Map.Entry> entry : multiValues.entrySet()) { + MultiValueReference ref = + (MultiValueReference) context.lookupReference(entry.getKey(), false); + ref.setValues(bean, entry.getValue()); + } + Reference ref = + context.lookupReference(DefaultArgumentProcessor.ARGUMENT_REFERNCE_NAME, false); + if (ref == null) { + return; + } + if (ref instanceof MultiValueReference) { + ((MultiValueReference) ref).setValues(bean, cli.getArguments()); + } else { + String value = cli.getArguments().isEmpty() ? null : cli.getArguments().get(0); + ((SingleValueReference) ref).setValue(bean, value); + } + } } diff --git a/src/main/java/org/cyclopsgroup/jcli/impl/DefaultHelpPrinter.java b/src/main/java/org/cyclopsgroup/jcli/impl/DefaultHelpPrinter.java index 7e69f4a..e7e02f6 100644 --- a/src/main/java/org/cyclopsgroup/jcli/impl/DefaultHelpPrinter.java +++ b/src/main/java/org/cyclopsgroup/jcli/impl/DefaultHelpPrinter.java @@ -8,38 +8,32 @@ import org.cyclopsgroup.caff.format.Formats; import org.cyclopsgroup.jcli.spi.Option; -class DefaultHelpPrinter -{ - static void printHelp( AnnotationParsingContext context, PrintWriter out ) - throws IOException - { - out.println( "[USAGE]" ); - out.println( " " + context.cli().getName() + ( context.options().isEmpty() ? "" : " " ) - + ( context.argument() == null ? "" : " " ) ); - if ( StringUtils.isNotBlank( context.cli().getDescription() ) ) - { - out.println( "[DESCRIPTION]" ); - out.println( " " + context.cli().getDescription() ); - } - if ( !context.options().isEmpty() ) - { - out.println( "[OPTIONS]" ); - Format helpFormat = Formats.newFixLengthFormat( OptionHelp.class ); - for ( Option option : context.options() ) - { - String line = helpFormat.formatToString( new OptionHelp( option ) ).trim(); - out.println( " " + line ); - } - } - if ( context.argument() != null ) - { - out.println( "[ARGS]" ); - out.println( " <" + context.argument().getDisplayName() + ">... " + context.argument().getDescription() ); - } - if ( !StringUtils.isBlank( context.cli().getNote() ) ) - { - out.println( "[NOTE]" ); - out.println( " " + context.cli().getNote() ); - } +class DefaultHelpPrinter { + static void printHelp(AnnotationParsingContext context, PrintWriter out) + throws IOException { + out.println("[USAGE]"); + out.println(" " + context.cli().getName() + (context.options().isEmpty() ? "" : " ") + + (context.argument() == null ? "" : " ")); + if (StringUtils.isNotBlank(context.cli().getDescription())) { + out.println("[DESCRIPTION]"); + out.println(" " + context.cli().getDescription()); } + if (!context.options().isEmpty()) { + out.println("[OPTIONS]"); + Format helpFormat = Formats.newFixLengthFormat(OptionHelp.class); + for (Option option : context.options()) { + String line = helpFormat.formatToString(new OptionHelp(option)).trim(); + out.println(" " + line); + } + } + if (context.argument() != null) { + out.println("[ARGS]"); + out.println(" <" + context.argument().getDisplayName() + ">... " + + context.argument().getDescription()); + } + if (!StringUtils.isBlank(context.cli().getNote())) { + out.println("[NOTE]"); + out.println(" " + context.cli().getNote()); + } + } } diff --git a/src/main/java/org/cyclopsgroup/jcli/impl/MultiValueReference.java b/src/main/java/org/cyclopsgroup/jcli/impl/MultiValueReference.java index c8defab..a4aec24 100644 --- a/src/main/java/org/cyclopsgroup/jcli/impl/MultiValueReference.java +++ b/src/main/java/org/cyclopsgroup/jcli/impl/MultiValueReference.java @@ -6,45 +6,35 @@ import org.cyclopsgroup.caff.conversion.Converter; import org.cyclopsgroup.caff.ref.ValueReference; -class MultiValueReference - extends Reference -{ - private final Class listType; +class MultiValueReference extends Reference { + private final Class listType; - MultiValueReference( Class beanType, Converter converter, ValueReference ref, String longName, - Class listType ) - { - super( converter, ref, longName ); - this.listType = listType; - } + MultiValueReference(Class beanType, Converter converter, ValueReference ref, + String longName, Class listType) { + super(converter, ref, longName); + this.listType = listType; + } - /** - * Write multi value to bean - * - * @param

Type of value to convert - * @param bean Bean to set values - * @param values List of values to set - */ - @SuppressWarnings( "unchecked" ) -

void setValues( T bean, List values ) - { - Collection

col; - try - { - col = (Collection

) listType.newInstance(); - } - catch ( InstantiationException e ) - { - throw new RuntimeException( "Can't instantiate " + listType, e ); - } - catch ( IllegalAccessException e ) - { - throw new RuntimeException( "Can't access default constructor of " + listType ); - } - for ( String value : values ) - { - col.add( (P) converter.fromCharacters( value ) ); - } - ref.writeValue( col, bean ); + /** + * Write multi value to bean + * + * @param

Type of value to convert + * @param bean Bean to set values + * @param values List of values to set + */ + @SuppressWarnings("unchecked") +

void setValues(T bean, List values) { + Collection

col; + try { + col = (Collection

) listType.newInstance(); + } catch (InstantiationException e) { + throw new RuntimeException("Can't instantiate " + listType, e); + } catch (IllegalAccessException e) { + throw new RuntimeException("Can't access default constructor of " + listType); + } + for (String value : values) { + col.add((P) converter.fromCharacters(value)); } + ref.writeValue(col, bean); + } } diff --git a/src/main/java/org/cyclopsgroup/jcli/impl/OptionHelp.java b/src/main/java/org/cyclopsgroup/jcli/impl/OptionHelp.java index 4715b5e..e3678c6 100644 --- a/src/main/java/org/cyclopsgroup/jcli/impl/OptionHelp.java +++ b/src/main/java/org/cyclopsgroup/jcli/impl/OptionHelp.java @@ -10,54 +10,47 @@ * * @author Jiaqi Guo */ -@FixLengthType( length = 256 ) -public class OptionHelp -{ - private final Option option; +@FixLengthType(length = 256) +public class OptionHelp { + private final Option option; - OptionHelp( Option option ) - { - this.option = option; - } + OptionHelp(Option option) { + this.option = option; + } - /** - * @return Description of option - */ - @FixLengthField( start = 26, length = 228 ) - public String getDescription() - { - String desc = option.getDescription(); - if ( !option.isFlag() && StringUtils.isNotBlank( option.getDefaultValue() ) ) - { - desc += "(Default value is " + option.getDefaultValue() + ")"; - } - return desc; + /** + * @return Description of option + */ + @FixLengthField(start = 26, length = 228) + public String getDescription() { + String desc = option.getDescription(); + if (!option.isFlag() && StringUtils.isNotBlank(option.getDefaultValue())) { + desc += "(Default value is " + option.getDefaultValue() + ")"; } + return desc; + } - /** - * @return Name of option value - */ - @FixLengthField( start = 15, length = 10 ) - public String getDisplayName() - { - return option.isFlag() ? null : "<" + option.getDisplayName() + ">"; - } + /** + * @return Name of option value + */ + @FixLengthField(start = 15, length = 10) + public String getDisplayName() { + return option.isFlag() ? null : "<" + option.getDisplayName() + ">"; + } - /** - * @return Long name of option - */ - @FixLengthField( start = 4, length = 10 ) - public String getLongName() - { - return StringUtils.isBlank( option.getLongName() ) ? null : "--" + option.getLongName(); - } + /** + * @return Long name of option + */ + @FixLengthField(start = 4, length = 10) + public String getLongName() { + return StringUtils.isBlank(option.getLongName()) ? null : "--" + option.getLongName(); + } - /** - * @return Short name of option - */ - @FixLengthField( start = 1, length = 2 ) - public String getName() - { - return "-" + option.getName(); - } + /** + * @return Short name of option + */ + @FixLengthField(start = 1, length = 2) + public String getName() { + return "-" + option.getName(); + } } diff --git a/src/main/java/org/cyclopsgroup/jcli/impl/ParsingContextBuilder.java b/src/main/java/org/cyclopsgroup/jcli/impl/ParsingContextBuilder.java index f4ef659..d0e88f4 100644 --- a/src/main/java/org/cyclopsgroup/jcli/impl/ParsingContextBuilder.java +++ b/src/main/java/org/cyclopsgroup/jcli/impl/ParsingContextBuilder.java @@ -25,99 +25,85 @@ * @author Jiaqi Guo * @param Type of bean to parse */ -class ParsingContextBuilder -{ - @SuppressWarnings( "unchecked" ) - private static Reference createReference( Class beanType, PropertyDescriptor descriptor, - String longName ) - { - Class

valueType = (Class

) descriptor.getPropertyType(); - MultiValue multiValue = getAnnotation( descriptor, MultiValue.class ); - if ( multiValue != null ) - { - valueType = (Class

) multiValue.valueType(); - } +class ParsingContextBuilder { + @SuppressWarnings("unchecked") + private static Reference createReference(Class beanType, + PropertyDescriptor descriptor, String longName) { + Class

valueType = (Class

) descriptor.getPropertyType(); + MultiValue multiValue = getAnnotation(descriptor, MultiValue.class); + if (multiValue != null) { + valueType = (Class

) multiValue.valueType(); + } - Converter

converter = new AnnotatedConverter

( valueType, descriptor ); - ValueReference reference = ValueReference.instanceOf( descriptor ); - if ( multiValue != null ) - { - return new MultiValueReference( beanType, converter, reference, longName, multiValue.listType() ); - } - return new SingleValueReference( beanType, converter, reference, longName ); + Converter

converter = new AnnotatedConverter

(valueType, descriptor); + ValueReference reference = ValueReference.instanceOf(descriptor); + if (multiValue != null) { + return new MultiValueReference(beanType, converter, reference, longName, + multiValue.listType()); } + return new SingleValueReference(beanType, converter, reference, longName); + } - /** - * Get annotation from either writer or reader of a Java property - * - * @param Type of annotation - * @param descriptor Field descriptor from which annotation is searched - * @param type Type of annotation - * @return Annotation or null if it's not found - */ - private static A getAnnotation( PropertyDescriptor descriptor, Class type ) - { - A a = null; - if ( descriptor.getWriteMethod() != null ) - { - a = descriptor.getWriteMethod().getAnnotation( type ); - } - if ( a == null && descriptor.getReadMethod() != null ) - { - a = descriptor.getReadMethod().getAnnotation( type ); - } - return a; + /** + * Get annotation from either writer or reader of a Java property + * + * @param Type of annotation + * @param descriptor Field descriptor from which annotation is searched + * @param type Type of annotation + * @return Annotation or null if it's not found + */ + private static A getAnnotation(PropertyDescriptor descriptor, + Class type) { + A a = null; + if (descriptor.getWriteMethod() != null) { + a = descriptor.getWriteMethod().getAnnotation(type); + } + if (a == null && descriptor.getReadMethod() != null) { + a = descriptor.getReadMethod().getAnnotation(type); } + return a; + } - private final Class beanType; + private final Class beanType; - ParsingContextBuilder( Class beanType ) - { - this.beanType = beanType; - } + ParsingContextBuilder(Class beanType) { + this.beanType = beanType; + } - AnnotationParsingContext build() - { - List options = new ArrayList(); - Map> references = new HashMap>(); - Cli cliAnnotation = beanType.getAnnotation( Cli.class ); - BeanInfo beanInfo; - try - { - beanInfo = Introspector.getBeanInfo( beanType ); - } - catch ( IntrospectionException e ) - { - throw new RuntimeException( "Bean " + beanType + " is not correctly defined", e ); - } - Argument argument = null; - for ( PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors() ) - { - Method writer = descriptor.getWriteMethod(); - if ( writer == null ) - { - continue; - } - Option option = getAnnotation( descriptor, Option.class ); - if ( option != null ) - { - boolean flag = - ( descriptor.getPropertyType() == Boolean.TYPE || descriptor.getPropertyType() == Boolean.class ); - boolean multiValue = getAnnotation( descriptor, MultiValue.class ) != null; - options.add( new AnnotationOption( option, flag, multiValue ) ); - references.put( option.name(), createReference( beanType, descriptor, option.longName() ) ); - continue; - } - Argument arg = getAnnotation( descriptor, Argument.class ); - if ( arg != null ) - { - argument = arg; - references.put( DefaultArgumentProcessor.ARGUMENT_REFERNCE_NAME, - createReference( beanType, descriptor, DefaultArgumentProcessor.ARGUMENT_REFERNCE_NAME ) ); - continue; - } - } - return new AnnotationParsingContext( references, options, new AnnotationCli( cliAnnotation ), - argument == null ? null : new AnnotationArgument( argument ) ); + AnnotationParsingContext build() { + List options = new ArrayList(); + Map> references = new HashMap>(); + Cli cliAnnotation = beanType.getAnnotation(Cli.class); + BeanInfo beanInfo; + try { + beanInfo = Introspector.getBeanInfo(beanType); + } catch (IntrospectionException e) { + throw new RuntimeException("Bean " + beanType + " is not correctly defined", e); + } + Argument argument = null; + for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) { + Method writer = descriptor.getWriteMethod(); + if (writer == null) { + continue; + } + Option option = getAnnotation(descriptor, Option.class); + if (option != null) { + boolean flag = (descriptor.getPropertyType() == Boolean.TYPE + || descriptor.getPropertyType() == Boolean.class); + boolean multiValue = getAnnotation(descriptor, MultiValue.class) != null; + options.add(new AnnotationOption(option, flag, multiValue)); + references.put(option.name(), createReference(beanType, descriptor, option.longName())); + continue; + } + Argument arg = getAnnotation(descriptor, Argument.class); + if (arg != null) { + argument = arg; + references.put(DefaultArgumentProcessor.ARGUMENT_REFERNCE_NAME, + createReference(beanType, descriptor, DefaultArgumentProcessor.ARGUMENT_REFERNCE_NAME)); + continue; + } } + return new AnnotationParsingContext(references, options, new AnnotationCli(cliAnnotation), + argument == null ? null : new AnnotationArgument(argument)); + } } diff --git a/src/main/java/org/cyclopsgroup/jcli/impl/SingleValueReference.java b/src/main/java/org/cyclopsgroup/jcli/impl/SingleValueReference.java index ea0adec..7a4bb05 100644 --- a/src/main/java/org/cyclopsgroup/jcli/impl/SingleValueReference.java +++ b/src/main/java/org/cyclopsgroup/jcli/impl/SingleValueReference.java @@ -3,22 +3,19 @@ import org.cyclopsgroup.caff.conversion.Converter; import org.cyclopsgroup.caff.ref.ValueReference; -class SingleValueReference - extends Reference -{ - SingleValueReference( Class beanType, Converter converter, ValueReference ref, String longName ) - { - super( converter, ref, longName ); - } +class SingleValueReference extends Reference { + SingleValueReference(Class beanType, Converter converter, ValueReference ref, + String longName) { + super(converter, ref, longName); + } - /** - * Set a string value to bean based on known conversion rule and value reference - * - * @param bean Bean to set value to - * @param value String expression of value to set - */ - void setValue( T bean, String value ) - { - ref.writeValue( converter.fromCharacters( value ), bean ); - } + /** + * Set a string value to bean based on known conversion rule and value reference + * + * @param bean Bean to set value to + * @param value String expression of value to set + */ + void setValue(T bean, String value) { + ref.writeValue(converter.fromCharacters(value), bean); + } } diff --git a/src/main/java/org/cyclopsgroup/jcli/jline/ArgumentsInspector.java b/src/main/java/org/cyclopsgroup/jcli/jline/ArgumentsInspector.java index 1ccc81a..cb3f34d 100644 --- a/src/main/java/org/cyclopsgroup/jcli/jline/ArgumentsInspector.java +++ b/src/main/java/org/cyclopsgroup/jcli/jline/ArgumentsInspector.java @@ -12,140 +12,115 @@ * * @author Jiaqi Guo */ -class ArgumentsInspector -{ - private final ParsingContext context; +class ArgumentsInspector { + private final ParsingContext context; - private Option currentOption; + private Option currentOption; - private String currentValue; + private String currentValue; - private final Set