com.mybatis.jpa.statement.DefinitionStatementScanner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-jpa Show documentation
Show all versions of mybatis-jpa Show documentation
The plugins for mybatis, in order to provider the ability to handler jpa.
package com.mybatis.jpa.statement;
import com.mybatis.jpa.annotation.InsertDefinition;
import com.mybatis.jpa.annotation.UpdateDefinition;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.builder.IncompleteElementException;
import org.apache.ibatis.builder.annotation.MethodResolver;
import org.apache.ibatis.session.Configuration;
import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
/**
* @author svili
**/
public class DefinitionStatementScanner {
private Configuration configuration;
private String[] basePackages;
private StatementBuildable statementBuilder;
private Set> registryAnnotationSet = new HashSet<>();
public void scan() {
for (String basePackage : basePackages) {
Reflections reflections = new Reflections(basePackage, new TypeAnnotationsScanner(),
new SubTypesScanner(), new MethodAnnotationsScanner());
Set> registryAnnotation = registryAnnotationSet;
Set> mappers = reflections.getTypesAnnotatedWith(Mapper.class);
for (Class> mapperClass : mappers) {
Method[] methods = mapperClass.getMethods();
for (Method method : methods) {
Annotation[] annotations = method.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
if (registryAnnotation.contains(annotation.annotationType())) {
statementBuilder.parseStatement(method, mapperClass);
}
}
}
}
}
parsePendingMethods();
}
private void parsePendingMethods() {
Collection incompleteMethods = configuration.getIncompleteMethods();
synchronized (incompleteMethods) {
Iterator iter = incompleteMethods.iterator();
while (iter.hasNext()) {
try {
iter.next().resolve();
iter.remove();
} catch (IncompleteElementException e) {
// This method is still missing a resource
}
}
}
}
public static class Builder {
private DefinitionStatementScanner instance = new DefinitionStatementScanner();
public Builder() {
instance.registryAnnotationSet.add(InsertDefinition.class);
instance.registryAnnotationSet.add(UpdateDefinition.class);
}
public Builder configuration(Configuration configuration) {
this.instance.configuration = configuration;
return this;
}
public Builder basePackages(String[] basePackages) {
this.instance.basePackages = basePackages;
return this;
}
public Builder statementBuilder(StatementBuildable statementBuilder) {
this.instance.statementBuilder = statementBuilder;
return this;
}
public Builder registryAnnotationSet(Set> registryAnnotationSet) {
this.instance.registryAnnotationSet = registryAnnotationSet;
return this;
}
public DefinitionStatementScanner build() {
return this.instance;
}
}
}