Skip to content

Commit 90c3c64

Browse files
author
Mihail Slavchev
committed
refactor processing of interface/abstract methods
1 parent 5001557 commit 90c3c64

7 files changed

Lines changed: 66 additions & 38 deletions

File tree

android-static-binding-generator/project/staticbindinggenerator/src/main/java/org/nativescript/staticbindinggenerator/ClassInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ public boolean isFinal() {
164164
return (isArray() || isPrimitive()) ? false : clazz.isFinal();
165165
}
166166

167+
@Override
168+
public boolean isAbstract() {
169+
return (isArray() || isPrimitive()) ? false : clazz.isAbstract();
170+
}
171+
167172
static ClassDescriptor fromPrimitive(ClassDescriptor primitiveType, int rank) {
168173
return (rank == 0) ? primitiveType : new ClassInfo(primitiveType, rank);
169174
}

android-static-binding-generator/project/staticbindinggenerator/src/main/java/org/nativescript/staticbindinggenerator/MethodInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public boolean isFinal() {
7171
return m.isFinal();
7272
}
7373

74+
@Override
75+
public boolean isAbstract() {
76+
return m.isAbstract();
77+
}
78+
7479
private ClassDescriptor convertType(Type t) {
7580
ClassDescriptor desc;
7681
String sig = t.getSignature();

binding-generator/src/main/java/com/tns/bindings/Dump.java

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -251,56 +251,56 @@ private String getMethodSignature(MethodDescriptor m)
251251
return m.getName() + sig.substring(nameIdx, endSigIdx);
252252
}
253253

254-
private void collectInterfaceMethods(ClassDescriptor clazz, HashSet<String> methodOverrides, List<MethodDescriptor> result) {
255-
if (!clazz.isInterface()) {
254+
private void collectAbstractMethods(final ClassDescriptor clazz, List<MethodDescriptor> result) {
255+
if (!clazz.isAbstract()) {
256256
return;
257257
}
258-
Set<String> objectMethods = new HashSet<String>();
258+
Set<String> alreadyAddedMethods = new HashSet<String>();
259+
for (MethodDescriptor md: result) {
260+
String sig = getMethodSignature(md);
261+
alreadyAddedMethods.add(sig);
262+
}
263+
264+
Set<String> concreteMethods = new HashSet<String>();
259265
// TODO refactor this
260-
ClassDescriptor objCD = new ClassInfo(Object.class);
261-
for (MethodDescriptor objMethod: objCD.getDeclaredMethods())
262-
{
263-
if (!objMethod.isStatic())
264-
{
266+
ClassDescriptor startingConcreteClassDesc = clazz.isInterface() ? new ClassInfo(Object.class) : clazz;
267+
for (MethodDescriptor objMethod: startingConcreteClassDesc.getDeclaredMethods()) {
268+
if (!objMethod.isStatic()) {
265269
String sig = getMethodSignature(objMethod);
266-
objectMethods.add(sig);
270+
concreteMethods.add(sig);
267271
}
268272
}
269273

270-
Deque<ClassDescriptor> extendedInterfaces = new ArrayDeque<ClassDescriptor>();
274+
Deque<ClassDescriptor> typesToProcess = new ArrayDeque<ClassDescriptor>();
275+
typesToProcess.add(startingConcreteClassDesc);
271276
if (clazz.isInterface()) {
272-
extendedInterfaces.add(clazz);
273-
} else {
274-
extendedInterfaces.addAll(Arrays.asList(clazz.getInterfaces()));
277+
typesToProcess.add(clazz);
275278
}
276279

277-
Set<MethodDescriptor> notImplementedObjectMethods = new HashSet<MethodDescriptor>();
278-
279-
while (!extendedInterfaces.isEmpty()) {
280-
ClassDescriptor currentInterface = extendedInterfaces.pollFirst();
281-
MethodDescriptor[] ifaceMethods = currentInterface.getDeclaredMethods();
282-
for (MethodDescriptor ifaceMethod: ifaceMethods)
283-
{
284-
if (!ifaceMethod.isStatic())
285-
{
286-
String sig = getMethodSignature(ifaceMethod);
287-
if (objectMethods.contains(sig) && !methodOverrides.contains(ifaceMethod.getName()))
288-
{
289-
notImplementedObjectMethods.add(ifaceMethod);
280+
while (!typesToProcess.isEmpty()) {
281+
ClassDescriptor currentType = typesToProcess.pollFirst();
282+
MethodDescriptor[] methods = currentType.getDeclaredMethods();
283+
for (MethodDescriptor m: methods) {
284+
if (m.isStatic()) {
285+
continue;
286+
}
287+
String sig = getMethodSignature(m);
288+
if (m.isAbstract()) {
289+
if (!concreteMethods.contains(sig) && !alreadyAddedMethods.contains(sig)) {
290+
result.add(m);
291+
alreadyAddedMethods.add(sig);
290292
}
293+
} else if (!concreteMethods.contains(sig)) {
294+
concreteMethods.add(sig);
291295
}
292296
}
293297

294-
for (MethodDescriptor ifaceMethod: ifaceMethods)
295-
{
296-
if (!notImplementedObjectMethods.contains(ifaceMethod))
297-
{
298-
result.add(ifaceMethod);
299-
}
298+
if (!currentType.isInterface() && !currentType.getName().equals("java.lang.Object")) {
299+
typesToProcess.addFirst(currentType.getSuperclass());
300300
}
301301

302-
for (ClassDescriptor iface: currentInterface.getInterfaces()) {
303-
extendedInterfaces.add(iface);
302+
for (ClassDescriptor iface: currentType.getInterfaces()) {
303+
typesToProcess.add(iface);
304304
}
305305
}
306306
}
@@ -309,11 +309,10 @@ private MethodDescriptor[] getSupportedMethods(ClassDescriptor clazz, HashSet<St
309309
{
310310
ArrayList<MethodDescriptor> result = new ArrayList<MethodDescriptor>();
311311

312-
collectInterfaceMethods(clazz, methodOverrides, result);
312+
collectAbstractMethods(clazz, result);
313313

314-
for (ClassDescriptor iface : interfacesToImplement)
315-
{
316-
collectInterfaceMethods(iface, methodOverrides, result);
314+
for (ClassDescriptor iface : interfacesToImplement) {
315+
collectAbstractMethods(iface, result);
317316
}
318317

319318
if (!clazz.isInterface())

binding-generator/src/main/java/com/tns/bindings/desc/ClassDescriptor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public boolean isFinal() {
8888
return false;
8989
}
9090

91+
@Override
92+
public boolean isAbstract() {
93+
return false;
94+
}
95+
9196
public static String getBoxedTypeName(ClassDescriptor c) {
9297
String name = null;
9398
if (c.isPrimitive()) {

binding-generator/src/main/java/com/tns/bindings/desc/Descriptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ public interface Descriptor {
1212
boolean isProtected();
1313

1414
boolean isFinal();
15+
16+
boolean isAbstract();
1517
}

binding-generator/src/main/java/com/tns/bindings/desc/reflection/ClassInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,9 @@ public boolean isProtected() {
140140
public boolean isFinal() {
141141
return Modifier.isFinal(clazz.getModifiers());
142142
}
143+
144+
@Override
145+
public boolean isAbstract() {
146+
return Modifier.isAbstract(clazz.getModifiers());
147+
}
143148
}

binding-generator/src/main/java/com/tns/bindings/desc/reflection/MethodInfo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class MethodInfo implements MethodDescriptor {
1414
private final boolean m_isPublic;
1515
private final boolean m_isProtected;
1616
private final boolean m_isFinal;
17+
private final boolean m_isAbstract;
1718
private Class<?>[] m_parameterTypes;
1819
private Class<?> m_retType;
1920
private String m_name;
@@ -43,6 +44,7 @@ private MethodInfo(int modifiers) {
4344
m_isPublic = Modifier.isPublic(modifiers);
4445
m_isProtected = Modifier.isProtected(modifiers);
4546
m_isFinal = Modifier.isFinal(modifiers);
47+
m_isAbstract = Modifier.isAbstract(modifiers);
4648
}
4749

4850
@Override
@@ -98,4 +100,9 @@ public boolean isProtected() {
98100
public boolean isFinal() {
99101
return m_isFinal;
100102
}
103+
104+
@Override
105+
public boolean isAbstract() {
106+
return m_isAbstract;
107+
}
101108
}

0 commit comments

Comments
 (0)