use of javax.lang.model.util.Elements in project requery by requery.
the class AttributeMember method processAssociativeAnnotations.
private void processAssociativeAnnotations(ProcessingEnvironment processingEnvironment, ElementValidator validator) {
Optional<Annotation> oneToOne = annotationOf(OneToOne.class);
Optional<Annotation> oneToMany = annotationOf(OneToMany.class);
Optional<Annotation> manyToOne = annotationOf(ManyToOne.class);
Optional<Annotation> manyToMany = annotationOf(ManyToMany.class);
oneToOne = oneToOne.isPresent() ? oneToOne : annotationOf(javax.persistence.OneToOne.class);
oneToMany = oneToMany.isPresent() ? oneToMany : annotationOf(javax.persistence.OneToMany.class);
manyToOne = manyToOne.isPresent() ? manyToOne : annotationOf(javax.persistence.ManyToOne.class);
manyToMany = manyToMany.isPresent() ? manyToMany : annotationOf(javax.persistence.ManyToMany.class);
if (Stream.of(oneToOne, oneToMany, manyToOne, manyToMany).filter(Optional::isPresent).count() > 1) {
validator.error("Cannot have more than one associative annotation per field");
}
if (oneToOne.isPresent()) {
cardinality = Cardinality.ONE_TO_ONE;
ReflectiveAssociation reflect = new ReflectiveAssociation(oneToOne.get());
mappedBy = reflect.mappedBy();
cascadeActions = reflect.cascade();
if (!isForeignKey()) {
isReadOnly = true;
if (!isKey()) {
isUnique = true;
}
}
}
if (oneToMany.isPresent()) {
isIterable = true;
cardinality = Cardinality.ONE_TO_MANY;
isReadOnly = true;
ReflectiveAssociation reflect = new ReflectiveAssociation(oneToMany.get());
mappedBy = reflect.mappedBy();
cascadeActions = reflect.cascade();
checkIterable(validator);
processOrderBy();
}
if (manyToOne.isPresent()) {
cardinality = Cardinality.MANY_TO_ONE;
isForeignKey = true;
ReflectiveAssociation reflect = new ReflectiveAssociation(manyToOne.get());
cascadeActions = reflect.cascade();
if (deleteAction == null) {
deleteAction = ReferentialAction.CASCADE;
}
if (updateAction == null) {
updateAction = ReferentialAction.CASCADE;
}
}
if (manyToMany.isPresent()) {
isIterable = true;
cardinality = Cardinality.MANY_TO_MANY;
ReflectiveAssociation reflect = new ReflectiveAssociation(manyToMany.get());
mappedBy = reflect.mappedBy();
cascadeActions = reflect.cascade();
Optional<JunctionTable> junctionTable = annotationOf(JunctionTable.class);
Optional<javax.persistence.JoinTable> joinTable = annotationOf(javax.persistence.JoinTable.class);
if (junctionTable.isPresent()) {
Elements elements = processingEnvironment.getElementUtils();
associativeDescriptor = new JunctionTableAssociation(elements, this, junctionTable.get());
} else if (joinTable.isPresent()) {
associativeDescriptor = new JoinTableAssociation(joinTable.get());
}
isReadOnly = true;
checkIterable(validator);
processOrderBy();
}
if (isForeignKey()) {
if (deleteAction == ReferentialAction.SET_NULL && !isNullable()) {
validator.error("Cannot SET_NULL on optional attribute", ForeignKey.class);
}
// user mirror so generated type can be referenced
Optional<? extends AnnotationMirror> mirror = Mirrors.findAnnotationMirror(element(), ForeignKey.class);
if (mirror.isPresent()) {
referencedType = mirror.flatMap(m -> Mirrors.findAnnotationValue(m, "references")).map(value -> value.getValue().toString()).orElse(null);
} else if (!typeMirror().getKind().isPrimitive()) {
referencedType = typeMirror().toString();
}
}
}
use of javax.lang.model.util.Elements in project requery by requery.
the class EntityProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// types to generate in this round
Map<TypeElement, EntityElement> entities = new HashMap<>();
SourceLanguage.map(processingEnv);
Types types = processingEnv.getTypeUtils();
Set<TypeElement> annotationElements = new LinkedHashSet<>();
if (isEmptyKotlinAnnotationSet(annotations)) {
annotationElements.addAll(SourceLanguage.getAnnotations());
} else {
annotationElements.addAll(annotations);
}
for (TypeElement annotation : annotationElements) {
for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
typeElementOf(element).ifPresent(typeElement -> {
EntityElement entity = null;
if (isEntity(typeElement)) {
entity = computeType(entities, typeElement);
String model = entity.modelName();
graphs.computeIfAbsent(model, key -> new EntityGraph(types, embeddedTypes)).add(entity);
} else if (isSuperclass(typeElement)) {
entity = computeType(superTypes, typeElement);
} else if (isEmbeddable(typeElement)) {
entity = computeType(embeddedTypes, typeElement);
}
if (entity != null) {
entity.addAnnotationElement(annotation, element);
}
});
}
}
// process
boolean hasErrors = false;
Set<ElementValidator> validators = new LinkedHashSet<>();
Elements elements = processingEnv.getElementUtils();
for (EntityElement entity : entities.values()) {
// add the annotated elements from the super type (if any)
if (entity.element().getKind() == ElementKind.INTERFACE) {
List<? extends TypeMirror> interfaces = entity.element().getInterfaces();
for (TypeMirror mirror : interfaces) {
TypeElement superElement = elements.getTypeElement(mirror.toString());
if (superElement != null) {
mergeSuperType(entity, superElement);
}
}
}
TypeMirror typeMirror = entity.element().getSuperclass();
while (typeMirror.getKind() != TypeKind.NONE) {
TypeElement superElement = elements.getTypeElement(typeMirror.toString());
if (superElement != null) {
mergeSuperType(entity, superElement);
typeMirror = superElement.getSuperclass();
} else {
break;
}
}
// process the entity
Set<ElementValidator> results = entity.process(processingEnv);
validators.addAll(results);
}
for (EntityElement entity : embeddedTypes.values()) {
Set<ElementValidator> results = entity.process(processingEnv);
validators.addAll(results);
}
for (EntityGraph graph : graphs.values()) {
EntityGraphValidator validator = new EntityGraphValidator(processingEnv, graph);
Set<ElementValidator> results = validator.validate();
validators.addAll(results);
}
if (ElementValidator.hasErrors(validators)) {
hasErrors = true;
StringBuilder sb = new StringBuilder("Model has error(s) code generation may fail: ");
validators.forEach(validator -> sb.append(validator.toString()));
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, sb);
}
// generate
Set<SourceGenerator> generators = new LinkedHashSet<>();
if (!hasErrors || getOption(GENERATE_ALWAYS, true)) {
for (EntityDescriptor entity : entities.values()) {
EntityGraph graph = graphs.get(entity.modelName());
if (graph != null) {
generators.add(new EntityGenerator(processingEnv, graph, entity, null));
}
}
}
if (getOption(GENERATE_MODEL, true)) {
Map<String, Collection<EntityDescriptor>> packagesMap = new LinkedHashMap<>();
Set<EntityDescriptor> allEntities = graphs.values().stream().flatMap(graph -> graph.entities().stream()).collect(Collectors.toSet());
for (EntityDescriptor entity : allEntities) {
EntityGraph graph = graphs.get(entity.modelName());
String packageName = findModelPackageName(graph);
packagesMap.computeIfAbsent(packageName, key -> new LinkedHashSet<>());
packagesMap.get(packageName).addAll(graph.entities());
}
for (EntityDescriptor entity : entities.values()) {
EntityGraph graph = graphs.get(entity.modelName());
String packageName = findModelPackageName(graph);
if (entity.generatesAdditionalTypes()) {
packagesMap.remove(packageName);
}
}
generators.addAll(packagesMap.entrySet().stream().filter(entry -> !entry.getValue().isEmpty()).filter(entry -> !generatedModelPackages.contains(entry.getKey())).map(entry -> {
generatedModelPackages.add(entry.getKey());
return entry;
}).map(entry -> new ModelGenerator(processingEnv, entry.getKey(), entry.getValue())).collect(Collectors.toList()));
}
for (SourceGenerator generator : generators) {
try {
generator.generate();
} catch (IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage());
}
}
return false;
}
use of javax.lang.model.util.Elements in project requery by requery.
the class EntityType method builderType.
@Override
public Optional<TypeMirror> builderType() {
Optional<Entity> entityAnnotation = annotationOf(Entity.class);
if (entityAnnotation.isPresent()) {
Entity entity = entityAnnotation.get();
Elements elements = processingEnvironment.getElementUtils();
TypeMirror mirror = null;
try {
// easiest way to get the class TypeMirror
Class<?> builderClass = entity.builder();
if (builderClass != void.class) {
mirror = elements.getTypeElement(builderClass.getName()).asType();
}
} catch (MirroredTypeException typeException) {
mirror = typeException.getTypeMirror();
}
if (mirror != null && mirror.getKind() != TypeKind.VOID) {
return Optional.of(mirror);
}
}
if (builderFactoryMethod().isPresent()) {
return Optional.of(builderFactoryMethod().get().getReturnType());
}
return ElementFilter.typesIn(element().getEnclosedElements()).stream().filter(element -> element.getSimpleName().toString().contains("Builder")).map(Element::asType).filter(Objects::nonNull).filter(type -> type.getKind() != TypeKind.VOID).findFirst();
}
use of javax.lang.model.util.Elements in project requery by requery.
the class EntityType method process.
@Override
public Set<ElementValidator> process(ProcessingEnvironment processingEnvironment) {
// create attributes for fields that have no annotations
if (element().getKind().isInterface() || isImmutable() || isUnimplementable()) {
ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(this::computeAttribute);
} else {
// private/static/final members fields are skipped
Set<VariableElement> elements = ElementFilter.fieldsIn(element().getEnclosedElements()).stream().filter(element -> !element.getModifiers().contains(Modifier.PRIVATE) && !element.getModifiers().contains(Modifier.STATIC) && (!element.getModifiers().contains(Modifier.FINAL) || isImmutable())).collect(Collectors.toSet());
if (elements.isEmpty()) {
// if nothing to process try the getters instead
ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(this::computeAttribute);
} else {
elements.forEach(this::computeAttribute);
}
}
// find listener annotated methods
ElementFilter.methodsIn(element().getEnclosedElements()).forEach(element -> ListenerAnnotations.all().forEach(annotation -> {
if (element.getAnnotation(annotation) != null) {
ListenerMethod listener = listeners.computeIfAbsent(element, key -> new ListenerMethod(element));
listener.annotations().put(annotation, element.getAnnotation(annotation));
}
}));
Set<ProcessableElement<?>> elements = new LinkedHashSet<>();
attributes().values().forEach(attribute -> elements.add((ProcessableElement<?>) attribute));
elements.addAll(listeners.values());
Set<ElementValidator> validations = new LinkedHashSet<>();
elements.forEach(element -> validations.addAll(element.process(processingEnvironment)));
ElementValidator validator = new ElementValidator(element(), processingEnvironment);
Entity entity = annotationOf(Entity.class).orElse(null);
if (entity != null && !Names.isEmpty(entity.name()) && !SourceVersion.isIdentifier(entity.name())) {
validator.error("Invalid class identifier " + entity.name(), Entity.class);
}
if (element().getNestingKind() == NestingKind.ANONYMOUS) {
validator.error("Entity annotation cannot be applied to anonymous class");
}
if (element().getKind() == ElementKind.ENUM) {
validator.error("Entity annotation cannot be applied to an enum class");
}
if (attributes.values().isEmpty()) {
validator.warning("Entity contains no attributes");
}
if (!isReadOnly() && !isEmbedded() && attributes.values().size() == 1 && attributes.values().iterator().next().isGenerated()) {
validator.warning("Entity contains only a single generated attribute may fail to persist");
}
checkReserved(tableName(), validator);
validations.add(validator);
return validations;
}
use of javax.lang.model.util.Elements in project requery by requery.
the class EntityType method typeName.
@Override
public QualifiedName typeName() {
String entityName = Stream.of(Mirrors.findAnnotationMirror(element(), Entity.class), Mirrors.findAnnotationMirror(element(), javax.persistence.Entity.class)).filter(Optional::isPresent).map(Optional::get).map(mirror -> Mirrors.findAnnotationValue(mirror, "name")).filter(Optional::isPresent).map(Optional::get).map(value -> value.getValue().toString()).filter(name -> !Names.isEmpty(name)).findAny().orElse("");
Elements elements = processingEnvironment.getElementUtils();
String packageName = elements.getPackageOf(element()).getQualifiedName().toString();
// if set in the annotation just use that
if (!Names.isEmpty(entityName)) {
return new QualifiedName(packageName, entityName);
}
String typeName = element().getSimpleName().toString();
if (element().getKind().isInterface()) {
// maybe I<Something> style
if (typeName.startsWith("I") && Character.isUpperCase(typeName.charAt(1))) {
entityName = typeName.substring(1);
} else {
entityName = typeName + "Entity";
}
} else {
entityName = Names.removeClassPrefixes(typeName);
if (entityName.equals(typeName)) {
entityName = typeName + (isImmutable() || isUnimplementable() ? "Type" : "Entity");
}
}
return new QualifiedName(packageName, entityName);
}
Aggregations