Skip to content

Support for jMolecules' @Identity annotation #2439

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -40,6 +40,7 @@
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.Optionals;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.data.util.StreamUtils;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
Expand All @@ -56,6 +57,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
extends AbstractPersistentProperty<P> {

private static final String SPRING_DATA_PACKAGE = "org.springframework.data";
private static final Class<? extends Annotation> IDENTITY_TYPE = loadIdentityType();

private final @Nullable String value;
private final Map<Class<? extends Annotation>, Optional<? extends Annotation>> annotationCache = new ConcurrentHashMap<>();
Expand All @@ -74,7 +76,8 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
.of(() -> !isTransient() && !isAnnotationPresent(ReadOnlyProperty.class));
private final Lazy<Boolean> isReference = Lazy.of(() -> !isTransient() //
&& (isAnnotationPresent(Reference.class) || super.isAssociation()));
private final Lazy<Boolean> isId = Lazy.of(() -> isAnnotationPresent(Id.class));
private final Lazy<Boolean> isId = Lazy
.of(() -> isAnnotationPresent(Id.class) || IDENTITY_TYPE != null && isAnnotationPresent(IDENTITY_TYPE));
private final Lazy<Boolean> isVersion = Lazy.of(() -> isAnnotationPresent(Version.class));
private final Lazy<TypeInformation<?>> associationTargetType = Lazy.of(() -> {

Expand Down Expand Up @@ -329,4 +332,19 @@ private Stream<? extends AnnotatedElement> getAccessors() {
return Optionals.toStream(Optional.ofNullable(getGetter()), Optional.ofNullable(getSetter()),
Optional.ofNullable(getField()));
}

/**
* Load jMolecules' {@code @Identity} type if present on the classpath. Dedicated method instead of a simple static
* initializer to be able to suppress the compiler warning.
*
* @return can be {@literal null}.
*/
@Nullable
@SuppressWarnings("unchecked")
private static Class<? extends Annotation> loadIdentityType() {

return (Class<? extends Annotation>) ReflectionUtils.loadIfPresent(
"org.jmolecules.ddd.annotation.Identity",
AbstractPersistentProperty.class.getClassLoader());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.stream.Stream;

import org.assertj.core.api.ClassAssert;
import org.jmolecules.ddd.annotation.Identity;
import org.jmolecules.ddd.types.AggregateRoot;
import org.jmolecules.ddd.types.Association;
import org.jmolecules.ddd.types.Identifier;
Expand Down Expand Up @@ -329,6 +330,14 @@ void exposesAssociationTargetClassAsPersistentEntityType() {
.allMatch(it -> it.equals(ClassTypeInformation.from(Sample.class)));
}

@Test // #2438
void detectsJMoleculesIdentity() {

SamplePersistentProperty property = getProperty(JMolecules.class, "identifier");

assertThat(property.isIdProperty()).isTrue();
}

@SuppressWarnings("unchecked")
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");
Expand Down Expand Up @@ -515,6 +524,7 @@ interface NoField {
}

static class JMolecules {
@Identity Long identifier;
Association<JMoleculesAggregate, Identifier> association;
}

Expand Down