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; }
/** * 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; }
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);