Skip to content

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

@jonathanlermitage

Description

@jonathanlermitage

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions