Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.processing;
 
 import static com.googlecode.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
 import static com.sun.codemodel.JExpr._new;
 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 extends AnnotationHelper implements ElementProcessor {
 
 	public static final String GET_INSTANCE_METHOD_NAME = "getInstance" + ;
 
 	public EBeanProcessor(ProcessingEnvironment processingEnv) {
 		super(processingEnv);
 	}
 
 	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);
 		
 		JClass contextClass = holder.refClass("android.content.Context");
 		
 		JClass activityClass = holder.refClass("android.app.Activity");
 
 		JFieldVar contextField = holder.eBean.field(contextClass"context_");
 		
 		holder.contextRef = contextField;
 		
 		{
 			// afterSetContentView
 			
 			holder.afterSetContentView = holder.eBean.method(codeModel.VOID"afterSetContentView_");
 			
 			JBlock afterSetContentViewBody = holder.afterSetContentView.body();
 			
 			afterSetContentViewBody._if(holder.contextRef._instanceof(activityClass).not())._then()._return();
 		}
 		
 		JClass viewClass = holder.refClass("android.view.View");
 		
 		{
 			// findViewById
 			
 			JMethod findViewById = holder.eBean.method(viewClass"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(activityClass"activity"cast(activityClassholder.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(contextClass"context");
			JBlock constructorBody = constructor.body();
			constructorBody.assign(contextFieldconstructorContextParam);
			constructorBody.invoke(holder.init);
		}
		{
			// Factory method
			JMethod factoryMethod = holder.eBean.method( | holder.eBean);
			JVar factoryMethodContextParam = factoryMethod.param(contextClass"context");
			JBlock factoryMethodBody = factoryMethod.body();
			factoryMethodBody._return(_new(holder.eBean).arg(factoryMethodContextParam));
		}
	}
New to GrepCode? Check out our FAQ X