From c3945e8342ecf7bebaf5fe42fe57405e1ce412a0 Mon Sep 17 00:00:00 2001 From: Joan Zapata Date: Thu, 16 Feb 2012 23:36:25 +0100 Subject: [PATCH 1/5] Added static factory methods to ViewGroup subclasses --- .../processing/EViewGroupProcessor.java | 15 ++++++-- .../functional-test-1-5/AndroidManifest.xml | 7 ++++ .../test15/CustomFrameLayoutActivity.java | 34 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java diff --git a/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java b/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java index be340350d3..b6fe8891d3 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java @@ -17,6 +17,7 @@ import static com.sun.codemodel.JMod.PRIVATE; import static com.sun.codemodel.JMod.PUBLIC; +import static com.sun.codemodel.JMod.STATIC; import static javax.lang.model.element.ElementKind.CONSTRUCTOR; import java.lang.annotation.Annotation; @@ -42,6 +43,7 @@ import com.sun.codemodel.JBlock; import com.sun.codemodel.JClass; import com.sun.codemodel.JCodeModel; +import com.sun.codemodel.JDefinedClass; import com.sun.codemodel.JExpr; import com.sun.codemodel.JFieldRef; import com.sun.codemodel.JFieldVar; @@ -49,6 +51,7 @@ import com.sun.codemodel.JMethod; import com.sun.codemodel.JMod; import com.sun.codemodel.JType; +import com.sun.codemodel.JVar; public class EViewGroupProcessor extends AnnotationHelper implements ElementProcessor { @@ -144,7 +147,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo // finally onFinishInflate.body().invoke(JExpr._super(), "onFinishInflate"); - copyConstructors(element, holder, onFinishInflate); + copyConstructorsAndStaticHelpers(element, codeModel, holder, onFinishInflate); { // init if activity @@ -155,7 +158,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo } - private void copyConstructors(Element element, EBeanHolder holder, JMethod setContentViewMethod) { + private void copyConstructorsAndStaticHelpers(Element element, JCodeModel codeModel, EBeanHolder holder, JMethod setContentViewMethod) { List constructors = new ArrayList(); for (Element e : element.getEnclosedElements()) { if (e.getKind() == CONSTRUCTOR) { @@ -165,15 +168,23 @@ private void copyConstructors(Element element, EBeanHolder holder, JMethod setCo for (ExecutableElement userConstructor : constructors) { JMethod copyConstructor = holder.eBean.constructor(PUBLIC); + JClass superType = holder.refClass(element.asType().toString()); + JMethod staticHelper = holder.eBean.method(PUBLIC | STATIC, superType, "getInflatedInstance"); JBlock body = copyConstructor.body(); JInvocation superCall = body.invoke("super"); + JInvocation newInvocation = JExpr._new(holder.eBean); for (VariableElement param : userConstructor.getParameters()) { String paramName = param.getSimpleName().toString(); String paramType = param.asType().toString(); copyConstructor.param(holder.refClass(paramType), paramName); + staticHelper.param(holder.refClass(paramType), paramName); superCall.arg(JExpr.ref(paramName)); + newInvocation.arg(JExpr.ref(paramName)); } + JVar newCall = staticHelper.body().decl(superType, "instance", newInvocation); + staticHelper.body().invoke(newCall, "onFinishInflate"); + staticHelper.body()._return(newCall); body.invoke(holder.init); } } diff --git a/AndroidAnnotations/functional-test-1-5/AndroidManifest.xml b/AndroidAnnotations/functional-test-1-5/AndroidManifest.xml index af1c3ea19c..e1aec31be8 100644 --- a/AndroidAnnotations/functional-test-1-5/AndroidManifest.xml +++ b/AndroidAnnotations/functional-test-1-5/AndroidManifest.xml @@ -25,6 +25,13 @@ android:name="com.googlecode.androidannotations.test15.roboguice.SampleRoboApplication_" android:icon="@drawable/icon" android:label="@string/app_name" > + + + + + + + diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java new file mode 100644 index 0000000000..d71aa8041e --- /dev/null +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java @@ -0,0 +1,34 @@ +/** + * Copyright (C) 2010-2011 eBusiness Information, Excilys Group + * + * 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 com.googlecode.androidannotations.test15; + +import android.app.Activity; + +import com.googlecode.androidannotations.annotations.AfterViews; +import com.googlecode.androidannotations.annotations.EActivity; + +@EActivity(R.layout.main) +public class CustomFrameLayoutActivity extends Activity { + + private CustomFrameLayout layout; + + @AfterViews + public void afterViews(){ + layout = CustomFrameLayout_.getInflatedInstance(this, 2); + layout.tv.setText("Plop"); + } + +} From 0edd4c8f34aaf35294d30744703f2a7673478379 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ricau Date: Tue, 6 Mar 2012 11:16:21 +0100 Subject: [PATCH 2/5] Renaming factory method to build() --- .../androidannotations/processing/EViewGroupProcessor.java | 7 +++---- .../test15/CustomFrameLayoutActivity.java | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java b/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java index b6fe8891d3..a333129abe 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java @@ -43,7 +43,6 @@ import com.sun.codemodel.JBlock; import com.sun.codemodel.JClass; import com.sun.codemodel.JCodeModel; -import com.sun.codemodel.JDefinedClass; import com.sun.codemodel.JExpr; import com.sun.codemodel.JFieldRef; import com.sun.codemodel.JFieldVar; @@ -147,7 +146,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo // finally onFinishInflate.body().invoke(JExpr._super(), "onFinishInflate"); - copyConstructorsAndStaticHelpers(element, codeModel, holder, onFinishInflate); + copyConstructorsAndAddStaticBuilders(element, codeModel, holder, onFinishInflate); { // init if activity @@ -158,7 +157,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo } - private void copyConstructorsAndStaticHelpers(Element element, JCodeModel codeModel, EBeanHolder holder, JMethod setContentViewMethod) { + private void copyConstructorsAndAddStaticBuilders(Element element, JCodeModel codeModel, EBeanHolder holder, JMethod setContentViewMethod) { List constructors = new ArrayList(); for (Element e : element.getEnclosedElements()) { if (e.getKind() == CONSTRUCTOR) { @@ -169,7 +168,7 @@ private void copyConstructorsAndStaticHelpers(Element element, JCodeModel codeMo for (ExecutableElement userConstructor : constructors) { JMethod copyConstructor = holder.eBean.constructor(PUBLIC); JClass superType = holder.refClass(element.asType().toString()); - JMethod staticHelper = holder.eBean.method(PUBLIC | STATIC, superType, "getInflatedInstance"); + JMethod staticHelper = holder.eBean.method(PUBLIC | STATIC, superType, "build"); JBlock body = copyConstructor.body(); JInvocation superCall = body.invoke("super"); JInvocation newInvocation = JExpr._new(holder.eBean); diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java index d71aa8041e..39c5c4bfb4 100644 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java @@ -27,7 +27,7 @@ public class CustomFrameLayoutActivity extends Activity { @AfterViews public void afterViews(){ - layout = CustomFrameLayout_.getInflatedInstance(this, 2); + layout = CustomFrameLayout_.build(this, 2); layout.tv.setText("Plop"); } From 2d9822860e3bb21e82972c02b7ef6a6b3608c8ca Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ricau Date: Tue, 6 Mar 2012 11:26:27 +0100 Subject: [PATCH 3/5] moving @EViewGroup tests to dedicated package --- .../test15/{ => eviewgroup}/CustomFrameLayoutTest.java | 6 +++++- AndroidAnnotations/functional-test-1-5/AndroidManifest.xml | 7 ++----- .../test15/{ => eviewgroup}/CustomFrameLayout.java | 3 ++- .../test15/{ => eviewgroup}/CustomFrameLayoutActivity.java | 3 ++- 4 files changed, 11 insertions(+), 8 deletions(-) rename AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/{ => eviewgroup}/CustomFrameLayoutTest.java (79%) rename AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/{ => eviewgroup}/CustomFrameLayout.java (95%) rename AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/{ => eviewgroup}/CustomFrameLayoutActivity.java (90%) diff --git a/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/CustomFrameLayoutTest.java b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayoutTest.java similarity index 79% rename from AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/CustomFrameLayoutTest.java rename to AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayoutTest.java index 47b2c9e93f..eb2609a004 100644 --- a/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/CustomFrameLayoutTest.java +++ b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayoutTest.java @@ -13,13 +13,17 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.googlecode.androidannotations.test15; +package com.googlecode.androidannotations.test15.eviewgroup; import static org.fest.assertions.Assertions.assertThat; import org.junit.Test; import org.junit.runner.RunWith; +import com.googlecode.androidannotations.test15.AndroidAnnotationsTestRunner; +import com.googlecode.androidannotations.test15.EmptyActivityWithoutLayout; +import com.googlecode.androidannotations.test15.EmptyActivityWithoutLayout_; + @RunWith(AndroidAnnotationsTestRunner.class) public class CustomFrameLayoutTest { diff --git a/AndroidAnnotations/functional-test-1-5/AndroidManifest.xml b/AndroidAnnotations/functional-test-1-5/AndroidManifest.xml index e1aec31be8..8e4af7a3c3 100644 --- a/AndroidAnnotations/functional-test-1-5/AndroidManifest.xml +++ b/AndroidAnnotations/functional-test-1-5/AndroidManifest.xml @@ -1,6 +1,5 @@ - - + + @@ -67,7 +65,6 @@ - \ No newline at end of file diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayout.java b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayout.java similarity index 95% rename from AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayout.java rename to AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayout.java index 990a3d8f7d..9faec1aff2 100644 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayout.java +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayout.java @@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.googlecode.androidannotations.test15; +package com.googlecode.androidannotations.test15.eviewgroup; import android.content.Context; import android.util.AttributeSet; @@ -33,6 +33,7 @@ import com.googlecode.androidannotations.annotations.ViewById; import com.googlecode.androidannotations.annotations.res.AnimationRes; import com.googlecode.androidannotations.annotations.res.StringRes; +import com.googlecode.androidannotations.test15.R; @EViewGroup(R.layout.component) public class CustomFrameLayout extends FrameLayout { diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayoutActivity.java similarity index 90% rename from AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java rename to AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayoutActivity.java index 39c5c4bfb4..be99ab9f76 100644 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/CustomFrameLayoutActivity.java +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayoutActivity.java @@ -13,12 +13,13 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.googlecode.androidannotations.test15; +package com.googlecode.androidannotations.test15.eviewgroup; import android.app.Activity; import com.googlecode.androidannotations.annotations.AfterViews; import com.googlecode.androidannotations.annotations.EActivity; +import com.googlecode.androidannotations.test15.R; @EActivity(R.layout.main) public class CustomFrameLayoutActivity extends Activity { From cf77465113d1a0e0e9f4b456d28159b971f9d0ca Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ricau Date: Tue, 6 Mar 2012 11:43:13 +0100 Subject: [PATCH 4/5] factorizing code for @EView and @EViewGroup, fixing visibility problems with onFinishInflate --- .../helper/APTCodeModelHelper.java | 35 +++++++++++++++ .../processing/EViewGroupProcessor.java | 44 ++----------------- .../processing/EViewProcessor.java | 34 ++------------ .../eviewgroup/CustomFrameLayoutTest.java | 3 +- .../test15/eviewgroup/CustomFrameLayout.java | 5 --- 5 files changed, 44 insertions(+), 77 deletions(-) diff --git a/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/APTCodeModelHelper.java b/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/APTCodeModelHelper.java index dd45a13297..1f4dae7f55 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/APTCodeModelHelper.java +++ b/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/APTCodeModelHelper.java @@ -16,11 +16,15 @@ package com.googlecode.androidannotations.helper; import static com.sun.codemodel.JExpr.cast; +import static com.sun.codemodel.JMod.PUBLIC; +import static com.sun.codemodel.JMod.STATIC; +import static javax.lang.model.element.ElementKind.CONSTRUCTOR; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; +import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.VariableElement; import javax.lang.model.type.DeclaredType; @@ -210,4 +214,35 @@ public JBlock ifContextInstanceOfActivity(EBeanHolder holder, JBlock methodBody) return methodBody._if(holder.contextRef._instanceof(activityClass))._then(); } + + public void copyConstructorsAndAddStaticEViewBuilders(Element element, JCodeModel codeModel, JClass eBeanClass, EBeanHolder holder, JMethod setContentViewMethod) { + List constructors = new ArrayList(); + for (Element e : element.getEnclosedElements()) { + if (e.getKind() == CONSTRUCTOR) { + constructors.add((ExecutableElement) e); + } + } + + for (ExecutableElement userConstructor : constructors) { + JMethod copyConstructor = holder.eBean.constructor(PUBLIC); + JMethod staticHelper = holder.eBean.method(PUBLIC | STATIC, eBeanClass, "build"); + JBlock body = copyConstructor.body(); + JInvocation superCall = body.invoke("super"); + JInvocation newInvocation = JExpr._new(holder.eBean); + for (VariableElement param : userConstructor.getParameters()) { + String paramName = param.getSimpleName().toString(); + String paramType = param.asType().toString(); + copyConstructor.param(holder.refClass(paramType), paramName); + staticHelper.param(holder.refClass(paramType), paramName); + superCall.arg(JExpr.ref(paramName)); + newInvocation.arg(JExpr.ref(paramName)); + } + + JVar newCall = staticHelper.body().decl(holder.eBean, "instance", newInvocation); + staticHelper.body().invoke(newCall, "onFinishInflate"); + staticHelper.body()._return(newCall); + body.invoke(holder.init); + } + } + } diff --git a/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java b/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java index a333129abe..66499fd5fe 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewGroupProcessor.java @@ -17,19 +17,13 @@ import static com.sun.codemodel.JMod.PRIVATE; import static com.sun.codemodel.JMod.PUBLIC; -import static com.sun.codemodel.JMod.STATIC; -import static javax.lang.model.element.ElementKind.CONSTRUCTOR; import java.lang.annotation.Annotation; -import java.util.ArrayList; -import java.util.List; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.Element; -import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; -import javax.lang.model.element.VariableElement; import com.googlecode.androidannotations.annotations.EViewGroup; import com.googlecode.androidannotations.annotations.Id; @@ -46,11 +40,9 @@ import com.sun.codemodel.JExpr; import com.sun.codemodel.JFieldRef; import com.sun.codemodel.JFieldVar; -import com.sun.codemodel.JInvocation; import com.sun.codemodel.JMethod; import com.sun.codemodel.JMod; import com.sun.codemodel.JType; -import com.sun.codemodel.JVar; public class EViewGroupProcessor extends AnnotationHelper implements ElementProcessor { @@ -70,9 +62,12 @@ public class EViewGroupProcessor extends AnnotationHelper implements ElementProc private final IRClass rClass; + private final APTCodeModelHelper codeModelHelper; + public EViewGroupProcessor(ProcessingEnvironment processingEnv, IRClass rClass) { super(processingEnv); this.rClass = rClass; + codeModelHelper = new APTCodeModelHelper(); } @Override @@ -146,7 +141,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo // finally onFinishInflate.body().invoke(JExpr._super(), "onFinishInflate"); - copyConstructorsAndAddStaticBuilders(element, codeModel, holder, onFinishInflate); + codeModelHelper.copyConstructorsAndAddStaticEViewBuilders(element, codeModel, eBeanClass, holder, onFinishInflate); { // init if activity @@ -157,35 +152,4 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo } - private void copyConstructorsAndAddStaticBuilders(Element element, JCodeModel codeModel, EBeanHolder holder, JMethod setContentViewMethod) { - List constructors = new ArrayList(); - for (Element e : element.getEnclosedElements()) { - if (e.getKind() == CONSTRUCTOR) { - constructors.add((ExecutableElement) e); - } - } - - for (ExecutableElement userConstructor : constructors) { - JMethod copyConstructor = holder.eBean.constructor(PUBLIC); - JClass superType = holder.refClass(element.asType().toString()); - JMethod staticHelper = holder.eBean.method(PUBLIC | STATIC, superType, "build"); - JBlock body = copyConstructor.body(); - JInvocation superCall = body.invoke("super"); - JInvocation newInvocation = JExpr._new(holder.eBean); - for (VariableElement param : userConstructor.getParameters()) { - String paramName = param.getSimpleName().toString(); - String paramType = param.asType().toString(); - copyConstructor.param(holder.refClass(paramType), paramName); - staticHelper.param(holder.refClass(paramType), paramName); - superCall.arg(JExpr.ref(paramName)); - newInvocation.arg(JExpr.ref(paramName)); - } - - JVar newCall = staticHelper.body().decl(superType, "instance", newInvocation); - staticHelper.body().invoke(newCall, "onFinishInflate"); - staticHelper.body()._return(newCall); - body.invoke(holder.init); - } - } - } diff --git a/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewProcessor.java b/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewProcessor.java index 3da550b2f8..c4c8d3063d 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewProcessor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/EViewProcessor.java @@ -17,18 +17,13 @@ import static com.sun.codemodel.JMod.PRIVATE; import static com.sun.codemodel.JMod.PUBLIC; -import static javax.lang.model.element.ElementKind.CONSTRUCTOR; import java.lang.annotation.Annotation; -import java.util.ArrayList; -import java.util.List; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.Element; -import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; -import javax.lang.model.element.VariableElement; import com.googlecode.androidannotations.annotations.EView; import com.googlecode.androidannotations.helper.APTCodeModelHelper; @@ -40,7 +35,6 @@ import com.sun.codemodel.JCodeModel; import com.sun.codemodel.JExpr; import com.sun.codemodel.JFieldVar; -import com.sun.codemodel.JInvocation; import com.sun.codemodel.JMethod; import com.sun.codemodel.JMod; import com.sun.codemodel.JType; @@ -61,8 +55,11 @@ public class EViewProcessor extends AnnotationHelper implements ElementProcessor + "are in a View." // ; + private final APTCodeModelHelper codeModelHelper; + public EViewProcessor(ProcessingEnvironment processingEnv) { super(processingEnv); + codeModelHelper = new APTCodeModelHelper(); } @Override @@ -127,7 +124,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo // finally onFinishInflate.body().invoke(JExpr._super(), "onFinishInflate"); - copyConstructors(element, holder, onFinishInflate); + codeModelHelper.copyConstructorsAndAddStaticEViewBuilders(element, codeModel, eBeanClass, holder, onFinishInflate); { // init if activity @@ -138,27 +135,4 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo } - private void copyConstructors(Element element, EBeanHolder holder, JMethod setContentViewMethod) { - List constructors = new ArrayList(); - for (Element e : element.getEnclosedElements()) { - if (e.getKind() == CONSTRUCTOR) { - constructors.add((ExecutableElement) e); - } - } - - for (ExecutableElement userConstructor : constructors) { - JMethod copyConstructor = holder.eBean.constructor(PUBLIC); - JBlock body = copyConstructor.body(); - JInvocation superCall = body.invoke("super"); - for (VariableElement param : userConstructor.getParameters()) { - String paramName = param.getSimpleName().toString(); - String paramType = param.asType().toString(); - copyConstructor.param(holder.refClass(paramType), paramName); - superCall.arg(JExpr.ref(paramName)); - } - - body.invoke(holder.init); - } - } - } diff --git a/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayoutTest.java b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayoutTest.java index eb2609a004..172bff9852 100644 --- a/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayoutTest.java +++ b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayoutTest.java @@ -30,8 +30,7 @@ public class CustomFrameLayoutTest { @Test public void shouldHaveLayoutAfterCreate() { EmptyActivityWithoutLayout activity = new EmptyActivityWithoutLayout_(); - CustomFrameLayout component = new CustomFrameLayout_(activity, 0); - component.onFinishInflate(); + CustomFrameLayout component = CustomFrameLayout_.build(activity, 0); assertThat(component.subtitle).isNotNull(); assertThat(component.tv).isNotNull(); } diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayout.java b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayout.java index 9faec1aff2..aaa5988094 100644 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayout.java +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eviewgroup/CustomFrameLayout.java @@ -75,11 +75,6 @@ protected void titleLongClick() { protected void titleTouched(MotionEvent e) { } - @Override - protected void onFinishInflate() { - super.onFinishInflate(); - } - @Background protected void someBackgroundTask(){ } From 401a4b19eae77d1fb52db0ae8fcd189573d988f5 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ricau Date: Tue, 6 Mar 2012 11:56:22 +0100 Subject: [PATCH 5/5] Adding a bit more tests --- .../test15/eview/CustomButtonTest.java | 31 +++++++++++++++++++ .../test15/eview/CustomButton.java | 30 ++++++++++++------ 2 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eview/CustomButtonTest.java diff --git a/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eview/CustomButtonTest.java b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eview/CustomButtonTest.java new file mode 100644 index 0000000000..9d46b3d417 --- /dev/null +++ b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/com/googlecode/androidannotations/test15/eview/CustomButtonTest.java @@ -0,0 +1,31 @@ +package com.googlecode.androidannotations.test15.eview; + +import static org.fest.assertions.Assertions.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import android.content.Context; + +import com.googlecode.androidannotations.test15.AndroidAnnotationsTestRunner; +import com.googlecode.androidannotations.test15.EmptyActivityWithoutLayout_; + +@RunWith(AndroidAnnotationsTestRunner.class) +public class CustomButtonTest { + + @Test + public void constructor_parameters_are_transmitted_from_factory_method() { + Context context = new EmptyActivityWithoutLayout_(); + int parameter = 42; + CustomButton button = CustomButton_.build(context, parameter); + assertThat(button.constructorParameter).isEqualTo(parameter); + } + + @Test + public void factory_method_builds_inflated_instance() { + Context context = new EmptyActivityWithoutLayout_(); + CustomButton button = CustomButton_.build(context); + assertThat(button.afterViewsCalled).isTrue(); + } + +} diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eview/CustomButton.java b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eview/CustomButton.java index 38fd93f1a1..9da2ebe757 100644 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eview/CustomButton.java +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/com/googlecode/androidannotations/test15/eview/CustomButton.java @@ -33,30 +33,40 @@ public class CustomButton extends Button { @StringRes(R.string.app_name) - protected String res; - + String res; + @AnimationRes(R.anim.fadein) - protected Animation anim; + Animation anim; + + public boolean afterViewsCalled = false; + + public int constructorParameter; - public CustomButton(Context context, int i) { + public CustomButton(Context context) { super(context); } - + + public CustomButton(Context context, int constructorParameter) { + super(context); + this.constructorParameter = constructorParameter; + } + public CustomButton(Context context, AttributeSet attrs) { super(context, attrs); } @Trace @AfterViews - protected void afterViews(){ + void afterViews() { + afterViewsCalled = true; } - + @Background - protected void someBackgroundTask(){ + void someBackgroundTask() { } - + @UiThread - protected void someUIThreadTask(){ + void someUIThreadTask() { } }