in Education by
I have two packages com.myapp.foo and com.myapp.bar and I want to know the most elegant way to explicitly check if those two packages (and only those, as there are some more com.myapp.XX) do not depend on each other. This is what I have right now (working splendid): SliceAssignment packagesFooAndBar = new SliceAssignment() { @Override public String getDescription() { return "foo and bar"; } @Override public SliceIdentifier getIdentifierOf(JavaClass javaClass) { if (javaClass.getPackageName().startsWith("com.myapp.foo")) { return SliceIdentifier.of("foo"); } if (javaClass.getPackageName().startsWith("com.myapp.bar")) { return SliceIdentifier.of("bar"); } return SliceIdentifier.ignore(); } }; @ArchTest final ArchRule packagesFooAndBarNotDependOnEachOther = SlicesRuleDefinition .slices() .assignedFrom(packagesFooAndBar) .should() .notDependOnEachOther(); Is there a more elegant way, maybe without using the SliceAssignment? Thanks! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I would probably use two rules. @ArchTest final ArchRule fooShouldNotDependOnBar = ArchRuleDefinition .classes() .that().resideInAnyPackage("com.myapp.foo") .should().onlyDependOnClassesThat() .resideOutsideOfPackage("com.myapp.bar"); @ArchTest final ArchRule barShouldNotDependOnFoo = ArchRuleDefinition .classes() .that().resideInAnyPackage("com.myapp.bar") .should().onlyDependOnClassesThat() .resideOutsideOfPackage("com.myapp.foo"); If you want to write the same in a single rule without custom classes, the following should work. I'm not sure if I would call it elegant though, since it's seems a bit complex. There are probably better ways to write it in a single rule. @ArchTest final ArchRule packagesFooAndBarNotDependOnEachOther = SlicesRuleDefinition .slices() .matching("com.myapp.(*)") .should() .notDependOnEachOther() .ignoreDependency( JavaClass.Predicates.resideOutsideOfPackages("com.myapp.foo", "com.myapp.bar"), DescribedPredicate.alwaysTrue()) .ignoreDependency( DescribedPredicate.alwaysTrue(), JavaClass.Predicates.resideOutsideOfPackages("com.myapp.foo", "com.myapp.bar")) ); The rule first matches all direct sub-packages of com.myapp. The first ignoreDependency then excludes all dependencies from classes outside of the given packages (to allow e.g. com.myapp.xx to access com.myapp.foo). The second ignoreDependency excludes all dependencies from the given packages to any outside package (to allow e.g. com.myapp.foo to access com.myapp.xx).

Related questions

0 votes
    How can I check for missing packages and call for installed.packages() to install them smartly? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Does Python have something like an empty string variable where you can do: if myString == string.empty: Regardless, ... not as good. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Which of these packages contain all the collection classes? (a) java.lang (b) java.util (c) java.net ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these packages contains all the classes and methods required for even handling in Java? (a) java.applet ... Handling of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of these packages contains all the event handling interfaces? (a) java.lang (b) java.awt (c) java.awt ... Event Handling of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of these keywords is used to define packages in Java? (a) pkg (b) Pkg (c) package (d) Package ... Interfaces & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following is an incorrect statement about packages? (a) Package defines a namespace in which classes ... Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    How to install for a package and all of the other packages on which for depends? (a) install.packages (for ... Started of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Which method can be used to check fileAccessiblity? (a) isReadable(path) (b) isWritable(path) (c) ... Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which command can be used to check maven version? (a) mvn -ver (b) maven -ver (c) maven -version ( ... Lifecycle & Annotations of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Myself and a colleague have a dispute about which of the following is more elegant. I won't say ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    Which of these is a Basic interface that all other interface inherits? (a) Set (b) Array (c) List (d ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Deadlock is a situation when thread is waiting for other thread to release acquired object. (a) True (b) ... Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
...