Skip to content

Commit

Permalink
Fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Apr 23, 2017
1 parent 3efd75f commit bdbbb21
Show file tree
Hide file tree
Showing 18 changed files with 129 additions and 79 deletions.
2 changes: 1 addition & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2">
<id>cn.nekocode.plugin.parcelablegenerator</id>
<name>Parcelable Code Generator(for kotlin)</name>
<version>0.6.2</version>
<version>0.7.0</version>
<vendor email="[email protected]" url="https://github.com/nekocode/android-parcelable-intellij-plugin-kotlin">nekocode</vendor>

<description><![CDATA[
Expand Down
6 changes: 3 additions & 3 deletions src/cn/nekocode/plugin/parcelablegenerator/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ private String generateConstructor(List<TypeSerializer> 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();
}
Expand All @@ -82,7 +82,7 @@ private String generateDescribeContents() {
private String generateWriteToParcel(List<TypeSerializer> 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("}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit bdbbb21

Please sign in to comment.