private static List<String> createQualifiedMethodNames(XmlClass xmlClass, List<String> methods) { List<String> vResult = Lists.newArrayList(); Class<?> cls = xmlClass.getSupportClass(); while (cls != null) { for (String im : methods) { Pattern pattern = Pattern.compile(methodName(im)); Method[] allMethods = ReflectionHelper.getLocalMethods(cls); for (Method m : allMethods) { if (pattern.matcher(m.getName()).matches()) { vResult.add(makeMethodName(m.getDeclaringClass().getName(), m.getName())); } } } cls = cls.getSuperclass(); } return vResult; }
/** * @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) but excludes the <code>main()</code> method alone. */ public static Method[] excludingMain(Class<?> clazz) { Method[] declaredMethods = clazz.getDeclaredMethods(); List<Method> pruned = new LinkedList<>(); for (Method declaredMethod :declaredMethods) { if ("main".equals(declaredMethod.getName()) && isStaticVoid(declaredMethod) && acceptsStringArray(declaredMethod)) { continue; } pruned.add(declaredMethod); } return pruned.toArray(new Method[pruned.size()]); }
/** * @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). */ public static Method[] getLocalMethods(Class<?> clazz) { Method[] result; Method[] declaredMethods = excludingMain(clazz); List<Method> defaultMethods = getDefaultMethods(clazz); if (defaultMethods != null) { 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 { List<Method> prunedMethods = Lists.newArrayList(); for (Method declaredMethod : declaredMethods) { if (!declaredMethod.isBridge()) { prunedMethods.add(declaredMethod); } } result = prunedMethods.toArray(new Method[prunedMethods.size()]); } return result; }
@Override public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { if (!annotation.getEnabled()) { return; } Class<?> typedTestClass = testClass; if (testMethod != null) { ignoreTest(annotation, testMethod.getAnnotation(Ignore.class)); typedTestClass = testMethod.getDeclaringClass(); } if (typedTestClass != null) { ignoreTest(annotation, ReflectionHelper.findAnnotation(typedTestClass, Ignore.class)); Package testPackage = typedTestClass.getPackage(); if (testPackage != null) { ignoreTest(annotation, findAnnotation(testPackage)); } } }
private ITestNGMethod[] privateFindTestMethods(INameFilter filter, Class cls) { List<ITestNGMethod> vResult = Lists.newArrayList(); // We do not want to walk up the class hierarchy and accept the // same method twice (e.g. setUp) which would lead to double-invocation. // All relevant JUnit methods are parameter-less so we store accepted // method names in a Set to filter out duplicates. Set<String> acceptedMethodNames = new HashSet<>(); // // Collect all methods that start with test // Class current = cls; while(!(current == Object.class)) { Method[] allMethods = ReflectionHelper.excludingMain(current); for(Method allMethod : allMethods) { ITestNGMethod m = new TestNGMethod(/* allMethods[i].getDeclaringClass(), */ allMethod, m_annotationFinder, null, null); /* @@@ */ ConstructorOrMethod method = m.getConstructorOrMethod(); String methodName = method.getName(); if(filter.accept(method) && !acceptedMethodNames.contains(methodName)) { // if (m.getName().startsWith("test")) { // ppp("Found JUnit test method: " + tm); vResult.add(m); acceptedMethodNames.add(methodName); } } current = current.getSuperclass(); } return vResult.toArray(new ITestNGMethod[vResult.size()]); }
private static void ignoreTestAtClass(Class<?> clazz, ITestAnnotation annotation) { if (clazz != null) { ignoreTest(annotation, ReflectionHelper.findAnnotation(clazz, Ignore.class)); Package testPackage = clazz.getPackage(); if (testPackage != null) { ignoreTest(annotation, findAnnotation(testPackage)); } } }
Method[] allMethods = ReflectionHelper.excludingMain(current); for (Method allMethod : allMethods) { ITestNGMethod m =
/** * Extract all callable methods of a class and all its super (keeping in mind * the Java access rules). */ public static Set<Method> getAvailableMethods(Class<?> clazz) { Map<String, Set<Method>> methods = Maps.newHashMap(); for (final Method declaredMethod : ReflectionHelper.getLocalMethods(clazz)) { appendMethod(methods, declaredMethod); } Class<?> parent = clazz.getSuperclass(); if (null != parent) { while (!Object.class.equals(parent)) { Set<Map.Entry<String, Set<Method>>> extractedMethods = extractMethods(clazz, parent, methods).entrySet(); for (Map.Entry<String, Set<Method>> extractedMethod : extractedMethods) { Set<Method> m = methods.get(extractedMethod.getKey()); if (m == null) { methods.put(extractedMethod.getKey(), extractedMethod.getValue()); } else { m.addAll(extractedMethod.getValue()); } } parent = parent.getSuperclass(); } } Set<Method> returnValue = Sets.newHashSet(); for (Set<Method> each : methods.values()) { returnValue.addAll(each); } return returnValue; }
/** * @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) but excludes the <code>main() * </code> method alone. */ public static Method[] excludingMain(Class<?> clazz) { Method[] declaredMethods = clazz.getDeclaredMethods(); List<Method> pruned = new LinkedList<>(); for (Method declaredMethod : declaredMethods) { if ("main".equals(declaredMethod.getName()) && isStaticVoid(declaredMethod) && acceptsStringArray(declaredMethod)) { continue; } pruned.add(declaredMethod); } return pruned.toArray(new Method[0]); }
/** * @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). */ public static Method[] getLocalMethods(Class<?> clazz) { Method[] declaredMethods = excludingMain(clazz); List<Method> defaultMethods = getDefaultMethods(clazz); if (defaultMethods == null) { List<Method> prunedMethods = Lists.newArrayList(); for (Method declaredMethod : declaredMethods) { if (!declaredMethod.isBridge()) { prunedMethods.add(declaredMethod); } } return prunedMethods.toArray(new Method[0]); } Method[] 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++; } return result; }
private static List<String> createQualifiedMethodNames(XmlClass xmlClass, List<String> methods) { List<String> vResult = Lists.newArrayList(); Class<?> cls = xmlClass.getSupportClass(); while (cls != null) { for (String im : methods) { Pattern pattern = Pattern.compile(methodName(im)); Method[] allMethods = ReflectionHelper.getLocalMethods(cls); for (Method m : allMethods) { if (pattern.matcher(m.getName()).matches()) { vResult.add(makeMethodName(m.getDeclaringClass().getName(), m.getName())); } } } cls = cls.getSuperclass(); } return vResult; }
for (final Method declaredMethod : ReflectionHelper.getLocalMethods(clazz)) { appendMethod(methods, declaredMethod);
public FakeTestNGMethod(Class<?> clazz, String methodName, XmlTest xmlTest) { ConstructorOrMethod temp = null; Method[] methods = ReflectionHelper.getLocalMethods(clazz); for (Method method : methods) { if (method.getName().equalsIgnoreCase(methodName)) { temp = new ConstructorOrMethod(method); break; } } this.xmlTest = xmlTest; this.clazz = clazz; this.consMethod = temp; }
@Test public void testMethodCount() { //Testing exclusion of synthetic methods Refer http://stackoverflow.com/a/5007394 to learn more Method[] methods = prune(ReflectionHelper.getLocalMethods(DuplicateCallsSample.class)); Assert.assertEquals(methods.length, 2); //Testing a straight forward use case of retrieving concrete methods methods = prune(ReflectionHelper.getLocalMethods(Dog.class)); Assert.assertEquals(methods.length, 1); //When class has no methods count should be zero. methods = prune(ReflectionHelper.getLocalMethods(Dinosaur.class)); Assert.assertEquals(methods.length, 0); //Abstract methods should be included. methods = prune(ReflectionHelper.getLocalMethods(Dragon.class)); Assert.assertEquals(methods.length, 2); //main methods should be pruned methods = prune(ReflectionHelper.getLocalMethods(TestClassSample.class)); Assert.assertEquals(methods.length, 1); }
Method[] methods = ReflectionHelper.getLocalMethods(cls); for (Method m : methods) { boolean hasMethodAnnotation = isAnnotationPresent(annotationFinder, m, annotationClass);
Method[] methods = ReflectionHelper.getLocalMethods(cls); for (Method m : methods) { boolean hasMethodAnnotation = isAnnotationPresent(annotationFinder, m, annotationClass);