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.sun.codemodel.JExpr._new;
 import static com.sun.codemodel.JExpr._super;
 import static com.sun.codemodel.JExpr._this;
 import static com.sun.codemodel.JExpr.cast;
 import static com.sun.codemodel.JExpr.invoke;
 
 import java.util.List;
 import java.util.Map;
 
 
 
 public class RoboGuiceProcessor implements ElementProcessor {
 
 	public Class<? extends AnnotationgetTarget() {
 		return RoboGuice.class;
 	}
 
 	public void process(Element elementJCodeModel codeModelEBeansHolder activitiesHolder) {
 		EBeanHolder holder = activitiesHolder.getRelativeEBeanHolder(element);
 
 		holder.eBean._implements(holder.classes().);
 
 		// Fields
 		JFieldVar scope = scopeField(holder);
 		JFieldVar eventManager = eventManagerField(holder);
 		listenerFields(elementholder);
 
 		// Methods
 		afterSetContentView(codeModelholderscopeeventManager);
 		onRestartMethod(codeModelholderscopeeventManager);
 		onStartMethod(codeModelholderscopeeventManager);
 		onResumeMethod(codeModelholderscopeeventManager);
 		onPauseMethod(codeModelholderscopeeventManager);
 		onNewIntentMethod(codeModelholderscopeeventManager);
 		onStopMethod(codeModelholderscopeeventManager);
 		onDestroyMethod(codeModelholderscopeeventManager);
 		onConfigurationChangedMethod(codeModelholderscopeeventManager);
 		onContentChangedMethod(codeModelholderscopeeventManager);
 		onActivityResultMethod(codeModelholderscopeeventManager);
 
 		JMethod getInjectorMethod = getInjectorMethod(holder);
 
 		beforeCreateMethod(holderscopeeventManagergetInjectorMethod);
 
 	}
 
 	private JMethod getInjectorMethod(EBeanHolder holder) {
 		JMethod method = holder.eBean.method(.holder.classes()."getInjector");
 		method.annotate(Override.class);
 		JExpression castApplication = cast(holder.classes().invoke("getApplication"));
 		method.body()._return(castApplication.invoke("getInjector"));
 		return method;
 	}
 
 	private void onRestartMethod(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
 		JMethod method = holder.eBean.method(.codeModel.VOID"onRestart");
 		method.annotate(Override.class);
 		JBlock body = method.body();
 		body.invoke(scope"enter").arg(_this());
 		body.invoke(_super(), method);
 		fireEvent(holdereventManagerbodyholder.classes().);
	}
	private void onStartMethod(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
		JMethod method = holder.eBean.method(.codeModel.VOID"onStart");
		method.annotate(Override.class);
		JBlock body = method.body();
		body.invoke(scope"enter").arg(_this());
		body.invoke(_super(), method);
		fireEvent(holdereventManagerbodyholder.classes().);
	}
	private void onResumeMethod(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
		JMethod method = holder.eBean.method(.codeModel.VOID"onResume");
		method.annotate(Override.class);
		JBlock body = method.body();
		body.invoke(scope"enter").arg(_this());
		body.invoke(_super(), method);
		fireEvent(holdereventManagerbodyholder.classes().);
	}
	private void onPauseMethod(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
		JMethod method = holder.eBean.method(.codeModel.VOID"onPause");
		method.annotate(Override.class);
		JBlock body = method.body();
		body.invoke(_super(), method);
		fireEvent(holdereventManagerbodyholder.classes().);
	}
	private void onNewIntentMethod(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
		JMethod method = holder.eBean.method(.codeModel.VOID"onNewIntent");
		method.annotate(Override.class);
		JVar intent = method.param(holder.classes()."intent");
		JBlock body = method.body();
		body.invoke(_super(), method).arg(intent);
		body.invoke(scope"enter").arg(_this());
		fireEvent(holdereventManagerbodyholder.classes().);
	}
	private void fireEvent(EBeanHolder holderJFieldVar eventManagerJBlock bodyJClass eventClassJExpression... eventArguments) {
		JInvocation newEvent = _new(eventClass);
		for (JExpression eventArgument : eventArguments) {
			newEvent.arg(eventArgument);
		}
		body.invoke(eventManager"fire").arg(newEvent);
	}
	private void onStopMethod(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
		JMethod method = holder.eBean.method(.codeModel.VOID"onStop");
		method.annotate(Override.class);
		JBlock body = method.body();
		body.invoke(scope"enter").arg(_this());
		JTryBlock tryBlock = body._try();
		fireEvent(holdereventManagertryBlock.body(), holder.classes().);
		JBlock finallyBody = tryBlock._finally();
		finallyBody.invoke(scope"exit").arg(_this());
		finallyBody.invoke(_super(), method);
	}
	private void onDestroyMethod(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
		JMethod method = holder.eBean.method(.codeModel.VOID"onDestroy");
		method.annotate(Override.class);
		JBlock body = method.body();
		body.invoke(scope"enter").arg(_this());
		JTryBlock tryBlock = body._try();
		fireEvent(holdereventManagertryBlock.body(), holder.classes().);
		JBlock finallyBody = tryBlock._finally();
		finallyBody.invoke(eventManager"clear").arg(_this());
		finallyBody.invoke(scope"exit").arg(_this());
		finallyBody.invoke(scope"dispose").arg(_this());
		finallyBody.invoke(_super(), method);
	}
	private void onConfigurationChangedMethod(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
		JMethod method = holder.eBean.method(.codeModel.VOID"onConfigurationChanged");
		method.annotate(Override.class);
		JClass configurationClass = holder.classes().;
		JVar newConfig = method.param(configurationClass"newConfig");
		JBlock body = method.body();
		JVar currentConfig = body.decl(configurationClass"currentConfig", JExpr.invoke("getResources").invoke("getConfiguration"));
		body.invoke(_super(), method).arg(newConfig);
		fireEvent(holdereventManagerbodyholder.classes().currentConfignewConfig);
	}
	private void onContentChangedMethod(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
		JMethod method = holder.eBean.method(.codeModel.VOID"onContentChanged");
		method.annotate(Override.class);
		JBlock body = method.body();
		body.invoke(_super(), method);
		fireEvent(holdereventManagerbodyholder.classes().);
	}
	private void onActivityResultMethod(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
		JMethod method = holder.eBean.method(.codeModel.VOID"onActivityResult");
		method.annotate(Override.class);
		JVar requestCode = method.param(codeModel.INT"requestCode");
		JVar resultCode = method.param(codeModel.INT"resultCode");
		JVar data = method.param(holder.classes()."data");
		JBlock body = method.body();
		body.invoke(_super(), method).arg(requestCode).arg(resultCode).arg(data);
		body.invoke(scope"enter").arg(_this());
		JTryBlock tryBlock = body._try();
		fireEvent(holdereventManagertryBlock.body(), holder.classes().requestCoderesultCodedata);
		JBlock finallyBody = tryBlock._finally();
		finallyBody.invoke(scope"exit").arg(_this());
	}
	private void afterSetContentView(JCodeModel codeModelEBeanHolder holderJFieldVar scopeJFieldVar eventManager) {
		JBlock afterSetContentViewBody = holder.afterSetContentView.body();
		afterSetContentViewBody.invoke(scope"injectViews");
		fireEvent(holdereventManagerafterSetContentViewBodyholder.classes().);
	}
		JFieldVar eventManager = holder.eBean.field(.holder.classes()."eventManager_");
		return eventManager;
	}
	private JFieldVar scopeField(EBeanHolder holder) {
		JFieldVar scope = holder.eBean.field(.holder.classes()."scope_");
		return scope;
	}
	private void listenerFields(Element elementEBeanHolder holder) {
		List<StringlistenerClasses = extractListenerClasses(element);
		if (listenerClasses.size() > 0) {
			int i = 1;
			for (String listenerClassName : listenerClasses) {
				JClass listenerClass = holder.refClass(listenerClassName);
				JFieldVar listener = holder.eBean.field(.listenerClass"listener" + i + "_");
				listener.annotate(SuppressWarnings.class).param("value""unused");
				listener.annotate(holder.classes().);
				i++;
			}
		}
	}
	private List<StringextractListenerClasses(Element activityElement) {
		List<? extends AnnotationMirrorannotationMirrors = activityElement.getAnnotationMirrors();
		String annotationName = RoboGuice.class.getName();
		AnnotationValue action = null;
		for (AnnotationMirror annotationMirror : annotationMirrors) {
			if (annotationName.equals(annotationMirror.getAnnotationType().toString())) {
				for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValueentry : annotationMirror.getElementValues().entrySet()) {
					if ("value".equals(entry.getKey().getSimpleName().toString())) {
						action = entry.getValue();
						@SuppressWarnings("unchecked")
						List<Objectvalues = (List<Object>) action.getValue();
						List<StringlistenerClasses = new ArrayList<String>();
						for (Object value : values) {
							listenerClasses.add(value.toString());
						}
						return listenerClasses;
					}
				}
			}
		}
		return new ArrayList<String>(0);
	}
	private void beforeCreateMethod(EBeanHolder holderJFieldVar scopeJFieldVar eventManagerJMethod getInjector) {
		Classes classes = holder.classes();
		JBlock body = holder.init.body();
		JVar injector = body.decl(classes.INJECTOR"injector_"invoke(getInjector));
		body.assign(scopeinvoke(injector"getInstance").arg(classes.CONTEXT_SCOPE.dotclass()));
		body.invoke(scope"enter").arg(_this());
		body.invoke(injector"injectMembers").arg(_this());
		body.assign(eventManagerinvoke(injector"getInstance").arg(classes.EVENT_MANAGER.dotclass()));
		fireEvent(holdereventManagerbodyholder.classes().holder.beforeCreateSavedInstanceStateParam);
	}
New to GrepCode? Check out our FAQ X