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

com.wesleyhome.dao.processor.DAOGeneratorProcessor Maven / Gradle / Ivy

/*
 * @(#)DAOGeneratorProcessor.java
 * 
 * (C) Copyright 2014 by Travelers
 * All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of the Travelers Corporation. ("Confidential Information").
 * Redistribution of the source code or binary form is not permitted
 * without prior authorization from Travelers.
 */
package com.wesleyhome.dao.processor;

import static com.wesleyhome.dao.processor.DAOGeneratorProcessor.DAO_NAMED_KEY;
import static com.wesleyhome.dao.processor.DAOGeneratorProcessor.ENTITY_MANANGER_INJECT_KEY;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.JavaFileObject;
import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JPackage;
import com.wesleyhome.dao.processor.model.EntityInfo;

/**
 * The DAOGeneratorProcessor class is a
 * 
 * @author
 * @since
 */
@SupportedAnnotationTypes("javax.persistence.Entity")
@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedOptions({
	DAO_NAMED_KEY, ENTITY_MANANGER_INJECT_KEY
})
public class DAOGeneratorProcessor extends AbstractProcessor {

	public static final String DAO_NAMED_KEY = "dao.generator.named";
	public static final String ENTITY_MANANGER_INJECT_KEY = "dao.generator.inject";

	/* (non-Javadoc)
	 * @see javax.annotation.processing.AbstractProcessor#init(javax.annotation.processing.ProcessingEnvironment)
	 */
	@Override
	public synchronized void init(final ProcessingEnvironment _processingEnv) {
		super.init(_processingEnv);
	}

	/* (non-Javadoc)
	 * @see javax.annotation.processing.AbstractProcessor#process(java.util.Set, javax.annotation.processing.RoundEnvironment)
	 */
	@Override
	public boolean process(final Set annotations, final RoundEnvironment roundEnv) {
		try {
			Map options = processingEnv.getOptions();
			JCodeModel jCodeModel = new JCodeModel();
			boolean writeFiles = true;
			for (TypeElement typeElement : annotations) {
				Set entityElements = roundEnv.getElementsAnnotatedWith(typeElement);
				EntityInformationMap entityInfoMap = getEntityInformation(entityElements);
				for (Element element : entityElements) {
					if (element instanceof TypeElement) {
						TypeElement entityElement = (TypeElement) element;
						List processors = getClassProcessors();
						for (ClassProcessor processor : processors) {
							EntityInfo entityInfo = entityInfoMap.get(entityElement.asType().toString());
							boolean validate = processor.validate(entityInfo, processingEnv.getMessager());
							if (validate) {
								processor.setOptions(options);
								processor.generate(entityInfo, jCodeModel, entityInfoMap);
							}
							writeFiles = validate && writeFiles;
						}
					}
				}
			}
			if (writeFiles) {
				writeFiles(jCodeModel);
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return false;
	}

	private EntityInformationMap getEntityInformation(final Set entityElements) {
		EntityInformationMap map = new EntityInformationMap();
		EntityInfoProcessor processor = new EntityInfoProcessor();
		for (Element element : entityElements) {
			if (element instanceof TypeElement) {
				TypeElement entityElement = (TypeElement) element;
				String string = entityElement.asType().toString();
				map.put(string, processor.getEntityInfo(entityElement));
			}
		}
		return map;
	}

	/**
	 * @param jCodeModel
	 * @throws IOException
	 */
	private void writeFiles(final JCodeModel jCodeModel) throws IOException {
		CodeWriter codeWriter = new CodeWriter() {

			private final Map outputStreamMap = new HashMap<>();
			private final Map createSourceFileMap = new HashMap<>();

			@Override
			public OutputStream openBinary(final JPackage pkg, final String fileName) throws IOException {
				String name = String.format("%s.%s", pkg.name(), fileName.substring(0, fileName.length() - 5));
				JavaFileObject createSourceFile = processingEnv.getFiler().createSourceFile(name);
				createSourceFileMap.put(name, createSourceFile);
				OutputStream outputStream = createSourceFile.openOutputStream();
				outputStreamMap.put(name, outputStream);
				return outputStream;
			}

			@Override
			public void close() throws IOException {
				Collection values = outputStreamMap.values();
				for (OutputStream outputStream : values) {
					outputStream.close();
				}
			}
		};
		jCodeModel.build(codeWriter);
	}

	/**
	 * @return
	 */
	private List getClassProcessors() {
		List list = new ArrayList<>();
		list.add(new DAOInterfaceProcessor());
		list.add(new DAOImplementationProcessor());
		return list;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy