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

rule is not breaking the test if class is accessed by another package #168

Closed
eddumelendez opened this issue Apr 12, 2019 · 4 comments
Closed

Comments

@eddumelendez
Copy link
Contributor

Currently, I have the following structure

  • com.example.module1. Module1Utils.java
  • com.example.module1. MyClass1.java
  • com.example.module2. Module2Utils.java
  • com.example.module2. MyClass2.java

and the rule I am writing is:

JavaClasses importedClasses = new ClassFileImporter()
				.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
				.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_JARS)
				.importPackages("com.example");

theClass(Module1Utils.class)
				.should()
				.onlyBeAccessed()
				.byClassesThat()
				.resideInAPackage("..module1..")
				.check(importedClasses);

when I change my code to make MyClass2 use ModuleUtils1, the test is still green.

@codecholeric
Copy link
Collaborator

I need some more information how you "use" ModuleUtils1. Since I've copied your example and the test fails correctly, if I add an access from MyClass2 to Module1Utils, so I can't really reproduce your problem at the moment.
One common pitfall would be, that the compiler inlines constant primitives or strings, so if you use Module1Utils by accessing a public static final String anything in Module1Utils, this might be a limitation of ArchUnit (because what is missing from the bytecode can't be asserted).

@eddumelendez
Copy link
Contributor Author

eddumelendez commented Apr 15, 2019

hi @codecholeric, that's exactly my case this Module1Utils contains some public static final String anything. So, this is an improvement in archunit? or are there another way to do this?

@codecholeric
Copy link
Collaborator

This is unfortunately a known limitation. ArchUnit analyzes bytecode, so what's missing from the bytecode can't be asserted. If you look at the compiled classes, each of those classes has their own inlined string value and the reference of MyClass2 to ModuleUtils1 is gone.
One could argue that this dependency is not the most harmful one, cause such a constant can easily be moved to any common place if necessary without big pain.
But AFAIK the only opinion to prevent this sort of dependency would be to enforce that no public static final fields would be allowed. If you would have to access this constant through a method, the reference is there and can be checked for.

@codecholeric
Copy link
Collaborator

Going to close this, because there is nothing ArchUnit can do, if the information is missing from the bytecode.

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

No branches or pull requests

2 participants