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