use of io.appium.java_client.pagefactory.bys.ContentType in project java-client by appium.
the class AppiumFieldDecorator method decorateWidget.
@SuppressWarnings("unchecked")
private Object decorateWidget(Field field) {
Class<?> type = field.getType();
if (!Widget.class.isAssignableFrom(type) && !List.class.isAssignableFrom(type)) {
return null;
}
Class<? extends Widget> widgetType;
boolean isAlist = false;
if (List.class.isAssignableFrom(type)) {
isAlist = true;
Type genericType = field.getGenericType();
if (!(genericType instanceof ParameterizedType)) {
return null;
}
Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
if (ParameterizedType.class.isAssignableFrom(listType.getClass())) {
listType = ((ParameterizedType) listType).getRawType();
}
if (listType instanceof Class) {
if (!Widget.class.isAssignableFrom((Class) listType)) {
return null;
}
widgetType = Class.class.cast(listType);
} else {
return null;
}
} else {
widgetType = (Class<? extends Widget>) field.getType();
}
CacheableLocator locator = widgetLocatorFactory.createLocator(field);
Map<ContentType, Constructor<? extends Widget>> map = OverrideWidgetReader.read(widgetType, field, platform, automation);
if (isAlist) {
return getEnhancedProxy(ArrayList.class, new WidgetListInterceptor(locator, webDriver, map, widgetType, duration));
}
Constructor<? extends Widget> constructor = WidgetConstructorUtil.findConvenientConstructor(widgetType);
return getEnhancedProxy(widgetType, new Class[] { constructor.getParameterTypes()[0] }, new Object[] { proxyForAnElement(locator) }, new WidgetInterceptor(locator, webDriver, null, map, duration));
}
use of io.appium.java_client.pagefactory.bys.ContentType in project java-client by appium.
the class WidgetInterceptor method getObject.
@Override
protected Object getObject(WebElement element, Method method, Object[] args) throws Throwable {
ContentType type = getCurrentContentType(element);
if (cachedElement == null || (locator != null && !((CacheableLocator) locator).isLookUpCached()) || cachedInstances.size() == 0) {
cachedElement = element;
Constructor<? extends Widget> constructor = instantiationMap.get(type);
Class<? extends Widget> clazz = constructor.getDeclaringClass();
int modifiers = clazz.getModifiers();
if (Modifier.isAbstract(modifiers)) {
throw new InstantiationException(clazz.getName() + " is abstract so " + "it can't be instantiated");
}
Widget widget = constructor.newInstance(cachedElement);
cachedInstances.put(type, widget);
PageFactory.initElements(new AppiumFieldDecorator(widget, duration), widget);
}
try {
method.setAccessible(true);
return method.invoke(cachedInstances.get(type), args);
} catch (Throwable t) {
throw extractReadableException(t);
}
}
use of io.appium.java_client.pagefactory.bys.ContentType in project java-client by appium.
the class WidgetListInterceptor method getObject.
@Override
protected Object getObject(List<WebElement> elements, Method method, Object[] args) throws Throwable {
if (cachedElements == null || (locator != null && !((CacheableLocator) locator).isLookUpCached())) {
cachedElements = elements;
cachedWidgets.clear();
ContentType type = null;
for (WebElement element : cachedElements) {
type = ofNullable(type).orElseGet(() -> getCurrentContentType(element));
Class<?>[] params = new Class<?>[] { instantiationMap.get(type).getParameterTypes()[0] };
cachedWidgets.add(ProxyFactory.getEnhancedProxy(declaredType, params, new Object[] { element }, new WidgetInterceptor(null, driver, element, instantiationMap, duration)));
}
}
try {
return method.invoke(cachedWidgets, args);
} catch (Throwable t) {
throw extractReadableException(t);
}
}
Aggregations