Skip to content

Commit bac3b1d

Browse files
authored
Merge pull request #11 from NativeScript/pete/improve-method-generation
Pete/improve method generation
2 parents af5eed6 + ac0fe09 commit bac3b1d

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

dts-generator/src/main/java/com/telerik/dts/DtsApi.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public String generateDtsContent(List<JavaClass> javaClasses) {
5151

5252
// process class scope
5353
for(int i = 0; i < javaClasses.size(); i++) {
54+
Set<String> methodsSet = new HashSet<>();
5455

5556
JavaClass currClass = javaClasses.get(i);
5657
currentFileClassname = currClass.getClassName();
@@ -84,7 +85,7 @@ public String generateDtsContent(List<JavaClass> javaClasses) {
8485
processInterfaceConstructor(currClass, allInterfacesMethods);
8586

8687
for(Method m : allInterfacesMethods) {
87-
processMethod(m, currClass);
88+
processMethod(m, currClass, methodsSet);
8889
}
8990

9091
for(Field f : allInterfaceFields) {
@@ -96,7 +97,7 @@ public String generateDtsContent(List<JavaClass> javaClasses) {
9697
if (fom instanceof Field) {
9798
processField((Field) fom, currClass);
9899
} else if (fom instanceof Method) {
99-
processMethod((Method) fom, currClass);
100+
processMethod((Method) fom, currClass, methodsSet);
100101
} else {
101102
throw new IllegalArgumentException("Argument is not method or field");
102103
}
@@ -108,10 +109,12 @@ public String generateDtsContent(List<JavaClass> javaClasses) {
108109
List<JavaClass> allInterfaces = getAllInterfaces(currClass);
109110
List<Method> allInterfacesMethods = getAllInterfacesMethods(allInterfaces);
110111
for(Method m : allInterfacesMethods) {
111-
processMethod(m, currClass);
112+
processMethod(m, currClass, methodsSet);
112113
}
113114
}
114115

116+
writeMethods(methodsSet);
117+
115118
sbContent.appendln(tabs + "}");
116119
if(getSimpleClassname(currClass).equals("AccessibilityDelegate")) {
117120
String innerClassAlias = "export type " + getSimpleClassname(currClass) + " = " + getFullClassNameConcatenated(currClass);
@@ -341,7 +344,7 @@ private Set<Field> getAllInterfacesFields(List<JavaClass> interfaces) {
341344
return allInterfacesFields;
342345
}
343346
//method related
344-
private void processMethod(Method m, JavaClass clazz) {
347+
private void processMethod(Method m, JavaClass clazz, Set<String> methodsSet) {
345348
String name = m.getName();
346349

347350
// TODO: Pete: won't generate static initializers as invalid typescript properties
@@ -355,34 +358,43 @@ private void processMethod(Method m, JavaClass clazz) {
355358

356359
cacheMethodBySignature(m); //cached in "mapNameMethod"
357360

358-
359361
//generate base method content
360362
if (baseMethodNames.contains(name)) {
361363
for (Method bm : baseMethods) {
362364
if (bm.getName().equals(name)) {
363365
String sig = getMethodFullSignature(bm);
364366
if (!mapNameMethod.containsKey(sig)) {
365367
mapNameMethod.put(sig, bm);
366-
generateMethodContent(clazz, tabs, bm);
368+
methodsSet.add(generateMethodContent(clazz, tabs, bm));
367369
}
368370
}
369371
}
370372
}
371373

372-
generateMethodContent(clazz, tabs, m);
374+
methodsSet.add(generateMethodContent(clazz, tabs, m));
373375
}
374376

375-
private void generateMethodContent(JavaClass clazz, String tabs, Method m) {
376-
sbContent.append(tabs + "public ");
377+
private String generateMethodContent(JavaClass clazz, String tabs, Method m) {
378+
StringBuilder2 sbTemp = new StringBuilder2();
379+
sbTemp.append(tabs + "public ");
377380
if (m.isStatic()) {
378-
sbContent.append("static ");
381+
sbTemp.append("static ");
379382
}
380-
sbContent.append(getMethodName(m) + getMethodParamSignature(clazz, m));
383+
sbTemp.append(getMethodName(m) + getMethodParamSignature(clazz, m));
381384
String bmSig = "";
382385
if (!isConstructor(m)) {
383386
bmSig += ": " + getTypeScriptTypeFromJavaType(clazz, m.getReturnType());
384387
}
385-
sbContent.appendln(bmSig + ";");
388+
389+
sbTemp.append(bmSig + ";");
390+
391+
return sbTemp.toString();
392+
}
393+
394+
private void writeMethods(Set<String> methodsSet) {
395+
for(String m : methodsSet) {
396+
sbContent.appendln(m);
397+
}
386398
}
387399

388400
private void cacheMethodBySignature(Method m) {

0 commit comments

Comments
 (0)