2
2
3
3
import com .annimon .ownlang .annotations .Modules ;
4
4
import com .annimon .ownlang .lib .Functions ;
5
+ import com .annimon .ownlang .lib .MapValue ;
5
6
import com .annimon .ownlang .lib .Types ;
6
7
import com .annimon .ownlang .lib .Value ;
7
8
import com .annimon .ownlang .lib .Variables ;
8
9
import com .annimon .ownlang .lib .modules .Module ;
9
10
import java .util .ArrayList ;
11
+ import java .util .Arrays ;
12
+ import java .util .Collections ;
10
13
import java .util .Comparator ;
11
14
import java .util .HashMap ;
12
15
import java .util .List ;
13
16
import java .util .Map ;
17
+ import java .util .Set ;
18
+ import java .util .stream .Collectors ;
19
+ import java .util .stream .Stream ;
14
20
import org .json .JSONArray ;
15
21
import org .json .JSONObject ;
16
22
@@ -32,6 +38,7 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
32
38
final ModuleInfo moduleInfo = new ModuleInfo (moduleClass .getSimpleName ());
33
39
moduleInfo .functions .addAll (Functions .getFunctions ().keySet ());
34
40
moduleInfo .constants .putAll (Variables .variables ());
41
+ moduleInfo .types .addAll (listValues (moduleClass ));
35
42
moduleInfos .add (moduleInfo );
36
43
}
37
44
@@ -52,15 +59,30 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
52
59
);
53
60
}
54
61
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
+
55
75
static class ModuleInfo {
56
76
private final String name ;
57
77
List <String > functions ;
58
78
Map <String , Value > constants ;
79
+ List <String > types ;
59
80
60
81
public ModuleInfo (String name ) {
61
82
this .name = name ;
62
83
functions = new ArrayList <>();
63
84
constants = new HashMap <>();
85
+ types = new ArrayList <>();
64
86
}
65
87
66
88
public JSONArray constantsJSON () {
@@ -74,7 +96,16 @@ public JSONArray constantsJSON() {
74
96
constant .put ("name" , entry .getKey ());
75
97
constant .put ("type" , value .type ());
76
98
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
+ }
78
109
result .put (constant );
79
110
});
80
111
return result ;
@@ -85,6 +116,15 @@ public JSONObject toJSON() {
85
116
json .put ("name" , name );
86
117
json .put ("functions" , functions .stream ().sorted ().toArray ());
87
118
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
+ }
88
128
return json ;
89
129
}
90
130
}
0 commit comments