Skip to content

Commit

Permalink
GH-301 - Avoid checking synthetic fields in JMoleculesDddRules.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Feb 27, 2025
1 parent 018b369 commit 051c783
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.tngtech.archunit.base.DescribedPredicate.*;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.*;
import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Predicates.*;
import static com.tngtech.archunit.core.domain.properties.HasModifiers.Predicates.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
Expand All @@ -35,15 +36,18 @@
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaField;
import com.tngtech.archunit.core.domain.JavaModifier;
import com.tngtech.archunit.core.domain.JavaParameterizedType;
import com.tngtech.archunit.core.domain.JavaType;
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
import com.tngtech.archunit.core.domain.properties.HasModifiers;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.CompositeArchRule;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition;
import com.tngtech.archunit.lang.syntax.elements.GivenFieldsConjunction;

/**
* A set of ArchUnit rules that allow verification of domain models. In short the rules here verify:
Expand Down Expand Up @@ -80,6 +84,7 @@ public class JMoleculesDddRules {
private static DescribedPredicate<JavaClass> IS_VALUE_OBJECT = IS_IMPLEMENTING_VALUE_OBJECT
.or(IS_ANNOTATED_VALUE_OBJECT);
private static DescribedPredicate<JavaClass> IS_IDENTIFIER = implement(Identifier.class);
private static DescribedPredicate<HasModifiers> IS_SYNTHETIC = modifier(JavaModifier.SYNTHETIC);

/**
* An {@link ArchRule} that's composed of all other rules declared in this class.
Expand Down Expand Up @@ -121,8 +126,8 @@ public static ArchRule all() {
*/
public static ArchRule entitiesShouldBeDeclaredForUseInSameAggregate() {

return ArchRuleDefinition.fields() //
.that(new OwnerMatches(IS_IDENTIFIABLE)) //
return nonSyntheticFields() //
.and(new OwnerMatches(IS_IDENTIFIABLE)) //
.and(areAssignableTo(Entity.class).and(not(areAssignableTo(AggregateRoot.class)))) //
.should(beDeclaredToBeUsedWithDeclaringAggregate()) //
.allowEmptyShould(true); //
Expand Down Expand Up @@ -152,8 +157,8 @@ public static ArchRule aggregateReferencesShouldBeViaIdOrAssociation() {
.or(hasFieldTypeAnnotatedWith(org.jmolecules.ddd.annotation.AggregateRoot.class))
.or(hasParameterizedFieldOfTypeAnnotatedWith(org.jmolecules.ddd.annotation.AggregateRoot.class));

return ArchRuleDefinition.fields() //
.that(new OwnerMatches(IS_IDENTIFIABLE).and(referenceAnAggregateRoot)) //
return nonSyntheticFields() //
.and(new OwnerMatches(IS_IDENTIFIABLE).and(referenceAnAggregateRoot))
.should(new ShouldUseIdReferenceOrAssociation())
.allowEmptyShould(true);
}
Expand All @@ -180,12 +185,16 @@ public static ArchRule annotatedEntitiesAndAggregatesNeedToHaveAnIdentifier() {
*/
public static ArchRule valueObjectsMustNotReferToIdentifiables() {

return ArchRuleDefinition.fields() //
.that(new OwnerMatches(IS_VALUE_OBJECT.or(IS_IDENTIFIER))) //
return nonSyntheticFields() //
.and(new OwnerMatches(IS_VALUE_OBJECT.or(IS_IDENTIFIER))) //
.should(new FieldTypeMustNotMatchCondition(IS_IDENTIFIABLE)) //
.allowEmptyShould(true);
}

private static GivenFieldsConjunction nonSyntheticFields() {
return ArchRuleDefinition.fields().that(not(IS_SYNTHETIC));
}

private static IsDeclaredToUseTheSameAggregate beDeclaredToBeUsedWithDeclaringAggregate() {
return new IsDeclaredToUseTheSameAggregate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
*/
package org.jmolecules.archunit;

import static com.tngtech.archunit.core.domain.JavaClass.Predicates.*;
import static org.assertj.core.api.Assertions.*;
import static org.jmolecules.archunit.TestUtils.*;

import lombok.Value;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jmolecules.ddd.annotation.Identity;
import org.jmolecules.ddd.types.AggregateRoot;
Expand Down Expand Up @@ -73,6 +77,15 @@ void detectsViolations(JavaClasses classes) {
);
}

@ArchTest // GH-301
void doesNotRejectSyntheticOwnerFieldsOfNonStaticInnerClasses(JavaClasses classes) {

EvaluationResult result = JMoleculesDddRules.valueObjectsMustNotReferToIdentifiables()
.evaluate(classes.that(simpleName("MyInnerClass")));

assertThat(result.hasViolation()).isFalse();
}

static class SampleIdentifier implements Identifier {}

static abstract class SampleAggregate implements AggregateRoot<SampleAggregate, SampleIdentifier> {
Expand Down Expand Up @@ -143,4 +156,18 @@ static class ThirdAnnotatedAggregate {
@org.jmolecules.ddd.annotation.Identity Long id;
AnnotatedEntity valid;
}

// GH-301

@org.jmolecules.ddd.annotation.AggregateRoot
static class MyAggregateRoot {

@Identity UUID id;
MyInnerClass myInnerClass;

@Value
class MyInnerClass implements ValueObject {
String param;
}
}
}

0 comments on commit 051c783

Please sign in to comment.