forked from appium/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterceptor.java
More file actions
150 lines (139 loc) · 5.61 KB
/
Copy pathInterceptor.java
File metadata and controls
150 lines (139 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.appium.java_client.proxy;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.implementation.bind.annotation.This;
import org.openqa.selenium.remote.RemoteWebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import static io.appium.java_client.proxy.MethodCallListener.UNSET;
public class Interceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(Interceptor.class);
private Interceptor() {
}
/**
* A magic method used to wrap public method calls in classes
* patched by ByteBuddy and acting as proxies. The performance
* of this method is mission-critical as it gets called upon
* every invocation of any method of the proxied class.
*
* @param self The reference to the original instance.
* @param method The reference to the original method.
* @param args The reference to method args.
* @param callable The reference to the non-patched callable to avoid call recursion.
* @return Either the original method result or the patched one.
*/
@SuppressWarnings("unused")
@RuntimeType
public static Object intercept(
@This Object self,
@Origin Method method,
@AllArguments Object[] args,
@SuperCall Callable<?> callable
) throws Throwable {
var listeners = ((HasMethodCallListeners) self).getMethodCallListeners();
if (listeners == null || listeners.length == 0) {
return callable.call();
}
for (var listener : listeners) {
try {
listener.beforeCall(self, method, args);
} catch (NotImplementedException e) {
// ignore
} catch (Exception e) {
LOGGER.atError().log("Got an unexpected error in beforeCall listener of {}.{} method",
self.getClass().getName(), method.getName(), e
);
}
}
Object result = UNSET;
for (var listener : listeners) {
try {
result = listener.call(self, method, args, callable);
if (result != UNSET) {
break;
}
} catch (NotImplementedException e) {
// ignore
} catch (Exception e) {
try {
result = listener.onError(self, method, args, e);
if (result != UNSET) {
return result;
}
} catch (NotImplementedException ignore) {
// ignore
}
throw e;
}
}
if (UNSET == result) {
try {
result = callable.call();
} catch (Exception e) {
for (var listener : listeners) {
try {
result = listener.onError(self, method, args, e);
if (result != UNSET) {
return result;
}
} catch (NotImplementedException ignore) {
// ignore
}
}
throw e;
}
}
if (result instanceof RemoteWebElement) {
result = Helpers.wrapElement((RemoteWebElement) result, (HasMethodCallListeners) self, listeners);
} else if (result instanceof List) {
List<?> originalList = (List<?>) result;
if (!originalList.isEmpty() && originalList.get(0) instanceof RemoteWebElement) {
List<Object> wrappedList = new ArrayList<>(originalList.size());
for (Object item : originalList) {
if (item instanceof RemoteWebElement) {
wrappedList.add(Helpers.wrapElement(
(RemoteWebElement) item,
(HasMethodCallListeners) self, listeners));
} else {
wrappedList.add(item);
}
}
result = wrappedList;
}
}
final Object endResult = result == UNSET ? null : result;
for (var listener : listeners) {
try {
listener.afterCall(self, method, args, endResult);
} catch (NotImplementedException e) {
// ignore
} catch (Exception e) {
LOGGER.atError().log("Got an unexpected error in afterCall listener of {}.{} method",
self.getClass().getName(), method.getName(), e
);
}
}
return endResult;
}
}