Skip to content

Commit 3efd75f

Browse files
committed
Add support for nullable field
1 parent de0748b commit 3efd75f

File tree

6 files changed

+136
-22
lines changed

6 files changed

+136
-22
lines changed

META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<idea-plugin version="2">
22
<id>cn.nekocode.plugin.parcelablegenerator</id>
33
<name>Parcelable Code Generator(for kotlin)</name>
4-
<version>0.6.1</version>
4+
<version>0.6.2</version>
55
<vendor email="[email protected]" url="https://github.com/nekocode/android-parcelable-intellij-plugin-kotlin">nekocode</vendor>
66

77
<description><![CDATA[

src/cn/nekocode/plugin/parcelablegenerator/typeserializers/NormalArraySerializer.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,12 @@ public NormalArraySerializer(ValueParameterDescriptor field) {
2727
}
2828

2929
public String readValue() {
30-
String typeProjection;
31-
if(field.getType().toString().startsWith("Array")) {
32-
typeProjection = field.getType().getArguments().get(0).getType().toString();
33-
} else {
34-
typeProjection = field.getType().toString();
35-
typeProjection = typeProjection.substring(0, typeProjection.length()-5);
36-
}
30+
String typeProjection = field.getType().getArguments().get(0).getType().toString();
3731
return "source.create" + typeProjection + "Array().toTypedArray()";
3832
}
3933

4034
public String writeValue() {
41-
String typeProjection;
42-
if(field.getType().toString().startsWith("Array")) {
43-
typeProjection = field.getType().getArguments().get(0).getType().toString();
44-
} else {
45-
typeProjection = field.getType().toString();
46-
typeProjection = typeProjection.substring(0, typeProjection.length()-5);
47-
}
35+
String typeProjection= field.getType().getArguments().get(0).getType().toString();
4836
return "dest?.write" + typeProjection + "Array(" + field.getName() + ".to" + typeProjection + "Array())";
4937
}
5038
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2016 Nekocode (https://github.com/nekocode)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package cn.nekocode.plugin.parcelablegenerator.typeserializers;
17+
18+
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
19+
20+
/**
21+
* Created by nekocode on 2017/4/22.
22+
*/
23+
public class NullableArraySerializer extends TypeSerializer {
24+
25+
public NullableArraySerializer(ValueParameterDescriptor field) {
26+
super(field);
27+
}
28+
29+
public String readValue() {
30+
String type = field.getType().toString();
31+
String typeProjection = field.getType().getArguments().get(0).getType().toString();
32+
return "source.readArray(" + typeProjection.substring(0, typeProjection.length() -1) + "::class.java.classLoader) as " + type;
33+
}
34+
35+
public String writeValue() {
36+
return "dest?.writeArray(" + field.getName() + ")";
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2016 Nekocode (https://github.com/nekocode)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package cn.nekocode.plugin.parcelablegenerator.typeserializers;
17+
18+
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
19+
20+
/**
21+
* Created by nekocode on 2016/2/2.
22+
*/
23+
public class NullableStringSerializer extends TypeSerializer {
24+
25+
public NullableStringSerializer(ValueParameterDescriptor field) {
26+
super(field);
27+
}
28+
29+
public String readValue() {
30+
return "source.readString()";
31+
}
32+
33+
public String writeValue() {
34+
return "dest?.writeString(" + field.getName() + ")";
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2016 Nekocode (https://github.com/nekocode)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package cn.nekocode.plugin.parcelablegenerator.typeserializers;
17+
18+
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
19+
20+
/**
21+
* Created by nekocode on 2017/4/22.
22+
*/
23+
public class NullableValueSerializer extends TypeSerializer {
24+
25+
public NullableValueSerializer(ValueParameterDescriptor field) {
26+
super(field);
27+
}
28+
29+
public String readValue() {
30+
String type = field.getType().toString();
31+
return "source.readValue(" + type.substring(0, type.length() - 1) + "::class.java.classLoader) as " + type;
32+
}
33+
34+
public String writeValue() {
35+
return "dest?.writeValue(" + field.getName() + ")";
36+
}
37+
}

src/cn/nekocode/plugin/parcelablegenerator/typeserializers/TypeSerializerFactory.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,46 @@ public static List<TypeSerializer> createTypeSerializers(java.util.List<ValuePar
1717
for(ValueParameterDescriptor field : fields) {
1818
KotlinType type = field.getType();
1919
String typeName = type.toString();
20+
boolean isNullable = type.isMarkedNullable();
21+
typeName = isNullable ? typeName.substring(0, typeName.length() - 1) : typeName;
2022

21-
if (typeName.equals("String") || typeName.equals("Byte") || typeName.equals("Double") ||
23+
if (typeName.equals("Byte") || typeName.equals("Double") ||
2224
typeName.equals("Float") || typeName.equals("Int") || typeName.equals("Long")) {
23-
typeSerializers.add(new NormalSerializer(field));
25+
typeSerializers.add(isNullable ?
26+
new NullableValueSerializer(field) : new NormalSerializer(field));
27+
28+
} else if (typeName.equals("String")) {
29+
typeSerializers.add(isNullable ?
30+
new NullableStringSerializer(field) : new NormalSerializer(field));
2431

2532
} else if (typeName.equals("Boolean")) {
26-
typeSerializers.add(new BooleanSerializer(field));
33+
typeSerializers.add(isNullable ?
34+
new NullableValueSerializer(field) : new BooleanSerializer(field));
2735

2836
} else if (typeName.equals("Char")) {
29-
typeSerializers.add(new CharSerializer(field));
37+
typeSerializers.add(isNullable ?
38+
new NullableValueSerializer(field) : new CharSerializer(field));
3039

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

35-
} else if (typeName.equals("Array<String>") || typeName.equals("ByteArray") || typeName.equals("DoubleArray") ||
36-
typeName.equals("FloatArray") || typeName.equals("IntArray") || typeName.equals("LongArray") ||
37-
typeName.equals("CharArray") || typeName.equals("BooleanArray")) {
44+
} else if (typeName.equals("Array<String>") || typeName.equals("Array<String?>") ||
45+
typeName.equals("ByteArray") || typeName.equals("DoubleArray") || typeName.equals("FloatArray") ||
46+
typeName.equals("IntArray") || typeName.equals("LongArray") || typeName.equals("CharArray") ||
47+
typeName.equals("BooleanArray")) {
3848
typeSerializers.add(new OriginalArraySerializer(field));
3949

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

55+
} else if (typeName.equals("Array<Byte?>") || typeName.equals("Array<Double?>") || typeName.equals("Array<Float?>") ||
56+
typeName.equals("Array<Int?>") || typeName.equals("Array<Long?>") || typeName.equals("Array<Char?>") ||
57+
typeName.equals("Array<Boolean?>")) {
58+
typeSerializers.add(new NullableArraySerializer(field));
59+
4560
} else {
4661
Collection<KotlinType> supertypes;
4762

0 commit comments

Comments
 (0)