Merge pull request #2523 from lpandzic/AbstractQuerydslProcessor-extension

opened up apt API for extension
This commit is contained in:
Robert Bain 2020-02-09 21:23:42 +00:00 committed by GitHub
commit edb2bb11e1
4 changed files with 47 additions and 14 deletions

View File

@ -85,8 +85,8 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
Set<Class<? extends Annotation>> entityAnnotations = conf.getEntityAnnotations();
TypeMappings typeMappings = conf.getTypeMappings();
QueryTypeFactory queryTypeFactory = conf.getQueryTypeFactory();
this.typeFactory = new ExtendedTypeFactory(processingEnv, entityAnnotations, typeMappings, queryTypeFactory, conf.getVariableNameFunction());
elementHandler = new TypeElementHandler(conf, typeFactory, typeMappings, queryTypeFactory);
this.typeFactory = createTypeFactory(entityAnnotations, typeMappings, queryTypeFactory);
elementHandler = createElementHandler(typeMappings, queryTypeFactory);
this.roundEnv = roundEnv;
// process annotations
@ -100,7 +100,16 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
}
private void processAnnotations() {
protected TypeElementHandler createElementHandler(TypeMappings typeMappings, QueryTypeFactory queryTypeFactory) {
return new TypeElementHandler(conf, typeFactory, typeMappings, queryTypeFactory);
}
protected ExtendedTypeFactory createTypeFactory(Set<Class<? extends Annotation>> entityAnnotations,
TypeMappings typeMappings, QueryTypeFactory queryTypeFactory) {
return new ExtendedTypeFactory(processingEnv, entityAnnotations, typeMappings, queryTypeFactory, conf.getVariableNameFunction());
}
protected void processAnnotations() {
processExclusions();
Set<TypeElement> elements = collectElements();
@ -120,7 +129,7 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
for (TypeElement element : elements) {
EntityType entityType = elementHandler.handleEntityType(element);
registerTypeElement(entityType.getFullName(), element);
if (element.getAnnotation(conf.getEntityAnnotation()) != null) {
if (typeFactory.isSimpleTypeEntity(element, conf.getEntityAnnotation())) {
context.entityTypes.put(entityType.getFullName(), entityType);
} else if (altEntityAnn && element.getAnnotation(conf.getAlternativeEntityAnnotation()) != null) {
context.entityTypes.put(entityType.getFullName(), entityType);
@ -567,9 +576,7 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
private void serialize(Serializer serializer, Collection<EntityType> models) {
for (EntityType model : models) {
try {
Type type = conf.getTypeMappings().getPathType(model, model, true);
String packageName = type.getPackageName();
String className = !packageName.isEmpty() ? (packageName + "." + type.getSimpleName()) : type.getSimpleName();
String className = getClassName(model);
// skip if type is excluded class or in excluded package
if (conf.isExcludedPackage(model.getPackageName()) || conf.isExcludedClass(model.getFullName())) {
@ -610,6 +617,11 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
}
}
protected String getClassName(EntityType model) {
Type type = conf.getTypeMappings().getPathType(model, model, true);
String packageName = type.getPackageName();
return packageName.isEmpty() ? type.getSimpleName() : (packageName + "." + type.getSimpleName());
}
protected abstract Configuration createConfiguration(RoundEnvironment roundEnv);

View File

@ -48,7 +48,7 @@ public class DefaultConfiguration implements Configuration {
private boolean unknownAsEmbedded;
private final CodegenModule module = new CodegenModule();
private final CodegenModule module;
private final SerializerConfig defaultSerializerConfig;
@ -87,11 +87,28 @@ public class DefaultConfiguration implements Configuration {
@Nullable Class<? extends Annotation> superTypeAnn,
@Nullable Class<? extends Annotation> embeddableAnn,
@Nullable Class<? extends Annotation> embeddedAnn,
@Nullable Class<? extends Annotation> skipAnn) {
@Nullable Class<? extends Annotation> skipAnn
) {
this(processingEnvironment, roundEnv, keywords, entitiesAnn, entityAnn, superTypeAnn, embeddableAnn,
embeddedAnn, skipAnn, new CodegenModule());
}
public DefaultConfiguration(
ProcessingEnvironment processingEnvironment,
RoundEnvironment roundEnv,
Collection<String> keywords,
@Nullable Class<? extends Annotation> entitiesAnn,
Class<? extends Annotation> entityAnn,
@Nullable Class<? extends Annotation> superTypeAnn,
@Nullable Class<? extends Annotation> embeddableAnn,
@Nullable Class<? extends Annotation> embeddedAnn,
@Nullable Class<? extends Annotation> skipAnn,
CodegenModule codegenModule) {
this.excludedClasses = new HashSet<String>();
this.excludedPackages = new HashSet<String>();
this.includedClasses = new HashSet<String>();
this.includedPackages = new HashSet<String>();
module = codegenModule;
module.bind(ProcessingEnvironment.class, processingEnvironment);
module.bind(RoundEnvironment.class, roundEnv);
module.bind(CodegenModule.KEYWORDS, keywords);

View File

@ -34,7 +34,7 @@ import com.querydsl.core.annotations.QueryExclude;
* @author tiwe
*
*/
final class ExtendedTypeFactory {
public class ExtendedTypeFactory {
private final Map<List<String>, Type> typeCache = new HashMap<List<String>, Type>();
@ -276,7 +276,7 @@ final class ExtendedTypeFactory {
return env.getTypeUtils().erasure(env.getElementUtils().getTypeElement(clazz.getName()).asType());
}
private Type createType(TypeElement typeElement, TypeCategory category,
protected Type createType(TypeElement typeElement, TypeCategory category,
List<? extends TypeMirror> typeArgs, boolean deep) {
String name = typeElement.getQualifiedName().toString();
String simpleName = typeElement.getSimpleName().toString();
@ -354,7 +354,7 @@ final class ExtendedTypeFactory {
} else if (typeCategory == TypeCategory.SIMPLE) {
for (Class<? extends Annotation> entityAnn : entityAnnotations) {
if (typeElement.getAnnotation(entityAnn) != null) {
if (isSimpleTypeEntity(typeElement, entityAnn)) {
typeCategory = TypeCategory.ENTITY;
}
}
@ -401,7 +401,11 @@ final class ExtendedTypeFactory {
return type;
}
private Type createMapType(Iterator<? extends TypeMirror> typeMirrors, boolean deep) {
public boolean isSimpleTypeEntity(TypeElement typeElement, Class<? extends Annotation> entityAnn) {
return typeElement.getAnnotation(entityAnn) != null;
}
protected Type createMapType(Iterator<? extends TypeMirror> typeMirrors, boolean deep) {
if (!typeMirrors.hasNext()) {
return new SimpleType(Types.MAP, defaultType, defaultType);
}

View File

@ -43,7 +43,7 @@ import com.querydsl.core.util.BeanUtils;
* @author tiwe
*
*/
final class TypeElementHandler {
public class TypeElementHandler {
private final TypeMappings typeMappings;