br.com.objectos.way.auto.pojo.AutoPojoProcessor Maven / Gradle / Ivy
/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.way.auto.pojo;
import java.lang.annotation.Annotation;
import java.util.List;
import br.com.objectos.way.code.AbstractAnnotationProcessor;
import br.com.objectos.way.code.ClassInfo;
import br.com.objectos.way.code.CodeCanvasArtifact;
import br.com.objectos.way.code.CodeCanvasWriter;
import br.com.objectos.way.code.TypeInfo;
import br.com.objectos.way.code.mustache.Writers;
import br.com.objectos.way.code.pojo.PojoBuilderContext;
import br.com.objectos.way.code.pojo.PojoBuilderPojoContext;
import br.com.objectos.way.code.pojo.PojoContext;
import br.com.objectos.way.core.auto.AutoPojo;
import br.com.objectos.way.core.tmpl.mustache.MustacheObject;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
/**
* @author [email protected] (Marcio Endo)
*/
public class AutoPojoProcessor extends AbstractAnnotationProcessor {
@Override
protected Class extends Annotation> annotationType() {
return AutoPojo.class;
}
@Override
protected boolean shouldProcessMethods() {
return false;
}
@Override
protected boolean shouldProcessTypes() {
return true;
}
@Override
protected List toArtifactList(TypeInfo typeInfo) {
List artifactList = ImmutableList.of();
Optional maybeClassInfo = typeInfo.toClassInfo();
if (maybeClassInfo.isPresent()) {
ClassInfo classInfo = maybeClassInfo.get();
artifactList = toArtifactList(classInfo);
}
return artifactList;
}
private List toArtifactList(ClassInfo classInfo) {
return ImmutableList. builder()
.add(pojo(classInfo))
.add(builder(classInfo))
.add(builderPojo(classInfo))
.build();
}
private CodeCanvasArtifact pojo(ClassInfo classInfo) {
MustacheObject context = new PojoContext(getClass(), classInfo).get();
return CodeCanvasWriter.forTemplate("/way-code-pojo/Pojo.mustache")
.namedAfter(classInfo, "Pojo")
.toCodeCanvasArtifact(Writers.INSTANCE, context);
}
private CodeCanvasArtifact builder(ClassInfo classInfo) {
MustacheObject context = new PojoBuilderContext(getClass(), classInfo).get();
return CodeCanvasWriter.forTemplate("/way-code-pojo/PojoBuilder.mustache")
.namedAfter(classInfo, "Builder")
.toCodeCanvasArtifact(Writers.INSTANCE, context);
}
private CodeCanvasArtifact builderPojo(ClassInfo classInfo) {
MustacheObject context = new PojoBuilderPojoContext(getClass(), classInfo).get();
return CodeCanvasWriter.forTemplate("/way-code-pojo/PojoBuilderPojo.mustache")
.namedAfter(classInfo, "BuilderPojo")
.toCodeCanvasArtifact(Writers.INSTANCE, context);
}
}