:: Java Stuff :: | home | ucontrol | contacts | macosx | gbasdk | java | jfunc ::

Will you be me by proxy?

This page details my efforts to create proxies for classes--not just interfaces--in Java. I managed to make it work with three different code bases. There is a fundamental flaw with the approach, and probably the reason why Sun never released the functionality. Primitive members of a class cannot, so far as I know, be cleanly proxied; and interfaces cannot declare any primitive members. Object members you might have some leeway with but not the primitive members. (How awful, I've already thought of some horrible hacks to get around this.)

Why would you ever want a proxy of a class rather than an interface?

Usually, it never comes about by choice. Here is the problem: there are a lot of classes which don't have interfaces that you might need to proxy.

Could you get around this "problem" by creating a new interface and making of proxy of that?

You could create an interface that looks just like that class. (The utility javap will do that for you practically.) You then have to extend the original class and implement that interface. Unfortunately, the problem is the code that calls that class already exists--that you can't change or don't have the source to--is already bound to the methods on the class, not the new interface you created which looks just like the class. So your new interface is impotent and won't solve your problem.

So how did you do it?

In my quest to be able to create proxies of classes, I found delegators. The were the original proxies, before Sun had released them. They had the same restriction that Sun did: interface only. I hacked up my own version to do classes, but the code base had fell into disuse, wasn't being maintained, and the byte code that was generated would break on occasion.

I figured I'd take a look at Sun's implementation of proxies and make my modifications there. The code was much cleaner than the delegators (no disrespect to the guys at develop.com since they probably reverse engineered the byte code; it's understandable). After I made the appropriate changes, I was confronted with the task of trying to get Sun's permission to distribute my modifications. I wasted my breathe trying to contact Sun. My recommendation to you kiddies, don't bother with code that has a policy of "look, don't touch." Even if it allows you surmount the technical barriers, the political/legal barriers are far worse.

Finally the ByteCode Engineering Library (BCEL) came out and was adopted by the Apache project. The library did byte code generation very well, but did not generate proxies. I later found this great piece code by Juozas Baliuka which used BCEL to generate proxies, but again with the restriction that it only work on interfaces (make no mistake, he did the hardest part of the work). I added the ability to create proxies for classes for the third time and was finally able to release my work for inclusion in JFunc. You can look and touch the code here.

-shane (at) gnufoo (dot) org