|
| 1 | +package com.annimon.ownlang.utils; |
| 2 | + |
| 3 | +import com.annimon.ownlang.annotations.Modules; |
| 4 | +import com.annimon.ownlang.lib.Functions; |
| 5 | +import com.annimon.ownlang.lib.Types; |
| 6 | +import com.annimon.ownlang.lib.Value; |
| 7 | +import com.annimon.ownlang.lib.Variables; |
| 8 | +import com.annimon.ownlang.lib.modules.Module; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Comparator; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import org.json.JSONArray; |
| 15 | +import org.json.JSONObject; |
| 16 | + |
| 17 | +public final class ModulesInfoCreator { |
| 18 | + |
| 19 | + public static void main(String[] args) throws InstantiationException, IllegalAccessException { |
| 20 | + final Class<Module> clazz = Module.class; // get classloader for package |
| 21 | + |
| 22 | + final List<ModuleInfo> moduleInfos = new ArrayList<>(); |
| 23 | + |
| 24 | + final Package modulesPackage = Package.getPackage("com.annimon.ownlang.lib.modules"); |
| 25 | + final Modules annotation = modulesPackage.getAnnotation(Modules.class); |
| 26 | + for (Class moduleClass : annotation.modules()) { |
| 27 | + Functions.getFunctions().clear(); |
| 28 | + Variables.variables().clear(); |
| 29 | + final Module module = (Module) moduleClass.newInstance(); |
| 30 | + module.init(); |
| 31 | + |
| 32 | + final ModuleInfo moduleInfo = new ModuleInfo(moduleClass.getSimpleName()); |
| 33 | + moduleInfo.functions.addAll(Functions.getFunctions().keySet()); |
| 34 | + moduleInfo.constants.putAll(Variables.variables()); |
| 35 | + moduleInfos.add(moduleInfo); |
| 36 | + } |
| 37 | + |
| 38 | + final JSONArray modulesJson = new JSONArray(); |
| 39 | + for (ModuleInfo moduleInfo : moduleInfos) { |
| 40 | + modulesJson.put(moduleInfo.toJSON()); |
| 41 | + } |
| 42 | + System.out.println(modulesJson.toString(2)); |
| 43 | + |
| 44 | + System.out.println("Total modules: " + moduleInfos.size()); |
| 45 | + System.out.println("Total functions: " + moduleInfos.stream() |
| 46 | + .flatMap(m -> m.functions.stream()) |
| 47 | + .count() |
| 48 | + ); |
| 49 | + System.out.println("Total constants: " + moduleInfos.stream() |
| 50 | + .flatMap(m -> m.constants.keySet().stream()) |
| 51 | + .count() |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + static class ModuleInfo { |
| 56 | + private final String name; |
| 57 | + List<String> functions; |
| 58 | + Map<String, Value> constants; |
| 59 | + |
| 60 | + public ModuleInfo(String name) { |
| 61 | + this.name = name; |
| 62 | + functions = new ArrayList<>(); |
| 63 | + constants = new HashMap<>(); |
| 64 | + } |
| 65 | + |
| 66 | + public JSONArray constantsJSON() { |
| 67 | + final JSONArray result = new JSONArray(); |
| 68 | + constants.entrySet().stream() |
| 69 | + .sorted(Comparator.comparing(e -> e.getKey())) |
| 70 | + .forEach(entry -> { |
| 71 | + final Value value = entry.getValue(); |
| 72 | + |
| 73 | + final JSONObject constant = new JSONObject(); |
| 74 | + constant.put("name", entry.getKey()); |
| 75 | + constant.put("type", value.type()); |
| 76 | + constant.put("typeName", Types.typeToString(value.type())); |
| 77 | + constant.put("value", value.asString()); |
| 78 | + result.put(constant); |
| 79 | + }); |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + public JSONObject toJSON() { |
| 84 | + final JSONObject json = new JSONObject(); |
| 85 | + json.put("name", name); |
| 86 | + json.put("functions", functions.stream().sorted().toArray()); |
| 87 | + json.put("constants", constantsJSON()); |
| 88 | + return json; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments