TNG / ArchUnit

A Java architecture test library, to specify and assert architecture rules in plain Java
http://archunit.org
Apache License 2.0
3.18k stars 288 forks source link

ClassFileImporter fails to load classes with synthetic methods #1214

Closed dpolivaev closed 9 months ago

dpolivaev commented 9 months ago

Add the following class as file ClassWithBuilder.java to folder archunit/src/test/java/com/tngtech/archunit/core/importer/testexamples/

and see how ClassFileImporterTest#imports_urls_of_folders fails, the test result containing exception trace is attached.

The decompiled version of the class contains lines

private ClassWithBuilder(Builder builder) {
        this.string = ClassWithBuilder.Builder.access$084(builder, "!");
    }

and this synthetic method call lets the loader fail.


package com.tngtech.archunit.core.importer.testexamples;

public class ClassWithBuilder {
    public final String string;
    private ClassWithBuilder(Builder builder) {
        string = builder.string += "!";
    }

    public static class Builder {
        private String string;
        public ClassWithBuilder build() {
            return new ClassWithBuilder(this);
        }

        public Builder number(int number) {
            return this;
        }

        public Builder string(String string) {
            this.string = string;
            return this;
        }
    }

    public static void main(String[] args) {
        ClassWithBuilder classWithBuilder = new Builder().number(1).string("string").build();
        System.out.println(classWithBuilder.string);
    }
}

ClassWithBuilder.java.zip TEST-com.tngtech.archunit.core.importer.ClassFileImporterTest.xml.zip

dpolivaev commented 9 months ago

I know that the example seems to make no sense because the use of the += operator is not correct, we need to use +. However, I found the error analyzing our code base, and it took me a while to understand what goes on here.

dpolivaev commented 9 months ago

Resolved in https://github.com/TNG/ArchUnit/pull/1203