Вывод информации о всех функция и константах модулей

This commit is contained in:
Victor 2016-04-06 16:46:03 +03:00
parent 66415a6c31
commit 3a75727982
4 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package com.annimon.ownlang.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PACKAGE)
public @interface Modules {
Class[] modules();
}

View File

@ -42,6 +42,10 @@ public final class Variables {
scope.variables.put("false", NumberValue.ZERO); scope.variables.put("false", NumberValue.ZERO);
} }
public static Map<String, Value> variables() {
return scope.variables;
}
public static void push() { public static void push() {
synchronized (lock) { synchronized (lock) {
final Scope newScope = new Scope(scope); final Scope newScope = new Scope(scope);

View File

@ -0,0 +1,16 @@
@Modules(modules = {
canvas.class,
canvasfx.class,
files.class,
functional.class,
http.class,
json.class,
math.class,
ounit.class,
robot.class,
std.class,
types.class
})
package com.annimon.ownlang.lib.modules;
import com.annimon.ownlang.annotations.Modules;

View File

@ -0,0 +1,91 @@
package com.annimon.ownlang.utils;
import com.annimon.ownlang.annotations.Modules;
import com.annimon.ownlang.lib.Functions;
import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.Variables;
import com.annimon.ownlang.lib.modules.Module;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
public final class ModulesInfoCreator {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
final Class<Module> clazz = Module.class; // get classloader for package
final List<ModuleInfo> moduleInfos = new ArrayList<>();
final Package modulesPackage = Package.getPackage("com.annimon.ownlang.lib.modules");
final Modules annotation = modulesPackage.getAnnotation(Modules.class);
for (Class moduleClass : annotation.modules()) {
Functions.getFunctions().clear();
Variables.variables().clear();
final Module module = (Module) moduleClass.newInstance();
module.init();
final ModuleInfo moduleInfo = new ModuleInfo(moduleClass.getSimpleName());
moduleInfo.functions.addAll(Functions.getFunctions().keySet());
moduleInfo.constants.putAll(Variables.variables());
moduleInfos.add(moduleInfo);
}
final JSONArray modulesJson = new JSONArray();
for (ModuleInfo moduleInfo : moduleInfos) {
modulesJson.put(moduleInfo.toJSON());
}
System.out.println(modulesJson.toString(2));
System.out.println("Total modules: " + moduleInfos.size());
System.out.println("Total functions: " + moduleInfos.stream()
.flatMap(m -> m.functions.stream())
.count()
);
System.out.println("Total constants: " + moduleInfos.stream()
.flatMap(m -> m.constants.keySet().stream())
.count()
);
}
static class ModuleInfo {
private final String name;
List<String> functions;
Map<String, Value> constants;
public ModuleInfo(String name) {
this.name = name;
functions = new ArrayList<>();
constants = new HashMap<>();
}
public JSONArray constantsJSON() {
final JSONArray result = new JSONArray();
constants.entrySet().stream()
.sorted(Comparator.comparing(e -> e.getKey()))
.forEach(entry -> {
final Value value = entry.getValue();
final JSONObject constant = new JSONObject();
constant.put("name", entry.getKey());
constant.put("type", value.type());
constant.put("typeName", Types.typeToString(value.type()));
constant.put("value", value.asString());
result.put(constant);
});
return result;
}
public JSONObject toJSON() {
final JSONObject json = new JSONObject();
json.put("name", name);
json.put("functions", functions.stream().sorted().toArray());
json.put("constants", constantsJSON());
return json;
}
}
}