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.rest;
 
 import static com.sun.codemodel.JExpr._this;
 
 import java.util.List;
 
 
 
 public class RestProcessor implements ElementProcessor {
 
 	private static final String SPRING_REST_TEMPLATE_QUALIFIED_NAME = "org.springframework.web.client.RestTemplate";
 	private static final String JAVA_STRING_QUALIFIED_NAME = "java.lang.String";
 
 	public RestProcessor(RestImplementationsHolder restImplementationHolder) {
 		this. = restImplementationHolder;
 	}
 
 	public Class<? extends AnnotationgetTarget() {
 		return Rest.class;
 	}
 
 	public void process(Element elementJCodeModel codeModelEBeansHolder activitiesHolderthrows Exception {
 
 
 		TypeElement typeElement = (TypeElementelement;
 		String interfaceName = typeElement.getQualifiedName().toString();
 
 		String implementationName = interfaceName + .;
 
 		// holder.restImplementationClass = codeModel._class(JMod.PUBLIC |
 		// JMod.ABSTRACT, implementationName, ClassType.CLASS);
 		holder.restImplementationClass = codeModel._class(.implementationName.);
 		JClass interfaceClass = holder.refClass(interfaceName);
 		holder.restImplementationClass._implements(interfaceClass);
 
 		// RestTemplate field
 		JClass restTemplateClass = holder.refClass();
 		holder.restTemplateField = holder.restImplementationClass.field(.restTemplateClass"restTemplate");
 
 		// RootUrl field
 		JClass stringClass = holder.refClass();
 		holder.rootUrlField = holder.restImplementationClass.field(.stringClass"rootUrl");
 
 		// Default constructor
 		JMethod defaultConstructor = holder.restImplementationClass.constructor(.);
 		defaultConstructor.body().assign(holder.restTemplateField, JExpr._new(restTemplateClass));
 		defaultConstructor.body().assign(holder.rootUrlField, JExpr.lit(typeElement.getAnnotation(Rest.class).value()));
 
 		// RestTemplate constructor
 		JMethod restTemplateConstructor = holder.restImplementationClass.constructor(.);
 		JVar restTemplateParam = restTemplateConstructor.param(restTemplateClass"restTemplate");
 		restTemplateConstructor.body().assign(JExpr._this().ref(holder.restTemplateField), restTemplateParam);
 
 		// RequestFactory constructor
 		JMethod requestFactoryConstructor = holder.restImplementationClass.constructor(.);
 		JClass requestFactoryClass = holder.refClass("org.springframework.http.client.ClientHttpRequestFactory");
 		JVar requestFactoryParam = requestFactoryConstructor.param(requestFactoryClass"requestFactory");
 		requestFactoryConstructor.body().assign(holder.restTemplateField, JExpr._new(restTemplateClass).arg(requestFactoryParam));
 
 		// Implement getRestTemplate method
 		List<? extends ElementenclosedElements = typeElement.getEnclosedElements();
 		List<ExecutableElementmethods = ElementFilter.methodsIn(enclosedElements);
		for (ExecutableElement method : methods) {
				String methodName = method.getSimpleName().toString();
				JMethod getRestTemplateMethod = holder.restImplementationClass.method(.restTemplateClassmethodName);
				getRestTemplateMethod.annotate(Override.class);
				getRestTemplateMethod.body()._return(holder.restTemplateField);
				break// Only one implementation
			}
		}
		for (ExecutableElement method : methods) {
			List<? extends VariableElementparameters = method.getParameters();
			if (parameters.size() == 1 && method.getReturnType().getKind() == .) {
				VariableElement firstParameter = parameters.get(0);
					String methodName = method.getSimpleName().toString();
					JMethod setRestTemplateMethod = holder.restImplementationClass.method(.codeModel.VOIDmethodName);
					setRestTemplateMethod.annotate(Override.class);
					JVar restTemplateSetterParam = setRestTemplateMethod.param(restTemplateClassfirstParameter.getSimpleName().toString());
					setRestTemplateMethod.body().assign(_this().ref(holder.restTemplateField), restTemplateSetterParam);
					break// Only one implementation
				}
			}
		}
		// Implement setRootUrl method
		for (ExecutableElement method : methods) {
			List<? extends VariableElementparameters = method.getParameters();
			if (parameters.size() == 1 && method.getReturnType().getKind() == .) {
				VariableElement firstParameter = parameters.get(0);
				if (firstParameter.asType().toString().equals() && method.getSimpleName().toString().equals("setRootUrl")) {
					JMethod setRootUrlMethod = holder.restImplementationClass.method(.codeModel.VOIDmethod.getSimpleName().toString());
					setRootUrlMethod.annotate(Override.class);
					JVar rootUrlSetterParam = setRootUrlMethod.param(stringClassfirstParameter.getSimpleName().toString());
					setRootUrlMethod.body().assign(_this().ref(holder.rootUrlField), rootUrlSetterParam);
					break// Only one implementation
				}
			}
		}
	}
New to GrepCode? Check out our FAQ X