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

Not all interfaces being reported #100

Closed
mmi-jmacomber opened this issue Aug 17, 2018 · 3 comments
Closed

Not all interfaces being reported #100

mmi-jmacomber opened this issue Aug 17, 2018 · 3 comments

Comments

@mmi-jmacomber
Copy link

Hi,

I am working on a rule for a project which uses a large amount of interfaces that extend interfaces. The rule i am working on is ensuring that certain interfaces exist in the tree for each interface in a certain package. For the majority of the cases this seems to work perfectly and even with many levels of extends it picks them all up but for some reason on some interfaces it goes one level deep then stops. When i check the JavaClass object for the interface on that first level it shows no interfaces even though there are interfaces defined.

For example:
Interface intA extends intB
Interface intB extends intC, intD, intE
Interface intC extends intF
interface intF extends intG

In the rule im looking at interfaces that are assignable to intG and it tells me intA fails that check. When using a breakpoint i see that intA has one interface in the set of interfaces (intB) and if i look at the JavaClass for intB it shows 0 items in the interfaces set, when i would expect to see 5 (intC, intD, intE, intF, intG).

I tried to duplicate it in a stand alone small project using a similar structure but have so far been unable to duplicate it, so im unsure what the variable is that causes it to behave differently here. If you could point me to the right location in the arch unit code, where interfaces are converted into JavaClass objects or similar, to set a breakpoint i can spend some more time trying to figure out why it is not being picked up.

I'm running in Java 7 (oracle, 1.7.0_80), MacOS
archunit version 0.8.3 and i am using the junit library as well

Thanks
Jeff

@codecholeric
Copy link
Collaborator

I could imagine this to be a problem with the import, are you sure, you're catching all these interfaces, when you do ClassFileImporter.import...?
Depending on your configuration, ArchUnit might try to resolve missing classes from the classpath, or just create stubs. In any way, once ArchUnit stops importing, dependencies will only be simple stubs and not have any further superclasses / interfaces.
Is it possible, that intB in your example is missing from the import? This could for example happen, if you have a scenario like

com.foo.Foo extends com.bar.Bar;
com.bar.Bar extends com.baz.Baz;

and then you import via

new ClassFileImporter().importPackages("com.foo", "com.baz")

In this case ArchUnit doesn't know anything about com.bar.Bar and will this just create a simple stub com.bar.Bar without any further super interfaces.

If all those classes are really contained in the import (you can check that, by calling classes.get(intA.class), classes.get(intB.class) and so on) I would need more info to help you, I think. Or I could give you more precise hints where to put your break points for debugging 😉

@mmi-jmacomber
Copy link
Author

mmi-jmacomber commented Aug 17, 2018

Thank you! the issue was what you guessed, i wasnt importing the classes. I had, wrongly, assumed that if the classes where on my classpath they would be found even when i had used the @AnalyzeClasses annotation to only bring in the classes i wished to check. The interfaces that where not being found where outside that specific package and once added worked correctly.

Closing.

@codecholeric
Copy link
Collaborator

Glad you could solve your problem and it was no bug 😃
The problem is, that it is IMHO not completely clear, what the correct behavior in this case is. But you can actually configure the behavior you expected, compare https://www.archunit.org/userguide/html/000_Index.html#_dealing_with_missing_classes.

In short, if you put an archunit.properties file in your classpath root, and add

resolveMissingDependenciesFromClassPath=true

then ArchUnit will resolve all missing classes from the classpath (maybe causing less surprises in such cases). It's deactivated by default since it makes the import slightly slower (in the end it has to import more classes, e.g. JDK base classes).

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