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 1 commit
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
Next Next commit
Added static factory methods to ViewGroup subclasses
  • Loading branch information
JoanZapata committed Feb 16, 2012
commit c3945e8342ecf7bebaf5fe42fe57405e1ce412a0
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,13 +43,15 @@
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;
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 {

Expand Down Expand Up @@ -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
Expand All @@ -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<ExecutableElement> constructors = new ArrayList<ExecutableElement>();
for (Element e : element.getEnclosedElements()) {
if (e.getKind() == CONSTRUCTOR) {
Expand All @@ -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);
}
}
Expand Down
7 changes: 7 additions & 0 deletions AndroidAnnotations/functional-test-1-5/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
android:name="com.googlecode.androidannotations.test15.roboguice.SampleRoboApplication_"
android:icon="@drawable/icon"
android:label="@string/app_name" >

<activity android:name=".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
Original file line number Diff line number Diff line change
@@ -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");
}

}