Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ExecutableElement> constructors = new ArrayList<ExecutableElement>();
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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.EViewGroup;
import com.googlecode.androidannotations.annotations.Id;
Expand All @@ -45,7 +40,6 @@
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;
Expand All @@ -68,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
Expand Down Expand Up @@ -144,7 +141,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
Expand All @@ -155,27 +152,4 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo

}

private void copyConstructors(Element element, EBeanHolder holder, JMethod setContentViewMethod) {
List<ExecutableElement> constructors = new ArrayList<ExecutableElement>();
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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -138,27 +135,4 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo

}

private void copyConstructors(Element element, EBeanHolder holder, JMethod setContentViewMethod) {
List<ExecutableElement> constructors = new ArrayList<ExecutableElement>();
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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@
* 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 {

@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();
}
Expand Down
10 changes: 7 additions & 3 deletions AndroidAnnotations/functional-test-1-5/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!--

Copyright (C) 2010-2011 eBusiness Information, Excilys Group

Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand All @@ -14,7 +13,6 @@
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.

-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.googlecode.androidannotations.test15"
Expand All @@ -25,6 +23,13 @@
android:name="com.googlecode.androidannotations.test15.roboguice.SampleRoboApplication_"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity android:name="com.googlecode.androidannotations.test15.eviewgroup.CustomFrameLayoutActivity_" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".EmptyActivityWithLayout_" />
<activity android:name=".EmptyActivityWithoutLayout_" />
<activity android:name=".ViewsInjectedActivity_" />
Expand Down Expand Up @@ -60,7 +65,6 @@
<service
android:name="com.googlecode.androidannotations.test15.eservice.MyService_"
exported="false" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -74,11 +75,6 @@ protected void titleLongClick() {
protected void titleTouched(MotionEvent e) {
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
}

@Background
protected void someBackgroundTask(){
}
Expand Down
Loading