Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 31e1ae6

Browse files
author
Aleksi Salmela
committed
Move the CommandFactory initialization to reload method.
1 parent d3446b3 commit 31e1ae6

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/main/java/fi/helsinki/cs/tmc/cli/core/CommandAnnotationProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void generateSourceFile(Map<String, String> map) throws IOException {
7373
}
7474

7575
bwriter.append("\npublic class " + CLASS_NAME + " {\n");
76-
bwriter.append(TAB + "static {\n");
76+
bwriter.append(TAB + "public " + CLASS_NAME + "() {\n");
7777
for (Entry<String, String> entry : map.entrySet()) {
7878
String classPath = entry.getValue();
7979
String[] parts = classPath.split("\\.");

src/main/java/fi/helsinki/cs/tmc/cli/core/CommandFactory.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.lang.annotation.Annotation;
66
import java.util.ArrayList;
77
import java.util.HashMap;
8-
import java.util.HashSet;
98
import java.util.List;
109
import java.util.Map;
1110
import java.util.Set;
@@ -16,19 +15,11 @@
1615
public class CommandFactory {
1716

1817
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CommandFactory.class);
19-
private static final Map<String, Class<Command>> commands = new HashMap<>();
20-
private static final Map<String, List<Class<Command>>> packages = new HashMap<>();
18+
private static Map<String, Class<Command>> commands;
19+
private static Map<String, List<Class<Command>>> packages;
2120

2221
static {
23-
/* Force load the CommandList so that it's static initialization block is executed.
24-
* This hack is used instead of import so that the IDEs won't cry about the nonexistent
25-
* class.
26-
*/
27-
try {
28-
Class.forName("fi.helsinki.cs.tmc.cli.core.CommandList");
29-
} catch (ClassNotFoundException ex) {
30-
System.out.println("Fail " + ex);
31-
}
22+
CommandFactory.reload();
3223
}
3324

3425
/**
@@ -121,4 +112,19 @@ public static List<Class<Command>> getCategoryCommands(String category) {
121112
public static Class<Command> castToCommandClass(Class command) {
122113
return command;
123114
}
115+
116+
protected static void reload() {
117+
CommandFactory.commands = new HashMap<>();
118+
CommandFactory.packages = new HashMap<>();
119+
120+
/* Run constructor of the CommandList.
121+
* This hack is used instead of import so that the IDEs won't cry about the nonexistent
122+
* class.
123+
*/
124+
try {
125+
Class.forName("fi.helsinki.cs.tmc.cli.core.CommandList").newInstance();
126+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
127+
logger.warn("CommandList initialization failed", ex);
128+
}
129+
}
124130
}

0 commit comments

Comments
 (0)