3

My Java project with multiple subprojects is using the Immutables library for value objects.

package foo.bar;

import org.immutables.value.Value;

public class MyContainerClass {

    @Value.Immutable
    public interface Baz {
        String name();
    }

    // more interfaces here
}

This generates a class in the same package named foo.bar.ImmutableBaz implementing the Baz interface. Another subproject recently declared another interface of the same name in the same package foo.bar, but a different container class. This resulted in the generation of a second foo.bar.ImmutableBaz class. Both classes inadvertently ended up with the same fully qualified name which caused issues at runtime with the wrong class being loaded.

How would I best detect these naming clashes, for example in a test or as part of a CI pipeline?

I know I can specify the target package with Immutables, but I'm looking to detect and avoid unintentional clashes.

Björn Marschollek
  • 9,899
  • 9
  • 40
  • 66
  • Maybe use different [modules](https://www.oracle.com/corporate/features/understanding-java-9-modules.html)? Two different modules where each module contains the same package and class name. – Abra Mar 24 '21 at 16:55
  • Thanks. I'm not looking to use both classes, but to avoid the naming conflict in the generated classes in the first place. The reason being that these classes will be used in the JSON deserialisation and I want to make sure that there's no (accidental) ambiguity. – Björn Marschollek Mar 25 '21 at 08:37

0 Answers0