All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wesleyhome.dao.processor.method.CodeGenerationHelper Maven / Gradle / Ivy

package com.wesleyhome.dao.processor.method;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JType;
import com.wesleyhome.dao.annotations.ReturnType;
import com.wesleyhome.dao.processor.model.ParamInfo;
import com.wesleyhome.dao.processor.model.ReturnInfo;

public class CodeGenerationHelper {

	public static void applyParameters(final JCodeModel model, final List paramInfoList, final JMethod method) {
		for (ParamInfo paramInfo : paramInfoList) {
			String parameterClassName = paramInfo.parameterClassName;
			JType parameterClassRef;
			try {
				Class parameterClass = Class.forName(parameterClassName);
				parameterClassRef = model.ref(parameterClass);
			} catch (ClassNotFoundException e) {
				parameterClassRef = model.ref(parameterClassName);
			}
			if (paramInfo.isCollection) {
				parameterClassRef = model.ref(Collection.class).narrow(parameterClassRef);
			}
			method.param(parameterClassRef, paramInfo.parameterName);
		}
	}

	/**
	 * @param model
	 * @param returnInfo
	 * @return
	 */
	public static JType getReturnType(final JCodeModel model, final ReturnInfo returnInfo) {
		String returnClassString = returnInfo.returnClassString;
		if(Void.class.getName().equals(returnClassString)) {
			return model.VOID;
		}
		JClass returnClass = getClassRef(model, returnClassString);
		ReturnType returnType = returnInfo.returnType;
		switch (returnType) {
			case LIST:
				return model.ref(List.class).narrow(returnClass);
			case SET:
				return model.ref(Set.class).narrow(returnClass);
			case MAP_TO_SINGLE:
				return model.ref(Map.class).narrow(getClassRef(model, returnInfo.keyClassString), returnClass);
			case MAP_TO_LIST:
				return model.ref(Map.class)
					.narrow(getClassRef(model, returnInfo.keyClassString), model.ref(List.class).narrow(returnClass));
			case SINGLE:
			default:
				return returnClass;
		}
	}

	/**
	 * @param model
	 * @param returnClassString
	 * @return
	 * @throws ClassNotFoundException
	 */
	public static JClass getClassRef(final JCodeModel model, final String returnClassString) {
		// first see if we created it
		JClass returnClass = model._getClass(returnClassString);
		if (returnClass == null) {
			try {
				// lets see if the class is in the class path
				Class returnClazz = Class.forName(returnClassString);
				returnClass = model.ref(returnClazz);
			} catch (ClassNotFoundException e1) {
				returnClass = model.ref(returnClassString);
			}
		}
		return returnClass;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy