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

Java 14 records with empty constructors violate beRecords() rule #998

Closed
SoylentBob opened this issue Nov 8, 2022 · 1 comment · Fixed by #999
Closed

Java 14 records with empty constructors violate beRecords() rule #998

SoylentBob opened this issue Nov 8, 2022 · 1 comment · Fixed by #999

Comments

@SoylentBob
Copy link

Given that I have an arch unit test like this:

package org.example;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

import com.tngtech.archunit.core.importer.ClassFileImporter;
import org.junit.jupiter.api.Test;

public class RecordTest
{

  @Test
  public void recordTest() {
    var javaClasses = new ClassFileImporter().importPackages("org.example");

    classes().that()
        .haveNameMatching("(.*)Record")
        .should()
        .beRecords()
        .because("they should be simple data structures")
        .check(javaClasses);
  }
}

And a record like this in my project:

package org.example;

public record EmptyRecord()
{
}

When I run the arch unit test, then a violation is reported, even though EmptyRecord is a valid (but useless) Java 14 record:

Architecture Violation [Priority: MEDIUM] - Rule 'classes that have name matching '(.*)Record' should be records, because they should be simple data structures' was violated (1 times):
Class <org.example.EmptyRecord> is no record in (EmptyRecord.java:0)
java.lang.AssertionError: Architecture Violation [Priority: MEDIUM] - Rule 'classes that have name matching '(.*)Record' should be records, because they should be simple data structures' was violated (1 times):
Class <org.example.EmptyRecord> is no record in (EmptyRecord.java:0)
	at com.tngtech.archunit.lang.ArchRule$Assertions.assertNoViolation(ArchRule.java:94)
	at com.tngtech.archunit.lang.ArchRule$Assertions.check(ArchRule.java:86)
	at com.tngtech.archunit.lang.ArchRule$Factory$SimpleArchRule.check(ArchRule.java:165)
	at org.example.RecordTest.recordTest(RecordTest.java:20)

When the record is extended with a parameter, then the violation is resolved:

package org.example;

public record EmptyRecord(String someValue)
{
}

A full example to reproduce the issue can be found here.

@hankem
Copy link
Member

hankem commented Nov 8, 2022

Thank you so much for the well crafted bug report including the example! 💚

This happens because ArchUnit uses ASM's ClassVisitor#visitRecordComponent to identify records, which is not invoked for records with a recordComponentsCount of 0. We should probably use ClassVisitor#visit's access flags instead.

codecholeric added a commit that referenced this issue Nov 21, 2022
As `ClassVisitor#visitRecordComponent` is not invoked for empty records
(without components), we better rely on the access flags provided to
`ClassVisitor#visit`.

resolves #998
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants