Skip to content

Commit

Permalink
Use a constant for JvmOverloads to avoid a crash due to relocation (#…
Browse files Browse the repository at this point in the history
…4008)

* Use a constant for JvmOverloads to avoid a crash due to relocation

* Move test to own subproject

* Add a note about `@JvmOverloads` being legal only in common or JVM targets
  • Loading branch information
BoD authored and martinbonnin committed Apr 11, 2022
1 parent 3e0dbe6 commit 1534372
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.apollographql.apollo3.compiler

import com.apollographql.apollo3.annotations.ApolloDeprecatedSince
import com.apollographql.apollo3.annotations.ApolloDeprecatedSince.Version.v3_0_0
import com.apollographql.apollo3.annotations.ApolloDeprecatedSince.Version.v3_1_1
import com.apollographql.apollo3.annotations.ApolloExperimental
import com.apollographql.apollo3.ast.Schema
import com.apollographql.apollo3.ast.toSchema
Expand Down Expand Up @@ -177,12 +176,13 @@ class Options(
val generateOptionalOperationVariables: Boolean = defaultGenerateOptionalOperationVariables,

/**
* Whether to generate kotlin constructors with @JvmOverloads for more graceful Java interop experience when default values are present.
* Whether to generate kotlin constructors with `@JvmOverloads` for more graceful Java interop experience when default values are present.
* Note: when enabled in a multi-platform setup, the generated code can only be used in the common or JVM sourcesets.
*
* Default: false
*/
val addJvmOverloads: Boolean = false,
val addTypename: String = defaultAddTypename
val addTypename: String = defaultAddTypename,
) {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ fun TypeSpec.withToStringImplementation(): TypeSpec {
Modifier.TRANSIENT)
.build())
.addMethod(MethodSpec.methodBuilder("toString")
.addAnnotation(Override::class.java)
.addAnnotation(JavaClassNames.Override)
.addModifiers(Modifier.PUBLIC)
.returns(java.lang.String::class.java)
.returns(JavaClassNames.String)
.addCode(methodCode())
.build())
.build()
Expand Down Expand Up @@ -151,7 +151,7 @@ fun TypeSpec.withEqualsImplementation(): TypeSpec {

return toBuilder()
.addMethod(MethodSpec.methodBuilder("equals")
.addAnnotation(Override::class.java)
.addAnnotation(JavaClassNames.Override)
.addModifiers(Modifier.PUBLIC)
.returns(TypeName.BOOLEAN)
.addParameter(ParameterSpec.builder(TypeName.OBJECT, "o").build())
Expand Down Expand Up @@ -199,7 +199,7 @@ fun TypeSpec.withHashCodeImplementation(): TypeSpec {
.addField(FieldSpec.builder(TypeName.BOOLEAN, MEMOIZED_HASH_CODE_FLAG_VAR, Modifier.PRIVATE,
Modifier.VOLATILE, Modifier.TRANSIENT).build())
.addMethod(MethodSpec.methodBuilder("hashCode")
.addAnnotation(Override::class.java)
.addAnnotation(JavaClassNames.Override)
.addModifiers(Modifier.PUBLIC)
.returns(TypeName.INT)
.addCode(methodCode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ internal object KotlinSymbols {
val Array = ClassName("kotlin", "Array")

val Suppress = ClassName("kotlin", "Suppress")
val JvmOverloads = ClassName("kotlin.jvm", "JvmOverloads")

/**
* Adapters
*/
Expand All @@ -92,4 +94,4 @@ object KotlinMemberNames {
val withTestResolver = MemberName(apolloApiTestPackageName, "withTestResolver")
val obj = MemberName(apolloApiPackageName, "obj")
val readTypename = MemberName(apolloApiJsonPackageName, "readTypename")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ class EnumAsEnumBuilder(
private val primaryConstructorSpec =
FunSpec
.constructorBuilder()
.addParameter("rawValue", String::class)
.addParameter("rawValue", KotlinSymbols.String)
.build()

private val rawValuePropertySpec =
PropertySpec
.builder("rawValue", String::class)
.builder("rawValue", KotlinSymbols.String)
.initializer("rawValue")
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun TypeSpec.Builder.makeDataClass(
}

if (addJvmOverloads && hasDefaultValues) {
addAnnotation(JvmOverloads::class)
addAnnotation(KotlinSymbols.JvmOverloads)
}
}
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ interface Service {
val useSchemaPackageNameForFragments: Property<Boolean>

/**
* Whether to generate kotlin constructors with @JvmOverloads for more graceful Java interop experience when default values are present.
* Whether to generate kotlin constructors with `@JvmOverloads` for more graceful Java interop experience when default values are present.
* Note: when enabled in a multi-platform setup, the generated code can only be used in the common or JVM sourcesets.
*
* Default value: false
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ query LaunchDetails($id:ID!) {
}
isBooked
}
}
}
15 changes: 15 additions & 0 deletions tests/jvmoverloads/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id("org.jetbrains.kotlin.jvm")
id("com.apollographql.apollo3")
}

dependencies {
implementation("com.apollographql.apollo3:apollo-runtime")
testImplementation(groovy.util.Eval.x(project, "x.dep.kotlinJunit"))
testImplementation(groovy.util.Eval.x(project, "x.dep.junit"))
}

apollo {
packageName.set("jvmoverloads")
addJvmOverloads.set(true)
}
6 changes: 6 additions & 0 deletions tests/jvmoverloads/src/main/graphql/operation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query GetPerson($id: ID = "0") {
person(id: $id) {
id
name
}
}
8 changes: 8 additions & 0 deletions tests/jvmoverloads/src/main/graphql/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type Query {
person(id: ID!): Person
}

type Person {
id: ID!
name: String!
}
13 changes: 13 additions & 0 deletions tests/jvmoverloads/src/test/java/test/JvmOverloadsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package test;

import com.apollographql.apollo3.api.Optional;
import jvmoverloads.GetPersonQuery;
import org.junit.Test;

public class JvmOverloadsTest {
@Test
public void jvmOverloads() {
new GetPersonQuery();
new GetPersonQuery(new Optional.Present<>("1"));
}
}

0 comments on commit 1534372

Please sign in to comment.