Skip to content

Commit

Permalink
Version 1.2.1: Fixed bug with @InjectViews
Browse files Browse the repository at this point in the history
Uploaded better version of 'Person' class
  • Loading branch information
jmartinesp committed Jan 12, 2015
1 parent fa9d9a0 commit e0fec2d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 67 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
...
}
```
Expand Down
2 changes: 1 addition & 1 deletion SwissKnife/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName "1.2.0"
versionName "1.2.1"
}

packagingOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
Expand All @@ -50,18 +51,20 @@ public class InjectViewsTransformation implements ASTTransformation, Opcodes {

List<Statement> 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 {}
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 5 additions & 33 deletions sample/src/main/groovy/com/dexafree/sample/Person.groovy
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,38 +26,11 @@ 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() {
}

public Person(String name, int age){
this.name = name;
this.age = age;
}

private Person(Parcel parcel) {
this.name = parcel.readString();
this.age = parcel.readInt();
}

public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
public Person createFromParcel(Parcel source) {
return new Person(source);
}

public Person[] newArray(int size) {
return new Person[size];
}
};
}

0 comments on commit e0fec2d

Please sign in to comment.