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

Add support for casts #710

Open
codecholeric opened this issue Oct 30, 2021 · 6 comments · May be fixed by #1081
Open

Add support for casts #710

codecholeric opened this issue Oct 30, 2021 · 6 comments · May be fixed by #1081

Comments

@codecholeric
Copy link
Collaborator

At the moment casts without any further dependencies on the target type are not detected as dependencies. I.e.

SomeClass example(Object rawInput) {
  return (SomeClassImpl) rawInput; // should forbid dependencies on SomeClassImpl
}

Since there are no further dependencies on SomeClassImpl this would be missed. It does likely not cause a maintenance problem later, because the cast could at any time be weakened to (SomeClass) without any further compile or runtime issues, but still it would be nice to detect these things. Also to be able to access all casts that a class or method applies could be useful for other scenarios.

We could add this the following way:

  • extend Set<TypeCast> JavaCodeUnit.getCasts() (compare InstanceofCheck)
  • add the TypeCasts to JavaClass.directDependencies{From/To}Self

(open for discussion: What is the best name for the domain object? TypeCast, Cast, JavaCast, ClassCast, ...?)

@Kaammill
Copy link

Is this issue open? If yes I would like to try to work on it.
I have a few questions about the task.
JavaClass.directDependencies{From/To}Self those methods are returning a set of Dependency objects. TypeCasts should be added to the dependency object or a new version of those methods is required like this: "getDirectDependenciesFromSelf(TypeCasts typeCasts)"?

@hankem
Copy link
Member

hankem commented Feb 23, 2022

IMO we want both: (1) a specific method to ask domain objects for casts, and (2) register those as dependencies.

Random previous examples:

  • instanceof checks
  • .class references

@codecholeric
Copy link
Collaborator Author

@Kaammill and yes, the issue is up for grabs and we're always super happy about contributions 😃

Kaammill added a commit to Kaammill/ArchUnit that referenced this issue Mar 3, 2022
Add support for casts
TNG#710

Signed-off-by: Kaammill  <>
Kaammill added a commit to Kaammill/ArchUnit that referenced this issue Mar 8, 2022
@ratoaq2
Copy link

ratoaq2 commented Mar 9, 2023

Some information about checkcast

The compiler might add some checkcast instructions:

  void foobar(java.lang.Object);
    Code:
       0: aload_1
       1: invokestatic  #29                 // Method com/foobar/SomeClass.of:(Ljava/lang/Object;)Lcom/foobar/SomeClass;
       4: ldc           #35                 // class java/lang/Boolean
       6: invokevirtual #37                 // Method com/foobar/SomeClass.someMethod:(Ljava/lang/Class;)Ljava/lang/Object;
       9: checkcast     #35                 // class java/lang/Boolean

What I noticed: if the previous instruction is invokevirtual or invokeinterface, the checkcast was generated by the compiler

@ratoaq2
Copy link

ratoaq2 commented Mar 9, 2023

Also bridge methods will have more compiler checkcast instructions

@ratoaq2
Copy link

ratoaq2 commented Mar 10, 2023

Detecting casts are also useful when working with polymorphism in hibernate, where instanceof and direct casts should not be used due to hibernate proxies.

Having this feature in archunit would also help in building rules for this case

ratoaq2 added a commit to ratoaq2/ArchUnit that referenced this issue Mar 12, 2023
Detect *checkcast* asm instructions as TypeCast (similar to
InstanceofCheck)
Implicit checkcast instructions generated by compiler due to method
invocations are ignored

Signed-off-by: Allan Jones <allan.jones@gmail.com>
@ratoaq2 ratoaq2 linked a pull request Mar 12, 2023 that will close this issue
ratoaq2 added a commit to ratoaq2/ArchUnit that referenced this issue Mar 12, 2023
Detect *checkcast* asm instructions as TypeCast (similar to
InstanceofCheck)
Implicit checkcast instructions generated by compiler due to method
invocations are ignored

Signed-off-by: Allan Jones <allan.jones@gmail.com>
ratoaq2 added a commit to ratoaq2/ArchUnit that referenced this issue Mar 12, 2023
Detect checkcast asm instructions as TypeCast (similar to InstanceofCheck)
Implicit checkcast instructions generated by compiler due to method invocations are ignored

Resolves TNG#710

Signed-off-by: Allan Jones <allan.jones@gmail.com>
ratoaq2 added a commit to ratoaq2/ArchUnit that referenced this issue Mar 14, 2023
Detect checkcast asm instructions as TypeCast (similar to InstanceofCheck)
Implicit checkcast instructions generated by compiler due to method invocations are ignored

Resolves TNG#710

Signed-off-by: Allan Jones <allan.jones@gmail.com>
ratoaq2 added a commit to ratoaq2/ArchUnit that referenced this issue Aug 12, 2023
Detect checkcast asm instructions as TypeCast (similar to InstanceofCheck)
Implicit checkcast instructions generated by compiler due to method invocations are ignored

Resolves TNG#710

Signed-off-by: Allan Jones <allan.jones@gmail.com>
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.

4 participants