From e0fec2d67bdde73ab5c2b5139e37abef44d795b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Marti=CC=81n?= Date: Mon, 12 Jan 2015 21:34:36 +0100 Subject: [PATCH] Version 1.2.1: Fixed bug with @InjectViews Uploaded better version of 'Person' class --- CHANGELOG.md | 1 + README.md | 2 +- SwissKnife/build.gradle | 2 +- .../InjectViewsTransformation.groovy | 58 +++++++++---------- gradle.properties | 2 +- .../groovy/com/dexafree/sample/Person.groovy | 38 ++---------- 6 files changed, 36 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a61286..2cacfd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog: ### Versions: +* **1.2.1** - Fixed bug on @InjectViews annotation and uploaded a new Person class version using @Parcelable. * **1.2.0** - Added @Parcelable annotation and DSL methods. * **1.1.4** - Fixed another minor bug with primitives. * **1.1.3** - Fixed minor bug where methods with primitive parameters weren't found on method search. diff --git a/README.md b/README.md index 77be04d..6f397e4 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ Once your project App Module is configured to use Groovy you can add this librar ```groovy dependencies { ... - compile 'com.arasthel:swissknife:1.2.0' + compile 'com.arasthel:swissknife:1.2.1' ... } ``` diff --git a/SwissKnife/build.gradle b/SwissKnife/build.gradle index 3cd7f7e..b5393c6 100644 --- a/SwissKnife/build.gradle +++ b/SwissKnife/build.gradle @@ -8,7 +8,7 @@ android { minSdkVersion 8 targetSdkVersion 21 versionCode 1 - versionName "1.2.0" + versionName "1.2.1" } packagingOptions { diff --git a/SwissKnife/src/main/groovy/com/arasthel/swissknife/annotations/InjectViewsTransformation.groovy b/SwissKnife/src/main/groovy/com/arasthel/swissknife/annotations/InjectViewsTransformation.groovy index a741998..8af5d83 100644 --- a/SwissKnife/src/main/groovy/com/arasthel/swissknife/annotations/InjectViewsTransformation.groovy +++ b/SwissKnife/src/main/groovy/com/arasthel/swissknife/annotations/InjectViewsTransformation.groovy @@ -7,6 +7,7 @@ import org.codehaus.groovy.ast.* import org.codehaus.groovy.ast.builder.AstBuilder import org.codehaus.groovy.ast.expr.ListExpression import org.codehaus.groovy.ast.stmt.BlockStatement +import org.codehaus.groovy.ast.stmt.ExpressionStatement import org.codehaus.groovy.ast.stmt.Statement import org.codehaus.groovy.control.CompilePhase import org.codehaus.groovy.control.SourceUnit @@ -29,7 +30,7 @@ public class InjectViewsTransformation implements ASTTransformation, Opcodes { def ids = []; - Class fieldClass = annotatedField.getType(); + ClassNode fieldClass = annotatedField.getType(); if(!AnnotationUtils.isSubtype(fieldClass, List.class)) { throw new Exception("The annotated field must extend List. Type: $fieldClass.name"); @@ -50,18 +51,20 @@ public class InjectViewsTransformation implements ASTTransformation, Opcodes { List statementList = ((BlockStatement) injectMethod.getCode()).getStatements(); - statementList.add(createViewListStatement()); + statementList.add(createViewListStatement(annotatedField)); - ids.each { statementList.add(createInjectViewStatement(it)); } + ids.each { statementList.add(createInjectViewStatement(annotatedField, it)); } - statementList.add(createFieldAssignStatement(annotatedField)); } - private Statement createViewListStatement() { + private Statement createViewListStatement(FieldNode field) { return new AstBuilder().buildFromSpec { expression{ - declaration { - variable "views" + binary { + property { + variable "this" + constant field.name + } token "=" constructorCall(ArrayList.class) { argumentList {} @@ -71,38 +74,31 @@ public class InjectViewsTransformation implements ASTTransformation, Opcodes { }[0]; } - private Statement createInjectViewStatement(String id) { + private Statement createInjectViewStatement(FieldNode field, String id) { + + ExpressionStatement injectStatement = AnnotationUtils.createInjectExpression(id) - def statement = + BlockStatement blockStatement = new BlockStatement() + + ExpressionStatement addToListExpression = new AstBuilder().buildFromSpec { - block { expression { - binary { - variable "views" - token "<<" - staticMethodCall(Finder.class, "findView") { - argumentList { - variable "view" - constant id - } + methodCall { + property { + variable "this" + constant field.name + } + constant "add" + argumentList { + variable "v" } } } - } }[0]; - return statement; - } + blockStatement.addStatement(injectStatement) + blockStatement.addStatement(addToListExpression) - private Statement createFieldAssignStatement(FieldNode field) { - return new AstBuilder().buildFromSpec { - expression { - binary { - variable field.name - token "=" - variable "views" - } - } - }[0] + return blockStatement; } } diff --git a/gradle.properties b/gradle.properties index 2628f21..14f5e29 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -VERSION_NAME=1.2.0 +VERSION_NAME=1.2.1 VERSION_CODE=11 GROUP=com.arasthel diff --git a/sample/src/main/groovy/com/dexafree/sample/Person.groovy b/sample/src/main/groovy/com/dexafree/sample/Person.groovy index e5c23ee..3e18be1 100644 --- a/sample/src/main/groovy/com/dexafree/sample/Person.groovy +++ b/sample/src/main/groovy/com/dexafree/sample/Person.groovy @@ -1,12 +1,11 @@ -package com.dexafree.sample; +package com.dexafree.sample - -import android.os.Parcel; -import android.os.Parcelable +import com.arasthel.swissknife.annotations.Parcelable; import groovy.transform.CompileStatic; - -public class Person implements Parcelable { +@Parcelable +@CompileStatic +public class Person { private String name; private int age; @@ -27,18 +26,6 @@ public class Person implements Parcelable { this.age = age; } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeString(this.name); - dest.writeInt(this.age); - } - public Person() { } @@ -46,19 +33,4 @@ public class Person implements Parcelable { this.name = name; this.age = age; } - - private Person(Parcel parcel) { - this.name = parcel.readString(); - this.age = parcel.readInt(); - } - - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - public Person createFromParcel(Parcel source) { - return new Person(source); - } - - public Person[] newArray(int size) { - return new Person[size]; - } - }; }