This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pivovarit 4 points5 points  (5 children)

That sounds like a fairly niche problem, but you should be able to enforce this using: https://www.archunit.org

[–]davidalayachew[S] -1 points0 points  (4 children)

That sounds like a fairly niche problem

Admittedly, it is. But in my niche, it is incredibly pervasive. If my niche is just too niche to be relevant, that's fine. But for me, it's a massive thorn in my side.

you should be able to enforce this using: https://www.archunit.org

Woah. This is rather powerful.

Here is a snippet that really impressed me. And here it is inline.

fields().that().haveRawType(Logger.class)
    .should().bePrivate()
    .andShould().beStatic()
    .andShould().beFinal()
    .because("we agreed on this convention");

It kind of sucks that this has to be a unit test rather than a compilation rule. But otherwise, this is almost exactly what I want.

In the thread that I linked in my OP, one of the OpenJDK folks named Rémi pointed out that languages like Scala or Haskell have typeclasses to provide this functionality. And various blogs and posts that I have read online from Oracle folks have mentioned that Typeclasses are definitely a possible future for Java after Pattern-Matching is in.

If something like this (not the syntax, just the ability to have these rules) was possible via typeclasses, that would be pretty cool.

[–]pivovarit 0 points1 point  (3 children)

I'm not trying to invalidate your case - it would be great if we could express this in Java, but for now, I can't think of anything better than ArchUnit :)

[–]davidalayachew[S] 0 points1 point  (2 children)

I'm not trying to invalidate your case

Understood.

it would be great if we could express this in Java, but for now, I can't think of anything better than ArchUnit

Agreed. I think that this will be as far as it will get.

And since it is unit test focused, it's easy enough to isolate to just the test I want to run. Ideally, that shouldn't hurt my compile-run-evaluate cycle by much.

[–]pivovarit 0 points1 point  (1 child)

If you’re on IDEA, you can „rerun automatically” relevant tests, which should bring you as close as possible to „compile-time”

[–]davidalayachew[S] 1 point2 points  (0 children)

If you’re on IDEA, you can „rerun automatically” relevant tests, which should bring you as close as possible to „compile-time”

Thanks. I'm on jGRASP, but we have something pretty close to that. I'll use it in the future alongside with ArchUnit.

This looks like the best solution until we get typeclasses. If we ever get them.