
br.com.objectos.sql.BootProcessor Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2015 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.sql;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import javax.annotation.Generated;
import javax.annotation.processing.Processor;
import br.com.objectos.code.AbstractAnnotationProcessor;
import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.code.Artifact;
import br.com.objectos.code.Configuration;
import br.com.objectos.code.TypeInfo;
import br.com.objectos.metainf.Services;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.JavaFile;
/**
* @author [email protected] (Marcio Endo)
*/
@Services(Processor.class)
public class BootProcessor extends AbstractAnnotationProcessor {
static final AnnotationSpec ANNOTATION_SPEC = AnnotationSpec.builder(Generated.class)
.addMember("value", "$S", BootProcessor.class.getName())
.build();
private static final String ANNOTATION_NAME = "br.com.objectos.sql.Boot";
@Override
protected Configuration configuration() {
return Configuration.builder()
.addAnnotationType(ANNOTATION_NAME)
.addTypeInfoArtifactGenerator(this::generate)
.build();
}
private Artifact generate(TypeInfo typeInfo) {
AnnotationInfo bootAnnotationInfo = typeInfo.annotationInfo(ANNOTATION_NAME).get();
int development = bootAnnotationInfo.intValue("development", 0);
int level = bootAnnotationInfo.intValue("level", 0);
if (level < 2) {
typeInfo.compilationError("level must be > 1");
return Artifact.empty();
}
if (development < 1) {
typeInfo.compilationError("development must be > 0");
return Artifact.empty();
}
if (development > level) {
typeInfo.compilationError("development must not be greater than level");
return Artifact.empty();
}
String packageName = typeInfo.packageInfo().toString();
List levelList = IntStream.rangeClosed(1, level)
.mapToObj(it -> Level.of(packageName, it))
.collect(Collectors.toList());
Stream levelStream = levelList.stream()
.filter(l -> l.filter(development))
.flatMap(Level::generate);
Stream moreStream = Stream.of(
SqlType.of(typeInfo, levelList).generate(),
InsertIntoType.of(levelList).generate(),
UpdateType.of(levelList).generate());
JavaFile[] files = Stream.concat(levelStream, moreStream).toArray(JavaFile[]::new);
return Artifact.of(files);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy