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.sun.codemodel.JExpr._this;
 import static com.sun.codemodel.JExpr.cast;
 import static com.sun.codemodel.JMod.PUBLIC;
 
 
 
 
 public class ExtraProcessor implements ElementProcessor {
 
 	private final APTCodeModelHelper helper = new APTCodeModelHelper();
 
 	public ExtraProcessor(ProcessingEnvironment processingEnv) {
 		this. = processingEnv;
 	}
 
 	public Class<? extends AnnotationgetTarget() {
 		return Extra.class;
 	}
 
 	public void process(Element elementJCodeModel codeModelEBeansHolder activitiesHolder) {
 		Extra annotation = element.getAnnotation(Extra.class);
 		String extraKey = annotation.value();
 		String fieldName = element.getSimpleName().toString();
 
 		TypeMirror elementType = element.asType();
 		boolean isPrimitive = elementType.getKind().isPrimitive();
 
 		EBeanHolder holder = activitiesHolder.getEnclosingEBeanHolder(element);
 
 		if (!isPrimitive && holder.cast == null) {
 			JType objectType = codeModel._ref(Object.class);
 			JMethod method = holder.eBean.method(.objectType"cast_");
 			JTypeVar genericType = method.generify("T");
 			method.type(genericType);
 			JVar objectParam = method.param(objectType"object");
 			method.annotate(SuppressWarnings.class).param("value""unchecked");
 			method.body()._return(JExpr.cast(genericTypeobjectParam));
 
 			holder.cast = method;
 		}
 
 		if (holder.extras == null) {
 			JClass bundleClass = holder.refClass("android.os.Bundle");
 			holder.extras = holder.initIfActivityBody.decl(bundleClass"extras_");
 			holder.extras.init(holder.initActivityRef.invoke("getIntent").invoke("getExtras"));
 
 			holder.extrasNotNullBlock = holder.initIfActivityBody._if(holder.extras.ne(JExpr._null()))._then();
 		}
 
 		JBlock ifContainsKey = holder.extrasNotNullBlock._if(JExpr.invoke(holder.extras"containsKey").arg(extraKey))._then();
 
 		JTryBlock containsKeyTry = ifContainsKey._try();
 
 		JFieldRef extraField = JExpr.ref(fieldName);
 
		if (isPrimitive) {
			JPrimitiveType primitiveType = JType.parse(codeModelelementType.toString());
			JClass wrapperType = primitiveType.boxify();
			containsKeyTry.body().assign(extraField, JExpr.cast(wrapperTypeholder.extras.invoke("get").arg(extraKey)));
else {
			containsKeyTry.body().assign(extraField, JExpr.invoke(holder.cast).arg(holder.extras.invoke("get").arg(extraKey)));
		}
		JCatchBlock containsKeyCatch = containsKeyTry._catch(holder.refClass(ClassCastException.class));
		JVar exceptionParam = containsKeyCatch.param("e");
		JInvocation errorInvoke = holder.refClass("android.util.Log").staticInvoke("e");
		errorInvoke.arg(holder.eBean.name());
		errorInvoke.arg("Could not cast extra to expected type, the field is left to its default value");
		errorInvoke.arg(exceptionParam);
		containsKeyCatch.body().add(errorInvoke);
		if (holder.intentBuilderClass != null) {
			{
				// flags()
				JMethod method = holder.intentBuilderClass.method(holder.intentBuilderClassfieldName);
				boolean castToSerializable = false;
				TypeMirror extraType = elementType;
				if (extraType.getKind() == .) {
					TypeMirror parcelableType = elementUtils.getTypeElement("android.os.Parcelable").asType();
					if (!typeUtils.isSubtype(extraTypeparcelableType)) {
						TypeMirror stringType = elementUtils.getTypeElement("java.lang.String").asType();
						if (!typeUtils.isSubtype(extraTypestringType)) {
							castToSerializable = true;
						}
					}
				}
				JClass paramClass = .typeMirrorToJClass(extraTypeholder);
				JVar extraParam = method.param(paramClassfieldName);
				JBlock body = method.body();
				JInvocation invocation = body.invoke(holder.intentField"putExtra").arg(extraKey);
				if (castToSerializable) {
					JClass serializableClass = holder.refClass(Serializable.class);
					invocation.arg(cast(serializableClassextraParam));
else {
					invocation.arg(extraParam);
				}
				body._return(_this());
			}
		}
	}
New to GrepCode? Check out our FAQ X