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

Add support to analyse list of classes #1195

Open
ArturoBlazquez opened this issue Nov 16, 2023 · 2 comments
Open

Add support to analyse list of classes #1195

ArturoBlazquez opened this issue Nov 16, 2023 · 2 comments

Comments

@ArturoBlazquez
Copy link

There are times when there are architecture rules that only apply to some classes, but that are not on the same package.
It would be nice if we could do something similar to this

@AnalyzeClasses(classes = {First.class, Second.class})
public class MyArchitectureTest {

    @ArchTest
    public static final ArchRule myRule = ...

Something similar can be achieved using locations, but it implies either adding a lot of boilerplate code

class ListOfClassesLocations implements LocationProvider {
    @Override
    public Set<Location> get(Class<?> testClass) {
        return Stream.of(First.class, Second.class).map(clazz -> Locations.ofClass(clazz).iterator().next()).collect(Collectors.toSet());
    }
}

@AnalyzeClasses(locations = ListOfClassesLocations.class)
public class MyArchitectureTest { ...

or having to develop a custom solution like this one, that still isn't completely clean as it implies adding an extra annotation

@Target(TYPE)
@Retention(RUNTIME)
public @interface AnalyzeListOfClasses {
    Class<?>[] value();
}
public class ListOfClassesLocations implements LocationProvider {
    @Override
    public Set<Location> get(Class<?> testClass) {
        return Arrays.stream(testClass.getAnnotation(AnalyzeListOfClasses.class).value()).map(clazz -> Locations.ofClass(clazz).iterator().next()).collect(Collectors.toSet());
    }
}
@AnalyzeClasses(locations = ListOfClassesLocations.class)
@AnalyzeListOfClasses({First.class, Second.class})
public class MyArchitectureTest {

    @ArchTest
    public static final ArchRule myRule = ...
@hankem
Copy link
Member

hankem commented Nov 16, 2023

You could already use @AnalyzeClasses's packagesOf:

@AnalyzeClasses(packagesOf = {First.class, Second.class})
public class MyArchitectureTest {
    // ...
}

This will however not only import First and Second, but their entire packages.
If your rules only apply to those classes, I'd be explicit about that in the rule definition.

@codecholeric
Copy link
Collaborator

I think principally this classes = syntax would be a fair addition, since there is also ClassFileImporter.importClasses(Class...). I originally left it out, because I thought it's a corner case and you can use LocationProvider exactly as you did to achieve the same. Still not sure if classes = ... would be used that often, but it would be consistent with ClassFileImporter and also not be a huge deal to add.
So, if anybody wants to give this a shot I'd be open for a PR 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants