-
Notifications
You must be signed in to change notification settings - Fork 297
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
support type cast detection #1081
base: main
Are you sure you want to change the base?
Conversation
2730507
to
bc7c038
Compare
I just changed my mind about not considering Using existing archunit API, it's not possible to detect which class cast So, I believe we should consider |
bc7c038
to
a4778c2
Compare
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>
a4778c2
to
9b3a512
Compare
Thanks a lot for your contribution and sorry for the long silence!! I was super overloaded 🙈 It looks widely very nice, just that it also seems to be much more complex than I anticipated with these "implicit" checkcasts 🙈 I'm not completely sure what is the right way to move forward here. At the moment, I'm not sure this "implict" vs "explicit" logic works reliable. For example I added some crazy code
And with the current logic I see zero I'm also not sure how I see 2 options to fix the problem, a) we invest more time to find what criteria we really need to distinguish explicit from implicit |
*/ | ||
package com.tngtech.archunit.core.importer; | ||
|
||
class TypeCastRecorder { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name was a little confusing to me on the first read, because technically this doesn't "record type casts", no? It just stores some meta-info that's queried later on when creating a type case. But it's not recording the actual checkcast
statement, etc. I think it should either get a different name or also handle what to do when we encounter the checkcast
, i.e. create the RawTypeCast
then when we encounter the respective place 🤷♂️
I'm revisiting the code now and indeed there's a basic bug. Most likely void registerMethodInstruction(int opcode) {
implicit = Opcodes.INVOKEINTERFACE == opcode;
} That way it will correctly detect for the given example. Of course I'll need to check if nothing else gets broken or the tests which might need to be updated.
indeed the Also in the code snippet, the
For me, option I believe this implicit check using only the PS: Just noticed the java 8 tests failed. I could fix that as well in a next push |
Yes, it's not that simple. Maybe option b is the only way |
Maybe option b) is also alright, no? 🤔 Only bad thing about is that users won't know what "check cast" is supposed to be, so we definitely would have to explain it well in Javadoc. Regarding your use case, I would think that any |
Probably option B is the only option. If the developer writes the following "compliant" code:
The compiler might still generate a |
You might be able to test if |
I decided to implement this feature myself since I have a real use case which I need this and the other PR that started implementing it has no activity since almost 1 year.
Just a brief info about the use case that I need:
Some notes to add:
cast
statement. SoFooBar foo = FooBar.class.cast(obj)
is not considered as a type cast. It's just a call to a method which does the cast.checkcast
instructions are ignored deliberately. In java bytecode we might encountercheckcast
after a call to a method, as the following example incom.tngtech.archunit.example.onionarchitecture.adapter.cli.AdministrationCLI
:Java compiler adds a
checkcast
after the method callport.getInstanceOf(ProductRepository.class)
. While implementing this feature, I was detecting this as animplicit
cast, but that proved of no use and brought only more confusion.Resolves #710