Skip to content

Commit

Permalink
Don't use reflection to test the existence of optional dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristan Meijer committed Oct 15, 2019
1 parent 80dd153 commit 0d4e4f8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ public final class ReflectionUtil {
private ReflectionUtil() {
}

@Nullable
public static Class<?> tryGetClassForName(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
return null;
}
}

@Nullable
public static Field tryGetDeclaredField(Class<?> theClass, String fieldName) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ public abstract class FragmentCompat<
private static final boolean sHasSupportFragment;

static {
sHasSupportFragment = ReflectionUtil.tryGetClassForName(
"androidx.fragment.app.Fragment") != null;
boolean hasSupportFragment = false;
try {
androidx.fragment.app.Fragment.class.getClass();
hasSupportFragment = true;
} catch (NoClassDefFoundError ignore) {
// AndroidX Fragment is not in classpath.
}
sHasSupportFragment = hasSupportFragment;
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import com.facebook.stetho.common.ExceptionUtil;
import com.facebook.stetho.common.LogUtil;
import com.facebook.stetho.common.ReflectionUtil;
import com.facebook.stetho.common.StringUtil;
import com.facebook.stetho.common.android.ResourcesUtil;
import com.facebook.stetho.inspector.elements.AbstractChainedDescriptor;
Expand Down Expand Up @@ -50,8 +49,14 @@ final class ViewDescriptor extends AbstractChainedDescriptor<View>
private static final boolean sHasSupportNodeInfo;

static {
sHasSupportNodeInfo = ReflectionUtil.tryGetClassForName(
"androidx.core.view.accessibility.AccessibilityNodeInfoCompat") != null;
boolean hasSupportNodeInfo = false;
try {
androidx.core.view.accessibility.AccessibilityNodeInfoCompat.class.getClass();
hasSupportNodeInfo = true;
} catch (NoClassDefFoundError ignore) {
// AndroidX Fragment is not in classpath.
}
sHasSupportNodeInfo = hasSupportNodeInfo;
}

/**
Expand Down

0 comments on commit 0d4e4f8

Please sign in to comment.