Skip to content

Commit

Permalink
Merge pull request testng-team#967 from ronald-d-rogers/master
Browse files Browse the repository at this point in the history
Preliminary support for default methods.
  • Loading branch information
cbeust committed Jan 27, 2016
2 parents f43c319 + d5b4296 commit a94277a
Showing 1 changed file with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.testng.ITestNGMethod;
Expand Down Expand Up @@ -217,7 +219,7 @@ public static ITestNGMethod[] findMethodsWithAnnotation(Class<?> rootClass,
// for (Class<?> cls : classes) {
while (null != cls) {
boolean hasClassAnnotation = isAnnotationPresent(annotationFinder, cls, annotationClass);
Method[] methods = cls.getDeclaredMethods();
Method[] methods = getLocalMethods(cls);
for (Method m : methods) {
boolean hasMethodAnnotation = isAnnotationPresent(annotationFinder, m, annotationClass);
boolean hasTestNGAnnotation =
Expand Down Expand Up @@ -317,5 +319,41 @@ private static String createMethodKey(Method m) {

return result.toString();
}

/**
* @return An array of all locally declared methods or equivalent thereof
* (such as default methods on Java 8 based interfaces that the given class
* implements).
*/
private static Method[] getLocalMethods(Class<?> clazz) {
Method[] result;
Method[] declaredMethods = clazz.getDeclaredMethods();
List<Method> defaultMethods = getDefaultMethods(clazz);
if (!defaultMethods.isEmpty()) {
result = new Method[declaredMethods.length + defaultMethods.size()];
System.arraycopy(declaredMethods, 0, result, 0, declaredMethods.length);
int index = declaredMethods.length;
for (Method defaultMethod : defaultMethods) {
result[index] = defaultMethod;
index++;
}
}
else {
result = declaredMethods;
}
return result;
}

private static List<Method> getDefaultMethods(Class<?> clazz) {
List<Method> result = new LinkedList<Method>();
for (Class<?> ifc : clazz.getInterfaces()) {
for (Method ifcMethod : ifc.getMethods()) {
if (!Modifier.isAbstract(ifcMethod.getModifiers())) {
result.add(ifcMethod);
}
}
}
return result;
}

}

0 comments on commit a94277a

Please sign in to comment.