Skip to content

Commit

Permalink
Try to fix issue#2
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Aug 23, 2016
1 parent ccd3b90 commit 86badea
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 119 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ lib/*
.DS_Store
workdir/*
/*.jar
*.iml
4 changes: 2 additions & 2 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<idea-plugin version="2">
<id>cn.nekocode.plugin.parcelablegenerator</id>
<name>Parcelable Code Generator(for kotlin's data class)</name>
<version>0.4.6</version>
<version>0.4.7</version>
<vendor email="[email protected]" url="https://github.com/nekocode/android-parcelable-intellij-plugin-kotlin">nekocode</vendor>

<description><![CDATA[
<a href="https://github.com/nekocode/android-parcelable-intellij-plugin-kotlin">Parcelable Code Generator</a> is a plugin which generates Android Parcelable boilerplate code for <strong>kotlin's data class</strong>.</br>
]]></description>

<!-- please see https://confluence.jetbrains.com/display/IDEADEV/Build+Number+Ranges for description -->
<idea-version since-build="131"/>
<idea-version since-build="123.72"/>

<!-- please see https://confluence.jetbrains.com/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products
on how to target different products -->
Expand Down
13 changes: 0 additions & 13 deletions android-parcelable-generator-kotlin.iml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void generate() {


// Save old declarations and clean Class Body
List<KtDeclaration> oldDeclarations = new ArrayList<>();
List<KtDeclaration> oldDeclarations = new ArrayList<KtDeclaration>();
KtClassBody body = mClass.getBody();

if(body != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected GenerateDialog(KtClass ktClass) {
super(ktClass.getProject());
setTitle("Select Fields for Parcelable Generation");

myFileds = new CollectionListModel<>(KtClassHelper.findParams(ktClass));
myFileds = new CollectionListModel<ValueParameterDescriptor>(KtClassHelper.findParams(ktClass));

JBList fieldList = new JBList(myFileds);
fieldList.setCellRenderer(new DefaultListCellRenderer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,122 +13,101 @@
public class TypeSerializerFactory {

public static List<TypeSerializer> createTypeSerializers(java.util.List<ValueParameterDescriptor> fields) {
List<TypeSerializer> typeSerializers = new ArrayList<>();
List<TypeSerializer> typeSerializers = new ArrayList<TypeSerializer>();
for(ValueParameterDescriptor field : fields) {
KotlinType type = field.getType();
String typeName = type.toString();

switch (typeName) {
case "String":
case "Byte":
case "Double":
case "Float":
case "Int":
case "Long":
typeSerializers.add(new NormalSerializer(field));
break;

case "Boolean":
typeSerializers.add(new BooleanSerializer(field));
break;

case "Char":
typeSerializers.add(new CharSerializer(field));
break;

case "List<String>":
case "ArrayList<String>":
case "MutableList<String>":
typeSerializers.add(new StringListSerializer(field));
break;

case "Array<String>":
case "ByteArray":
case "DoubleArray":
case "FloatArray":
case "IntArray":
case "LongArray":
case "CharArray":
case "BooleanArray":
typeSerializers.add(new OriginalArraySerializer(field));
break;

case "Array<Byte>":
case "Array<Double>":
case "Array<Float>":
case "Array<Int>":
case "Array<Long>":
case "Array<Char>":
case "Array<Boolean>":
typeSerializers.add(new NormalArraySerializer(field));
break;

default:
Collection<KotlinType> supertypes;

// Check if type is List or Array
if(typeName.startsWith("List") || typeName.startsWith("ArrayList") || typeName.startsWith("MutableList")) {
KotlinType typeProjectionType = type.getArguments().get(0).getType();

Boolean isParcelable = false;
supertypes = typeProjectionType.getConstructor().getSupertypes();
for(KotlinType supertype : supertypes) {
String supertypeName = supertype.toString();
if(supertypeName.equals("Parcelable")) {
typeSerializers.add(new ParcelableListSerializer(field));
isParcelable = true;
break;
}
if (typeName.equals("String") || typeName.equals("Byte") || typeName.equals("Double") ||
typeName.equals("Float") || typeName.equals("Int") || typeName.equals("Long")) {
typeSerializers.add(new NormalSerializer(field));

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

} else if (typeName.equals("Char")) {
typeSerializers.add(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")) {
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 {
Collection<KotlinType> supertypes;

// Check if type is List or Array
if(typeName.startsWith("List") || typeName.startsWith("ArrayList") || typeName.startsWith("MutableList")) {
KotlinType typeProjectionType = type.getArguments().get(0).getType();

Boolean isParcelable = false;
supertypes = typeProjectionType.getConstructor().getSupertypes();
for(KotlinType supertype : supertypes) {
String supertypeName = supertype.toString();
if(supertypeName.equals("Parcelable")) {
typeSerializers.add(new ParcelableListSerializer(field));
isParcelable = true;
break;
}
}

if(!isParcelable) {
typeSerializers.add(new NormalListSerializer(field));
}
if(!isParcelable) {
typeSerializers.add(new NormalListSerializer(field));
}


} else if(typeName.startsWith("Array")) {
KotlinType typeProjectionType = type.getArguments().get(0).getType();
} else if(typeName.startsWith("Array")) {
KotlinType typeProjectionType = type.getArguments().get(0).getType();

boolean found = false;
supertypes = typeProjectionType.getConstructor().getSupertypes();
for(KotlinType supertype : supertypes) {
String supertypeName = supertype.toString();
if(supertypeName.equals("Parcelable")) {
typeSerializers.add(new ParcelableArraySerializer(field));
found = true;
break;
}
boolean found = false;
supertypes = typeProjectionType.getConstructor().getSupertypes();
for(KotlinType supertype : supertypes) {
String supertypeName = supertype.toString();
if(supertypeName.equals("Parcelable")) {
typeSerializers.add(new ParcelableArraySerializer(field));
found = true;
break;
}
}

// Not found
if(!found) {
typeSerializers.add(new NormalSerializer(field));
}
// Not found
if(!found) {
typeSerializers.add(new NormalSerializer(field));
}


} else {
// Check if supertype is Parcelable or Serializable
boolean found = false;
supertypes = type.getConstructor().getSupertypes();
for(KotlinType supertype : supertypes) {
String supertypeName = supertype.toString();
if(supertypeName.equals("Parcelable")) {
typeSerializers.add(new ParcelableObjectSerializer(field));
found = true;
break;

} else if(supertypeName.equals("Serializable")) {
typeSerializers.add(new SerializableObjectSerializer(field));
found = true;
break;
}
} else {
// Check if supertype is Parcelable or Serializable
boolean found = false;
supertypes = type.getConstructor().getSupertypes();
for(KotlinType supertype : supertypes) {
String supertypeName = supertype.toString();
if(supertypeName.equals("Parcelable")) {
typeSerializers.add(new ParcelableObjectSerializer(field));
found = true;
break;

} else if(supertypeName.equals("Serializable")) {
typeSerializers.add(new SerializableObjectSerializer(field));
found = true;
break;
}
}

// Not found
if(!found) {
typeSerializers.add(new NormalSerializer(field));
}
// Not found
if(!found) {
typeSerializers.add(new NormalSerializer(field));
}
}
}
}
return typeSerializers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
*/
public class KtClassHelper {
public static List<ValueParameterDescriptor> findParams(KtClass ktClass) {
List<KtElement> list = new ArrayList<>();
List<KtElement> list = new ArrayList<KtElement>();
list.add(ktClass);

ResolveSession resolveSession = KotlinCacheService.Companion.getInstance(ktClass.getProject()).
getResolutionFacade(list).getFrontendService(ResolveSession.class);
ClassDescriptor classDescriptor = resolveSession.getClassDescriptor(ktClass, NoLookupLocation.FROM_IDE);

List<ValueParameterDescriptor> valueParameters = new ArrayList<>();
List<ValueParameterDescriptor> valueParameters = new ArrayList<ValueParameterDescriptor>();
if (classDescriptor.isData()) {
ConstructorDescriptor constructorDescriptor = classDescriptor.getUnsubstitutedPrimaryConstructor();

Expand Down

0 comments on commit 86badea

Please sign in to comment.