
br.com.objectos.way.sql.compiler.SqlModuleProcessor Maven / Gradle / Ivy
The newest version!
/*
* 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.sql.compiler;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.Set;
import br.com.objectos.way.code.AbstractAnnotationProcessor;
import br.com.objectos.way.code.CodeCanvasArtifact;
import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.MethodInfoHasAnnotation;
import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.code.TypeInfo;
import br.com.objectos.way.core.util.WayIterables;
import br.com.objectos.way.sql.TableInfo;
import br.com.objectos.way.sql.annotation.Defines;
import br.com.objectos.way.sql.annotation.SqlModule;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
/**
* @author [email protected] (Marcio Endo)
*/
public class SqlModuleProcessor extends AbstractAnnotationProcessor {
private final Map instanceMap = Maps.newHashMap();
@Override
protected Class extends Annotation> annotationType() {
return SqlModule.class;
}
@Override
protected boolean shouldProcessMethods() {
return false;
}
@Override
protected boolean shouldProcessTypes() {
return true;
}
@Override
protected List toArtifactList(TypeInfo typeInfo) {
List definesMethodList = newArrayList();
Set schemaNameSet = newHashSet();
typeInfo.getMethodInfoIterable()
.filter(MethodInfoHasAnnotation.get(Defines.class))
.transform(new MethodInfoToDefinesMethod(typeInfo))
.toList()
.appendTo(definesMethodList)
.transform(new DefinesMethodToSchemaName())
.appendTo(schemaNameSet);
if (schemaNameSet.size() > 1) {
typeInfo.compilationError("@SqlModule can only define tables in the same schema.");
return ImmutableList.of();
}
List validMethodList = WayIterables.from(definesMethodList)
.filter(new DefinesMethodIsValid())
.toImmutableList();
return WayIterables.from(validMethodList)
.transform(new DefinesMethodToCodeCanvasArtifact())
.add(sqlModule(typeInfo, validMethodList))
.toImmutableList();
}
private Object newInstance(SimpleTypeInfo simpleTypeInfo) {
Object instance = instanceMap.get(simpleTypeInfo);
if (instance == null) {
try {
instance = newInstanceOrCompileAndInstantiate(simpleTypeInfo);
instanceMap.put(simpleTypeInfo, instance);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
return instance;
}
private Object newInstanceOrCompileAndInstantiate(SimpleTypeInfo typeInfo) throws ClassNotFoundException {
try {
return typeInfo.newInstance();
} catch (ClassNotFoundException e) {
return compileAndInstantiate(typeInfo);
}
}
private CodeCanvasArtifact sqlModule(TypeInfo typeInfo, List validMethodList) {
return new SqlModuleContext(typeInfo, validMethodList).toCodeCanvasArtifact();
}
private class DefinesMethodIsValid implements Predicate {
@Override
public boolean apply(DefinesMethod input) {
return input.isValid();
}
}
private class DefinesMethodToCodeCanvasArtifact implements Function {
@Override
public CodeCanvasArtifact apply(DefinesMethod input) {
return input.toCodeCanvasArtifact();
}
}
private class DefinesMethodToSchemaName implements Function {
@Override
public String apply(DefinesMethod input) {
return input.toSchemaName();
}
}
private class MethodInfoToDefinesMethod implements Function {
private final TypeInfo typeInfo;
private final Object moduleInstance;
public MethodInfoToDefinesMethod(TypeInfo typeInfo) {
this.typeInfo = typeInfo;
SimpleTypeInfo simpleTypeInfo = typeInfo.toSimpleTypeInfo();
moduleInstance = newInstance(simpleTypeInfo);
}
@Override
public DefinesMethod apply(MethodInfo methodInfo) {
TableInfo tableInfo = tableInfo(methodInfo);
return new DefinesMethod(typeInfo, methodInfo, tableInfo);
}
private TableInfo tableInfo(MethodInfo methodInfo) {
return (TableInfo) methodInfo.invoke(moduleInstance);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy