Skip to content

Commit

Permalink
Switch to spotless and format code (#1196)
Browse files Browse the repository at this point in the history
* Add spotless configuration

* Reformat!

* Add copyright config for build.gradle.kts files

* Add toeholds for headers
  • Loading branch information
ZacSweers authored Aug 28, 2020
1 parent 701d6ba commit 538890e
Show file tree
Hide file tree
Showing 109 changed files with 6,806 additions and 5,030 deletions.
16 changes: 8 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ _2019-10-30_

_2019-10-29_

* **This release requires kotlin-reflect or moshi-kotlin-codegen for all Kotlin classes.**
* **This release requires kotlin-reflect or moshi-kotlin-codegen for all Kotlin classes.**

Previously Moshi wouldn't differentiate between Kotlin classes and Java classes if Kotlin was
not configured. This caused bad runtime behavior such as putting null into non-nullable fields!
If you attempt to create an adapter for a Kotlin type, Moshi will throw an
`IllegalArgumentException`.

Fix this with either the reflection adapter:

```kotlin
val moshi = Moshi.Builder()
// ... add your own JsonAdapters and factories ...
.add(KotlinJsonAdapterFactory())
.build()
```

Or the codegen annotation processor:

```kotlin
Expand All @@ -77,12 +77,12 @@ _2019-10-29_
val visible_cards: List<Card>
)
```

The [Kotlin documentation][moshi_kotlin_docs] explains the required build configuration changes.

* New: Change how Moshi's generated adapters call constructors. Previous generated code used a
* New: Change how Moshi's generated adapters call constructors. Previous generated code used a
combination of the constructor and `copy()` method to set properties that have default values.
With this update we call the same synthetic constructor that Kotlin uses. This is less surprising
With this update we call the same synthetic constructor that Kotlin uses. This is less surprising
though it requires us to generate some tricky code.
* New: Make `Rfc3339DateJsonAdapter` null-safe. Previously Moshi would refuse to decode null dates.
Restore that behavior by explicitly forbidding nulls with `Rfc3339DateJsonAdapter().nonNull()`.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ reflection, Moshi is designed to help you out when things go wrong.
JsonDataException: Expected one of [CLUBS, DIAMONDS, HEARTS, SPADES] but was ANCHOR at path $.visible_cards[2].suit
at com.squareup.moshi.JsonAdapters$11.fromJson(JsonAdapters.java:188)
at com.squareup.moshi.JsonAdapters$11.fromJson(JsonAdapters.java:180)
...
...
```

Moshi always throws a standard `java.io.IOException` if there is an error reading the JSON document,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
* The new class is {@code com.squareup.moshi.adapters.Rfc3339DateJsonAdapter}.
*/
public final class Rfc3339DateJsonAdapter extends JsonAdapter<Date> {
private final com.squareup.moshi.adapters.Rfc3339DateJsonAdapter delegate
= new com.squareup.moshi.adapters.Rfc3339DateJsonAdapter();
private final com.squareup.moshi.adapters.Rfc3339DateJsonAdapter delegate =
new com.squareup.moshi.adapters.Rfc3339DateJsonAdapter();

@Override public Date fromJson(JsonReader reader) throws IOException {
@Override
public Date fromJson(JsonReader reader) throws IOException {
return delegate.fromJson(reader);
}

@Override public void toJson(JsonWriter writer, Date value) throws IOException {
@Override
public void toJson(JsonWriter writer, Date value) throws IOException {
delegate.toJson(writer, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@
* not match any enum value. To use, add this as an adapter for your enum type on your {@link
* com.squareup.moshi.Moshi.Builder Moshi.Builder}:
*
* <pre> {@code
*
* Moshi moshi = new Moshi.Builder()
* .add(CurrencyCode.class, EnumJsonAdapter.create(CurrencyCode.class)
* .withUnknownFallback(CurrencyCode.USD))
* .build();
* <pre>{@code
* Moshi moshi = new Moshi.Builder()
* .add(CurrencyCode.class, EnumJsonAdapter.create(CurrencyCode.class)
* .withUnknownFallback(CurrencyCode.USD))
* .build();
* }</pre>
*/
public final class EnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
Expand Down Expand Up @@ -78,15 +77,21 @@ public EnumJsonAdapter<T> withUnknownFallback(@Nullable T fallbackValue) {
}
}

@Override public @Nullable T fromJson(JsonReader reader) throws IOException {
@Override
public @Nullable T fromJson(JsonReader reader) throws IOException {
int index = reader.selectString(options);
if (index != -1) return constants[index];

String path = reader.getPath();
if (!useFallbackValue) {
String name = reader.nextString();
throw new JsonDataException("Expected one of "
+ Arrays.asList(nameStrings) + " but was " + name + " at path " + path);
throw new JsonDataException(
"Expected one of "
+ Arrays.asList(nameStrings)
+ " but was "
+ name
+ " at path "
+ path);
}
if (reader.peek() != JsonReader.Token.STRING) {
throw new JsonDataException(
Expand All @@ -96,15 +101,17 @@ public EnumJsonAdapter<T> withUnknownFallback(@Nullable T fallbackValue) {
return fallbackValue;
}

@Override public void toJson(JsonWriter writer, T value) throws IOException {
@Override
public void toJson(JsonWriter writer, T value) throws IOException {
if (value == null) {
throw new NullPointerException(
"value was null! Wrap in .nullSafe() to write nullable values.");
}
writer.value(nameStrings[value.ordinal()]);
}

@Override public String toString() {
@Override
public String toString() {
return "EnumJsonAdapter(" + enumType.getName() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
* Jackson’s date formatter, pruned to Moshi's needs. Forked from this file:
* https://github.com/FasterXML/jackson-databind/blob/67ebf7305f492285a8f9f4de31545f5f16fc7c3a/src/main/java/com/fasterxml/jackson/databind/util/ISO8601Utils.java
*
* Utilities methods for manipulating dates in iso8601 format. This is much much faster and GC
* <p>Utilities methods for manipulating dates in iso8601 format. This is much much faster and GC
* friendly than using SimpleDateFormat so highly suitable if you (un)serialize lots of date
* objects.
*
* Supported parse format: [yyyy-MM-dd|yyyyMMdd][T(hh:mm[:ss[.sss]]|hhmm[ss[.sss]])]?[Z|[+-]hh[:]mm]]
* <p>Supported parse format:
* [yyyy-MM-dd|yyyyMMdd][T(hh:mm[:ss[.sss]]|hhmm[ss[.sss]])]?[Z|[+-]hh[:]mm]]
*
* @see <a href="http://www.w3.org/TR/NOTE-datetime">this specification</a>
*/
Expand Down Expand Up @@ -167,8 +168,11 @@ public static Date parse(String date) {
*/
String cleaned = act.replace(":", "");
if (!cleaned.equals(timezoneId)) {
throw new IndexOutOfBoundsException("Mismatching time zone indicator: "
+ timezoneId + " given, resolves to " + timezone.getID());
throw new IndexOutOfBoundsException(
"Mismatching time zone indicator: "
+ timezoneId
+ " given, resolves to "
+ timezone.getID());
}
}
}
Expand Down Expand Up @@ -259,8 +263,7 @@ private static void padInt(StringBuilder buffer, int value, int length) {
}

/**
* Returns the index of the first character in the string that is not a digit, starting at
* offset.
* Returns the index of the first character in the string that is not a digit, starting at offset.
*/
private static int indexOfNonDigit(String string, int offset) {
for (int i = offset; i < string.length(); i++) {
Expand Down
Loading

0 comments on commit 538890e

Please sign in to comment.