Source:
private By[] getBys(Class<? extends Annotation> singleLocator, Class<? extends Annotation> chainedLocator, Class<? extends Annotation> allLocator) { AnnotationComparator comparator = new AnnotationComparator(); AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated(); List<Annotation> annotations = new ArrayList<>(asList(annotatedElement.getAnnotationsByType(singleLocator))); annotations.addAll(asList(annotatedElement.getAnnotationsByType(chainedLocator))); annotations.addAll(asList(annotatedElement.getAnnotationsByType(allLocator))); Annotation[] annotationsArray = annotations.toArray(new Annotation[]{}); sort(annotationsArray, comparator); By[] result = new By[] {}; for (Annotation a: annotationsArray) { Class<?> annotationClass = a.annotationType(); if (singleLocator.equals(annotationClass)) { result = add(result, createBy(new Annotation[] {a}, HowToUseSelectors.USE_ONE)); continue; } Method value; Annotation[] set; try { value = annotationClass.getMethod(VALUE, ANNOTATION_ARGUMENTS); set = (Annotation[]) value.invoke(a, ANNOTATION_PARAMETERS); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new ClassCastException(String.format("The annotation '%s' has no convenient '%s' method which " + "returns array of annotations", annotationClass.getName(), VALUE)); } sort(set, comparator); if (chainedLocator.equals(annotationClass)) { result = add(result, createBy(set, HowToUseSelectors.BUILD_CHAINED)); continue; } if (allLocator.equals(annotationClass)) { result = add(result, createBy(set, HowToUseSelectors.USE_ANY)); } } return result; }

Target:
private By[] getBys(Class<? extends Annotation> singleLocator, Class<? extends Annotation> chainedLocator, Class<? extends Annotation> allLocator) { AnnotationComparator comparator = new AnnotationComparator(); AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated(); List<Annotation> annotations = new ArrayList<>(asList(annotatedElement.getAnnotationsByType(singleLocator))); annotations.addAll(asList(annotatedElement.getAnnotationsByType(chainedLocator))); annotations.addAll(asList(annotatedElement.getAnnotationsByType(allLocator))); annotations.sort(comparator); List<By> result = new ArrayList<>(); for (Annotation a : annotations) { Class<?> annotationClass = a.annotationType(); if (singleLocator.equals(annotationClass)) { result.add(createBy(new Annotation[]{a}, HowToUseSelectors.USE_ONE)); continue; } Method value; Annotation[] subLocators; try { value = annotationClass.getMethod(VALUE, ANNOTATION_ARGUMENTS); subLocators = (Annotation[]) value.invoke(a, ANNOTATION_PARAMETERS); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new ClassCastException(String.format("The annotation '%s' has no convenient '%s' method which " + "returns array of annotations", annotationClass.getName(), VALUE)); } Arrays.sort(subLocators, comparator); if (chainedLocator.equals(annotationClass)) { result.add(createBy(subLocators, HowToUseSelectors.BUILD_CHAINED)); continue; } if (allLocator.equals(annotationClass)) { result.add(createBy(subLocators, HowToUseSelectors.USE_ANY)); } } return result.toArray(new By[result.size()]); }