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

Reference to constant in class is not recognized #554

Closed
zhoujia1974 opened this issue Mar 16, 2021 · 2 comments
Closed

Reference to constant in class is not recognized #554

zhoujia1974 opened this issue Mar 16, 2021 · 2 comments

Comments

@zhoujia1974
Copy link

zhoujia1974 commented Mar 16, 2021

Hi,

We are using archunit-junit5:0.17.0. In our test, we have a rule as below.

    noClasses()
        .that()
        .resideInAPackage("com.example.package1..")
        .should()
        .dependOnClassesThat()
        .resideInAnyPackage("com.example.package2..")
        .check(classes);

In package1, we have a controller reference to a constant defined in a constant class in package2. See below.

package com.example.package1.controller;

import com.example.package2.MessageConstant;

public class SomeController {
  ...
  private Message addHeader(final Message message) throws JMSException {
    message.setStringProperty(MessageConstant.REQUEST_ID_PROPERTY, requestContext.getRequestId());
    ...
    return message;
  }
}

In package2, we define the constant class.

package com.example.package2;
public final class MessageConstant {
  public static final String REQUEST_ID_PROPERTY = "request_id";
 
  private MessageConstant() {}
}

The rule doesn't fail. Is it a bug or did I misunderstand how the dependOnClassesThat() should work? When I debug the rule, I found dependOnClassesThat condition evaluation doesn't return MessageConstant class as a target.

Thanks,

@hankem
Copy link
Member

hankem commented Mar 17, 2021

A String constant is actually no runtime (but only a compile-time) dependency; its value is copied into the constant pool of the using class, cf. #298 (comment). This is why ArchUnit currently doesn't detect it as a dependency.

@zhoujia1974
Copy link
Author

Thanks @hankem.

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

2 participants