Changeset 2608
- Timestamp:
- 10/30/08 08:56:29 (2 months ago)
- Location:
- XerialJ/trunk/xerial-core/src
- Files:
-
- 2 added
- 11 modified
-
main/java/org/xerial/core/XerialErrorCode.java (modified) (1 diff)
-
main/java/org/xerial/util/Factory.java (added)
-
main/java/org/xerial/util/bean/TypeReference.java (modified) (4 diffs)
-
main/java/org/xerial/util/shell/ArgumentItem.java (modified) (1 diff)
-
main/java/org/xerial/util/shell/OptionItem.java (modified) (2 diffs)
-
main/java/org/xerial/util/shell/OptionParser.java (modified) (1 diff)
-
main/java/org/xerial/util/shell/OptionSchema.java (modified) (1 diff)
-
main/java/org/xerial/util/shell/OptionSetter.java (modified) (1 diff)
-
main/java/org/xerial/util/shell/OptionSetterViaField.java (modified) (4 diffs)
-
main/java/org/xerial/util/shell/OptionSetterViaMethod.java (modified) (1 diff)
-
test/java/org/xerial/util/FactoryTest.java (added)
-
test/java/org/xerial/util/bean/TypeReferenceTest.java (modified) (2 diffs)
-
test/java/org/xerial/util/shell/OptionParserTest.java (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
XerialJ/trunk/xerial-core/src/main/java/org/xerial/core/XerialErrorCode.java
r2607 r2608 36 36 INVALID_STATE, 37 37 SYNTAX_ERROR, 38 38 NOT_INITIALIZED, 39 39 40 // option parser error 40 41 DUPLICATE_OPTION, 41 42 NO_OPTION, 42 43 NO_USAGE_ANNOTATION, 44 43 45 // type 44 46 MISSING_TYPE_PARAMETER, 47 NOT_A_COLLECTION, 48 INACCESSIBLE_METHOD, 45 49 46 50 ; -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/bean/TypeReference.java
r2606 r2608 27 27 import java.lang.reflect.ParameterizedType; 28 28 import java.lang.reflect.Type; 29 import java.util.Collection;30 29 31 30 import org.xerial.core.XerialError; … … 42 41 * @author leo 43 42 */ 44 public class TypeReference<T extends Collection<?>> { 43 public class TypeReference<T> 44 { 45 45 46 46 private final Type type; … … 54 54 this.type = ((ParameterizedType) superClass).getActualTypeArguments()[0]; 55 55 } 56 57 56 58 57 /** … … 66 65 67 66 /** 68 * Gets the row type of the referenced type 67 * Gets the element type of the referenced type 68 * 69 69 * @return 70 70 */ -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/ArgumentItem.java
r2607 r2608 106 106 } 107 107 108 public void initialize(Object optionHolder) throws OptionParserException 109 { 110 argumentSetter.initialize(optionHolder); 111 } 112 108 113 } -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionItem.java
r2607 r2608 28 28 import java.lang.reflect.Method; 29 29 30 import org.xerial.util.bean.BeanException; 31 import org.xerial.util.bean.TypeConverter; 30 import org.xerial.util.bean.TypeInformation; 32 31 import org.xerial.util.cui.OptionParserException; 33 32 … … 100 99 return optionDescriptor; 101 100 } 101 102 public boolean takesMultipleArguments() 103 { 104 return TypeInformation.isCollection(optionSetter.getOptionDataType()); 105 } 106 102 107 103 108 public void setOption(Object bean, String value) throws OptionParserException 104 109 { 105 Class< ? > optionType = optionSetter.getOptionDataType(); 106 try 107 { 108 Object convertedValue = TypeConverter.convertType(optionType, value); 109 optionSetter.setOption(bean, convertedValue); 110 } 111 catch (BeanException e) 112 { 113 throw new OptionParserException(ShellError.WRONG_DATA_TYPE, e); 114 } 110 optionSetter.setOption(bean, value); 111 112 // Class< ? > optionType = optionSetter.getOptionDataType(); 113 // try 114 // { 115 // Object convertedValue = TypeConverter.convertType(optionType, value); 116 // optionSetter.setOption(bean, convertedValue); 117 // } 118 // catch (BeanException e) 119 // { 120 // throw new OptionParserException(ShellError.WRONG_DATA_TYPE, e); 121 // } 115 122 116 123 } 117 124 125 public void initialize(Object optionHolder) throws OptionParserException 126 { 127 optionSetter.initialize(optionHolder); 128 } 129 118 130 } -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionParser.java
r2607 r2608 75 75 public void parse(String[] args) throws OptionParserException 76 76 { 77 // initialize collections in the OptionHolder 78 for (OptionItem each : schema.getOptionItemList()) 79 { 80 each.initialize(optionHolder); 81 } 82 for (ArgumentItem each : schema.getArgumentItemList()) 83 { 84 each.initialize(optionHolder); 85 } 86 77 87 HashSet<Option> activatedOption = new HashSet<Option>(); 78 88 HashSet<Argument> activatedArgument = new HashSet<Argument>(); -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionSchema.java
r2607 r2608 246 246 if (arg.index() == argumentIndex) 247 247 return each; 248 if (arg.index() >argumentIndex && each.takesMultipleArguments())248 if (arg.index() < argumentIndex && each.takesMultipleArguments()) 249 249 return each; 250 250 } -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionSetter.java
r2603 r2608 38 38 39 39 boolean takesArgument(); 40 41 void initialize(Object bean) throws OptionParserException; 40 42 } -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionSetterViaField.java
r2607 r2608 26 26 27 27 import java.lang.reflect.Field; 28 import java.lang.reflect.InvocationTargetException; 29 import java.lang.reflect.Method; 28 30 31 import org.xerial.core.XerialError; 32 import org.xerial.core.XerialErrorCode; 33 import org.xerial.util.bean.BeanException; 34 import org.xerial.util.bean.TypeConverter; 29 35 import org.xerial.util.bean.TypeInformation; 30 36 import org.xerial.util.cui.OptionParserException; … … 50 56 } 51 57 52 public void setOption(Object bean, Object value) throws OptionParserException 58 protected Object getValue(Object bean) 59 { 60 Object value = null; 61 try 62 { 63 value = field.get(bean); 64 } 65 catch (IllegalAccessException e) 66 { 67 field.setAccessible(true); 68 try 69 { 70 value = field.get(bean); 71 } 72 catch (IllegalAccessException e1) 73 { 74 throw new IllegalAccessError(e1.getMessage()); 75 } 76 } 77 return value; 78 } 79 80 protected void setValue(Object bean, Object value) 53 81 { 54 82 try … … 68 96 } 69 97 } 98 99 } 100 101 public void setOption(Object bean, Object value) throws OptionParserException 102 { 103 try 104 { 105 if (setterTakesMultipleArguments()) 106 { 107 Object collection = getValue(bean); 108 if (collection == null) 109 { 110 throw new XerialError(XerialErrorCode.NOT_INITIALIZED); 111 } 112 113 // use adder 114 try 115 { 116 Method adder = getOptionDataType().getMethod("add", Object.class); 117 // TODO type convertion of value to the collection element 118 adder.invoke(collection, value); 119 } 120 catch (SecurityException e) 121 { 122 throw new XerialError(XerialErrorCode.INACCESSIBLE_METHOD, "add of " 123 + getOptionDataType().toString()); 124 } 125 catch (NoSuchMethodException e) 126 { 127 throw new XerialError(XerialErrorCode.NOT_A_COLLECTION, getOptionDataType().toString()); 128 } 129 catch (IllegalAccessException e) 130 { 131 throw new XerialError(XerialErrorCode.INACCESSIBLE_METHOD, "add of " 132 + getOptionDataType().toString()); 133 } 134 catch (InvocationTargetException e) 135 { 136 throw new XerialError(XerialErrorCode.INACCESSIBLE_METHOD, e); 137 } 138 } 139 else 140 { 141 Class< ? > optionType = getOptionDataType(); 142 Object convertedValue = TypeConverter.convertType(optionType, value); 143 setValue(bean, convertedValue); 144 } 145 } 70 146 catch (IllegalArgumentException e) 147 { 148 throw new OptionParserException(ShellError.WRONG_DATA_TYPE, e); 149 } 150 catch (BeanException e) 71 151 { 72 152 throw new OptionParserException(ShellError.WRONG_DATA_TYPE, e); … … 86 166 } 87 167 168 169 public void initialize(Object bean) throws OptionParserException 170 { 171 try 172 { 173 if (setterTakesMultipleArguments()) 174 { 175 Object collection = getValue(bean); 176 177 // initialize the array 178 if (collection == null) 179 { 180 collection = TypeInformation.createInstance(getOptionDataType()); 181 setValue(bean, collection); 182 } 183 } 184 } 185 catch (BeanException e) 186 { 187 throw new OptionParserException(e.getErrorCode()); 188 } 189 } 190 88 191 } -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionSetterViaMethod.java
r2607 r2608 92 92 } 93 93 94 public void initialize(Object bean) throws OptionParserException 95 { 96 // do nothing 97 } 98 94 99 } -
XerialJ/trunk/xerial-core/src/test/java/org/xerial/util/bean/TypeReferenceTest.java
r2606 r2608 25 25 package org.xerial.util.bean; 26 26 27 import static org.junit.Assert.*; 27 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.assertNotNull; 28 29 29 30 import java.lang.reflect.Type; 30 31 import java.util.List; 32 import java.util.Map; 31 33 32 34 import org.junit.Test; … … 41 43 assertEquals(String.class, c); 42 44 45 Type k = new TypeReference<Map<String, Integer>>() {}.getElementType()[0]; 46 Type v = new TypeReference<Map<String, Integer>>() {}.getElementType()[1]; 47 assertEquals(String.class, k); 48 assertEquals(Integer.class, v); 43 49 } 50 51 @Test 52 public void elementType() 53 { 54 Type c = new TypeReference<String>() {}.getType(); 55 assertEquals(String.class, c); 56 } 57 58 class GenericReference 59 { 60 Class< ? > c; 61 62 public GenericReference(Class< ? > c) 63 { 64 this.c = c; 65 } 66 67 Object newInstance() 68 { 69 try 70 { 71 return TypeInformation.createInstance(c); 72 } 73 catch (BeanException e) 74 { 75 return null; 76 } 77 } 78 79 } 80 81 @Test 82 public void genericTypeReference() 83 { 84 GenericReference gref = new GenericReference(String.class); 85 86 String str = (String) gref.newInstance(); 87 88 assertNotNull(str); 89 assertEquals(String.class, str.getClass()); 90 91 } 92 44 93 } -
XerialJ/trunk/xerial-core/src/test/java/org/xerial/util/shell/OptionParserTest.java
r2607 r2608 26 26 27 27 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.assertFalse; 28 29 import static org.junit.Assert.assertNotNull; 29 30 import static org.junit.Assert.assertTrue; … … 35 36 import org.junit.Test; 36 37 import org.xerial.util.cui.OptionParserException; 38 import org.xerial.util.log.LogLevel; 39 import org.xerial.util.log.Logger; 37 40 38 41 public class OptionParserTest 39 42 { 40 43 private static Logger _logger = Logger.getLogger(OptionParserTest.class); 44 41 45 @Before 42 46 public void setUp() throws Exception … … 60 64 61 65 @Test 62 public void option () throws OptionParserException66 public void optionBinding() throws OptionParserException 63 67 { 64 68 MyOption myOption = new MyOption(); … … 74 78 assertEquals("2.txt", myOption.fileList.get(1)); 75 79 80 76 81 } 82 83 @Test 84 public void initializeCollectionsInOptionHolder() throws OptionParserException 85 { 86 MyOption myOption = new MyOption(); 87 OptionParser parser = new OptionParser(myOption); 88 89 parser.parse(new String[] { "add" }); 90 91 assertEquals("add", myOption.subCommand); 92 assertFalse(myOption.displayHelp); 93 assertNotNull(myOption.fileList); 94 assertEquals(0, myOption.fileList.size()); 95 96 } 97 98 class LoglevelCommand 99 { 100 @Option(symbol = "l", longName = "loglevel", varName = "LOG_LEVEL") 101 private LogLevel loglevel = LogLevel.INFO; 102 } 103 104 @Test 105 public void enumOption() throws OptionParserException 106 { 107 LoglevelCommand opt = new LoglevelCommand(); 108 OptionParser parser = new OptionParser(opt); 109 parser.parse(new String[] { "--loglevel=DEBUG" }); 110 111 assertEquals(LogLevel.DEBUG, opt.loglevel); 112 } 113 114 class MultipleName 115 { 116 @Option(longName = "name") 117 private List<String> name; 118 } 119 120 @Test 121 public void mulpleOptionOccurences() throws OptionParserException 122 { 123 MultipleName mn = new MultipleName(); 124 OptionParser parser = new OptionParser(mn); 125 parser.parse(new String[] { "--name=leo", "--name=yui" }); 126 127 assertNotNull(mn.name); 128 assertEquals(2, mn.name.size()); 129 assertEquals("leo", mn.name.get(0)); 130 assertEquals("leo", mn.name.get(1)); 131 } 132 77 133 }


