Skip to content

Commit

Permalink
Reuse Platform configs
Browse files Browse the repository at this point in the history
This eliminates boilerplate code across several platform classes.
  • Loading branch information
Goooler committed Jan 10, 2024
1 parent a4b81ed commit d594b1a
Showing 1 changed file with 5 additions and 83 deletions.
88 changes: 5 additions & 83 deletions retrofit/src/main/java/retrofit2/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,36 +107,16 @@ List<? extends Converter.Factory> createDefaultConverterFactories() {

@IgnoreJRERequirement // Only used on Android API 24+
@TargetApi(24)
private static final class Android24 extends Platform {
private static final class Android24 extends Java8 {
static boolean isSupported() {
return Build.VERSION.SDK_INT >= 24;
}

private @Nullable Constructor<Lookup> lookupConstructor;

@Override
Executor defaultCallbackExecutor() {
return MainThreadExecutor.INSTANCE;
}

@Override
List<? extends CallAdapter.Factory> createDefaultCallAdapterFactories(
@Nullable Executor callbackExecutor) {
return asList(
new CompletableFutureCallAdapterFactory(),
new DefaultCallAdapterFactory(callbackExecutor));
}

@Override
List<? extends Converter.Factory> createDefaultConverterFactories() {
return singletonList(new OptionalConverterFactory());
}

@Override
public boolean isDefaultMethod(Method method) {
return method.isDefault();
}

@Nullable
@Override
public Object invokeDefaultMethod(
Expand All @@ -145,17 +125,7 @@ public Object invokeDefaultMethod(
throw new UnsupportedOperationException(
"Calling default methods on API 24 and 25 is not supported");
}
Constructor<Lookup> lookupConstructor = this.lookupConstructor;
if (lookupConstructor == null) {
lookupConstructor = Lookup.class.getDeclaredConstructor(Class.class, int.class);
lookupConstructor.setAccessible(true);
this.lookupConstructor = lookupConstructor;
}
return lookupConstructor
.newInstance(declaringClass, -1 /* trusted */)
.unreflectSpecial(method, declaringClass)
.bindTo(proxy)
.invokeWithArguments(args);
return super.invokeDefaultMethod(method, declaringClass, proxy, args);
}
}

Expand Down Expand Up @@ -192,7 +162,7 @@ Object invokeDefaultMethod(

@IgnoreJRERequirement // Only used on JVM and Java 8 is the minimum-supported version.
@SuppressWarnings("NewApi") // Not used for Android.
private static final class Java8 extends Platform {
private static class Java8 extends Platform {
private @Nullable Constructor<Lookup> lookupConstructor;

@Nullable
Expand Down Expand Up @@ -243,7 +213,7 @@ public boolean isDefaultMethod(Method method) {
*/
@IgnoreJRERequirement // Only used on JVM and Java 14.
@SuppressWarnings("NewApi") // Not used for Android.
private static final class Java14 extends Platform {
private static final class Java14 extends Java8 {
static boolean isSupported() {
try {
Object version = Runtime.class.getMethod("version").invoke(null);
Expand All @@ -254,30 +224,6 @@ static boolean isSupported() {
}
}

@Nullable
@Override
Executor defaultCallbackExecutor() {
return null;
}

@Override
List<? extends CallAdapter.Factory> createDefaultCallAdapterFactories(
@Nullable Executor callbackExecutor) {
return asList(
new CompletableFutureCallAdapterFactory(),
new DefaultCallAdapterFactory(callbackExecutor));
}

@Override
List<? extends Converter.Factory> createDefaultConverterFactories() {
return singletonList(new OptionalConverterFactory());
}

@Override
public boolean isDefaultMethod(Method method) {
return method.isDefault();
}

@Nullable
@Override
public Object invokeDefaultMethod(
Expand All @@ -295,7 +241,7 @@ public Object invokeDefaultMethod(
*/
@IgnoreJRERequirement // Only used on JVM and Java 16.
@SuppressWarnings("NewApi") // Not used for Android.
private static final class Java16 extends Platform {
private static final class Java16 extends Java8 {
static boolean isSupported() {
try {
Object version = Runtime.class.getMethod("version").invoke(null);
Expand All @@ -308,30 +254,6 @@ static boolean isSupported() {

private @Nullable Method invokeDefaultMethod;

@Nullable
@Override
Executor defaultCallbackExecutor() {
return null;
}

@Override
List<? extends CallAdapter.Factory> createDefaultCallAdapterFactories(
@Nullable Executor callbackExecutor) {
return asList(
new CompletableFutureCallAdapterFactory(),
new DefaultCallAdapterFactory(callbackExecutor));
}

@Override
List<? extends Converter.Factory> createDefaultConverterFactories() {
return singletonList(new OptionalConverterFactory());
}

@Override
public boolean isDefaultMethod(Method method) {
return method.isDefault();
}

@SuppressWarnings("JavaReflectionMemberAccess") // Only available on Java 16, as we expect.
@Nullable
@Override
Expand Down

0 comments on commit d594b1a

Please sign in to comment.