Skip to content

Commit

Permalink
Filter only fields annotations and throw error when searching for a n…
Browse files Browse the repository at this point in the history
…on-index class
  • Loading branch information
leo-bogastry committed Dec 27, 2023
1 parent 2d2e1f9 commit 1278d02
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassInfo;
Expand Down Expand Up @@ -207,9 +206,10 @@ private void validateClassHasFieldId(IndexView index, String entityType) {
DotName className = DotName.createSimple(entityType);
ClassInfo classInfo = index.getClassByName(className);

if (classInfo != null) {
validateRec(index, entityType, classInfo);
if (classInfo == null) {
throw new RuntimeException(String.format("Class '%s' was not found", classInfo));
}
validateRec(index, entityType, classInfo);
}

/**
Expand All @@ -220,27 +220,30 @@ private void validateClassHasFieldId(IndexView index, String entityType) {
private void validateRec(IndexView index, String entityType, ClassInfo classInfo) {
List<FieldInfo> fieldsNamedId = classInfo.fields().stream()
.filter(f -> f.name().equals("id"))
.collect(Collectors.toList());
List<AnnotationInstance> fieldsAnnotatedWithId = classInfo.annotations().stream()
.toList();

List<AnnotationInstance> fieldsAnnotatedWithId = classInfo.fields().stream()
.flatMap(f -> f.annotations().stream())
.filter(a -> a.name().toString().endsWith("persistence.Id"))
.collect(Collectors.toList());
.toList();

// Id field found, break the loop
if (!fieldsNamedId.isEmpty() || !fieldsAnnotatedWithId.isEmpty())
return;

// Id field not found and hope is gone
DotName superClassName = classInfo.superName();
if (fieldsNamedId.isEmpty() && fieldsAnnotatedWithId.isEmpty() && superClassName == null) {
if (superClassName == null) {
throw new IllegalStateException("Cannot generate web links for the class " + entityType +
" because is either missing an `id` field or a field with an `@Id` annotation");
}

// Id field not found but there's still hope
classInfo = index.getClassByName(superClassName);
if (superClassName != null && classInfo != null) {
validateRec(index, entityType, classInfo);
if (classInfo == null) {
throw new RuntimeException(String.format("Class '%s' was not found", classInfo));
}
validateRec(index, entityType, classInfo);
}

/**
Expand Down

0 comments on commit 1278d02

Please sign in to comment.