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

Dependency should also consider parameter annotations #373

Closed
uhafner opened this issue Jun 7, 2020 · 1 comment · Fixed by #701
Closed

Dependency should also consider parameter annotations #373

uhafner opened this issue Jun 7, 2020 · 1 comment · Fixed by #701

Comments

@uhafner
Copy link

uhafner commented Jun 7, 2020

In #171 dependencies have been enhanced to check for annotations (see also #136).
What has not been fixed in this PR is the support for annotations for parameters.

When I define the following rule:

    public static final ArchRule NO_FORBIDDEN_ANNOTATIONS_USED =
            noClasses().should().dependOnClassesThat(
                    have(type(edu.umd.cs.findbugs.annotations.CheckForNull.class)));

Then the following code will produce only a violation for the member, but not for the parameter.

@CheckForNull
public boolean exists(@CheckForNull final String fileName) {...}

It would be quite helpful if ArchUnit would also consider parameter annotations as a dependency (the types of parameters are correctly considered a dependency).

@codecholeric
Copy link
Collaborator

Yes, this is still a shortcoming 😞 It was not forgotten, but it is blocked by #113, i.e. we do not have the info yet, because parameter annotations are not yet imported.
But I agree that once #113 is solved parameter annotations should also be added to the dependencies.

codecholeric added a commit that referenced this issue Oct 27, 2021
This will extend the domain model of ArchUnit by providing 

* `List<Parameter> JavaCodeUnit.getParameters()`
* `List<List<JavaAnnotation<Parameter>> JavaCodeUnit.getParameterAnnotations()`

where `Parameter` also offers `getParameterType()`, `getRawParameterType()` and `getAnnotations()`.

Furthermore parameter annotations are now part of `JavaClass.directDependencies{From/To}Self`.

On the contrary to the Java Reflection API the `JavaCodeUnit.getParameters()` will exactly mirror the (possibly generic) signature and not the raw types. This means that for generic signatures synthetic raw type parameter types (like `name` and `ordinal` for an enum constructor) will not be visible in the `List<Parameter>`. I think for ArchUnit this makes sense, as there is limited interest in synthetic parts of the code and users are only interested in the parameters that have really been introduced by source code. Unfortunately there are many cases where the bytecode does not contain the signature attribute (whenever the signature is non-generic, i.e. does not contain any parameterized types, type variables, etc.). In these cases the parameters will still mirror the raw types (including possible synthetic parameters), because there is no other source of information where the parameters could be derived from. It is also not i.g. possible to derive which parameters are synthetic and which are not, so ArchUnit does not attempt this at all at the moment.

Resolves: #404
Resolves: #373
Resolves: #113
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants