提交 7cbfa055 编写于 作者: J Jake Wharton

Add built-in support for Kotlin's Unit type

This is built in a way such that its absence from the classpath will not cause failures.
上级 5e957e41
......@@ -46,6 +46,7 @@
<!-- Compilation -->
<java.version>1.7</java.version>
<kotlin.version>1.2.60</kotlin.version>
<!-- Dependencies -->
<android.version>4.1.1.4</android.version>
......@@ -108,6 +109,11 @@
<artifactId>android</artifactId>
<version>${android.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-annotations</artifactId>
......
......@@ -24,6 +24,11 @@
<artifactId>android</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
......
......@@ -18,11 +18,15 @@ package retrofit2;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import kotlin.Unit;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.http.Streaming;
final class BuiltInConverters extends Converter.Factory {
/** Not volatile because we don't mind multiple threads discovering this. */
private boolean checkForKotlinUnit = true;
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
......@@ -34,6 +38,15 @@ final class BuiltInConverters extends Converter.Factory {
if (type == Void.class) {
return VoidResponseBodyConverter.INSTANCE;
}
if (checkForKotlinUnit) {
try {
if (type == Unit.class) {
return UnitResponseBodyConverter.INSTANCE;
}
} catch (NoClassDefFoundError ignored) {
checkForKotlinUnit = false;
}
}
return null;
}
......@@ -55,6 +68,15 @@ final class BuiltInConverters extends Converter.Factory {
}
}
static final class UnitResponseBodyConverter implements Converter<ResponseBody, Unit> {
static final UnitResponseBodyConverter INSTANCE = new UnitResponseBodyConverter();
@Override public Unit convert(ResponseBody value) {
value.close();
return Unit.INSTANCE;
}
}
static final class RequestBodyConverter implements Converter<RequestBody, RequestBody> {
static final RequestBodyConverter INSTANCE = new RequestBodyConverter();
......
......@@ -11,3 +11,6 @@
# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**
# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit
/*
* Copyright (C) 2018 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit2;
import java.io.IOException;
import kotlin.Unit;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import retrofit2.http.GET;
import static org.assertj.core.api.Assertions.assertThat;
public final class KotlinUnitTest {
@Rule public final MockWebServer server = new MockWebServer();
interface Service {
@GET("/")
Call<Unit> empty();
}
@Test public void unitOnClasspath() throws IOException {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setBody("Hi"));
Response<Unit> response = example.empty().execute();
assertThat(response.isSuccessful()).isTrue();
assertThat(response.body()).isSameAs(Unit.INSTANCE);
}
@Ignore("This is implicitly tested by integration tests of the adapters and converters")
@Test public void unitMissingFromClasspath() {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册