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

improve method.returnType when it's a collection #413

Closed
jonathanlermitage opened this issue Aug 18, 2020 · 3 comments
Closed

improve method.returnType when it's a collection #413

jonathanlermitage opened this issue Aug 18, 2020 · 3 comments

Comments

@jonathanlermitage
Copy link

jonathanlermitage commented Aug 18, 2020

I'm trying to detect if a class has methods that return a specific class (Foo), or a list of this specific class (List<Foo>).

I tested this condition:

    private static final ArchCondition<JavaClass> NOT_HAVE_METHODS_RETURNING_FOO =
        new ArchCondition<JavaClass>("methods should not return Foo") {
            @Override
            public void check(@NotNull JavaClass item, ConditionEvents events) {
                item.getMethods()
                    .forEach(method -> {
                        JavaClass rawReturnType = method.getRawReturnType();
                        if (rawReturnType.getName().contains("my.package.Foo")) {
                            String message = String.format("Class %s's method %s returns an entity of type %s",
                                item.getSimpleName(),
                                method.getName(),
                                rawReturnType.getName());
                            events.add(SimpleConditionEvent.violated(method, message));
                        }
                    });
            }
        };

It works for methods that return Foo: rawReturnType.getName() looks like JavaClass{name='my.package.Foo'},
Problem -> for methods that return List<Foo>, rawReturnType.getName() is equal to JavaClass{name='java.util.List'}. I can't check it's a list of Foo.
Maybe I am missing something. At least, is it feasible?
Thx for your help!

@hankem
Copy link
Member

hankem commented Aug 18, 2020

The current release ArchUnit 0.14.1 does unfortunately not yet provide access to generic type parameters.

@jbkervyn
Copy link

jbkervyn commented Oct 23, 2020

Don't know if this is of any help, but I this seems to work for me :

getGenericReturnType().getTypeName() ->returns : java.util.List < Object >

 private static DescribedPredicate<? super JavaMethod> returnsDomainObject() {
        return new DescribedPredicate<JavaMethod>("returns a domain object") {
            @Override
            public boolean apply(JavaMethod javaMethod) {
                return javaMethod.reflect().getGenericReturnType().getTypeName().contains("domain");
            }
        };
    }

@jonathanlermitage
Copy link
Author

It works. Thx @jbkervyn !! 👍

jonathanlermitage added a commit to jonathanlermitage/manon that referenced this issue Oct 26, 2020
jonathanlermitage added a commit to jonathanlermitage/manon that referenced this issue Oct 26, 2020
hankem added a commit that referenced this issue Dec 9, 2022
Bumps [actions/setup-java](https://github.com/actions/setup-java) from
3.6.0 to 3.8.0.

from [actions/setup-java's releases](https://github.com/actions/setup-java/releases):

> # v3.8.0
> 
> In scope of this release we added logic to pass the token input through on GHES for Microsoft Build of OpenJDK ([actions/setup-java#395](https://github-redirect.dependabot.com/actions/setup-java/pull/395)) and updated [minimatch](https://github-redirect.dependabot.com/actions/setup-java/pull/413) dependency.

Commits

*   [`c3ac5dd`](actions/setup-java@c3ac5dd) Revert "Add support for Oracle JDK ([#401](https://github-redirect.dependabot.com/actions/setup-java/issues/401))" ([#421](https://github-redirect.dependabot.com/actions/setup-java/issues/421))
*   [`dcd29da`](actions/setup-java@dcd29da) Fix typo in README.md ([#419](https://github-redirect.dependabot.com/actions/setup-java/issues/419))
*   [`19eeec5`](actions/setup-java@19eeec5) Update to latest `actions/publish-action` ([#411](https://github-redirect.dependabot.com/actions/setup-java/issues/411))
*   [`bd7e5d2`](actions/setup-java@bd7e5d2) Update minimatch to 3.1.2 ([#413](https://github-redirect.dependabot.com/actions/setup-java/issues/413))
*   [`6cdf39a`](actions/setup-java@6cdf39a) Add support for Oracle JDK ([#401](https://github-redirect.dependabot.com/actions/setup-java/issues/401))
*   [`7db6b45`](actions/setup-java@7db6b45) Eclipse Temurin instead of Adopt OpenJDK ([#398](https://github-redirect.dependabot.com/actions/setup-java/issues/398))
*   [`bf2f02c`](actions/setup-java@bf2f02c) Pass the token input through on GHES for Microsoft Build of OpenJDK ([#395](https://github-redirect.dependabot.com/actions/setup-java/issues/395))
*   See full diff in [compare view](actions/setup-java@v3.6.0...v3.8.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants