I want to be able to write an aspect to detect when I am casting something in one of my org.mypackage
classes.
package org.mypackage;
class Foo {
public static void main(String[] args) {
Bar casted = (Bar) args[0]; // want to detect this casting action!
}
}
How do you write a pointcut to express the casting operation, not just for Foo
class, but for any class in org.mypackage
?
Background: So Hibernate 5 + Spring Data JPA requires casting entities with inheritance:
if (isInstanceOfMyEntity(someEntity)) {
// formerly, this was sufficient:
// MyEntity myEntity = (MyEntity) someEntity;
// now, this is required *everywhere* it is casted:
MyEntity myEntity = (MyEntity) Hibernate.unproxy(someEntity);
...
}
...which, on a large code base, is scary to consider because there are a lot of places this could break. So if it's possible to write an aspect/pointcut to at least detect it, then we can at least log it and also identify places in our tests where the issue needs addressing.
This technique is recommended in this question How to convert a Hibernate proxy to a real entity object by @Vlad-Mihalcea.