Skip to content

Commit a232681

Browse files
authored
Merge pull request #2 from Auto81/reflect
Reflect
2 parents 8313002 + 070d7c2 commit a232681

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/main/java/io/appium/java_client/proxy/Helpers.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
package io.appium.java_client.proxy;
1818

19-
import com.google.common.annotations.VisibleForTesting;
2019
import com.google.common.base.Preconditions;
21-
import java.util.Map;
22-
import java.util.WeakHashMap;
2320
import lombok.Value;
2421
import net.bytebuddy.ByteBuddy;
2522
import net.bytebuddy.description.method.MethodDescription;
@@ -34,7 +31,9 @@
3431
import java.lang.reflect.Method;
3532
import java.util.Collection;
3633
import java.util.Collections;
34+
import java.util.Map;
3735
import java.util.Set;
36+
import java.util.WeakHashMap;
3837
import java.util.stream.Collectors;
3938
import java.util.stream.Stream;
4039

@@ -51,18 +50,8 @@ public class Helpers {
5150
// the performance and to avoid extensive memory usage for our case, where
5251
// the amount of instrumented proxy classes we create is low in comparison to the amount
5352
// of proxy instances.
54-
private static final Map<ProxyClassSignature, Class<?>> CACHED_PROXY_CLASSES = Collections.synchronizedMap(new WeakHashMap<>());
55-
56-
/**
57-
* Gets CACHED_PROXY_CLASSES size.
58-
* Used for cache clear up tests {@see io.appium.java_client.pagefactory_tests.widget.tests.combined.CombinedWidgetTest}.
59-
*
60-
* @return the cached proxy classes size
61-
*/
62-
@VisibleForTesting
63-
public static int getCachedProxyClassesSize() {
64-
return CACHED_PROXY_CLASSES.size();
65-
}
53+
private static final Map<ProxyClassSignature, Class<?>> CACHED_PROXY_CLASSES =
54+
Collections.synchronizedMap(new WeakHashMap<>());
6655

6756
private Helpers() {
6857
}

src/test/java/io/appium/java_client/pagefactory_tests/widget/tests/combined/CombinedWidgetTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
import io.appium.java_client.pagefactory_tests.widget.tests.AbstractStubWebDriver;
77
import io.appium.java_client.pagefactory_tests.widget.tests.DefaultStubWidget;
88
import io.appium.java_client.pagefactory_tests.widget.tests.android.DefaultAndroidWidget;
9-
import io.appium.java_client.proxy.Helpers;
109
import org.junit.jupiter.params.ParameterizedTest;
1110
import org.junit.jupiter.params.provider.Arguments;
1211
import org.junit.jupiter.params.provider.MethodSource;
1312
import org.openqa.selenium.WebDriver;
1413
import org.openqa.selenium.WebElement;
1514

15+
import java.lang.reflect.Field;
1616
import java.util.ArrayList;
1717
import java.util.List;
18+
import java.util.Map;
1819
import java.util.stream.Stream;
1920

2021
import static java.util.stream.Collectors.toList;
@@ -178,8 +179,19 @@ private void assertProxyClassCacheGrowth() {
178179
int thresholdSize = 50;
179180
assertThat(
180181
"Proxy Class Cache threshold is " + thresholdSize,
181-
Helpers.getCachedProxyClassesSize(),
182+
getCachedProxyClassesSize(),
182183
lessThan(thresholdSize)
183184
);
184185
}
186+
187+
private int getCachedProxyClassesSize() {
188+
try {
189+
Field cpc = Class.forName("io.appium.java_client.proxy.Helpers").getDeclaredField("CACHED_PROXY_CLASSES");
190+
cpc.setAccessible(true);
191+
Map<?, ?> cachedProxyClasses = (Map<?, ?>) cpc.get(null);
192+
return cachedProxyClasses.size();
193+
} catch (Exception e) {
194+
throw new RuntimeException(e);
195+
}
196+
}
185197
}

0 commit comments

Comments
 (0)