Skip to content

Commit 9b5c84e

Browse files
committed
Fix issues #12 #13
1 parent b50b6b3 commit 9b5c84e

File tree

5 files changed

+68
-19
lines changed

5 files changed

+68
-19
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.5.1</version>
4+
<version>0.6.0</version>
55
<vendor email="[email protected]" url="https://github.com/nekocode/android-parcelable-intellij-plugin-kotlin">nekocode</vendor>
66

77
<description><![CDATA[

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Just press ALT + Insert (or your equivalent keybinding for code generation) in y
2727
- Primitive Kotlin types: `String`, `Byte`, `Double`, `Float`, `Int`, `Long`, `Boolean`, `Char`
2828
- Kotlin Array types: `Array<String>`, `ByteArray`, `DoubleArray`, `FloatArray`, `IntArray`, `LongArray`, `CharArray`, `BooleanArray`
2929
- List of any objects **(Warning: validation is not performed)**
30+
- Enum type
3031

3132
## License
3233
```

src/cn/nekocode/plugin/parcelablegenerator/CodeGenerator.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
import cn.nekocode.plugin.parcelablegenerator.typeserializers.*;
1919
import com.intellij.psi.PsiElement;
2020
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
21+
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils;
22+
import org.jetbrains.kotlin.idea.util.ImportInsertHelper;
23+
import org.jetbrains.kotlin.name.FqName;
2124
import org.jetbrains.kotlin.psi.*;
2225
import org.jetbrains.kotlin.resolve.ImportPath;
2326

2427
import java.util.ArrayList;
2528
import java.util.List;
2629

27-
2830
/**
2931
* Created by nekocode on 2015/12/1.
3032
*/
@@ -87,17 +89,22 @@ private String generateWriteToParcel(List<TypeSerializer> typeSerializers) {
8789
return sb.toString();
8890
}
8991

92+
private void insertImport(KtFile ktFile, String fqName) {
93+
ImportInsertHelper.getInstance(ktFile.getProject())
94+
.importDescriptor(
95+
ktFile,
96+
ResolutionUtils.resolveImportReference(
97+
ktFile, new FqName(fqName)
98+
).iterator().next(),
99+
false
100+
);
101+
}
90102

91-
public void generate() {
92-
KtPsiFactory elementFactory = new KtPsiFactory(mClass.getProject());
93-
94-
KtFile parent = mClass.getContainingKtFile();
95-
103+
private void insertImports(KtFile ktFile) {
96104
// Check if already imported Parcel and Parcelable
97105
boolean importedParcelable = false;
98106
boolean importedParcel = false;
99-
boolean importedJavaUtil = false;
100-
List<KtImportDirective> importList = parent.getImportDirectives();
107+
List<KtImportDirective> importList = ktFile.getImportDirectives();
101108
for(KtImportDirective importDirective : importList) {
102109
ImportPath importPath = importDirective.getImportPath();
103110
if(importPath != null) {
@@ -108,22 +115,23 @@ public void generate() {
108115
if(pathStr.equals("android.os.Parcel")) {
109116
importedParcel = true;
110117
}
111-
if(pathStr.equals("java.util.*")) {
112-
importedJavaUtil = true;
113-
}
114118
}
115119
}
116120

117121
if(!importedParcelable) {
118-
parent.addAfter(elementFactory.createImportDirective(new ImportPath("android.os.Parcelable")), parent.getFirstChild());
122+
insertImport(ktFile, "android.os.Parcelable");
119123
}
120124
if(!importedParcel) {
121-
parent.addAfter(elementFactory.createImportDirective(new ImportPath("android.os.Parcel")), parent.getFirstChild());
122-
}
123-
if(!importedJavaUtil) {
124-
parent.addAfter(elementFactory.createImportDirective(new ImportPath("java.util.*")), parent.getFirstChild());
125+
insertImport(ktFile, "android.os.Parcel");
125126
}
127+
}
128+
129+
130+
public void generate() {
131+
KtPsiFactory elementFactory = new KtPsiFactory(mClass.getProject());
126132

133+
// Insert imports
134+
insertImports(mClass.getContainingKtFile());
127135

128136
// Save old declarations and clean Class Body
129137
List<KtDeclaration> oldDeclarations = new ArrayList<KtDeclaration>();
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 EnumSerializer extends TypeSerializer {
24+
25+
public EnumSerializer(ValueParameterDescriptor field) {
26+
super(field);
27+
}
28+
29+
public String readValue() {
30+
return field.getType() + ".values()[source.readInt()]";
31+
}
32+
33+
public String writeValue() {
34+
return "dest?.writeInt(" + field.getName() + ".ordinal)";
35+
}
36+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static List<TypeSerializer> createTypeSerializers(java.util.List<ValuePar
4545
} else {
4646
Collection<KotlinType> supertypes;
4747

48-
// Check if type is List or Array
48+
// Check whether type is List or Array
4949
if(typeName.startsWith("List") || typeName.startsWith("ArrayList") || typeName.startsWith("MutableList")) {
5050
KotlinType typeProjectionType = type.getArguments().get(0).getType();
5151

@@ -86,7 +86,7 @@ public static List<TypeSerializer> createTypeSerializers(java.util.List<ValuePar
8686

8787

8888
} else {
89-
// Check if supertype is Parcelable or Serializable
89+
// Check whether the type inherits from some known types
9090
boolean found = false;
9191
supertypes = type.getConstructor().getSupertypes();
9292
for(KotlinType supertype : supertypes) {
@@ -100,6 +100,10 @@ public static List<TypeSerializer> createTypeSerializers(java.util.List<ValuePar
100100
typeSerializers.add(new SerializableObjectSerializer(field));
101101
found = true;
102102
break;
103+
} else if (supertypeName.equals("Enum<" + typeName + ">")) {
104+
typeSerializers.add(new EnumSerializer(field));
105+
found = true;
106+
break;
103107
}
104108
}
105109

0 commit comments

Comments
 (0)