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

Violations not detected by ArchUnit #446

Closed
enriquemolinari opened this issue Oct 3, 2020 · 3 comments
Closed

Violations not detected by ArchUnit #446

enriquemolinari opened this issue Oct 3, 2020 · 3 comments

Comments

@enriquemolinari
Copy link

enriquemolinari commented Oct 3, 2020

Trying to understand why ArchUnit does not detect the cases below...
(1) I guess this one is because the compiler remove that unused variable at all and nothing is translated to bycode.
(3) Casting is just for the compiler nothing goes into bytecode.
(2) Why is not detected? can this be fixed?

Thanks!!!

package app.ui;
import app.business.AServiceImpl;

public class View {
 public void aMethod( ) {
  AServiceImpl a; // (1)
  Class a2 = AServiceImpl.class; // (2)
  System.out.println(a2.getName());
  if (AServiceImpl.class.equals(this.getClass())) { // (2)
   ....
  }
  Object o = new Object();
  AServiceImpl b = (AServiceImpl)o; // (3)
 }
}
	
 @Test
 public void arch() {
  JavaClasses jc = new ClassFileImporter().importPackages("app");
  noClasses().that()
   .resideInAnyPackage("app.ui..")
   .should()
   .accessClassesThat()
   .resideInAPackage("app.business")
   .check(jc);
 }
@codecholeric
Copy link
Collaborator

ArchUnit does currently not handle all the information the bytecode would offer, e.g. the constant pool (compare #131).
Local variables without use like (1) are not detected at the moment. If you call a method or access a field on this concrete local variable, it will be detected.
Generic properties of Class<?> are currently also not detected, the only way to find this in the bytecode would again be the constant pool. Because all the accesses / dependencies are to generic Class<?> in the bytecode.
Casts like (3) could be detected (they are related closely to e.g. #417) but it hasn't been implemented yet 😞
Does that answer your questions?

@enriquemolinari
Copy link
Author

Many thanks for your reply. ArchUnit is an excellent tool, hope can be improved further.

codecholeric added a commit that referenced this issue Jan 31, 2021
So far ArchUnit has not been able to detect the pure usage of class objects as dependencies. E.g. the following example would not have detected a dependency on `Evil`, since besides the reference to the class object no further dependency on `Evil` (like a field access or method call) has occurred.

```
class Example {
  final Map<Class<?>, Object> association = Map.of(Evil.class, anything);
}
```

With this PR `JavaClass` now knows its `referencedClassObjects`, including the respective line number. Furthermore class objects are now parts of the `dependencies{From/To}Self` of a `JavaClass`.

Resolves: #309 
Issue: #446 
Resolves: #474 
Resolves: #515
codecholeric added a commit that referenced this issue Feb 21, 2021
So far ArchUnit has not been able to detect the pure usage of class objects as dependencies. E.g. the following example would not have detected a dependency on `Evil`, since besides the reference to the class object no further dependency on `Evil` (like a field access or method call) has occurred.

```
class Example {
  final Map<Class<?>, Object> association = Map.of(Evil.class, anything);
}
```

With this PR `JavaClass` now knows its `referencedClassObjects`, including the respective line number. Furthermore class objects are now parts of the `dependencies{From/To}Self` of a `JavaClass`.

Resolves: #309 
Issue: #446 
Resolves: #474 
Resolves: #515
@codecholeric
Copy link
Collaborator

I'm gonna close this for now, since it's a little undirected. (2) has been implemented meanwhile, about (1) I'm not sure, because the compiler might also remove it if it's unused and and unused local variable will also never cause a real maintenance problem. For (3) I have created #710

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