From 52deed6a7f22d2079923ffa06d2f093c194c291e Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Fri, 1 Mar 2024 20:23:22 +0100 Subject: [PATCH 1/2] fix: Always release annotated element reference from the builder instance --- .../AppiumElementLocatorFactory.java | 19 +++++++++++-------- .../pagefactory/WidgetByBuilder.java | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java b/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java index 787e34e9e..5562086fb 100644 --- a/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java +++ b/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java @@ -19,7 +19,6 @@ import io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder; import io.appium.java_client.pagefactory.locator.CacheableElementLocatorFactory; import io.appium.java_client.pagefactory.locator.CacheableLocator; -import org.openqa.selenium.By; import org.openqa.selenium.SearchContext; import javax.annotation.Nullable; @@ -90,12 +89,16 @@ public CacheableLocator createLocator(AnnotatedElement annotatedElement) { customDuration = duration; } builder.setAnnotated(annotatedElement); - By byResult = builder.buildBy(); - return ofNullable(byResult) - .map(by -> searchContextReference != null - ? new AppiumElementLocator(searchContextReference, by, builder.isLookupCached(), customDuration) - : new AppiumElementLocator(searchContext, by, builder.isLookupCached(), customDuration) - ) - .orElse(null); + try { + return ofNullable(builder.buildBy()) + .map(by -> searchContextReference != null + ? new AppiumElementLocator(searchContextReference, by, builder.isLookupCached(), customDuration) + : new AppiumElementLocator(searchContext, by, builder.isLookupCached(), customDuration) + ) + .orElse(null); + } finally { + // unleak element reference after the locator is built + builder.setAnnotated(null); + } } } diff --git a/src/main/java/io/appium/java_client/pagefactory/WidgetByBuilder.java b/src/main/java/io/appium/java_client/pagefactory/WidgetByBuilder.java index 905844e4a..b87996357 100644 --- a/src/main/java/io/appium/java_client/pagefactory/WidgetByBuilder.java +++ b/src/main/java/io/appium/java_client/pagefactory/WidgetByBuilder.java @@ -51,7 +51,7 @@ private static Class getClassFromAListField(Field field) { @SuppressWarnings("unchecked") private By getByFromDeclaredClass(WhatIsNeeded whatIsNeeded) { AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated(); - Field field = Field.class.cast(annotatedElement); + Field field = (Field) annotatedElement; Class declaredClass; By result = null; From 10b52def7e1cf71dd72dd6a985f5052134ecc8b1 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Fri, 1 Mar 2024 20:54:37 +0100 Subject: [PATCH 2/2] fix style --- .../pagefactory/AppiumElementLocatorFactory.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java b/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java index 5562086fb..77b3120fc 100644 --- a/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java +++ b/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java @@ -91,10 +91,12 @@ public CacheableLocator createLocator(AnnotatedElement annotatedElement) { builder.setAnnotated(annotatedElement); try { return ofNullable(builder.buildBy()) - .map(by -> searchContextReference != null - ? new AppiumElementLocator(searchContextReference, by, builder.isLookupCached(), customDuration) - : new AppiumElementLocator(searchContext, by, builder.isLookupCached(), customDuration) - ) + .map(by -> { + var isLookupCached = builder.isLookupCached(); + return searchContextReference != null + ? new AppiumElementLocator(searchContextReference, by, isLookupCached, customDuration) + : new AppiumElementLocator(searchContext, by, isLookupCached, customDuration); + }) .orElse(null); } finally { // unleak element reference after the locator is built