1
2
3
4
5
6
7
8
9
10
11
12 package org.hyphenType.optionsextractor;
13
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationHandler;
17 import java.lang.reflect.InvocationTargetException;
18 import java.lang.reflect.Proxy;
19
20 import org.hyphenType.datastructure.Options;
21 import org.hyphenType.dynamicproxy.ConcreteArgumentsInvocationHandler;
22 import org.hyphenType.exceptions.InvalidOptionsInterfaceException;
23 import org.hyphenType.exit.StatusCode;
24 import org.hyphenType.input.StandardUserInput;
25 import org.hyphenType.input.UserInput;
26 import org.hyphenType.lexerparser.LexerParser;
27 import org.hyphenType.lexerparser.OptionValues;
28 import org.hyphenType.lexerparser.exceptions.MandatoryMapValueNotFoundException;
29 import org.hyphenType.lexerparser.exceptions.MandatorySimpleArgumentNotFoundException;
30 import org.hyphenType.lexerparser.exceptions.MandatoryValueNotFoundException;
31 import org.hyphenType.lexerparser.exceptions.RegexMismatchException;
32 import org.hyphenType.lexerparser.exceptions.StringParseException;
33 import org.hyphenType.lexerparser.exceptions.StringParsingErrorException;
34 import org.hyphenType.optionprocessors.ArgumentsProcessorEngine;
35 import org.hyphenType.util.DefaultAnnotation;
36 import org.hyphenType.util.I18NResourceBundle;
37 import org.hyphenType.wrapper.StandAloneAppWrapper;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class OptionsExtractor<T extends Options<?>> {
65
66 private final Class<T> optionInterfaceClass;
67 private final LexerParser<T> lexPar;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public OptionsExtractor(final Class<T> optionInterfaceClass) throws InvalidOptionsInterfaceException {
92
93 this.optionInterfaceClass = optionInterfaceClass;
94 this.lexPar = new LexerParser<T>(optionInterfaceClass);
95 }
96
97 public T options(final String... arguments) throws StringParsingErrorException, MandatoryValueNotFoundException, RegexMismatchException, StringParseException, MandatoryMapValueNotFoundException, MandatorySimpleArgumentNotFoundException, OptionsExtractorException {
98 return options(new StandardUserInput(), arguments);
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 @SuppressWarnings("unchecked")
121 public T options(final UserInput userInput, final String... arguments) throws OptionsExtractorException, StringParsingErrorException, MandatoryValueNotFoundException, RegexMismatchException, StringParseException, MandatoryMapValueNotFoundException, MandatorySimpleArgumentNotFoundException {
122 OptionValues<T> values = new OptionValues<T>(lexPar.lexArguments(arguments), lexPar, userInput);
123 InvocationHandler handler = buildInvocationHandler(values, lexPar.getOptionsInterface(), lexPar.getArgsObject().statusCodeEnum(), arguments);
124 Class proxyClass = Proxy.getProxyClass(OptionsExtractor.class.getClassLoader(), new Class[] {optionInterfaceClass});
125 T f;
126 try {
127 f = (T) proxyClass.getConstructor(new Class[] {InvocationHandler.class}).newInstance(new Object[] {handler});
128
129 } catch (IllegalArgumentException e) {
130 e.printStackTrace();
131 return null;
132 } catch (SecurityException e) {
133 e.printStackTrace();
134 return null;
135 } catch (InstantiationException e) {
136 e.printStackTrace();
137 return null;
138 } catch (IllegalAccessException e) {
139 e.printStackTrace();
140 return null;
141 } catch (InvocationTargetException e) {
142 e.printStackTrace();
143 return null;
144 } catch (NoSuchMethodException e) {
145 e.printStackTrace();
146 return null;
147 }
148
149
150
151
152
153 for (Annotation annotation : DefaultAnnotation.getAnnotations(optionInterfaceClass)) {
154
155 DefaultAnnotation.fillWithResourceBundle(annotation, new I18NResourceBundle(optionInterfaceClass));
156
157 if (annotation.annotationType().getEnclosingClass() != null && ArgumentsProcessorEngine.class.isAssignableFrom(annotation.annotationType().getEnclosingClass())) {
158
159 Class<? extends ArgumentsProcessorEngine<?>> processorClass = (Class<? extends ArgumentsProcessorEngine<?>>) annotation.annotationType().getEnclosingClass();
160 Constructor<?> processorConstructor;
161 try {
162 processorConstructor = processorClass.getConstructor();
163 } catch (SecurityException e) {
164 throw new OptionsExtractorException("Could not access default constructor in argument processor.");
165 } catch (NoSuchMethodException e) {
166 throw new OptionsExtractorException("Argument processor constructor not found.");
167 }
168 ArgumentsProcessorEngine processor;
169 try {
170 processor = (ArgumentsProcessorEngine) processorConstructor.newInstance();
171 } catch (IllegalArgumentException e) {
172 throw new OptionsExtractorException("Illegal argument while trying to create a new processor.", e);
173 } catch (InstantiationException e) {
174 throw new OptionsExtractorException("Could not instantiate processor.", e);
175 } catch (IllegalAccessException e) {
176 throw new OptionsExtractorException("Illegal access to constructor of argument processor.", e);
177 } catch (InvocationTargetException e) {
178 throw new OptionsExtractorException("Exception thrown by something inside the constructor of argument processor.", e);
179 }
180 processor.process(optionInterfaceClass, f, annotation);
181 }
182 }
183
184 return f;
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198 protected InvocationHandler buildInvocationHandler(final OptionValues<T> values, final Class<? extends Options<?>> optionsInterface, final Class<? extends StatusCode> exitCodeEnumClass, final String[] rawArguments) {
199
200 return new ConcreteArgumentsInvocationHandler<T>(values, optionsInterface, exitCodeEnumClass, rawArguments);
201 }
202 }