Skip to content

Commit a742c3e

Browse files
committed
Add static and default interface methods support
1 parent 5231e67 commit a742c3e

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

test-app/build-tools/android-metadata-generator/src/src/com/telerik/metadata/Builder.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.HashSet;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.Set;
1516

1617
import com.telerik.metadata.TreeNode.FieldInfo;
1718
import com.telerik.metadata.TreeNode.MethodInfo;
@@ -22,6 +23,7 @@
2223
import com.telerik.metadata.desc.MethodDescriptor;
2324
import com.telerik.metadata.desc.TypeDescriptor;
2425
import com.telerik.metadata.dx.DexFile;
26+
import com.telerik.metadata.parsing.ClassParser;
2527

2628
public class Builder {
2729
private static class MethodNameComparator implements Comparator<MethodDescriptor> {
@@ -250,33 +252,14 @@ private static void getFieldsFromImplementedInterfaces(ClassDescriptor clazz, Tr
250252
}
251253

252254
private static MethodDescriptor[] getDefaultMethodsFromImplementedInterfaces(ClassDescriptor clazz, MethodDescriptor[] originalClassMethodDescriptors) {
253-
HashSet<MethodDescriptor> defaultMethods = getAllDefaultMethodsFromImplementedInterfaces(clazz);
254-
HashSet<MethodDescriptor> classMethods = new HashSet<MethodDescriptor>(Arrays.asList(originalClassMethodDescriptors));
255-
defaultMethods.removeAll(classMethods);
256-
257-
return defaultMethods.toArray(new MethodDescriptor[0]);
258-
}
259-
260-
private static HashSet<MethodDescriptor> getAllDefaultMethodsFromImplementedInterfaces(ClassDescriptor clazz) {
261-
return getAllDefaultMethodsFromImplementedInterfacesRecursively(clazz, new HashSet<MethodDescriptor>());
262-
}
263-
264-
private static HashSet<MethodDescriptor> getAllDefaultMethodsFromImplementedInterfacesRecursively(ClassDescriptor clazz, HashSet<MethodDescriptor> collectedDefaultMethods) {
265-
String[] implementedInterfacesNames = clazz.getInterfaceNames();
266-
267-
for (String implementedInterfaceName : implementedInterfacesNames) {
268-
ClassDescriptor interfaceClass = ClassRepo.findClass(implementedInterfaceName);
255+
ClassParser parser = ClassParser.forClassDescriptor(clazz);
269256

270-
for (MethodDescriptor md : interfaceClass.getMethods()) {
271-
if (!md.isStatic() && !md.isAbstract()) {
272-
collectedDefaultMethods.add(md);
273-
}
274-
}
257+
Set<MethodDescriptor> defaultMethods = parser.getAllDefaultMethodsFromImplementedInterfaces();
258+
Set<MethodDescriptor> classMethods = new HashSet<MethodDescriptor>(Arrays.asList(originalClassMethodDescriptors));
275259

276-
collectedDefaultMethods.addAll(getAllDefaultMethodsFromImplementedInterfacesRecursively(interfaceClass, new HashSet<MethodDescriptor>()));
277-
}
260+
defaultMethods.removeAll(classMethods);
278261

279-
return collectedDefaultMethods;
262+
return defaultMethods.toArray(new MethodDescriptor[0]);
280263
}
281264

282265
private static TreeNode getOrCreateNode(TreeNode root, TypeDescriptor type)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.telerik.metadata.parsing;
2+
3+
import com.telerik.metadata.ClassRepo;
4+
import com.telerik.metadata.desc.ClassDescriptor;
5+
import com.telerik.metadata.desc.MethodDescriptor;
6+
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
public final class ClassParser {
11+
12+
private final ClassDescriptor clazz;
13+
14+
private ClassParser(ClassDescriptor clazz) {
15+
this.clazz = clazz;
16+
}
17+
18+
public static ClassParser forClassDescriptor(ClassDescriptor clazz) {
19+
return new ClassParser(clazz);
20+
}
21+
22+
public Set<MethodDescriptor> getAllDefaultMethodsFromImplementedInterfaces() {
23+
return getAllDefaultMethodsFromImplementedInterfacesRecursively(clazz, new HashSet<MethodDescriptor>());
24+
}
25+
26+
private HashSet<MethodDescriptor> getAllDefaultMethodsFromImplementedInterfacesRecursively(ClassDescriptor clazz, HashSet<MethodDescriptor> collectedDefaultMethods) {
27+
String[] implementedInterfacesNames = clazz.getInterfaceNames();
28+
29+
for (String implementedInterfaceName : implementedInterfacesNames) {
30+
ClassDescriptor interfaceClass = ClassRepo.findClass(implementedInterfaceName);
31+
32+
for (MethodDescriptor md : interfaceClass.getMethods()) {
33+
if (!md.isStatic() && !md.isAbstract()) {
34+
collectedDefaultMethods.add(md);
35+
}
36+
}
37+
38+
collectedDefaultMethods.addAll(getAllDefaultMethodsFromImplementedInterfacesRecursively(interfaceClass, new HashSet<MethodDescriptor>()));
39+
}
40+
41+
return collectedDefaultMethods;
42+
}
43+
}

0 commit comments

Comments
 (0)