Skip to content

Commit 3a75727

Browse files
committed
Вывод информации о всех функция и константах модулей
1 parent 66415a6 commit 3a75727

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.annimon.ownlang.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.PACKAGE)
10+
public @interface Modules {
11+
12+
Class[] modules();
13+
}

src/com/annimon/ownlang/lib/Variables.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public static void clear() {
4141
scope.variables.put("true", NumberValue.ONE);
4242
scope.variables.put("false", NumberValue.ZERO);
4343
}
44+
45+
public static Map<String, Value> variables() {
46+
return scope.variables;
47+
}
4448

4549
public static void push() {
4650
synchronized (lock) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@Modules(modules = {
2+
canvas.class,
3+
canvasfx.class,
4+
files.class,
5+
functional.class,
6+
http.class,
7+
json.class,
8+
math.class,
9+
ounit.class,
10+
robot.class,
11+
std.class,
12+
types.class
13+
})
14+
package com.annimon.ownlang.lib.modules;
15+
16+
import com.annimon.ownlang.annotations.Modules;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

Comments
 (0)