1

General package dependency question

Under what circumstances can there be a dependency cycle between packages P1 and P2 if no class and interface of P1 uses P2? I.e. where does the "hidden dependency" come from?

Concrete example with jdepend

jdepend says I have a package dependency between stsimulator and stsimulator.ststraversal, but no class or interface of stsimulator uses any part of stsimulator.ststraversal, meaning

  • jdepend's dependencies-explorer in Eclipse says so, and
  • all classes/interfaces of stsimulator neither import stsimuator.* nor use the string "ststraversal".

How can that be?

Part of my package dependency cycle as printed by jdepend

--------------------------------------------------
- Package Dependency Cycles:
--------------------------------------------------

stsimulator
    |
    |   stsimulator.ststraversal
    |-> stsimulator


stsimulator.sts
    |
    |   stsimulator.interpreter.javacc
    |-> stsimulator
    |   stsimulator.ststraversal
    |-> stsimulator

stsimulator.ststraversal
    |
    |   stsimulator
    |-> stsimulator.ststraversal
DaveFar
  • 7,078
  • 4
  • 50
  • 90

1 Answers1

5

The same thing happened to us. The scenario was as follows:
We had test classes in the same package structure but in different source directories. Both the actual code and the test code was compiled into the same output directory (this is the default in Eclipse unless you specify otherwise, I think). Since JDepend doesn't analyse the source code but the compiled code, our test classes have accidentally introduced phantom dependencies between packages.

Frettman
  • 2,251
  • 1
  • 13
  • 9
  • +1 for "jdepend analyses the compiled code". But my test classes are in the directory workspace/stsimulatorLazyOTFBranch/build/test/, the production code is in workspace/stsimulatorLazyOTFBranch/build/classes. Furthermore, I use `jdepend.addDirectory("/home/dball/workspace/stsimulatorLazyOTFBranch/build/classes");` in a junit cycleTest, which indeterministically fails in about 90% of the cases. And jdepend's dependencies-explorer in Eclipse does not list dependencies on stsimulator.ststraversal when I inspect stsimulator. *Very strange*. Would be great if you had another idea/hint. – DaveFar Oct 14 '11 at 16:04
  • First of all, do your **test** classes of stsimulator actually have dependencies on stsimulator.ststraversal? I mean, is it even possible that your test classes could somehow introduce these dependencies? Second, I would check if the input given to JDepend is actually always the same in your JUnit tests. If it isn't that might explain the seemingly random failures, or at least it would indicate that the problem doesn't lie with JDepend. – Frettman Oct 17 '11 at 07:53
  • Yes, there is one dependencies on stsimulator.ststraversal caused by a test class in stsimulator that has `@SuiteClasses(value = {AClassInStsimulatorStstraversal.class})`. So I think you are right, my tests are the cause. I do not understand how, though: My input to JDepend is constant, and I could not reproduce the cycleTest-fails the last couple of days... – DaveFar Oct 20 '11 at 15:17
  • maybe with your JDepend experience you also have a good solution for http://stackoverflow.com/q/7284130/750378? That would be great and honored with upvote + accept :) – DaveFar Oct 20 '11 at 15:20
  • That can only be because at somepoint the whole of your sources and tests are in the classpath loaded by the JDepend class loader. – Gepsens Oct 21 '11 at 13:30
  • @Gepsens: I thought by using `jdepend.addDirectory("/home/dball/workspace/stsimulatorLazyOTFBranch/build/clas‌​ses");`, jdepend only sees that path. – DaveFar Oct 21 '11 at 15:01