ArchUnit icon indicating copy to clipboard operation
ArchUnit copied to clipboard

Duplicate error messages for noClasses().should().dependOnClassesThat().resideInAPackage(...) when culprit is in finally clause

Open ttho opened this issue 6 months ago • 1 comments

Since 1.3.0, the following test prints duplicate messages for the incorrect() method:

package test;

import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;

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

    public static class TestClass {

        public void incorrectReporting() throws Exception {
            try {
                int a=0;
            } finally {
                Field field = null;
                field.setBoolean(null, false);
            }
        }

        public void correctReporting() throws Exception {
            Field field = null;
            field.setBoolean(null, false);
        }

        public void alsoCorrectReporting() throws Exception {
            try {
            } finally {
                Field field = null;
                field.setBoolean(null, false);
            }
        }

    }

    @Test
    public void doNotUseReflection() {
        JavaClasses importedClasses = new ClassFileImporter().importPackages("test");
        ArchRule rule = noClasses().should().dependOnClassesThat().resideInAPackage("java.lang.reflect..");
        rule.check(importedClasses);
    }
}

Actual output:

java.lang.AssertionError: Architecture Violation [Priority: MEDIUM] - Rule 'no classes should depend on classes that reside in a package 'java.lang.reflect..'' was violated (4 times):
Method <test.MyArchitectureTest$TestClass.alsoCorrectReporting()> calls method <java.lang.reflect.Field.setBoolean(java.lang.Object, boolean)> in (MyArchitectureTest.java:42)
Method <test.MyArchitectureTest$TestClass.correctReporting()> calls method <java.lang.reflect.Field.setBoolean(java.lang.Object, boolean)> in (MyArchitectureTest.java:35)
Method <test.MyArchitectureTest$TestClass.incorrectReporting()> calls method <java.lang.reflect.Field.setBoolean(java.lang.Object, boolean)> in (MyArchitectureTest.java:29)
Method <test.MyArchitectureTest$TestClass.incorrectReporting()> calls method <java.lang.reflect.Field.setBoolean(java.lang.Object, boolean)> in (MyArchitectureTest.java:29)

Expected output:

java.lang.AssertionError: Architecture Violation [Priority: MEDIUM] - Rule 'no classes should depend on classes that reside in a package 'java.lang.reflect..'' was violated (4 times):
Method <test.MyArchitectureTest$TestClass.alsoCorrectReporting()> calls method <java.lang.reflect.Field.setBoolean(java.lang.Object, boolean)> in (MyArchitectureTest.java:42)
Method <test.MyArchitectureTest$TestClass.correctReporting()> calls method <java.lang.reflect.Field.setBoolean(java.lang.Object, boolean)> in (MyArchitectureTest.java:35)
Method <test.MyArchitectureTest$TestClass.incorrectReporting()> calls method <java.lang.reflect.Field.setBoolean(java.lang.Object, boolean)> in (MyArchitectureTest.java:29)

ttho avatar Aug 05 '24 09:08 ttho