Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore bridge methods and improve error message in @Embeddable annotation checks #36713

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.Declaration;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.IndexView;
Expand Down Expand Up @@ -317,10 +318,17 @@ private void enlistEmbeddedsAndElementCollections(Collector collector) throws Bu

switch (target.kind()) {
case FIELD:
collectEmbeddedType(embeddedTypes, target.asField().type(), true);
var field = target.asField();
collectEmbeddedType(embeddedTypes, field.declaringClass(), field, field.type(), true);
break;
case METHOD:
collectEmbeddedType(embeddedTypes, target.asMethod().returnType(), true);
var method = target.asMethod();
if (method.isBridge()) {
// Generated by javac for covariant return type override.
// There's another method with a more specific return type, ignore this one.
continue;
}
collectEmbeddedType(embeddedTypes, method.declaringClass(), method, method.returnType(), true);
break;
default:
throw new IllegalStateException(
Expand All @@ -335,10 +343,17 @@ private void enlistEmbeddedsAndElementCollections(Collector collector) throws Bu

switch (target.kind()) {
case FIELD:
collectElementCollectionTypes(embeddedTypes, target.asField().type());
var field = target.asField();
collectElementCollectionTypes(embeddedTypes, field.declaringClass(), field, field.type());
break;
case METHOD:
collectElementCollectionTypes(embeddedTypes, target.asMethod().returnType());
var method = target.asMethod();
if (method.isBridge()) {
// Generated by javac for covariant return type override.
// There's another method with a more specific return type, ignore this one.
continue;
}
collectElementCollectionTypes(embeddedTypes, method.declaringClass(), method, method.returnType());
break;
default:
throw new IllegalStateException(
Expand Down Expand Up @@ -495,44 +510,48 @@ private static void collectModelType(Collector collector, ClassInfo modelClass)
}
}

private void collectEmbeddedType(Set<DotName> embeddedTypes, Type embeddedType, boolean validate)
private void collectEmbeddedType(Set<DotName> embeddedTypes, ClassInfo declaringClass,
Declaration attribute, Type attributeType, boolean validate)
throws BuildException {
DotName className;
switch (embeddedType.kind()) {
switch (attributeType.kind()) {
case CLASS:
className = embeddedType.asClassType().name();
className = attributeType.asClassType().name();
break;
case PARAMETERIZED_TYPE:
className = embeddedType.name();
className = attributeType.name();
break;
default:
// do nothing
return;
}
if (validate && !index.getClassByName(className).hasAnnotation(ClassNames.EMBEDDABLE)) {
throw new BuildException(
className + " is used as an embeddable but does not have an @Embeddable annotation.");
"Type " + className + " must be annotated with @Embeddable, because it is used as an embeddable."
+ " This type is used in class " + declaringClass
+ " for attribute " + attribute + ".");
}
embeddedTypes.add(embeddedType.name());
embeddedTypes.add(attributeType.name());
}

private void collectElementCollectionTypes(Set<DotName> embeddedTypes, Type indexType)
private void collectElementCollectionTypes(Set<DotName> embeddedTypes, ClassInfo declaringClass,
Declaration attribute, Type attributeType)
throws BuildException {
switch (indexType.kind()) {
switch (attributeType.kind()) {
case CLASS:
// Raw collection type, nothing we can do
break;
case PARAMETERIZED_TYPE:
embeddedTypes.add(indexType.name());
var typeArguments = indexType.asParameterizedType().arguments();
embeddedTypes.add(attributeType.name());
var typeArguments = attributeType.asParameterizedType().arguments();
for (Type typeArgument : typeArguments) {
// We don't validate @Embeddable annotations on element collections at the moment
// See https://github.com/quarkusio/quarkus/pull/35822
collectEmbeddedType(embeddedTypes, typeArgument, false);
collectEmbeddedType(embeddedTypes, declaringClass, attribute, typeArgument, false);
}
break;
case ARRAY:
collectEmbeddedType(embeddedTypes, indexType.asArrayType().constituent(), true);
collectEmbeddedType(embeddedTypes, declaringClass, attribute, attributeType.asArrayType().constituent(), true);
break;
default:
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ class HibernateEntityEnhancerMissingEmbeddableAnnotationTest {
.assertException(ex -> assertThat(ex)
.isNotNull()
.hasMessageContainingAll(
EntityWithEmbedded.EmbeddableMissingAnnotation.class.getName(),
"is used as an embeddable but does not have an @Embeddable annotation"));
"Type " + EntityWithEmbedded.EmbeddableMissingAnnotation.class.getName(),
"must be annotated with @Embeddable, because it is used as an embeddable",
"This type is used in class " + EntityWithEmbedded.EmbeddableWithAnnotation.class.getName(),
"for attribute ", "embeddableMissingAnnotation"));

// Just test that the embedded non-ID works correctly over a persist/retrieve cycle
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityManager;
import jakarta.persistence.GeneratedValue;
Expand All @@ -31,6 +32,9 @@
* Checks that the missing @Embeddable check doesn't mistakely report
* types that are annotated with @Embeddable (https://github.com/quarkusio/quarkus/issues/35598)
* or generic type parameters on @Embedded field types (https://github.com/quarkusio/quarkus/issues/36065)
* or overriden getters annotated with @EmbeddedId/@Embedded where the supertype getter returns a type not annotated
* with @Embeddable
* (https://github.com/quarkusio/quarkus/issues/36421).
*/
public class HibernateEntityEnhancerPresentEmbeddableTest {

Expand All @@ -41,7 +45,9 @@ public class HibernateEntityEnhancerPresentEmbeddableTest {
.addClasses(EntityWithEmbedded.class, EmbeddableWithAnnotation.class,
ExtendedEmbeddableWithAnnotation.class,
NestingEmbeddableWithAnnotation.class,
GenericEmbeddableWithAnnotation.class))
GenericEmbeddableWithAnnotation.class,
EntityWithEmbeddedId.class, EntityWithEmbeddedIdAndOverriddenGetter.class,
EmbeddableIdWithAnnotation.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-orm.implicit-naming-strategy", "component-path");

Expand All @@ -50,7 +56,7 @@ public class HibernateEntityEnhancerPresentEmbeddableTest {

// Just test that the generic embeddeds work correctly over a persist/retrieve cycle
@Test
public void smokeTest() {
public void embedded_smokeTest() {
Long id = QuarkusTransaction.requiringNew().call(() -> {
EntityWithEmbedded entity = new EntityWithEmbedded();
entity.setName("name");
Expand Down Expand Up @@ -97,6 +103,36 @@ public void smokeTest() {
});
}

// Just test that the embeddedIds work correctly over a persist/retrieve cycle
@Test
public void embeddedId_smokeTest() {
QuarkusTransaction.requiringNew().run(() -> {
EntityWithEmbeddedId entity1 = new EntityWithEmbeddedId();
entity1.setId(new EmbeddableIdWithAnnotation("1"));
em.persist(entity1);
});

QuarkusTransaction.requiringNew().run(() -> {
EntityWithEmbeddedId entity = em.find(EntityWithEmbeddedId.class, new EmbeddableIdWithAnnotation("1"));
assertThat(entity).isNotNull();
});
}

@Test
public void embeddedIdAndOverriddenGetter_smokeTest() {
QuarkusTransaction.requiringNew().run(() -> {
EntityWithEmbeddedIdAndOverriddenGetter entity1 = new EntityWithEmbeddedIdAndOverriddenGetter();
entity1.setId(new EmbeddableIdWithAnnotation("2"));
em.persist(entity1);
});

QuarkusTransaction.requiringNew().run(() -> {
EntityWithEmbeddedIdAndOverriddenGetter entity = em.find(EntityWithEmbeddedIdAndOverriddenGetter.class,
new EmbeddableIdWithAnnotation("2"));
assertThat(entity).isNotNull();
});
}

@Entity
public static class EntityWithEmbedded {

Expand Down Expand Up @@ -290,4 +326,55 @@ public void setValue(T value) {
}
}

@Entity
public static class EntityWithEmbeddedId {
@EmbeddedId
private EmbeddableIdWithAnnotation id;

public EmbeddableIdWithAnnotation getId() {
return id;
}

public void setId(EmbeddableIdWithAnnotation id) {
this.id = id;
}
}

@MappedSuperclass
public interface Identifiable {
Object getId();
}

@Entity
public static class EntityWithEmbeddedIdAndOverriddenGetter implements Identifiable {
private EmbeddableIdWithAnnotation id;

@Override
@EmbeddedId
public EmbeddableIdWithAnnotation getId() {
return id;
}

public void setId(EmbeddableIdWithAnnotation id) {
this.id = id;
}
}

@Embeddable
public static class EmbeddableIdWithAnnotation {
private String text;

protected EmbeddableIdWithAnnotation() {
// For Hibernate ORM only - it will change the property value through reflection
}

public EmbeddableIdWithAnnotation(String text) {
this.text = text;
}

public String getText() {
return text;
}
}

}
Loading