Skip to content

Commit

Permalink
Fix issues #12 #13
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Nov 9, 2016
1 parent b50b6b3 commit 9b5c84e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 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.5.1</version>
<version>0.6.0</version>
<vendor email="[email protected]" url="https://github.com/nekocode/android-parcelable-intellij-plugin-kotlin">nekocode</vendor>

<description><![CDATA[
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Just press ALT + Insert (or your equivalent keybinding for code generation) in y
- Primitive Kotlin types: `String`, `Byte`, `Double`, `Float`, `Int`, `Long`, `Boolean`, `Char`
- Kotlin Array types: `Array<String>`, `ByteArray`, `DoubleArray`, `FloatArray`, `IntArray`, `LongArray`, `CharArray`, `BooleanArray`
- List of any objects **(Warning: validation is not performed)**
- Enum type

## License
```
Expand Down
40 changes: 24 additions & 16 deletions src/cn/nekocode/plugin/parcelablegenerator/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -87,17 +89,22 @@ private String generateWriteToParcel(List<TypeSerializer> 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<KtImportDirective> importList = parent.getImportDirectives();
List<KtImportDirective> importList = ktFile.getImportDirectives();
for(KtImportDirective importDirective : importList) {
ImportPath importPath = importDirective.getImportPath();
if(importPath != null) {
Expand All @@ -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<KtDeclaration> oldDeclarations = new ArrayList<KtDeclaration>();
Expand Down
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 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)";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static List<TypeSerializer> createTypeSerializers(java.util.List<ValuePar
} else {
Collection<KotlinType> 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();

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


} else {
// Check if supertype is Parcelable or Serializable
// Check whether the type inherits from some known types
boolean found = false;
supertypes = type.getConstructor().getSupertypes();
for(KotlinType supertype : supertypes) {
Expand All @@ -100,6 +100,10 @@ public static List<TypeSerializer> createTypeSerializers(java.util.List<ValuePar
typeSerializers.add(new SerializableObjectSerializer(field));
found = true;
break;
} else if (supertypeName.equals("Enum<" + typeName + ">")) {
typeSerializers.add(new EnumSerializer(field));
found = true;
break;
}
}

Expand Down

0 comments on commit 9b5c84e

Please sign in to comment.