Intercept calls to a concrete class

I have a class that looks something like this:

class SomeClass : ConcreteView {
   var condition : Boolean = false
}

I want to prevent any function calls on SomeClass if condition is true.

I tried doing this with a Java Proxy but that requires an interface, which complicates things because SomeClass extends ConcreteView (Which isn’t an interface).

Another option would be to use AOP but I’m not sure I want to do that after reading this post: Why I Don't Recommend Aspect-Oriented Programming in Android in 2023 | JD Porterfield | Articles.

Is there any other way I can achieve this? Any ideas would be appreciated.

What do you mean by ‘prevent’?

It can’t give a compile-time error, presumably, as the compiler can’t tell (in general) whether the property will be true at runtime.

Do you want calls to give some exception or other throwable? Or to be ignored, and execution continue after the call? Or something else?

Also, note that your class is free to implement any interfaces you like, even if has a parent, as long as it provides the relevant methods (or is abstract).

“Ignore” would be the better word.

If someClass.someFunction() is called and condition is true, it should just do nothing.
If someClass.someFunction() is called and condition is false, it should execute as normal.

This should be the behavior for any function called on that class.

ConcreteView is not a class I own, so interfacing it out will be a pain.

What if the function returns a value?

2 Likes