Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2010-2012 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.processing;
 
 import static com.googlecode.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
 import static com.sun.codemodel.JExpr._new;
 import static com.sun.codemodel.JExpr._null;
 import static com.sun.codemodel.JExpr.cast;
 import static com.sun.codemodel.JMod.FINAL;
 import static com.sun.codemodel.JMod.PRIVATE;
 import static com.sun.codemodel.JMod.PUBLIC;
 import static com.sun.codemodel.JMod.STATIC;
 
 
 
 
 public class EBeanProcessor implements ElementProcessor {
 
 	public static final String GET_INSTANCE_METHOD_NAME = "getInstance" + ;
 
 	public Class<? extends AnnotationgetTarget() {
 		return EBean.class;
 	}
 
 	public void process(Element elementJCodeModel codeModelEBeansHolder eBeansHolderthrows Exception {
 
 		EBeanHolder holder = eBeansHolder.create(element);
 
 		TypeElement typeElement = (TypeElementelement;
 
 		String eBeanQualifiedName = typeElement.getQualifiedName().toString();
 
 		String generatedBeanQualifiedName = eBeanQualifiedName + ;
 
 		holder.eBean = codeModel._class( | generatedBeanQualifiedName.);
 
 		JClass eBeanClass = codeModel.directClass(eBeanQualifiedName);
 
 		holder.eBean._extends(eBeanClass);
 
 		Classes classes = holder.classes();
 
 		JFieldVar contextField = holder.eBean.field(classes.CONTEXT"context_");
 
 		holder.contextRef = contextField;
 
 		{
 			// afterSetContentView
 
 			holder.afterSetContentView = holder.eBean.method(codeModel.VOID"afterSetContentView_");
 
 			JBlock afterSetContentViewBody = holder.afterSetContentView.body();
 
 			afterSetContentViewBody._if(holder.contextRef._instanceof(classes.ACTIVITY).not())._then()._return();
 		}
 
 		{
 			// findViewById
 
 			JMethod findViewById = holder.eBean.method(classes.VIEW"findViewById");
 			JVar idParam = findViewById.param(codeModel.INT"id");
 
 			findViewById.javadoc().add("You should check that context is an activity before calling this method");
 
 			JBlock findViewByIdBody = findViewById.body();
 
 			JVar activityVar = findViewByIdBody.decl(classes.ACTIVITY"activity_"cast(classes.ACTIVITYholder.contextRef));
 
 			findViewByIdBody._return(activityVar.invoke(findViewById).arg(idParam));
 		}
		{
			// init
			holder.init = holder.eBean.method(codeModel.VOID"init_");
		}
		{
			// init if activity
			/*
			 * We suppress all warnings because we generate an unused warning
			 * that may or may not valid
			 */
			holder.init.annotate(SuppressWarnings.class).param("value""all");
			holder.initIfActivityBody = helper.ifContextInstanceOfActivity(holderholder.init.body());
			holder.initActivityRef = helper.castContextToActivity(holderholder.initIfActivityBody);
		}
		{
			// Constructor
			JMethod constructor = holder.eBean.constructor();
			JVar constructorContextParam = constructor.param(classes.CONTEXT"context");
			JBlock constructorBody = constructor.body();
			constructorBody.assign(contextFieldconstructorContextParam);
			constructorBody.invoke(holder.init);
		}
		EBean eBeanAnnotation = element.getAnnotation(EBean.class);
		Scope eBeanScope = eBeanAnnotation.scope();
		boolean hasSingletonScope = eBeanScope == .;
		{
			// Factory method
			JMethod factoryMethod = holder.eBean.method( | holder.eBean);
			JVar factoryMethodContextParam = factoryMethod.param(classes.CONTEXT"context");
			JBlock factoryMethodBody = factoryMethod.body();
			/*
			 * Singletons are bound to the application context
			 */
			if (hasSingletonScope) {
				JFieldVar instanceField = holder.eBean.field( | holder.eBean"instance_");
				factoryMethodBody //
						._if(instanceField.eq(_null())) //
						._then() //
						.assign(instanceField_new(holder.eBean).arg(factoryMethodContextParam.invoke("getApplicationContext")));
				factoryMethodBody._return(instanceField);
else {
				factoryMethodBody._return(_new(holder.eBean).arg(factoryMethodContextParam));
			}
		}
		{
			// rebind(Context)
			JMethod rebindMethod = holder.eBean.method(codeModel.VOID"rebind");
			JVar contextParam = rebindMethod.param(classes.CONTEXT"context");
			/*
			 * No rebinding of context for singletons, their are bound to the
			 * application context
			 */
			if (!hasSingletonScope) {
				JBlock body = rebindMethod.body();
				body.assign(contextFieldcontextParam);
				body.invoke(holder.init);
			}
		}
	}
New to GrepCode? Check out our FAQ X