1616
1717package io .appium .java_client .pagefactory ;
1818
19+ import static java .lang .Integer .signum ;
1920import static java .util .Arrays .asList ;
20- import static java .util .Arrays .sort ;
2121import static java .util .Optional .ofNullable ;
22- import static org .apache .commons .lang3 .ArrayUtils .add ;
2322
2423import io .appium .java_client .pagefactory .bys .ContentMappedBy ;
2524import io .appium .java_client .pagefactory .bys .ContentType ;
4039import java .lang .reflect .InvocationTargetException ;
4140import java .lang .reflect .Method ;
4241import java .util .ArrayList ;
42+ import java .util .Arrays ;
4343import java .util .Comparator ;
4444import java .util .HashMap ;
4545import java .util .List ;
4646import java .util .Map ;
47+ import java .util .Optional ;
4748
4849public class DefaultElementByBuilder extends AppiumByBuilder {
4950
5051 private static final String PRIORITY = "priority" ;
5152 private static final String VALUE = "value" ;
52- private static final Class [] ANNOTATION_ARGUMENTS = new Class [] {};
53- private static final Object [] ANNOTATION_PARAMETERS = new Object [] {};
53+ private static final Class [] ANNOTATION_ARGUMENTS = new Class []{};
54+ private static final Object [] ANNOTATION_PARAMETERS = new Object []{};
5455
5556 public DefaultElementByBuilder (String platform , String automation ) {
5657 super (platform , automation );
5758 }
5859
5960 private static void checkDisallowedAnnotationPairs (Annotation a1 , Annotation a2 )
60- throws IllegalArgumentException {
61+ throws IllegalArgumentException {
6162 if (a1 != null && a2 != null ) {
6263 throw new IllegalArgumentException (
63- "If you use a '@" + a1 .getClass ().getSimpleName () + "' annotation, "
64- + "you must not also use a '@" + a2 .getClass ().getSimpleName ()
65- + "' annotation" );
64+ "If you use a '@" + a1 .getClass ().getSimpleName () + "' annotation, "
65+ + "you must not also use a '@" + a2 .getClass ().getSimpleName ()
66+ + "' annotation" );
6667 }
6768 }
6869
@@ -78,7 +79,8 @@ private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, By[]
7879 return new ByChained (bys );
7980 }
8081
81- @ Override protected void assertValidAnnotations () {
82+ @ Override
83+ protected void assertValidAnnotations () {
8284 AnnotatedElement annotatedElement = annotatedElementContainer .getAnnotated ();
8385 FindBy findBy = annotatedElement .getAnnotation (FindBy .class );
8486 FindBys findBys = annotatedElement .getAnnotation (FindBys .class );
@@ -88,7 +90,8 @@ private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, By[]
8890 checkDisallowedAnnotationPairs (findBys , findAll );
8991 }
9092
91- @ Override protected By buildDefaultBy () {
93+ @ Override
94+ protected By buildDefaultBy () {
9295 AnnotatedElement annotatedElement = annotatedElementContainer .getAnnotated ();
9396 By defaultBy = null ;
9497 FindBy findBy = annotatedElement .getAnnotation (FindBy .class );
@@ -117,79 +120,83 @@ private By[] getBys(Class<? extends Annotation> singleLocator, Class<? extends A
117120 AnnotationComparator comparator = new AnnotationComparator ();
118121 AnnotatedElement annotatedElement = annotatedElementContainer .getAnnotated ();
119122
120- List <Annotation > annotations = new ArrayList <>(asList (annotatedElement .getAnnotationsByType (singleLocator )));
123+ List <Annotation > annotations = new ArrayList <>(asList (annotatedElement .getAnnotationsByType (singleLocator )));
121124 annotations .addAll (asList (annotatedElement .getAnnotationsByType (chainedLocator )));
122125 annotations .addAll (asList (annotatedElement .getAnnotationsByType (allLocator )));
123126
124- Annotation [] annotationsArray = annotations .toArray (new Annotation []{});
125- sort (annotationsArray , comparator );
126- By [] result = new By [] {};
127+ annotations .sort (comparator );
128+ List <By > result = new ArrayList <>();
127129
128- for (Annotation a : annotationsArray ) {
130+ for (Annotation a : annotations ) {
129131 Class <?> annotationClass = a .annotationType ();
130132 if (singleLocator .equals (annotationClass )) {
131- result = add (result , createBy (new Annotation [] {a }, HowToUseSelectors .USE_ONE ));
133+ result . add (createBy (new Annotation []{a }, HowToUseSelectors .USE_ONE ));
132134 continue ;
133135 }
134136
135137 Method value ;
136- Annotation [] set ;
138+ Annotation [] subLocators ;
137139 try {
138140 value = annotationClass .getMethod (VALUE , ANNOTATION_ARGUMENTS );
139- set = (Annotation []) value .invoke (a , ANNOTATION_PARAMETERS );
141+ subLocators = (Annotation []) value .invoke (a , ANNOTATION_PARAMETERS );
140142 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
141143 throw new ClassCastException (String .format ("The annotation '%s' has no convenient '%s' method which "
142144 + "returns array of annotations" , annotationClass .getName (), VALUE ));
143145 }
144146
145- sort (set , comparator );
147+ Arrays . sort (subLocators , comparator );
146148 if (chainedLocator .equals (annotationClass )) {
147- result = add (result , createBy (set , HowToUseSelectors .BUILD_CHAINED ));
149+ result . add (createBy (subLocators , HowToUseSelectors .BUILD_CHAINED ));
148150 continue ;
149151 }
150152
151153 if (allLocator .equals (annotationClass )) {
152- result = add (result , createBy (set , HowToUseSelectors .USE_ANY ));
154+ result . add (createBy (subLocators , HowToUseSelectors .USE_ANY ));
153155 }
154156 }
155157
156- return result ;
158+ return result . toArray ( new By [ result . size ()]) ;
157159 }
158160
159- @ Override protected By buildMobileNativeBy () {
161+ @ Override
162+ protected By buildMobileNativeBy () {
160163 AnnotatedElement annotatedElement = annotatedElementContainer .getAnnotated ();
161164 HowToUseLocators howToUseLocators = annotatedElement .getAnnotation (HowToUseLocators .class );
162165
166+ Optional <HowToUseLocators > howToUseLocatorsOptional = ofNullable (howToUseLocators );
167+
163168 By result = null ;
164169 if (isSelendroidAutomation ()) {
165- result = buildMobileBy (howToUseLocators != null ? howToUseLocators .selendroidAutomation () : null ,
170+ result = buildMobileBy (howToUseLocatorsOptional
171+ .map (HowToUseLocators ::selendroidAutomation ).orElse (null ),
166172 getBys (SelendroidFindBy .class , SelendroidFindBys .class , SelendroidFindAll .class ));
167173 }
168174
169- if (isAndroid () && result == null ) {
170- result = buildMobileBy (howToUseLocators != null ? howToUseLocators . androidAutomation () : null ,
175+ if (isAndroid () && result == null ) {
176+ return buildMobileBy (howToUseLocatorsOptional . map ( HowToUseLocators :: androidAutomation ). orElse ( null ) ,
171177 getBys (AndroidFindBy .class , AndroidFindBys .class , AndroidFindAll .class ));
172178 }
173179
174- if (isIOSXcuit () && result == null ) {
175- result = buildMobileBy (howToUseLocators != null ? howToUseLocators . iOSXCUITAutomation () : null ,
180+ if (isIOSXcuit ()) {
181+ result = buildMobileBy (howToUseLocatorsOptional . map ( HowToUseLocators :: iOSXCUITAutomation ). orElse ( null ) ,
176182 getBys (iOSXCUITFindBy .class , iOSXCUITFindBys .class , iOSXCUITFindAll .class ));
177183 }
178184
179185 if (isIOS () && result == null ) {
180- result = buildMobileBy (howToUseLocators != null ? howToUseLocators . iOSAutomation () : null ,
186+ return buildMobileBy (howToUseLocatorsOptional . map ( HowToUseLocators :: iOSAutomation ). orElse ( null ) ,
181187 getBys (iOSFindBy .class , iOSFindBys .class , iOSFindAll .class ));
182188 }
183189
184- if (isWindows () && result == null ) {
185- result = buildMobileBy (howToUseLocators != null ? howToUseLocators . windowsAutomation () : null ,
190+ if (isWindows ()) {
191+ return buildMobileBy (howToUseLocatorsOptional . map ( HowToUseLocators :: windowsAutomation ). orElse ( null ) ,
186192 getBys (WindowsFindBy .class , WindowsFindBys .class , WindowsFindAll .class ));
187193 }
188194
189195 return ofNullable (result ).orElse (null );
190196 }
191197
192- @ Override public boolean isLookupCached () {
198+ @ Override
199+ public boolean isLookupCached () {
193200 AnnotatedElement annotatedElement = annotatedElementContainer .getAnnotated ();
194201 return (annotatedElement .getAnnotation (CacheLookup .class ) != null );
195202 }
@@ -201,7 +208,8 @@ private By returnMappedBy(By byDefault, By nativeAppBy) {
201208 return new ContentMappedBy (contentMap );
202209 }
203210
204- @ Override public By buildBy () {
211+ @ Override
212+ public By buildBy () {
205213 assertValidAnnotations ();
206214
207215 By defaultBy = buildDefaultBy ();
@@ -211,14 +219,14 @@ private By returnMappedBy(By byDefault, By nativeAppBy) {
211219
212220 if (defaultBy == null && mobileNativeBy == null ) {
213221 defaultBy =
214- new ByIdOrName (((Field ) annotatedElementContainer .getAnnotated ()).getName ());
222+ new ByIdOrName (((Field ) annotatedElementContainer .getAnnotated ()).getName ());
215223 mobileNativeBy = new By .ById (idOrName );
216224 return returnMappedBy (defaultBy , mobileNativeBy );
217225 }
218226
219227 if (defaultBy == null ) {
220228 defaultBy =
221- new ByIdOrName (((Field ) annotatedElementContainer .getAnnotated ()).getName ());
229+ new ByIdOrName (((Field ) annotatedElementContainer .getAnnotated ()).getName ());
222230 return returnMappedBy (defaultBy , mobileNativeBy );
223231 }
224232
@@ -261,13 +269,7 @@ public int compare(Annotation o1, Annotation o2) {
261269 int p1 = getPriorityValue (priority1 , o1 , c1 );
262270 int p2 = getPriorityValue (priority2 , o2 , c2 );
263271
264- if (p2 > p1 ) {
265- return -1 ;
266- } else if (p2 < p1 ) {
267- return 1 ;
268- } else {
269- return 0 ;
270- }
272+ return signum (p1 - p2 );
271273 }
272274 }
273275}
0 commit comments