Code cleanups

This commit is contained in:
Goooler
2021-08-19 02:51:52 +08:00
committed by GitHub
parent ce78d34c14
commit 2572c29e42
8 changed files with 15 additions and 26 deletions

View File

@@ -47,7 +47,7 @@ class JsonClassCodegenProcessorTest {
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
class PrivateConstructor private constructor(var a: Int, var b: Int) {
class PrivateConstructor private constructor(val a: Int, val b: Int) {
fun a() = a
fun b() = b
companion object {

View File

@@ -27,7 +27,6 @@ import com.squareup.moshi.internal.Util.resolve
import com.squareup.moshi.rawType
import java.lang.reflect.Modifier
import java.lang.reflect.Type
import java.util.AbstractMap.SimpleEntry
import kotlin.reflect.KFunction
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.KParameter

View File

@@ -437,10 +437,7 @@ class GeneratedAdaptersTest {
}
@JsonClass(generateAdapter = true)
class ImmutableProperties(a: Int, b: Int) {
val a = a
val b = b
}
class ImmutableProperties(val a: Int, val b: Int)
@Test fun constructorDefaults() {
val moshi = Moshi.Builder().build()

View File

@@ -115,10 +115,7 @@ class KotlinJsonAdapterTest {
assertThat(decoded.b).isEqualTo(5)
}
class ImmutableProperties(a: Int, b: Int) {
val a = a
val b = b
}
class ImmutableProperties(val a: Int, val b: Int)
@Test fun constructorDefaults() {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
@@ -432,13 +429,13 @@ class KotlinJsonAdapterTest {
val decoded = jsonAdapter.fromJson("""{"a":4,"buf":[0,0],"size":0}""")!!
assertThat(decoded.a).isEqualTo(4)
assertThat(decoded.buf()).isEqualTo(ByteArray(2, { 0 }))
assertThat(decoded.buf()).isEqualTo(ByteArray(2) { 0 })
assertThat(decoded.count()).isEqualTo(0)
}
internal class ExtendsPlatformClassWithProtectedField(var a: Int) : ByteArrayOutputStream(2) {
fun buf() = buf
fun count() = count
fun buf(): ByteArray = buf
fun count(): Int = count
}
@Test fun platformTypeThrows() {

View File

@@ -49,7 +49,7 @@ final class JsonUtf8Writer extends JsonWriter {
static {
REPLACEMENT_CHARS = new String[128];
for (int i = 0; i <= 0x1f; i++) {
REPLACEMENT_CHARS[i] = String.format("\\u%04x", (int) i);
REPLACEMENT_CHARS[i] = String.format("\\u%04x", i);
}
REPLACEMENT_CHARS['"'] = "\\\"";
REPLACEMENT_CHARS['\\'] = "\\\\";

View File

@@ -43,7 +43,7 @@ final class LinkedHashTreeMap<K, V> extends AbstractMap<K, V> implements Seriali
}
};
Comparator<? super K> comparator;
final Comparator<? super K> comparator;
Node<K, V>[] table;
final Node<K, V> header;
int size = 0;
@@ -346,13 +346,11 @@ final class LinkedHashTreeMap<K, V> extends AbstractMap<K, V> implements Seriali
int rightLeftHeight = rightLeft != null ? rightLeft.height : 0;
int rightDelta = rightLeftHeight - rightRightHeight;
if (rightDelta == -1 || (rightDelta == 0 && !insert)) {
rotateLeft(node); // AVL right right
} else {
if (rightDelta != -1 && (rightDelta != 0 || insert)) {
assert (rightDelta == 1);
rotateRight(right); // AVL right left
rotateLeft(node);
}
rotateLeft(node); // AVL right right
if (insert) {
break; // no further rotations will be necessary
}
@@ -364,13 +362,11 @@ final class LinkedHashTreeMap<K, V> extends AbstractMap<K, V> implements Seriali
int leftLeftHeight = leftLeft != null ? leftLeft.height : 0;
int leftDelta = leftLeftHeight - leftRightHeight;
if (leftDelta == 1 || (leftDelta == 0 && !insert)) {
rotateRight(node); // AVL left left
} else {
if (leftDelta != 1 && (leftDelta != 0 || insert)) {
assert (leftDelta == -1);
rotateLeft(left); // AVL left right
rotateRight(node);
}
rotateRight(node); // AVL left left
if (insert) {
break; // no further rotations will be necessary
}

View File

@@ -184,7 +184,7 @@ public final class Moshi {
@CheckReturnValue
public Moshi.Builder newBuilder() {
Builder result = new Builder();
for (int i = 0, limit = lastOffset; i < limit; i++) {
for (int i = 0; i < lastOffset; i++) {
result.add(factories.get(i));
}
for (int i = lastOffset, limit = factories.size() - BUILT_IN_FACTORIES.size(); i < limit; i++) {

View File

@@ -61,7 +61,7 @@ class KotlinExtensionsTest {
fun addAdapterInferred() {
// An adapter that always returns -1
val customIntdapter = object : JsonAdapter<Int>() {
override fun fromJson(reader: JsonReader): Int? {
override fun fromJson(reader: JsonReader): Int {
reader.skipValue()
return -1
}
@@ -81,7 +81,7 @@ class KotlinExtensionsTest {
fun addAdapterInferred_parameterized() {
// An adapter that always returns listOf(-1)
val customIntListAdapter = object : JsonAdapter<List<Int>>() {
override fun fromJson(reader: JsonReader): List<Int>? {
override fun fromJson(reader: JsonReader): List<Int> {
reader.skipValue()
return listOf(-1)
}