-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCLI.java
More file actions
95 lines (86 loc) · 2.39 KB
/
CLI.java
File metadata and controls
95 lines (86 loc) · 2.39 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package api.cli;
import java.util.Scanner;
/**
* The Class CLI.
*
* @author Gwindow
*/
public class CLI {
/** The scanner. */
private Scanner scanner = new Scanner(System.in);
/** The reflector. */
private Reflector reflector = new Reflector();
/**
* Inits the.
*
* @param path
* the path
*/
public void init(String path) {
ClassLoader.setRootPath(path);
ClassLoader.loadClassFiles();
inputListener();
}
/**
* Input listener.
*/
private void inputListener() {
while (true) {
String s = scanner.nextLine();
if (s.trim().equalsIgnoreCase("/HELP")) {
showHelpMenu();
} else if (s.trim().equalsIgnoreCase("/QUIT")) {
Utils.print("Quiting...");
System.exit(0);
} else if (s.trim().equalsIgnoreCase("/CLEAR")) {
reflector.clearStacks();
} else if (s.trim().equalsIgnoreCase("/POP")) {
reflector.popStacks();
} else if (s.trim().equalsIgnoreCase("/STACK")) {
reflector.printStack();
} else if (s.trim().equalsIgnoreCase("/CLASSES")) {
reflector.printClasses();
} else if (s.trim().equalsIgnoreCase("/METHODS")) {
reflector.printMethods();
} else if (!s.trim().isEmpty()) {
reflector.invoke(s);
} else {
Utils.print("Create new object or enter /help or /quit");
}
}
}
/**
* Show help menu.
*/
private void showHelpMenu() {
Utils.print("Type /exit at anytime to exit help");
Class<?> myClass;
while (true) {
Utils.print("Enter class you want help with or type /list");
String input = scanner.nextLine();
if (input.trim().equalsIgnoreCase("/LIST")) {
Utils.print("List of classes");
Utils.printSingleIndent(ClassLoader.getClassMap().keySet());
} else if (input.trim().equalsIgnoreCase("/EXIT")) {
Utils.print("Exited");
break;
} else {
try {
String className = input;
myClass = ClassLoader.getClassForKey(className);
Utils.print("List of methods");
for (int i = 0; i < myClass.getDeclaredMethods().length; i++) {
String name = myClass.getDeclaredMethods()[i].getName();
String params = "";
for (int j = 0; j < myClass.getDeclaredMethods()[i].getParameterTypes().length; j++) {
params = params + myClass.getDeclaredMethods()[i].getParameterTypes()[j].getName() + ",";
}
Utils.printSingleIndent(name + "(" + params + ")");
}
} catch (Exception e) {
Utils.print("Command does not exist");
}
}
}
}
}