@@ -33,13 +33,16 @@ public class DtsApi {
33
33
private Set <String > baseMethodNames ;
34
34
private List <Method > baseMethods ;
35
35
private Map <String , Method > mapNameMethod ;
36
+ private Map <String , String > aliasedTypes ;
37
+ private String [] namespaceParts ;
36
38
private int indent = 0 ;
37
39
38
-
39
40
public DtsApi () {
40
41
this .indent = 0 ;
41
42
42
43
overrideFieldComparator ();
44
+
45
+ this .aliasedTypes = new HashMap <>();
43
46
}
44
47
45
48
public String generateDtsContent (List <JavaClass > javaClasses ) {
@@ -64,6 +67,11 @@ public String generateDtsContent(List<JavaClass> javaClasses) {
64
67
continue ;
65
68
}
66
69
70
+ // check if processed class hijacks a namespace
71
+ // TODO: optimize
72
+
73
+ this .namespaceParts = currentFileClassname .split ("\\ ." );
74
+
67
75
boolean isInterface = currClass .isInterface ();
68
76
boolean isAbstract = currClass .isAbstract ();
69
77
@@ -166,13 +174,48 @@ private String getExtendsLine(JavaClass superClass, List<JavaClass> interfaces)
166
174
implementsSegmentSb .append (" implements " );
167
175
168
176
for (JavaClass clazz : interfaces ) {
169
- implementsSegmentSb .append (clazz .getClassName ().replaceAll ("\\ $" , "\\ ." ) + ", " );
177
+ String implementedInterface = clazz .getClassName ().replaceAll ("\\ $" , "\\ ." );
178
+ if (!typeBelongsInCurrentTopLevelNamespace (implementedInterface )) {
179
+ implementedInterface = getAliasedClassName (implementedInterface );
180
+ }
181
+
182
+ implementsSegmentSb .append (implementedInterface + ", " );
170
183
}
171
184
172
185
implementsSegment = implementsSegmentSb .substring (0 , implementsSegmentSb .lastIndexOf ("," ));
186
+
187
+ }
188
+
189
+ String extendedClass = superClass .getClassName ().replaceAll ("\\ $" , "\\ ." );
190
+ if (!typeBelongsInCurrentTopLevelNamespace (extendedClass )) {
191
+ extendedClass = getAliasedClassName (extendedClass );
192
+ }
193
+
194
+ return " extends " + extendedClass + implementsSegment ;
195
+ }
196
+
197
+ private String getAliasedClassName (String className ) {
198
+ String aliasedType = aliasedTypes .get (className );
199
+ if (aliasedType == null ) {
200
+ aliasedType = mangleClassname (className );
201
+ aliasedTypes .put (className , aliasedType );
202
+
203
+ sbHeaders .append ("import " );
204
+ sbHeaders .append (aliasedType );
205
+ sbHeaders .append (" = " );
206
+ sbHeaders .append (className );
207
+ sbHeaders .appendln (";" );
173
208
}
174
209
175
- return " extends " + superClass .getClassName ().replaceAll ("\\ $" , "\\ ." ) + implementsSegment ;
210
+ return aliasedType ;
211
+ }
212
+
213
+ private boolean typeBelongsInCurrentTopLevelNamespace (String className ) {
214
+ return className .startsWith (this .namespaceParts [0 ] + "." );
215
+ }
216
+
217
+ private String mangleClassname (String className ) {
218
+ return className .replaceAll ("\\ ." , "" );
176
219
}
177
220
178
221
private int closePackage (JavaClass prevClass , JavaClass currClass ) {
@@ -641,8 +684,12 @@ private void convertToTypeScriptType(Type type, StringBuilder tsType) {
641
684
typeName = typeName .replaceAll ("\\ $" , "\\ ." );
642
685
}
643
686
644
- // TODO: Pete:
645
- tsType .append (typeName );
687
+ if (!typeBelongsInCurrentTopLevelNamespace (typeName ) && !typeName .startsWith ("java.util.function." )) {
688
+ tsType .append (getAliasedClassName (typeName ));
689
+ } else {
690
+ tsType .append (typeName );
691
+ }
692
+
646
693
addReference (type );
647
694
} else {
648
695
throw new RuntimeException ("Unhandled type=" + type .getSignature ());
0 commit comments