added "main" class as main entrypoint
This commit is contained in:
87
src/main/java/de/szimnau/zeitlaeufer/Main.java
Normal file
87
src/main/java/de/szimnau/zeitlaeufer/Main.java
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package de.szimnau.zeitlaeufer;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static de.szimnau.zeitlaeufer.tools.CommonTools.print;
|
||||||
|
import static de.szimnau.zeitlaeufer.tools.CommonTools.println;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
|
||||||
|
if (args.length == 0) {
|
||||||
|
askParametersAndRun();
|
||||||
|
} else {
|
||||||
|
parseParametersAndRun(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void askParametersAndRun() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||||
|
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
||||||
|
Map<Integer, Class<?>> classes = getAllrelevantClasses();
|
||||||
|
String printedClasses = classes.entrySet().stream()
|
||||||
|
.map(e -> e.getKey() + ": " + e.getValue().getCanonicalName().substring(e.getValue().getCanonicalName().lastIndexOf('.') + 1))
|
||||||
|
.reduce("", (a, b) -> a + "\n" + b);
|
||||||
|
print("Welcher Zeitläufer soll verwendet werden? " + printedClasses + "\n: ");
|
||||||
|
var num = Integer.parseInt(br.readLine());
|
||||||
|
Class<?> selectedClass = classes.get(num);
|
||||||
|
var mainMethod = selectedClass.getMethod("main", String[].class);
|
||||||
|
mainMethod.invoke(null, (Object) new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static Map<Integer, Class<?>> getAllrelevantClasses() {
|
||||||
|
InputStream stream = ClassLoader.getSystemClassLoader()
|
||||||
|
.getResourceAsStream("de/szimnau/zeitlaeufer");
|
||||||
|
var br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
|
||||||
|
var increment = new AtomicInteger();
|
||||||
|
return Collections.unmodifiableSortedMap(new TreeMap<>(
|
||||||
|
br.lines()
|
||||||
|
.filter(line -> line.endsWith(".class") && !"Main.class".equals(line))
|
||||||
|
.sorted()
|
||||||
|
.map(Main::getClass)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.filter(clazz ->
|
||||||
|
Arrays.stream(clazz.getMethods()).anyMatch(m ->
|
||||||
|
"main".equals(m.getName()) && m.getParameterCount() == 1 && m.getParameterTypes()[0] == String[].class
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.collect(Collectors.toMap(c -> increment.incrementAndGet(), Function.identity()))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static Class<?> getClass(String className) {
|
||||||
|
return getClassForName(className.substring(0, className.lastIndexOf('.')));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static Class<?> getClassForName(String className) {
|
||||||
|
String fqnClassName = "de.szimnau.zeitlaeufer." + className;
|
||||||
|
try {
|
||||||
|
return Class.forName(fqnClassName);
|
||||||
|
} catch (ClassNotFoundException cnfe) {
|
||||||
|
println("ERROR: could not find Class for Name: de.szimnau.zeitlaeufer." + fqnClassName);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void parseParametersAndRun(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||||
|
Class<?> selectedClass = getClassForName(args[0]);
|
||||||
|
if (selectedClass == null) {
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
var mainMethod = selectedClass.getMethod("main", String[].class);
|
||||||
|
mainMethod.invoke(null, (Object) Arrays.copyOfRange(args, 1, args.length));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user