Skip to content

Commit 059b4c4

Browse files
committed
Обновлён ModulesInfoCreator
1 parent 7e16198 commit 059b4c4

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/com/annimon/ownlang/utils/ModulesInfoCreator.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
import com.annimon.ownlang.annotations.Modules;
44
import com.annimon.ownlang.lib.Functions;
5+
import com.annimon.ownlang.lib.MapValue;
56
import com.annimon.ownlang.lib.Types;
67
import com.annimon.ownlang.lib.Value;
78
import com.annimon.ownlang.lib.Variables;
89
import com.annimon.ownlang.lib.modules.Module;
910
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.Collections;
1013
import java.util.Comparator;
1114
import java.util.HashMap;
1215
import java.util.List;
1316
import java.util.Map;
17+
import java.util.Set;
18+
import java.util.stream.Collectors;
19+
import java.util.stream.Stream;
1420
import org.json.JSONArray;
1521
import org.json.JSONObject;
1622

@@ -32,6 +38,7 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
3238
final ModuleInfo moduleInfo = new ModuleInfo(moduleClass.getSimpleName());
3339
moduleInfo.functions.addAll(Functions.getFunctions().keySet());
3440
moduleInfo.constants.putAll(Variables.variables());
41+
moduleInfo.types.addAll(listValues(moduleClass));
3542
moduleInfos.add(moduleInfo);
3643
}
3744

@@ -52,15 +59,30 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
5259
);
5360
}
5461

62+
private static List<String> listValues(Class moduleClass) {
63+
return Arrays.stream(moduleClass.getDeclaredClasses())
64+
.filter(clazz -> getAllInterfaces(clazz).stream().anyMatch(i -> i.equals(Value.class)))
65+
.map(Class::getSimpleName)
66+
.collect(Collectors.toList());
67+
}
68+
69+
private static Set<Class> getAllInterfaces(Class clazz) {
70+
if (clazz.getSuperclass() == null) return Collections.emptySet();
71+
return Stream.concat(Arrays.stream(clazz.getInterfaces()), getAllInterfaces(clazz.getSuperclass()).stream())
72+
.collect(Collectors.toSet());
73+
}
74+
5575
static class ModuleInfo {
5676
private final String name;
5777
List<String> functions;
5878
Map<String, Value> constants;
79+
List<String> types;
5980

6081
public ModuleInfo(String name) {
6182
this.name = name;
6283
functions = new ArrayList<>();
6384
constants = new HashMap<>();
85+
types = new ArrayList<>();
6486
}
6587

6688
public JSONArray constantsJSON() {
@@ -74,7 +96,16 @@ public JSONArray constantsJSON() {
7496
constant.put("name", entry.getKey());
7597
constant.put("type", value.type());
7698
constant.put("typeName", Types.typeToString(value.type()));
77-
constant.put("value", value.asString());
99+
if (value.type() == Types.MAP) {
100+
String text = ((Map<Value, Value>) value.raw()).entrySet().stream()
101+
.sorted(Comparator.comparing(
102+
e -> ((MapValue)value).size() > 16 ? e.getKey() : e.getValue()))
103+
.map(s -> s.toString())
104+
.collect(Collectors.joining(", ", "{", "}"));
105+
constant.put("value", text);
106+
} else {
107+
constant.put("value", value.asString());
108+
}
78109
result.put(constant);
79110
});
80111
return result;
@@ -85,6 +116,15 @@ public JSONObject toJSON() {
85116
json.put("name", name);
86117
json.put("functions", functions.stream().sorted().toArray());
87118
json.put("constants", constantsJSON());
119+
if (!types.isEmpty()) {
120+
json.put("types", types.stream().sorted()
121+
.map(s -> {
122+
final JSONObject type = new JSONObject();
123+
type.put("name", s);
124+
return type;
125+
})
126+
.toArray());
127+
}
88128
return json;
89129
}
90130
}

0 commit comments

Comments
 (0)