1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package jp.liq.container.vocabulary;
17
18 import static jp.liq.container.vocabulary.VocabularyException.Reason.*;
19 import jp.liq.container.reflect.ConstructorWrapper;
20
21
22
23
24
25
26 public class Ctor {
27
28
29
30
31
32
33
34
35
36 public <T> ConstructorWrapper<T> of(Class<T> type)
37 throws VocabularyException {
38 java.lang.reflect.Constructor<?>[] ctors = type.getConstructors();
39 if (ctors.length > 1) {
40 throw new VocabularyException(type,
41 TOO_MANY_CONSTRUCTOR);
42 }
43
44 java.lang.reflect.Constructor<T> ctor = null;
45 try {
46 ctor = type.getConstructor(ctors[0].getParameterTypes());
47 } catch (Exception e) {
48 throw new VocabularyException(e, type,
49 FAILED_TO_FIND_CONSTRUCTOR);
50 }
51 return new ConstructorWrapper<T>(ctor);
52 }
53
54
55
56
57
58
59
60
61
62
63 public <T> ConstructorWrapper<T> of(Class<T> type, Class<?>... args)
64 throws VocabularyException {
65 try {
66 return new ConstructorWrapper<T>(type.getConstructor(args));
67 } catch (Exception e) {
68 throw new VocabularyException(e, type,
69 FAILED_TO_FIND_CONSTRUCTOR);
70 }
71 }
72
73 }