25
25
import org .openqa .selenium .By ;
26
26
import org .openqa .selenium .support .pagefactory .AbstractAnnotations ;
27
27
28
+ import javax .annotation .Nullable ;
28
29
import java .lang .annotation .Annotation ;
29
30
import java .lang .reflect .AnnotatedElement ;
30
31
import java .lang .reflect .Constructor ;
33
34
import java .lang .reflect .Proxy ;
34
35
import java .util .ArrayList ;
35
36
import java .util .List ;
37
+ import java .util .stream .Collectors ;
36
38
import java .util .stream .Stream ;
37
39
38
40
/**
42
44
* - https://code.google.com/p/selenium/wiki/PageFactory
43
45
*/
44
46
public 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 <?>[]{};
46
48
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 )
51
54
.map (Class ::getDeclaredMethods )
52
55
.map (AppiumByBuilder ::getMethodNames )
53
56
.flatMap (List ::stream )
54
57
.forEach (this ::add );
55
- }
56
- };
58
+ }
59
+ };
57
60
protected final AnnotatedElementContainer annotatedElementContainer ;
58
61
protected final String platform ;
59
62
protected final String automation ;
@@ -65,79 +68,68 @@ protected AppiumByBuilder(String platform, String automation) {
65
68
}
66
69
67
70
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 ());
73
72
}
74
73
75
74
private static Method [] prepareAnnotationMethods (Class <? extends Annotation > annotation ) {
76
- List <String > targetAnnotationMethodNamesList =
77
- getMethodNames (annotation .getDeclaredMethods ());
75
+ List <String > targetAnnotationMethodNamesList = getMethodNames (annotation .getDeclaredMethods ());
78
76
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 );
89
85
}
90
86
91
87
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
+ ));
112
105
}
113
106
114
107
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
+ ));
124
116
}
125
117
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 );
132
122
try {
133
123
Constructor <T > c = requiredByClass .getConstructor (By [].class );
134
- Object [] values = new Object [] {byArray };
124
+ Object [] values = new Object []{byArray };
135
125
return c .newInstance (values );
136
- } catch (Exception e ) {
126
+ } catch (InvocationTargetException | NoSuchMethodException | InstantiationException
127
+ | IllegalAccessException e ) {
137
128
throw new RuntimeException (e );
138
129
}
139
130
}
140
131
132
+ @ Nullable
141
133
protected static By createBy (Annotation [] annotations , HowToUseSelectors howToUseLocators ) {
142
134
if (annotations == null || annotations .length == 0 ) {
143
135
return null ;
0 commit comments