mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Code cleanups
This commit is contained in:
@@ -47,7 +47,7 @@ class JsonClassCodegenProcessorTest {
|
|||||||
import com.squareup.moshi.JsonClass
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@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 a() = a
|
||||||
fun b() = b
|
fun b() = b
|
||||||
companion object {
|
companion object {
|
||||||
|
@@ -27,7 +27,6 @@ import com.squareup.moshi.internal.Util.resolve
|
|||||||
import com.squareup.moshi.rawType
|
import com.squareup.moshi.rawType
|
||||||
import java.lang.reflect.Modifier
|
import java.lang.reflect.Modifier
|
||||||
import java.lang.reflect.Type
|
import java.lang.reflect.Type
|
||||||
import java.util.AbstractMap.SimpleEntry
|
|
||||||
import kotlin.reflect.KFunction
|
import kotlin.reflect.KFunction
|
||||||
import kotlin.reflect.KMutableProperty1
|
import kotlin.reflect.KMutableProperty1
|
||||||
import kotlin.reflect.KParameter
|
import kotlin.reflect.KParameter
|
||||||
|
@@ -437,10 +437,7 @@ class GeneratedAdaptersTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
class ImmutableProperties(a: Int, b: Int) {
|
class ImmutableProperties(val a: Int, val b: Int)
|
||||||
val a = a
|
|
||||||
val b = b
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test fun constructorDefaults() {
|
@Test fun constructorDefaults() {
|
||||||
val moshi = Moshi.Builder().build()
|
val moshi = Moshi.Builder().build()
|
||||||
|
@@ -115,10 +115,7 @@ class KotlinJsonAdapterTest {
|
|||||||
assertThat(decoded.b).isEqualTo(5)
|
assertThat(decoded.b).isEqualTo(5)
|
||||||
}
|
}
|
||||||
|
|
||||||
class ImmutableProperties(a: Int, b: Int) {
|
class ImmutableProperties(val a: Int, val b: Int)
|
||||||
val a = a
|
|
||||||
val b = b
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test fun constructorDefaults() {
|
@Test fun constructorDefaults() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||||
@@ -432,13 +429,13 @@ class KotlinJsonAdapterTest {
|
|||||||
|
|
||||||
val decoded = jsonAdapter.fromJson("""{"a":4,"buf":[0,0],"size":0}""")!!
|
val decoded = jsonAdapter.fromJson("""{"a":4,"buf":[0,0],"size":0}""")!!
|
||||||
assertThat(decoded.a).isEqualTo(4)
|
assertThat(decoded.a).isEqualTo(4)
|
||||||
assertThat(decoded.buf()).isEqualTo(ByteArray(2, { 0 }))
|
assertThat(decoded.buf()).isEqualTo(ByteArray(2) { 0 })
|
||||||
assertThat(decoded.count()).isEqualTo(0)
|
assertThat(decoded.count()).isEqualTo(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class ExtendsPlatformClassWithProtectedField(var a: Int) : ByteArrayOutputStream(2) {
|
internal class ExtendsPlatformClassWithProtectedField(var a: Int) : ByteArrayOutputStream(2) {
|
||||||
fun buf() = buf
|
fun buf(): ByteArray = buf
|
||||||
fun count() = count
|
fun count(): Int = count
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test fun platformTypeThrows() {
|
@Test fun platformTypeThrows() {
|
||||||
|
@@ -49,7 +49,7 @@ final class JsonUtf8Writer extends JsonWriter {
|
|||||||
static {
|
static {
|
||||||
REPLACEMENT_CHARS = new String[128];
|
REPLACEMENT_CHARS = new String[128];
|
||||||
for (int i = 0; i <= 0x1f; i++) {
|
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['"'] = "\\\"";
|
||||||
REPLACEMENT_CHARS['\\'] = "\\\\";
|
REPLACEMENT_CHARS['\\'] = "\\\\";
|
||||||
|
@@ -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;
|
Node<K, V>[] table;
|
||||||
final Node<K, V> header;
|
final Node<K, V> header;
|
||||||
int size = 0;
|
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 rightLeftHeight = rightLeft != null ? rightLeft.height : 0;
|
||||||
|
|
||||||
int rightDelta = rightLeftHeight - rightRightHeight;
|
int rightDelta = rightLeftHeight - rightRightHeight;
|
||||||
if (rightDelta == -1 || (rightDelta == 0 && !insert)) {
|
if (rightDelta != -1 && (rightDelta != 0 || insert)) {
|
||||||
rotateLeft(node); // AVL right right
|
|
||||||
} else {
|
|
||||||
assert (rightDelta == 1);
|
assert (rightDelta == 1);
|
||||||
rotateRight(right); // AVL right left
|
rotateRight(right); // AVL right left
|
||||||
rotateLeft(node);
|
|
||||||
}
|
}
|
||||||
|
rotateLeft(node); // AVL right right
|
||||||
if (insert) {
|
if (insert) {
|
||||||
break; // no further rotations will be necessary
|
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 leftLeftHeight = leftLeft != null ? leftLeft.height : 0;
|
||||||
|
|
||||||
int leftDelta = leftLeftHeight - leftRightHeight;
|
int leftDelta = leftLeftHeight - leftRightHeight;
|
||||||
if (leftDelta == 1 || (leftDelta == 0 && !insert)) {
|
if (leftDelta != 1 && (leftDelta != 0 || insert)) {
|
||||||
rotateRight(node); // AVL left left
|
|
||||||
} else {
|
|
||||||
assert (leftDelta == -1);
|
assert (leftDelta == -1);
|
||||||
rotateLeft(left); // AVL left right
|
rotateLeft(left); // AVL left right
|
||||||
rotateRight(node);
|
|
||||||
}
|
}
|
||||||
|
rotateRight(node); // AVL left left
|
||||||
if (insert) {
|
if (insert) {
|
||||||
break; // no further rotations will be necessary
|
break; // no further rotations will be necessary
|
||||||
}
|
}
|
||||||
|
@@ -184,7 +184,7 @@ public final class Moshi {
|
|||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
public Moshi.Builder newBuilder() {
|
public Moshi.Builder newBuilder() {
|
||||||
Builder result = new Builder();
|
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));
|
result.add(factories.get(i));
|
||||||
}
|
}
|
||||||
for (int i = lastOffset, limit = factories.size() - BUILT_IN_FACTORIES.size(); i < limit; i++) {
|
for (int i = lastOffset, limit = factories.size() - BUILT_IN_FACTORIES.size(); i < limit; i++) {
|
||||||
|
@@ -61,7 +61,7 @@ class KotlinExtensionsTest {
|
|||||||
fun addAdapterInferred() {
|
fun addAdapterInferred() {
|
||||||
// An adapter that always returns -1
|
// An adapter that always returns -1
|
||||||
val customIntdapter = object : JsonAdapter<Int>() {
|
val customIntdapter = object : JsonAdapter<Int>() {
|
||||||
override fun fromJson(reader: JsonReader): Int? {
|
override fun fromJson(reader: JsonReader): Int {
|
||||||
reader.skipValue()
|
reader.skipValue()
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ class KotlinExtensionsTest {
|
|||||||
fun addAdapterInferred_parameterized() {
|
fun addAdapterInferred_parameterized() {
|
||||||
// An adapter that always returns listOf(-1)
|
// An adapter that always returns listOf(-1)
|
||||||
val customIntListAdapter = object : JsonAdapter<List<Int>>() {
|
val customIntListAdapter = object : JsonAdapter<List<Int>>() {
|
||||||
override fun fromJson(reader: JsonReader): List<Int>? {
|
override fun fromJson(reader: JsonReader): List<Int> {
|
||||||
reader.skipValue()
|
reader.skipValue()
|
||||||
return listOf(-1)
|
return listOf(-1)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user