-
Notifications
You must be signed in to change notification settings - Fork 297
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
ArchUnit type Predicate Problem When Class Used in Array Declaration #183
Comments
From my point of view this behavior seems as expected. The JLS considers
This could be written in a nicer way, if the component type could already be queried from |
Perhaps my Java thinking is weak, but I view What I'm trying to express with a That's why I find it surprising that |
That this works differently with |
@codecholeric said:
Ah so that's why So an array of Back to my While I think I understand all that, I still struggle to understand why I'd want to allow a dependency on Thinking of it another way. In my mind, the purpose of ArchUnit is to enforce things like modularity, "levelization", no circular dependencies. Treating array types as second class seems to put ArchUnit into a different realm. But from your explanation @codecholeric, I have come to understand that ArchUnit may actually be a tool for expressing what types (in detail) that a class may reference. If that's what ArchUnit is actually doing, then wouldn't we want to let it talk about primitive types as well (not just object types)? That would give rise to predicates like: Maybe what I just described already exists right underneath the surface API. I guess it just wasn't apparent from my first explorations into the tool. It does seem though, that there are two levels here. The level of "types" (TypeUnit? anybody?) and the level of "dependencies" (between classes and between packages): ArchUnit. In closing: I really appreciate the discussion, the workaround ( |
Maybe we also talked a little bit about different things 😉 I completely agree with your case, if you want to allow a dependency to |
You can already talk about primitive types or array types to some extent, e.g.:
|
I am running into a similar problem with disallowed dependencies not being properly discovered: public class Foo {
private List<Bar> bars;
private Bar bar;
}
// Foo should not depend on Bar
ArchRuleDefinition.noClasses().that() //
.haveSimpleName(Foo.class.getSimpleName()) //
.should().dependOnClassesThat().haveSimpleName(Bar.class.getSimpleName()) //
.check(classes); This check reports Assume you had all disallowed references hidden in some generics context ( Just one note about generics reification as it's quite often misunderstood – and actually is here. By no means generics information is discarded after compilation. Of course that information is around as how would the compiler of client code verify the proper usage of the generic code. Reification talks about generics information of objects, not types. The component type of a |
The need to access generic type parameters has already been described with #115, but not yet implemented. 🤷 |
That's a helpful pointer, Manfred, thanks. 👍 |
Sorry @odrotbohm, yes, that's still open
On the upside, if your only dependency is the type parameter, that pretty much means, that you never do anything with those dependencies in your class. So it is hopefully easy to resolve by relaxing the type bound (the only time I can imagine that not to be correct is, if |
Nevertheless I do of course plan to have this this issue solved at some point in the future, if I do it myself I just need to allocate some sufficient time slot to focus in my schedule 😉 |
Hi, I'm writing a rule that checks for all constructor calls to be wrapped into a factory. So no class shall call the constructor of Foo except the factory which is annotated with With ArchUnit, I can get all constructors like |
I wrote a custom
|
(description below is excerpted from the README on this repo: https://github.com/Bill/archunitarraytest)
ArchUnit's
type
predicate doesn't seem to match a class used in an array declaration, when thattype
predicate is used inside anor
predicate, to define an exception to some global condition.Class Bar.java has a method with this signature:
Our rule over in ArchUnitArrayTest.java says
Bar
can depend on classes in its own package orjava.lang
or:We expect the test to pass. But it fails with:
if you go over to Bar.java and eliminate the
Foo[]
reference by changing thebaz()
signature to:…then ArchUnit will throw no exception.
Array declarations, seem to work in other situations. See, for instance, the definition of
baz2()
:That signature generates no exception. ArchUnit recognizes that the array type is derived from
Quux
and thatQuux
resides in thecore.sub
package.A workaround is to add a clause like this to the rule:
But it seems wrong to have to explicitly mention array types.
The text was updated successfully, but these errors were encountered: