Skip to content

Commit

Permalink
Add built-in support for Kotlin's Unit type
Browse files Browse the repository at this point in the history
This is built in a way such that its absence from the classpath will not cause failures.
  • Loading branch information
JakeWharton committed Aug 18, 2018
1 parent 5e957e4 commit 7cbfa05
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
5 changes: 5 additions & 0 deletions retrofit/pom.xml
Expand Up @@ -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>
Expand Down
22 changes: 22 additions & 0 deletions retrofit/src/main/java/retrofit2/BuiltInConverters.java
Expand Up @@ -18,11 +18,15 @@
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) {
Expand All @@ -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;
}

Expand All @@ -55,6 +68,15 @@ static final class VoidResponseBodyConverter implements Converter<ResponseBody,
}
}

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();

Expand Down
3 changes: 3 additions & 0 deletions retrofit/src/main/resources/META-INF/proguard/retrofit2.pro
Expand Up @@ -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
53 changes: 53 additions & 0 deletions retrofit/src/test/java/retrofit2/KotlinUnitTest.java
@@ -0,0 +1,53 @@
/*
* 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() {
}
}

0 comments on commit 7cbfa05

Please sign in to comment.