Skip to content

Commit 6c85323

Browse files
#565: Annotation comparator.
1 parent d447014 commit 6c85323

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

src/main/java/io/appium/java_client/pagefactory/DefaultElementByBuilder.java

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,20 @@
3030
import java.lang.annotation.Annotation;
3131
import java.lang.reflect.AnnotatedElement;
3232
import java.lang.reflect.Field;
33+
import java.lang.reflect.InvocationTargetException;
34+
import java.lang.reflect.Method;
35+
import java.util.Comparator;
3336
import java.util.HashMap;
3437
import java.util.Map;
3538
import java.util.Optional;
3639

3740
public class DefaultElementByBuilder extends AppiumByBuilder {
3841

42+
private static final String PRIORITY = "priority";
43+
private static final String VALUE = "value";
44+
private static final Class[] ANNOTATION_ARGUMENTS = new Class[] {};
45+
private static final Object[] ANNOTATION_PARAMETERS = new Object[] {};
46+
3947
public DefaultElementByBuilder(String platform, String automation) {
4048
super(platform, automation);
4149
}
@@ -65,28 +73,6 @@ private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, Annot
6573

6674
@Override protected void assertValidAnnotations() {
6775
AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
68-
AndroidFindBy androidBy = annotatedElement.getAnnotation(AndroidFindBy.class);
69-
AndroidFindBys androidBys = annotatedElement.getAnnotation(AndroidFindBys.class);
70-
checkDisallowedAnnotationPairs(androidBy, androidBys);
71-
AndroidFindAll androidFindAll = annotatedElement.getAnnotation(AndroidFindAll.class);
72-
checkDisallowedAnnotationPairs(androidBy, androidFindAll);
73-
checkDisallowedAnnotationPairs(androidBys, androidFindAll);
74-
75-
SelendroidFindBy selendroidBy = annotatedElement.getAnnotation(SelendroidFindBy.class);
76-
SelendroidFindBys selendroidBys = annotatedElement.getAnnotation(SelendroidFindBys.class);
77-
checkDisallowedAnnotationPairs(selendroidBy, selendroidBys);
78-
SelendroidFindAll selendroidFindAll =
79-
annotatedElement.getAnnotation(SelendroidFindAll.class);
80-
checkDisallowedAnnotationPairs(selendroidBy, selendroidFindAll);
81-
checkDisallowedAnnotationPairs(selendroidBys, selendroidFindAll);
82-
83-
iOSFindBy iOSBy = annotatedElement.getAnnotation(iOSFindBy.class);
84-
iOSFindBys iOSBys = annotatedElement.getAnnotation(iOSFindBys.class);
85-
checkDisallowedAnnotationPairs(iOSBy, iOSBys);
86-
iOSFindAll iOSFindAll = annotatedElement.getAnnotation(iOSFindAll.class);
87-
checkDisallowedAnnotationPairs(iOSBy, iOSFindAll);
88-
checkDisallowedAnnotationPairs(iOSBys, iOSFindAll);
89-
9076
FindBy findBy = annotatedElement.getAnnotation(FindBy.class);
9177
FindBys findBys = annotatedElement.getAnnotation(FindBys.class);
9278
checkDisallowedAnnotationPairs(findBy, findBys);
@@ -260,4 +246,45 @@ private By returnMappedBy(By byDefault, By nativeAppBy) {
260246

261247
return returnMappedBy(defaultBy, mobileNativeBy);
262248
}
249+
250+
private static class AnnotationComparator implements Comparator<Annotation> {
251+
252+
@Override
253+
public int compare(Annotation o1, Annotation o2) {
254+
int priority1;
255+
int priority2;
256+
Method priority;
257+
258+
Class<? extends Annotation> c1 = o1.getClass();
259+
Class<? extends Annotation> c2 = o2.getClass();
260+
261+
if (!c1.equals(c2)) {
262+
throw new ClassCastException(String.format("Given annotations have different classes (%s, %s). " +
263+
"Annotations of the same classes are required.", c1.getName(), c2.getName()));
264+
}
265+
266+
try {
267+
priority = c1.getMethod(PRIORITY, ANNOTATION_ARGUMENTS);
268+
} catch (NoSuchMethodException e) {
269+
throw new ClassCastException(String.format("Class %s has no '%s' method", c1.getName(), PRIORITY));
270+
}
271+
272+
try {
273+
priority1 = (int) priority.invoke(o1, ANNOTATION_PARAMETERS);
274+
priority2 = (int) priority.invoke(o2, ANNOTATION_PARAMETERS);
275+
276+
if (priority2 > priority1) {
277+
return -1;
278+
}
279+
else if (priority2 < priority1){
280+
return 1;
281+
}
282+
else {
283+
return 0;
284+
}
285+
} catch (IllegalAccessException|InvocationTargetException e) {
286+
throw new RuntimeException(e);
287+
}
288+
}
289+
}
263290
}

0 commit comments

Comments
 (0)