Обновлён ModulesInfoCreator

This commit is contained in:
Victor 2016-05-19 14:00:46 +03:00
parent 7e161985da
commit 059b4c46d0

View File

@ -2,15 +2,21 @@ package com.annimon.ownlang.utils;
import com.annimon.ownlang.annotations.Modules; import com.annimon.ownlang.annotations.Modules;
import com.annimon.ownlang.lib.Functions; import com.annimon.ownlang.lib.Functions;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.Types; import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.Value; import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.Variables; import com.annimon.ownlang.lib.Variables;
import com.annimon.ownlang.lib.modules.Module; import com.annimon.ownlang.lib.modules.Module;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -32,6 +38,7 @@ public final class ModulesInfoCreator {
final ModuleInfo moduleInfo = new ModuleInfo(moduleClass.getSimpleName()); final ModuleInfo moduleInfo = new ModuleInfo(moduleClass.getSimpleName());
moduleInfo.functions.addAll(Functions.getFunctions().keySet()); moduleInfo.functions.addAll(Functions.getFunctions().keySet());
moduleInfo.constants.putAll(Variables.variables()); moduleInfo.constants.putAll(Variables.variables());
moduleInfo.types.addAll(listValues(moduleClass));
moduleInfos.add(moduleInfo); moduleInfos.add(moduleInfo);
} }
@ -52,15 +59,30 @@ public final class ModulesInfoCreator {
); );
} }
private static List<String> listValues(Class moduleClass) {
return Arrays.stream(moduleClass.getDeclaredClasses())
.filter(clazz -> getAllInterfaces(clazz).stream().anyMatch(i -> i.equals(Value.class)))
.map(Class::getSimpleName)
.collect(Collectors.toList());
}
private static Set<Class> getAllInterfaces(Class clazz) {
if (clazz.getSuperclass() == null) return Collections.emptySet();
return Stream.concat(Arrays.stream(clazz.getInterfaces()), getAllInterfaces(clazz.getSuperclass()).stream())
.collect(Collectors.toSet());
}
static class ModuleInfo { static class ModuleInfo {
private final String name; private final String name;
List<String> functions; List<String> functions;
Map<String, Value> constants; Map<String, Value> constants;
List<String> types;
public ModuleInfo(String name) { public ModuleInfo(String name) {
this.name = name; this.name = name;
functions = new ArrayList<>(); functions = new ArrayList<>();
constants = new HashMap<>(); constants = new HashMap<>();
types = new ArrayList<>();
} }
public JSONArray constantsJSON() { public JSONArray constantsJSON() {
@ -74,7 +96,16 @@ public final class ModulesInfoCreator {
constant.put("name", entry.getKey()); constant.put("name", entry.getKey());
constant.put("type", value.type()); constant.put("type", value.type());
constant.put("typeName", Types.typeToString(value.type())); constant.put("typeName", Types.typeToString(value.type()));
constant.put("value", value.asString()); if (value.type() == Types.MAP) {
String text = ((Map<Value, Value>) value.raw()).entrySet().stream()
.sorted(Comparator.comparing(
e -> ((MapValue)value).size() > 16 ? e.getKey() : e.getValue()))
.map(s -> s.toString())
.collect(Collectors.joining(", ", "{", "}"));
constant.put("value", text);
} else {
constant.put("value", value.asString());
}
result.put(constant); result.put(constant);
}); });
return result; return result;
@ -85,6 +116,15 @@ public final class ModulesInfoCreator {
json.put("name", name); json.put("name", name);
json.put("functions", functions.stream().sorted().toArray()); json.put("functions", functions.stream().sorted().toArray());
json.put("constants", constantsJSON()); json.put("constants", constantsJSON());
if (!types.isEmpty()) {
json.put("types", types.stream().sorted()
.map(s -> {
final JSONObject type = new JSONObject();
type.put("name", s);
return type;
})
.toArray());
}
return json; return json;
} }
} }