Skip to content

Commit

Permalink
Support for jMolecules' Association type.
Browse files Browse the repository at this point in the history
We no recognize properties of type org.jmolecules.ddd.types.Association as associations in our PersistentProperty model.

Also, we now register JMolecules Converter implementations for Association and Identifier in CustomConversions so that they can persisted like their embedded primitive value out of the box.

Fixes #2315.
Original pull request: #2316.
  • Loading branch information
odrotbohm authored and mp911de committed Mar 1, 2021
1 parent 3f16095 commit 6b0292c
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 9 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@
<version>0.1.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jmolecules.integrations</groupId>
<artifactId>jmolecules-spring</artifactId>
<version>${jmolecules-integration}</version>
<optional>true</optional>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class CustomConversions {
defaults.addAll(JodaTimeConverters.getConvertersToRegister());
defaults.addAll(Jsr310Converters.getConvertersToRegister());
defaults.addAll(ThreeTenBackPortConverters.getConvertersToRegister());
defaults.addAll(JMoleculesConverters.getConvertersToRegister());

DEFAULT_CONVERTERS = Collections.unmodifiableList(defaults);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.convert;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import org.jmolecules.spring.AssociationToPrimitivesConverter;
import org.jmolecules.spring.IdentifierToPrimitivesConverter;
import org.jmolecules.spring.PrimitivesToAssociationConverter;
import org.jmolecules.spring.PrimitivesToIdentifierConverter;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.util.ClassUtils;

/**
* Registers jMolecules converter implementations with {@link CustomConversions} if the former is on the classpath.
*
* @author Oliver Drotbohm
* @since 2.5
*/
public class JMoleculesConverters {

private static final boolean JMOLECULES_PRESENT = ClassUtils.isPresent(
"org.jmolecules.spring.IdentifierToPrimitivesConverter",
JMoleculesConverters.class.getClassLoader());

/**
* Returns all jMolecules-specific converters to be registered.
*
* @return will never be {@literal null}.
*/
public static Collection<Object> getConvertersToRegister() {

if (!JMOLECULES_PRESENT) {
return Collections.emptyList();
}

List<Object> converters = new ArrayList<>();

Supplier<ConversionService> conversionService = () -> DefaultConversionService.getSharedInstance();

IdentifierToPrimitivesConverter toPrimitives = new IdentifierToPrimitivesConverter(conversionService);
PrimitivesToIdentifierConverter toIdentifier = new PrimitivesToIdentifierConverter(conversionService);

converters.add(toPrimitives);
converters.add(toIdentifier);
converters.add(new AssociationToPrimitivesConverter<>(toPrimitives));
converters.add(new PrimitivesToAssociationConverter<>(toIdentifier));

return converters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@
public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>> implements PersistentProperty<P> {

private static final Field CAUSE_FIELD;
private static final Class<?> ASSOCIATION_TYPE;

static {

CAUSE_FIELD = ReflectionUtils.findRequiredField(Throwable.class, "cause");
ASSOCIATION_TYPE = ReflectionUtils.loadIfPresent("org.jmolecules.ddd.types.Association",
AbstractPersistentProperty.class.getClassLoader());
}

private final String name;
Expand Down Expand Up @@ -241,7 +245,8 @@ public boolean isImmutable() {
*/
@Override
public boolean isAssociation() {
return isAnnotationPresent(Reference.class);
return isAnnotationPresent(Reference.class) //
|| ASSOCIATION_TYPE != null && ASSOCIATION_TYPE.isAssignableFrom(rawType);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp

private final Lazy<Boolean> isWritable = Lazy
.of(() -> !isTransient() && !isAnnotationPresent(ReadOnlyProperty.class));
private final Lazy<Boolean> isReference = Lazy.of(() -> !isTransient() && isAnnotationPresent(Reference.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> isVersion = Lazy.of(() -> isAnnotationPresent(Version.class));

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/springframework/data/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,21 @@ public static Object getPrimitiveDefault(Class<?> type) {
throw new IllegalArgumentException(String.format("Primitive type %s not supported!", type));
}

/**
* Loads the class with the given name using the given {@link ClassLoader}.
*
* @param name the name of the class to be loaded.
* @param classLoader the {@link ClassLoader} to use to load the class.
* @return the {@link Class} or {@literal null} in case the class can't be loaded for any reason.
* @since 2.5
*/
@Nullable
public static Class<?> loadIfPresent(String name, ClassLoader classLoader) {

try {
return ClassUtils.forName(name, classLoader);
} catch (Exception o_O) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.convert;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.text.DateFormat;
Expand All @@ -28,6 +29,8 @@
import java.util.Map;
import java.util.function.Predicate;

import org.jmolecules.ddd.types.Association;
import org.jmolecules.ddd.types.Identifier;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import org.springframework.aop.framework.ProxyFactory;
Expand Down Expand Up @@ -272,6 +275,24 @@ void doesNotSkipUserConverterConverterEvenWhenConfigurationWouldNotAllowIt() {
verify(registry).addConverter(any(LocalDateTimeToDateConverter.class));
}

@Test // GH-2315
void addsAssociationConvertersByDefault() {

CustomConversions conversions = new CustomConversions(StoreConversions.NONE, Collections.emptyList());

assertThat(conversions.hasCustomWriteTarget(Association.class)).isTrue();
assertThat(conversions.hasCustomReadTarget(Object.class, Association.class)).isTrue();
}

@Test // GH-2315
void addsIdentifierConvertersByDefault() {

CustomConversions conversions = new CustomConversions(StoreConversions.NONE, Collections.emptyList());

assertThat(conversions.hasCustomWriteTarget(Identifier.class)).isTrue();
assertThat(conversions.hasCustomReadTarget(String.class, Identifier.class)).isTrue();
}

private static Class<?> createProxyTypeFor(Class<?> type) {

ProxyFactory factory = new ProxyFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ void returnsAccessorsForGenericReturnType() {
assertThat(property.getGetter()).isNotNull();
}

@Test // GH-2315
void detectsJMoleculesAssociation() {

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

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

private <T> BasicPersistentEntity<T, SamplePersistentProperty> getEntity(Class<T> type) {
return new BasicPersistentEntity<>(ClassTypeInformation.from(type));
}
Expand Down Expand Up @@ -344,11 +352,6 @@ public boolean isVersionProperty() {
return false;
}

@Override
public boolean isAssociation() {
return false;
}

@Override
protected Association<SamplePersistentProperty> createAssociation() {
return null;
Expand Down Expand Up @@ -387,4 +390,8 @@ static class Sample {
class TreeMapWrapper {
TreeMap<String, TreeMap<String, String>> map;
}

class JMolecules {
org.jmolecules.ddd.types.Association association;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Optional;
import java.util.stream.Stream;

import org.jmolecules.ddd.types.Association;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.annotation.AliasFor;
Expand Down Expand Up @@ -293,6 +294,11 @@ public void missingRequiredFieldThrowsException() {
.withMessageContaining(NoField.class.getName());
}

@Test // GH-2315
void detectesJMoleculesAssociation() {
assertThat(getProperty(JMolecules.class, "association").isAssociation()).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 @@ -414,8 +420,7 @@ public String getProperty() {
@Retention(RetentionPolicy.RUNTIME)
@Target(value = { FIELD, METHOD, ANNOTATION_TYPE })
@Id
public @interface MyId {
}
public @interface MyId {}

static class FieldAccess {
String name;
Expand Down Expand Up @@ -477,4 +482,8 @@ interface NoField {

String getFirstname();
}

static class JMolecules {
Association association;
}
}

0 comments on commit 6b0292c

Please # to comment.