diff --git a/META-INF/plugin.xml b/META-INF/plugin.xml index 958dbeb..577808f 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.5.1 + 0.6.0 nekocode `, `ByteArray`, `DoubleArray`, `FloatArray`, `IntArray`, `LongArray`, `CharArray`, `BooleanArray` - List of any objects **(Warning: validation is not performed)** +- Enum type ## License ``` diff --git a/src/cn/nekocode/plugin/parcelablegenerator/CodeGenerator.java b/src/cn/nekocode/plugin/parcelablegenerator/CodeGenerator.java index 6534c0d..5641fd3 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/CodeGenerator.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/CodeGenerator.java @@ -18,13 +18,15 @@ import cn.nekocode.plugin.parcelablegenerator.typeserializers.*; import com.intellij.psi.PsiElement; import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor; +import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils; +import org.jetbrains.kotlin.idea.util.ImportInsertHelper; +import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.resolve.ImportPath; import java.util.ArrayList; import java.util.List; - /** * Created by nekocode on 2015/12/1. */ @@ -87,17 +89,22 @@ private String generateWriteToParcel(List typeSerializers) { return sb.toString(); } + private void insertImport(KtFile ktFile, String fqName) { + ImportInsertHelper.getInstance(ktFile.getProject()) + .importDescriptor( + ktFile, + ResolutionUtils.resolveImportReference( + ktFile, new FqName(fqName) + ).iterator().next(), + false + ); + } - public void generate() { - KtPsiFactory elementFactory = new KtPsiFactory(mClass.getProject()); - - KtFile parent = mClass.getContainingKtFile(); - + private void insertImports(KtFile ktFile) { // Check if already imported Parcel and Parcelable boolean importedParcelable = false; boolean importedParcel = false; - boolean importedJavaUtil = false; - List importList = parent.getImportDirectives(); + List importList = ktFile.getImportDirectives(); for(KtImportDirective importDirective : importList) { ImportPath importPath = importDirective.getImportPath(); if(importPath != null) { @@ -108,22 +115,23 @@ public void generate() { if(pathStr.equals("android.os.Parcel")) { importedParcel = true; } - if(pathStr.equals("java.util.*")) { - importedJavaUtil = true; - } } } if(!importedParcelable) { - parent.addAfter(elementFactory.createImportDirective(new ImportPath("android.os.Parcelable")), parent.getFirstChild()); + insertImport(ktFile, "android.os.Parcelable"); } if(!importedParcel) { - parent.addAfter(elementFactory.createImportDirective(new ImportPath("android.os.Parcel")), parent.getFirstChild()); - } - if(!importedJavaUtil) { - parent.addAfter(elementFactory.createImportDirective(new ImportPath("java.util.*")), parent.getFirstChild()); + insertImport(ktFile, "android.os.Parcel"); } + } + + + public void generate() { + KtPsiFactory elementFactory = new KtPsiFactory(mClass.getProject()); + // Insert imports + insertImports(mClass.getContainingKtFile()); // Save old declarations and clean Class Body List oldDeclarations = new ArrayList(); diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/EnumSerializer.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/EnumSerializer.java new file mode 100644 index 0000000..88b4f9c --- /dev/null +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/EnumSerializer.java @@ -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 EnumSerializer extends TypeSerializer { + + public EnumSerializer(ValueParameterDescriptor field) { + super(field); + } + + public String readValue() { + return field.getType() + ".values()[source.readInt()]"; + } + + public String writeValue() { + return "dest?.writeInt(" + field.getName() + ".ordinal)"; + } +} diff --git a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/TypeSerializerFactory.java b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/TypeSerializerFactory.java index b8915ed..a55587b 100644 --- a/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/TypeSerializerFactory.java +++ b/src/cn/nekocode/plugin/parcelablegenerator/typeserializers/TypeSerializerFactory.java @@ -45,7 +45,7 @@ public static List createTypeSerializers(java.util.List supertypes; - // Check if type is List or Array + // Check whether type is List or Array if(typeName.startsWith("List") || typeName.startsWith("ArrayList") || typeName.startsWith("MutableList")) { KotlinType typeProjectionType = type.getArguments().get(0).getType(); @@ -86,7 +86,7 @@ public static List createTypeSerializers(java.util.List createTypeSerializers(java.util.List")) { + typeSerializers.add(new EnumSerializer(field)); + found = true; + break; } }