-
Notifications
You must be signed in to change notification settings - Fork 761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement reflective support for Java Records #1381
Merged
Merged
Changes from 10 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
fba0e63
Standardize around JDK 8
ZacSweers 8ea1047
Update GJF to support newer JDKs
ZacSweers 2e21d08
Fix misc java 8 issues in tests
ZacSweers 261c263
Prepare java 16/records checking at runtime
ZacSweers 25c4c1c
Implement real RecordJsonAdapter
ZacSweers 2c4f98f
Spotless
ZacSweers 055257d
Prepare build for JDK 16+
ZacSweers 5f30573
Fix property name for kapt
ZacSweers 10e5d0f
Small cleanup
ZacSweers 7d1c602
Make FallbackEnum java-8 happy
ZacSweers cc6d1b6
Remove animalsniffer
ZacSweers 5f851ea
Fix format
ZacSweers 5841cfc
Add opens for ExtendsPlatformClassWithProtectedFields
ZacSweers f8391bd
Return null every time in shim for main tests
ZacSweers bd0a70f
Use JDK 16 + release 8 to replace animalsniffer
ZacSweers 0f94869
Simplify accessor accessible handling
ZacSweers 3eb768f
Remove manifest attrs
ZacSweers 9913e17
Fix typo
ZacSweers 76a6bb8
Fix KCT tests + upgrade it
ZacSweers 6668795
Cover another
ZacSweers c49b119
Try explicit kotlin daemon args for java 17?
ZacSweers 360179f
Disable 17-ea for now until kotlin 1.5.30
ZacSweers ef41c63
Add JsonQualifier and Json(name) tests + fix qualifiers
ZacSweers 68fcc68
Ensure constructor is accessible
ZacSweers d98ee90
GJF it properly
ZacSweers 20d9715
GJF 1.11
ZacSweers 765a642
Unwrap InvocationTargetException
ZacSweers 355422c
Use MethodHandle for constructor
ZacSweers 4700a63
Use MethodHandle for accessor too
ZacSweers b89f722
Revert "Remove manifest attrs"
ZacSweers 5906d57
Proper MR jar
ZacSweers d025c15
*actually* fix GJF, which wasn't getting applied before
ZacSweers 3863c9b
Make IDE happy about modules access
ZacSweers 5d74681
Fixup records tests to play nice with modules
ZacSweers 82d22e9
Add complex smoke test
ZacSweers c68005f
Remove comment
ZacSweers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,11 +22,43 @@ plugins { | |
id("ru.vyarus.animalsniffer") | ||
} | ||
|
||
val mainSourceSet by sourceSets.named("main") | ||
val java16 by sourceSets.creating { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat |
||
java { | ||
srcDir("src/main/java16") | ||
} | ||
} | ||
|
||
tasks.named<JavaCompile>("compileJava16Java") { | ||
javaCompiler.set( | ||
javaToolchains.compilerFor { | ||
languageVersion.set(JavaLanguageVersion.of(16)) | ||
} | ||
) | ||
options.release.set(16) | ||
} | ||
|
||
// We need to include our actual RecordJsonAdapter from java16 sources and replace the shime | ||
tasks.named<Jar>("jar") { | ||
from(java16.output) { | ||
duplicatesStrategy = DuplicatesStrategy.INCLUDE | ||
} | ||
manifest { | ||
// Indicate that this is a multi release jar | ||
ZacSweers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
attributes("Multi-Release" to "true") | ||
} | ||
} | ||
|
||
configurations { | ||
"java16Implementation" { | ||
extendsFrom(api.get()) | ||
extendsFrom(implementation.get()) | ||
} | ||
} | ||
|
||
tasks.withType<KotlinCompile>() | ||
.configureEach { | ||
kotlinOptions { | ||
jvmTarget = "1.6" | ||
|
||
if (name.contains("test", true)) { | ||
@Suppress("SuspiciousCollectionReassignment") // It's not suspicious | ||
freeCompilerArgs += listOf("-Xopt-in=kotlin.ExperimentalStdlibApi") | ||
|
@@ -35,6 +67,8 @@ tasks.withType<KotlinCompile>() | |
} | ||
|
||
dependencies { | ||
// So the j16 source set can "see" main Moshi sources | ||
"java16Implementation"(mainSourceSet.output) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. off topic: this syntax offends me so much |
||
compileOnly(Dependencies.jsr305) | ||
api(Dependencies.okio) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2021 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
plugins { | ||
`java-library` | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(16)) | ||
} | ||
} | ||
|
||
tasks.withType<JavaCompile>().configureEach { | ||
options.release.set(16) | ||
} | ||
|
||
dependencies { | ||
testImplementation(project(":moshi")) | ||
testCompileOnly(Dependencies.jsr305) | ||
testImplementation(Dependencies.Testing.junit) | ||
testImplementation(Dependencies.Testing.truth) | ||
} |
44 changes: 44 additions & 0 deletions
44
moshi/records-tests/src/test/java/com/squareup/moshi/RecordsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2021 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 | ||
* | ||
* https://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 com.squareup.moshi; | ||
|
||
import java.io.IOException; | ||
import org.junit.Test; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
public final class RecordsTest { | ||
|
||
private final Moshi moshi = new Moshi.Builder().build(); | ||
|
||
@Test public void genericRecord() throws IOException { | ||
var adapter = | ||
moshi.<GenericRecord<String>>adapter(Types.newParameterizedType(GenericRecord.class, | ||
String.class)); | ||
assertThat(adapter.fromJson("{\"value\":\"Okay!\"}")).isEqualTo(new GenericRecord<>("Okay!")); | ||
} | ||
|
||
@Test public void genericBoundedRecord() throws IOException { | ||
var adapter = moshi.<GenericBoundedRecord<Integer>>adapter(Types.newParameterizedType( | ||
GenericBoundedRecord.class, | ||
Integer.class)); | ||
assertThat(adapter.fromJson("{\"value\":4}")).isEqualTo(new GenericBoundedRecord<>(4)); | ||
} | ||
} | ||
|
||
record GenericBoundedRecord<T extends Number>(T value) {} | ||
|
||
record GenericRecord<T>(T value) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
moshi/src/main/java/com/squareup/moshi/RecordJsonAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.squareup.moshi; | ||
|
||
import java.io.IOException; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.Set; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
// This is just a simple shim for linking in StandardJsonAdapters and excluded from the final jar. | ||
final class RecordJsonAdapter<T> extends JsonAdapter<T> { | ||
|
||
static final JsonAdapter.Factory FACTORY = new JsonAdapter.Factory() { | ||
|
||
@Nullable @Override | ||
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) { | ||
throw new AssertionError(); | ||
} | ||
}; | ||
|
||
@Nullable @Override public T fromJson(JsonReader reader) throws IOException { | ||
throw new AssertionError(); | ||
} | ||
|
||
@Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException { | ||
throw new AssertionError(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not testing on Java 8 is annoying. Something to look into separately from this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can make the main tests run with java 8 via Gradle toolchains