diff --git a/META-INF/plugin.xml b/META-INF/plugin.xml index 4a2fe25..7b374f7 100644 --- a/META-INF/plugin.xml +++ b/META-INF/plugin.xml @@ -1,7 +1,7 @@ cn.nekocode.plugin.parcelablegenerator Parcelable Code Generator(for kotlin) - 0.6.2 + 0.7.0 nekocode typeSerializers) { StringBuilder sb = new StringBuilder("constructor(source: Parcel) : this("); String content = ""; for(TypeSerializer typeSerializer : typeSerializers) { - content += typeSerializer.readValue() + ","; + content += "\n" + typeSerializer.generateReadValue() + ","; } if(content.length() > 0) { content = content.substring(0, content.length() - 1); } - sb.append(content).append(")"); + sb.append(content).append("\n)"); return sb.toString(); } @@ -82,7 +82,7 @@ private String generateDescribeContents() { private String generateWriteToParcel(List typeSerializers) { StringBuilder sb = new StringBuilder("override fun writeToParcel(dest: Parcel?, flags: Int) {"); for(TypeSerializer typeSerializer : typeSerializers) { - sb.append(typeSerializer.writeValue()).append("\n"); + sb.append(typeSerializer.generateWriteValue()).append("\n"); } sb.append("}"); diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/BooleanSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/BooleanSerializer.java index 787ab16..6abe264 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/BooleanSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/BooleanSerializer.java @@ -26,11 +26,11 @@ public BooleanSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { + public String generateReadValue() { return "1.equals(source.readInt())"; } - public String writeValue() { - return "dest?.writeInt((if(" + field.getName() + ") 1 else 0))"; + public String generateWriteValue() { + return "dest?.writeInt((if(" + getFieldName() + ") 1 else 0))"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/CharSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/CharSerializer.java index a0871e9..e6c9958 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/CharSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/CharSerializer.java @@ -26,11 +26,11 @@ public CharSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { + public String generateReadValue() { return "source.readByte().toChar()"; } - public String writeValue() { - return "dest?.writeByte(" + field.getName() + ".toByte())"; + public String generateWriteValue() { + return "dest?.writeByte(" + getFieldName() + ".toByte())"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/EnumSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/EnumSerializer.java index 88b4f9c..6f8243f 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/EnumSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/EnumSerializer.java @@ -26,11 +26,19 @@ public EnumSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - return field.getType() + ".values()[source.readInt()]"; + public String generateReadValue() { + if (!isTypeNullable()) { + return getNoneNullType() + ".values()[source.readInt()]"; + } else { + return "source.readValue(Int::class.java.classLoader)?.let { Enum.values()[it as Int] }"; + } } - public String writeValue() { - return "dest?.writeInt(" + field.getName() + ".ordinal)"; + public String generateWriteValue() { + if (!isTypeNullable()) { + return "dest?.writeInt(" + getFieldName() + ".ordinal)"; + } else { + return "dest?.writeValue(" + getFieldName() + (isTypeNullable() ? "?" : "") + ".ordinal)"; + } } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalArraySerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalArraySerializer.java index a4cd5d3..e3e5f62 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalArraySerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalArraySerializer.java @@ -26,13 +26,11 @@ public NormalArraySerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - String typeProjection = field.getType().getArguments().get(0).getType().toString(); - return "source.create" + typeProjection + "Array().toTypedArray()"; + public String generateReadValue() { + return "source.create" + getNoneNullProjectionType() + "Array().toTypedArray()"; } - public String writeValue() { - String typeProjection= field.getType().getArguments().get(0).getType().toString(); - return "dest?.write" + typeProjection + "Array(" + field.getName() + ".to" + typeProjection + "Array())"; + public String generateWriteValue() { + return "dest?.write" + getNoneNullProjectionType() + "Array(" + getFieldName() + ".to" + getNoneNullProjectionType() + "Array())"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalListSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalListSerializer.java index d9abbc6..b75df6d 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalListSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalListSerializer.java @@ -26,12 +26,11 @@ public NormalListSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - String typeProjection = field.getType().getArguments().get(0).getType().toString(); - return "ArrayList<" + typeProjection + ">().apply{ source.readList(this, " + typeProjection + "::class.java.classLoader) }"; + public String generateReadValue() { + return "ArrayList<" + getProjectionType() + ">().apply{ source.readList(this, " + getNoneNullProjectionType() + "::class.java.classLoader) }"; } - public String writeValue() { - return "dest?.writeList(" + field.getName() + ")"; + public String generateWriteValue() { + return "dest?.writeList(" + getFieldName() + ")"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalSerializer.java index 5e89c5a..32c6b15 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalSerializer.java @@ -26,11 +26,11 @@ public NormalSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - return "source.read" + field.getType() + "()"; + public String generateReadValue() { + return "source.read" + getNoneNullType() + "()"; } - public String writeValue() { - return "dest?.write" + field.getType() + "(" + field.getName() + ")"; + public String generateWriteValue() { + return "dest?.write" + getNoneNullType() + "(" + getFieldName() + ")"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableArraySerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableArraySerializer.java index c22fd91..df25394 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableArraySerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableArraySerializer.java @@ -26,13 +26,11 @@ public NullableArraySerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - String type = field.getType().toString(); - String typeProjection = field.getType().getArguments().get(0).getType().toString(); - return "source.readArray(" + typeProjection.substring(0, typeProjection.length() -1) + "::class.java.classLoader) as " + type; + public String generateReadValue() { + return "source.readArray(" + getNoneNullProjectionType() + "::class.java.classLoader) as " + getType(); } - public String writeValue() { - return "dest?.writeArray(" + field.getName() + ")"; + public String generateWriteValue() { + return "dest?.writeArray(" + getFieldName() + ")"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableStringSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableStringSerializer.java index e3b782e..f24d5ae 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableStringSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableStringSerializer.java @@ -26,11 +26,11 @@ public NullableStringSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { + public String generateReadValue() { return "source.readString()"; } - public String writeValue() { - return "dest?.writeString(" + field.getName() + ")"; + public String generateWriteValue() { + return "dest?.writeString(" + getFieldName() + ")"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableValueSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableValueSerializer.java index 34dd0d4..8607006 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableValueSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NullableValueSerializer.java @@ -26,12 +26,11 @@ public NullableValueSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - String type = field.getType().toString(); - return "source.readValue(" + type.substring(0, type.length() - 1) + "::class.java.classLoader) as " + type; + public String generateReadValue() { + return "source.readValue(" + getNoneNullType() + "::class.java.classLoader) as " + getType(); } - public String writeValue() { - return "dest?.writeValue(" + field.getName() + ")"; + public String generateWriteValue() { + return "dest?.writeValue(" + getFieldName() + ")"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/OriginalArraySerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/OriginalArraySerializer.java index 79747d8..1c2d10e 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/OriginalArraySerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/OriginalArraySerializer.java @@ -26,25 +26,19 @@ public OriginalArraySerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - String typeProjection; - if(field.getType().toString().startsWith("Array")) { - typeProjection = field.getType().getArguments().get(0).getType().toString(); + public String generateReadValue() { + if (getProjectionType() == null) { + return "source.create" + getNoneNullType() + "()"; } else { - typeProjection = field.getType().toString(); - typeProjection = typeProjection.substring(0, typeProjection.length()-5); + return "source.create" + getNoneNullProjectionType() + "Array()"; } - return "source.create" + typeProjection + "Array()"; } - public String writeValue() { - String typeProjection; - if(field.getType().toString().startsWith("Array")) { - typeProjection = field.getType().getArguments().get(0).getType().toString(); + public String generateWriteValue() { + if (getProjectionType() == null) { + return "dest?.write" + getNoneNullType() + "(" + getFieldName() + ")"; } else { - typeProjection = field.getType().toString(); - typeProjection = typeProjection.substring(0, typeProjection.length()-5); + return "dest?.write" + getNoneNullProjectionType() + "Array(" + getFieldName() + ")"; } - return "dest?.write" + typeProjection + "Array(" + field.getName() + ")"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableArraySerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableArraySerializer.java index dd17d80..3886d75 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableArraySerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableArraySerializer.java @@ -26,11 +26,11 @@ public ParcelableArraySerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - return "source.readParcelableArray(" + field.getType().getArguments().get(0).getType() + "::class.java.classLoader) as " + field.getType(); + public String generateReadValue() { + return "source.readParcelableArray(" + getNoneNullProjectionType() + "::class.java.classLoader) as " + getType(); } - public String writeValue() { - return "dest?.writeParcelableArray(" + field.getName() + ", 0)"; + public String generateWriteValue() { + return "dest?.writeParcelableArray(" + getFieldName()+ ", 0)"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableListSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableListSerializer.java index 4ac9d16..d25441d 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableListSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableListSerializer.java @@ -26,11 +26,11 @@ public ParcelableListSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - return "source.createTypedArrayList(" + field.getType().getArguments().get(0).getType() + ".CREATOR)"; + public String generateReadValue() { + return "source.createTypedArrayList(" + getNoneNullProjectionType() + ".CREATOR)"; } - public String writeValue() { - return "dest?.writeTypedList(" + field.getName() + ")"; + public String generateWriteValue() { + return "dest?.writeTypedList(" + getFieldName() + ")"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableObjectSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableObjectSerializer.java index b860a08..91f1928 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableObjectSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/ParcelableObjectSerializer.java @@ -26,11 +26,11 @@ public ParcelableObjectSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - return "source.readParcelable<" + field.getType() + ">(" + field.getType() + "::class.java.classLoader)"; + public String generateReadValue() { + return "source.readParcelable<" + getType() + ">(" + getNoneNullType() + "::class.java.classLoader)"; } - public String writeValue() { - return "dest?.writeParcelable(" + field.getName() + ", 0)"; + public String generateWriteValue() { + return "dest?.writeParcelable(" + getFieldName() + ", 0)"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/SerializableObjectSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/SerializableObjectSerializer.java index a7689de..1d7a126 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/SerializableObjectSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/SerializableObjectSerializer.java @@ -26,11 +26,11 @@ public SerializableObjectSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { - return "source.readSerializable() as " + field.getType(); + public String generateReadValue() { + return "source.readSerializable() as " + getType(); } - public String writeValue() { - return "dest?.writeSerializable(" + field.getName() + ")"; + public String generateWriteValue() { + return "dest?.writeSerializable(" + getFieldName() + ")"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/StringListSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/StringListSerializer.java index dbdbc8c..c3a2c5b 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/StringListSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/StringListSerializer.java @@ -26,11 +26,11 @@ public StringListSerializer(ValueParameterDescriptor field) { super(field); } - public String readValue() { + public String generateReadValue() { return "source.createStringArrayList()"; } - public String writeValue() { - return "dest?.writeStringList(" + field.getName() + ")"; + public String generateWriteValue() { + return "dest?.writeStringList(" + getFieldName() + ")"; } } diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/TypeSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/TypeSerializer.java index f6930e3..317c383 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/TypeSerializer.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/TypeSerializer.java @@ -21,13 +21,67 @@ * Created by nekocode on 2016/2/2. */ public abstract class TypeSerializer { - protected final ValueParameterDescriptor field; + private final ValueParameterDescriptor field; + private final String fieldName; + private final String type; + private final String noneNullType; + private final boolean isTypeNullable; + private final String projectionType; + private final boolean isProjectionTypeNullable; + private final String noneNullProjectionType; public TypeSerializer(ValueParameterDescriptor field) { this.field = field; + fieldName = field.getName().toString(); + type = field.getType().toString(); + isTypeNullable = type.endsWith("?"); + noneNullType = isTypeNullable ? type.substring(0, type.length() - 1) : type; + if (field.getType().getArguments().size() > 0) { + projectionType = field.getType().getArguments().get(0).getType().toString(); + isProjectionTypeNullable = projectionType.endsWith("?"); + noneNullProjectionType = isProjectionTypeNullable ? + projectionType.substring(0, projectionType.length() - 1) : projectionType; + + } else { + projectionType = null; + isProjectionTypeNullable = true; + noneNullProjectionType = null; + } } - abstract public String readValue(); + abstract public String generateReadValue(); + + abstract public String generateWriteValue(); - abstract public String writeValue(); + public ValueParameterDescriptor getField() { + return field; + } + + public String getFieldName() { + return fieldName; + } + + public String getType() { + return type; + } + + public String getNoneNullType() { + return noneNullType; + } + + public boolean isTypeNullable() { + return isTypeNullable; + } + + public String getProjectionType() { + return projectionType; + } + + public boolean isProjectionTypeNullable() { + return isProjectionTypeNullable; + } + + public String getNoneNullProjectionType() { + return noneNullProjectionType; + } }