Java – ‘Swizzle’ (maybe reflection?) addview() on Android

I know you can't actually do Swizzle in Java

I'm doing some research. I think 'maybe' you can use Java as reflection to complete Swizzle like behavior (you can do it on IOS)

The culprit (and one of the worst design decisions I've ever seen) is the addview() function on all Android ViewGroup objects. You must explicitly check whether the parent is empty (sometimes you even need to cast the parent to get the required behavior!). Gross

I want to change this behavior (without creating a million subclasses) by having the addview () method automatically perform this check so that client code can ignore it

This is what I can do with reflection (from my grasp, it requires special runtime calls rather than actually changing the root method call [so it may not be good enough]), or what else? Or am I barking on the wrong tree?

resolvent:

As you mentioned, it seems impossible not to create a million subclasses (or better put, a subclass of each class, and you want to override the addview Implementation). This is actually the correct method, which also means that you can use these subclasses in the XML layout file

Reflection will allow you to check classes / interfaces / fields / methods at run time, but it does not allow you to change the defined behavior provided by the compiled base implementation. Although some changes can be made, such as access to private methods / fields

Gray area options passed in

One direction worth studying is mockito, which allows you to create spies around existing object instances to allow you to override the default implementation. This is implemented through Java proxies and invocationhandlers. However, at this time, I will stop and say that your production code should not contain any test code, and mockito is obviously designed for this. Considering this, at least in the past, it was difficult in the past (if not impossible) place the proxy in some classes of Android (such as views and contexts) I believe this has been solved in dexmaker. I know little about myself, but it does allow you to perform runtime code generation. This will be another direction worthy of attention. I personally think these should not be regarded as possible solutions worth making code, but for your own curiosity and learning

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>