1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.pgm;
45
46 import java.lang.reflect.Constructor;
47 import java.lang.reflect.InvocationTargetException;
48 import java.text.MessageFormat;
49
50 import org.eclipse.jgit.pgm.internal.CLIText;
51
52
53
54
55
56
57
58
59
60 public class CommandRef {
61 private final Class<? extends TextBuiltin> impl;
62
63 private final String name;
64
65 private String usage;
66
67 boolean common;
68
69 CommandRef(Class<? extends TextBuiltin> clazz) {
70 this(clazz, guessName(clazz));
71 }
72
73 CommandRef(Class<? extends TextBuiltin> clazz, Command cmd) {
74 this(clazz, cmd.name().length() > 0 ? cmd.name() : guessName(clazz));
75 usage = cmd.usage();
76 common = cmd.common();
77 }
78
79 private CommandRef(Class<? extends TextBuiltin> clazz, String cn) {
80 impl = clazz;
81 name = cn;
82 usage = "";
83 }
84
85 private static String guessName(Class<? extends TextBuiltin> clazz) {
86 final StringBuilder s = new StringBuilder();
87 if (clazz.getName().startsWith("org.eclipse.jgit.pgm.debug."))
88 s.append("debug-");
89
90 boolean lastWasDash = true;
91 for (char c : clazz.getSimpleName().toCharArray()) {
92 if (Character.isUpperCase(c)) {
93 if (!lastWasDash)
94 s.append('-');
95 lastWasDash = !lastWasDash;
96 s.append(Character.toLowerCase(c));
97 } else {
98 s.append(c);
99 lastWasDash = false;
100 }
101 }
102 return s.toString();
103 }
104
105
106
107
108
109
110 public String getName() {
111 return name;
112 }
113
114
115
116
117
118
119 public String getUsage() {
120 return usage;
121 }
122
123
124
125
126
127
128 public boolean isCommon() {
129 return common;
130 }
131
132
133
134
135
136
137 public String getImplementationClassName() {
138 return impl.getName();
139 }
140
141
142
143
144
145
146 public ClassLoader getImplementationClassLoader() {
147 return impl.getClassLoader();
148 }
149
150
151
152
153
154
155 public TextBuiltin create() {
156 final Constructor<? extends TextBuiltin> c;
157 try {
158 c = impl.getDeclaredConstructor();
159 } catch (SecurityException e) {
160 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
161 } catch (NoSuchMethodException e) {
162 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
163 }
164 c.setAccessible(true);
165
166 final TextBuiltin r;
167 try {
168 r = c.newInstance();
169 } catch (InstantiationException e) {
170 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
171 } catch (IllegalAccessException e) {
172 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
173 } catch (IllegalArgumentException e) {
174 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
175 } catch (InvocationTargetException e) {
176 throw new RuntimeException(MessageFormat.format(CLIText.get().cannotCreateCommand, getName(), e));
177 }
178 r.setCommandName(getName());
179 return r;
180 }
181
182
183 @SuppressWarnings("nls")
184 @Override
185 public String toString() {
186 return "CommandRef [impl=" + impl + ", name=" + name + ", usage="
187 + CLIText.get().resourceBundle().getString(usage) + ", common="
188 + common + "]";
189 }
190 }