Skip to content

Commit

Permalink
Add support for nullable field
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Apr 21, 2017
1 parent de0748b commit 3efd75f
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 22 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.1</version>
<version>0.6.2</version>
<vendor email="[email protected]" url="https://github.com/nekocode/android-parcelable-intellij-plugin-kotlin">nekocode</vendor>

<description><![CDATA[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,12 @@ public NormalArraySerializer(ValueParameterDescriptor field) {
}

public String readValue() {
String typeProjection;
if(field.getType().toString().startsWith("Array")) {
typeProjection = field.getType().getArguments().get(0).getType().toString();
} else {
typeProjection = field.getType().toString();
typeProjection = typeProjection.substring(0, typeProjection.length()-5);
}
String typeProjection = field.getType().getArguments().get(0).getType().toString();
return "source.create" + typeProjection + "Array().toTypedArray()";
}

public String writeValue() {
String typeProjection;
if(field.getType().toString().startsWith("Array")) {
typeProjection = field.getType().getArguments().get(0).getType().toString();
} else {
typeProjection = field.getType().toString();
typeProjection = typeProjection.substring(0, typeProjection.length()-5);
}
String typeProjection= field.getType().getArguments().get(0).getType().toString();
return "dest?.write" + typeProjection + "Array(" + field.getName() + ".to" + typeProjection + "Array())";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2016 Nekocode (https://github.com/nekocode)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.nekocode.plugin.parcelablegenerator.typeserializers;

import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;

/**
* Created by nekocode on 2017/4/22.
*/
public class NullableArraySerializer extends TypeSerializer {

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 writeValue() {
return "dest?.writeArray(" + field.getName() + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2016 Nekocode (https://github.com/nekocode)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.nekocode.plugin.parcelablegenerator.typeserializers;

import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;

/**
* Created by nekocode on 2016/2/2.
*/
public class NullableStringSerializer extends TypeSerializer {

public NullableStringSerializer(ValueParameterDescriptor field) {
super(field);
}

public String readValue() {
return "source.readString()";
}

public String writeValue() {
return "dest?.writeString(" + field.getName() + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2016 Nekocode (https://github.com/nekocode)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.nekocode.plugin.parcelablegenerator.typeserializers;

import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;

/**
* Created by nekocode on 2017/4/22.
*/
public class NullableValueSerializer extends TypeSerializer {

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 writeValue() {
return "dest?.writeValue(" + field.getName() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,46 @@ public static List<TypeSerializer> createTypeSerializers(java.util.List<ValuePar
for(ValueParameterDescriptor field : fields) {
KotlinType type = field.getType();
String typeName = type.toString();
boolean isNullable = type.isMarkedNullable();
typeName = isNullable ? typeName.substring(0, typeName.length() - 1) : typeName;

if (typeName.equals("String") || typeName.equals("Byte") || typeName.equals("Double") ||
if (typeName.equals("Byte") || typeName.equals("Double") ||
typeName.equals("Float") || typeName.equals("Int") || typeName.equals("Long")) {
typeSerializers.add(new NormalSerializer(field));
typeSerializers.add(isNullable ?
new NullableValueSerializer(field) : new NormalSerializer(field));

} else if (typeName.equals("String")) {
typeSerializers.add(isNullable ?
new NullableStringSerializer(field) : new NormalSerializer(field));

} else if (typeName.equals("Boolean")) {
typeSerializers.add(new BooleanSerializer(field));
typeSerializers.add(isNullable ?
new NullableValueSerializer(field) : new BooleanSerializer(field));

} else if (typeName.equals("Char")) {
typeSerializers.add(new CharSerializer(field));
typeSerializers.add(isNullable ?
new NullableValueSerializer(field) : new CharSerializer(field));

} else if (typeName.equals("List<String>") || typeName.equals("ArrayList<String>") ||
typeName.equals("MutableList<String>")) {
typeSerializers.add(new StringListSerializer(field));

} else if (typeName.equals("Array<String>") || typeName.equals("ByteArray") || typeName.equals("DoubleArray") ||
typeName.equals("FloatArray") || typeName.equals("IntArray") || typeName.equals("LongArray") ||
typeName.equals("CharArray") || typeName.equals("BooleanArray")) {
} else if (typeName.equals("Array<String>") || typeName.equals("Array<String?>") ||
typeName.equals("ByteArray") || typeName.equals("DoubleArray") || typeName.equals("FloatArray") ||
typeName.equals("IntArray") || typeName.equals("LongArray") || typeName.equals("CharArray") ||
typeName.equals("BooleanArray")) {
typeSerializers.add(new OriginalArraySerializer(field));

} else if (typeName.equals("Array<Byte>") || typeName.equals("Array<Double>") || typeName.equals("Array<Float>") ||
typeName.equals("Array<Int>") || typeName.equals("Array<Long>") || typeName.equals("Array<Char>") ||
typeName.equals("Array<Boolean>")) {
typeSerializers.add(new NormalArraySerializer(field));

} else if (typeName.equals("Array<Byte?>") || typeName.equals("Array<Double?>") || typeName.equals("Array<Float?>") ||
typeName.equals("Array<Int?>") || typeName.equals("Array<Long?>") || typeName.equals("Array<Char?>") ||
typeName.equals("Array<Boolean?>")) {
typeSerializers.add(new NullableArraySerializer(field));

} else {
Collection<KotlinType> supertypes;

Expand Down

0 comments on commit 3efd75f

Please sign in to comment.