|
| 1 | +/* |
| 2 | + * ParseFor class. |
| 3 | + * Copyright (C) 2024 Takayuki Sato. All Rights Reserved. |
| 4 | + */ |
| 5 | +package com.github.sttk.cliargs; |
| 6 | + |
| 7 | +import static com.github.sttk.cliargs.Util.isEmpty; |
| 8 | +import static java.util.Collections.emptyList; |
| 9 | +import static java.util.Collections.emptyMap; |
| 10 | + |
| 11 | +import com.github.sttk.cliargs.CliArgs.IllegalOptionType; |
| 12 | +import com.github.sttk.cliargs.CliArgs.FailToSetOptionStoreField; |
| 13 | +import com.github.sttk.cliargs.CliArgs.FailToConvertDefaultsInOptAnnotation; |
| 14 | +import com.github.sttk.cliargs.annotation.Opt; |
| 15 | +import com.github.sttk.cliargs.convert.Converter; |
| 16 | +import com.github.sttk.cliargs.OptCfg.Postparser; |
| 17 | +import com.github.sttk.exception.ReasonedException; |
| 18 | +import java.lang.reflect.Field; |
| 19 | +import java.util.List; |
| 20 | +import java.util.ArrayList; |
| 21 | + |
| 22 | +interface ParseFor { |
| 23 | + |
| 24 | + static OptCfg[] makeOptCfgsFor(Object options) throws ReasonedException { |
| 25 | + var list = new ArrayList<OptCfg>(); |
| 26 | + |
| 27 | + Class<?> cls = options.getClass(); |
| 28 | + while (cls != null) { |
| 29 | + for (var fld : cls.getDeclaredFields()) { |
| 30 | + var annotation = fld.getAnnotation(Opt.class); |
| 31 | + if (annotation == null) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + var type = fld.getType(); |
| 36 | + var hasArg = !((type == boolean.class) || (type == Boolean.class)); |
| 37 | + var isArray = type.isArray(); |
| 38 | + if (isArray) { |
| 39 | + type = type.getComponentType(); |
| 40 | + } |
| 41 | + |
| 42 | + list.add(OptCfgFactory.create(type, hasArg, isArray, fld, options)); |
| 43 | + } |
| 44 | + |
| 45 | + cls = cls.getSuperclass(); |
| 46 | + } |
| 47 | + |
| 48 | + return list.toArray(new OptCfg[list.size()]); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +interface OptCfgFactory { |
| 53 | + |
| 54 | + static <T> OptCfg create( |
| 55 | + Class<T> type, boolean hasArg, boolean isArray, Field fld, Object options |
| 56 | + ) throws ReasonedException { |
| 57 | + var annotation = fld.getAnnotation(Opt.class); |
| 58 | + var cfg = annotation.cfg(); |
| 59 | + var desc = annotation.desc(); |
| 60 | + var arg = annotation.arg(); |
| 61 | + |
| 62 | + var storeKey = fld.getName(); |
| 63 | + |
| 64 | + Converter<T> converter = OptCfg.findConverter(type); |
| 65 | + if (converter == null) { |
| 66 | + if (hasArg && type != String.class) { |
| 67 | + throw new ReasonedException(new IllegalOptionType(type, storeKey)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + Postparser<T> postparser = optArgs -> { |
| 72 | + try { |
| 73 | + setOptionStoreFieldValue(options, fld, optArgs, type, hasArg, isArray); |
| 74 | + } catch (Exception e) { |
| 75 | + var reason = new FailToSetOptionStoreField(storeKey, type, optArgs); |
| 76 | + throw new ReasonedException(reason, e); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + var namesAndDefs = cfg.split("=", 2); |
| 81 | + var names = parseNames(namesAndDefs[0], storeKey); |
| 82 | + var defs = parseDefaults(namesAndDefs, converter, storeKey); |
| 83 | + |
| 84 | + return new OptCfg( |
| 85 | + storeKey, names, hasArg, isArray, type, defs, desc, arg, |
| 86 | + converter, postparser |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + private static List<String> parseNames(String namesStr, String storeKey) { |
| 91 | + if (isEmpty(namesStr)) { |
| 92 | + return List.of(storeKey); |
| 93 | + } |
| 94 | + return List.of(namesStr.split(",")); |
| 95 | + } |
| 96 | + |
| 97 | + private static <T> List<T> parseDefaults( |
| 98 | + String[] namesAndDefaults, Converter<T> converter, String storeKey |
| 99 | + ) throws ReasonedException { |
| 100 | + if (namesAndDefaults.length < 2) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + var str = namesAndDefaults[1]; |
| 105 | + int len = str.length(); |
| 106 | + |
| 107 | + String[] arr = null; |
| 108 | + if (str.endsWith("]")) { |
| 109 | + if (str.startsWith("[")) { |
| 110 | + if (len > 2) { |
| 111 | + arr = str.substring(1, len-1).split(","); |
| 112 | + } else { |
| 113 | + arr = new String[0]; |
| 114 | + } |
| 115 | + } else if (str.charAt(1) == '[') { |
| 116 | + if (len > 3) { |
| 117 | + var sep = str.substring(0, 1); |
| 118 | + arr = str.substring(2, len-1).split("\\" + sep); // Escape because String#split takes a regexp string. |
| 119 | + } else { |
| 120 | + arr = new String[0]; |
| 121 | + } |
| 122 | + } else { |
| 123 | + arr = new String[]{str}; |
| 124 | + } |
| 125 | + } else { |
| 126 | + arr = new String[]{str}; |
| 127 | + } |
| 128 | + |
| 129 | + var lst = new ArrayList<T>(); |
| 130 | + if (converter != null) { |
| 131 | + for (var s : arr) { |
| 132 | + try { |
| 133 | + lst.add(converter.convert(s, storeKey, storeKey)); |
| 134 | + } catch (Exception e) { |
| 135 | + var reason = new FailToConvertDefaultsInOptAnnotation(storeKey, str); |
| 136 | + throw new ReasonedException(reason, e); |
| 137 | + } |
| 138 | + } |
| 139 | + } else { |
| 140 | + for (var s : arr) { |
| 141 | + @SuppressWarnings("unchecked") |
| 142 | + T t = (T) s; |
| 143 | + lst.add(t); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return lst; |
| 148 | + } |
| 149 | + |
| 150 | + private static <T> void setOptionStoreFieldValue( |
| 151 | + Object options, Field fld, List<T> optArgs, Class<T> type, |
| 152 | + boolean hasArg, boolean isArray |
| 153 | + ) throws Exception { |
| 154 | + if (optArgs == null) { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + if (type == boolean.class || type == Boolean.class) { |
| 159 | + fld.setAccessible(true); |
| 160 | + fld.set(options, true); |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + if (! isArray) { |
| 165 | + fld.setAccessible(true); |
| 166 | + fld.set(options, optArgs.get(0)); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + int n = optArgs.size(); |
| 171 | + if (type == int.class) { |
| 172 | + int[] arr = new int[n]; |
| 173 | + for (int i = 0; i < n; i++) { |
| 174 | + arr[i] = Integer.class.cast(optArgs.get(i)); |
| 175 | + } |
| 176 | + fld.setAccessible(true); |
| 177 | + fld.set(options, arr); |
| 178 | + } else if (type == double.class) { |
| 179 | + double[] arr = new double[n]; |
| 180 | + for (int i = 0; i < n; i++) { |
| 181 | + arr[i] = Double.class.cast(optArgs.get(i)); |
| 182 | + } |
| 183 | + fld.setAccessible(true); |
| 184 | + fld.set(options, arr); |
| 185 | + } else if (type == long.class) { |
| 186 | + long[] arr = new long[n]; |
| 187 | + for (int i = 0; i < n; i++) { |
| 188 | + arr[i] = Long.class.cast(optArgs.get(i)); |
| 189 | + } |
| 190 | + fld.setAccessible(true); |
| 191 | + fld.set(options, arr); |
| 192 | + } else if (type == float.class) { |
| 193 | + float[] arr = new float[n]; |
| 194 | + for (int i = 0; i < n; i++) { |
| 195 | + arr[i] = Float.class.cast(optArgs.get(i)); |
| 196 | + } |
| 197 | + fld.setAccessible(true); |
| 198 | + fld.set(options, arr); |
| 199 | + } else if (type == short.class) { |
| 200 | + short[] arr = new short[n]; |
| 201 | + for (int i = 0; i < n; i++) { |
| 202 | + arr[i] = Short.class.cast(optArgs.get(i)); |
| 203 | + } |
| 204 | + fld.setAccessible(true); |
| 205 | + fld.set(options, arr); |
| 206 | + } else if (type == byte.class) { |
| 207 | + byte[] arr = new byte[n]; |
| 208 | + for (int i = 0; i < n; i++) { |
| 209 | + arr[i] = Byte.class.cast(optArgs.get(i)); |
| 210 | + } |
| 211 | + fld.setAccessible(true); |
| 212 | + fld.set(options, arr); |
| 213 | + } else { |
| 214 | + @SuppressWarnings("unchecked") |
| 215 | + T[] arr = (T[]) java.lang.reflect.Array.newInstance(type, n); |
| 216 | + for (int i = 0; i < n; i++) { |
| 217 | + arr[i] = optArgs.get(i); |
| 218 | + } |
| 219 | + fld.setAccessible(true); |
| 220 | + fld.set(options, arr); |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments