2525import org .openqa .selenium .By ;
2626import org .openqa .selenium .support .pagefactory .AbstractAnnotations ;
2727
28+ import javax .annotation .Nullable ;
2829import java .lang .annotation .Annotation ;
2930import java .lang .reflect .AnnotatedElement ;
3031import java .lang .reflect .Constructor ;
3334import java .lang .reflect .Proxy ;
3435import java .util .ArrayList ;
3536import java .util .List ;
37+ import java .util .stream .Collectors ;
3638import java .util .stream .Stream ;
3739
3840/**
4244 * - https://code.google.com/p/selenium/wiki/PageFactory
4345 */
4446public abstract class AppiumByBuilder extends AbstractAnnotations {
45- protected static final Class <?>[] DEFAULT_ANNOTATION_METHOD_ARGUMENTS = new Class <?>[] {};
47+ protected static final Class <?>[] DEFAULT_ANNOTATION_METHOD_ARGUMENTS = new Class <?>[]{};
4648
47- private static final List <String > METHODS_TO_BE_EXCLUDED_WHEN_ANNOTATION_IS_READ =
48- new ArrayList <String >() {
49- private static final long serialVersionUID = 1L ; {
50- Stream .of (Object .class , Annotation .class , Proxy .class )
49+ private static final List <String > METHODS_TO_BE_EXCLUDED_WHEN_ANNOTATION_IS_READ = new ArrayList <String >() {
50+ private static final long serialVersionUID = 1L ;
51+
52+ {
53+ Stream .of (Object .class , Annotation .class , Proxy .class )
5154 .map (Class ::getDeclaredMethods )
5255 .map (AppiumByBuilder ::getMethodNames )
5356 .flatMap (List ::stream )
5457 .forEach (this ::add );
55- }
56- };
58+ }
59+ };
5760 protected final AnnotatedElementContainer annotatedElementContainer ;
5861 protected final String platform ;
5962 protected final String automation ;
@@ -65,79 +68,68 @@ protected AppiumByBuilder(String platform, String automation) {
6568 }
6669
6770 private static List <String > getMethodNames (Method [] methods ) {
68- List <String > names = new ArrayList <>();
69- for (Method m : methods ) {
70- names .add (m .getName ());
71- }
72- return names ;
71+ return Stream .of (methods ).map (Method ::getName ).collect (Collectors .toList ());
7372 }
7473
7574 private static Method [] prepareAnnotationMethods (Class <? extends Annotation > annotation ) {
76- List <String > targetAnnotationMethodNamesList =
77- getMethodNames (annotation .getDeclaredMethods ());
75+ List <String > targetAnnotationMethodNamesList = getMethodNames (annotation .getDeclaredMethods ());
7876 targetAnnotationMethodNamesList .removeAll (METHODS_TO_BE_EXCLUDED_WHEN_ANNOTATION_IS_READ );
79- Method [] result = new Method [targetAnnotationMethodNamesList .size ()];
80- for (String methodName : targetAnnotationMethodNamesList ) {
81- try {
82- result [targetAnnotationMethodNamesList .indexOf (methodName )] =
83- annotation .getMethod (methodName , DEFAULT_ANNOTATION_METHOD_ARGUMENTS );
84- } catch (NoSuchMethodException | SecurityException e ) {
85- throw new RuntimeException (e );
86- }
87- }
88- return result ;
77+ return targetAnnotationMethodNamesList .stream ()
78+ .map ((methodName ) -> {
79+ try {
80+ return annotation .getMethod (methodName , DEFAULT_ANNOTATION_METHOD_ARGUMENTS );
81+ } catch (NoSuchMethodException | SecurityException e ) {
82+ throw new RuntimeException (e );
83+ }
84+ }).toArray (Method []::new );
8985 }
9086
9187 private static String getFilledValue (Annotation mobileBy ) {
92- Method [] values = prepareAnnotationMethods (mobileBy .getClass ());
93- for (Method value : values ) {
94- if (!String .class .equals (value .getReturnType ())) {
95- continue ;
96- }
97-
98- try {
99- String strategyParameter = value .invoke (mobileBy ).toString ();
100- if (!strategyParameter .isEmpty ()) {
101- return value .getName ();
102- }
103- } catch (IllegalAccessException
104- | IllegalArgumentException
105- | InvocationTargetException e ) {
106- throw new RuntimeException (e );
107- }
108- }
109- throw new IllegalArgumentException (
110- "@" + mobileBy .getClass ().getSimpleName () + ": one of " + Strategies .strategiesNames ()
111- .toString () + " should be filled" );
88+ return Stream .of (prepareAnnotationMethods (mobileBy .getClass ()))
89+ .filter ((method ) -> String .class == method .getReturnType ())
90+ .filter ((method ) -> {
91+ try {
92+ Object strategyParameter = method .invoke (mobileBy );
93+ return strategyParameter != null && !String .valueOf (strategyParameter ).isEmpty ();
94+ } catch (IllegalAccessException | IllegalArgumentException
95+ | InvocationTargetException e ) {
96+ throw new RuntimeException (e );
97+ }
98+ })
99+ .findFirst ()
100+ .map (Method ::getName )
101+ .orElseThrow (() -> new IllegalArgumentException (
102+ String .format ("@%s: one of %s should be filled" ,
103+ mobileBy .getClass ().getSimpleName (), Strategies .strategiesNames ())
104+ ));
112105 }
113106
114107 private static By getMobileBy (Annotation annotation , String valueName ) {
115- Strategies [] strategies = Strategies .values ();
116- for (Strategies strategy : strategies ) {
117- if (strategy .returnValueName ().equals (valueName )) {
118- return strategy .getBy (annotation );
119- }
120- }
121- throw new IllegalArgumentException (
122- "@" + annotation .getClass ().getSimpleName () + ": There is an unknown strategy "
123- + valueName );
108+ return Stream .of (Strategies .values ())
109+ .filter ((strategy ) -> strategy .returnValueName ().equals (valueName ))
110+ .findFirst ()
111+ .map ((strategy ) -> strategy .getBy (annotation ))
112+ .orElseThrow (() -> new IllegalArgumentException (
113+ String .format ("@%s: There is an unknown strategy %s" ,
114+ annotation .getClass ().getSimpleName (), valueName )
115+ ));
124116 }
125117
126- private static <T extends By > T getComplexMobileBy (Annotation [] annotations ,
127- Class <T > requiredByClass ) {
128- By [] byArray = new By [annotations .length ];
129- for (int i = 0 ; i < annotations .length ; i ++) {
130- byArray [i ] = getMobileBy (annotations [i ], getFilledValue (annotations [i ]));
131- }
118+ private static <T extends By > T getComplexMobileBy (Annotation [] annotations , Class <T > requiredByClass ) {
119+ By [] byArray = Stream .of (annotations )
120+ .map ((annotation ) -> getMobileBy (annotation , getFilledValue (annotation )))
121+ .toArray (By []::new );
132122 try {
133123 Constructor <T > c = requiredByClass .getConstructor (By [].class );
134- Object [] values = new Object [] {byArray };
124+ Object [] values = new Object []{byArray };
135125 return c .newInstance (values );
136- } catch (Exception e ) {
126+ } catch (InvocationTargetException | NoSuchMethodException | InstantiationException
127+ | IllegalAccessException e ) {
137128 throw new RuntimeException (e );
138129 }
139130 }
140131
132+ @ Nullable
141133 protected static By createBy (Annotation [] annotations , HowToUseSelectors howToUseLocators ) {
142134 if (annotations == null || annotations .length == 0 ) {
143135 return null ;
0 commit comments