Proxies and final methods

18 views
Skip to first unread message

pmf

unread,
Mar 14, 2008, 10:19:16 PM3/14/08
to Clojure
I have a problem (or possible misunderstanding) related to the proxy-
functionality.

It seems that proxies for classes that contain final methods that are
original to the class (i.e. that are not inherited from some base)
work as expected, whereas proxies for classes that inherit a method
from some base and implement it (using a final-modifier) cause the
proxy creation to fail.

Is this expected behaviour? I've looked at proxy.clj and there's
already a check 'isFinal' in place.

I know that the proxy-functionality is not primarily intended for
class-inheritance but for interface-implementation, but I just thought
I'd let you know about this.

Below follow my test-classes; javac accepts everything. I run these
with
java -cp .:clojure.jar clojure.lang.Repl g.clj

;;;;;; BEGIN g.clj

(import '(proxytest Base Derived BaseWithFinal))

; this makes no sense since Base is abstract, but it still works
(def proxy-for-base (proxy [Base]
[]
))

; this works
(def proxy-for-base-with-final (proxy [BaseWithFinal]
[]
))
; the following fails
(def proxy-for-derived (proxy [Derived]
[]
))

;;;;;; END g.clj


// BEGIN proxytest/Base.java
package proxytest;

public abstract class Base {
public void nonFinalMethod () {};
public abstract void finalMethod ();
}
// END proxytest/Base.java


// BEGIN proxytest/Derived.java
package proxytest;

public class Derived extends Base {
public void nonFinalMethod () {};
public final void finalMethod () {};
}
// END proxytest/Derived.java


// BEGIN proxytest/BaseWithFinal.java
package proxytest;

public class BaseWithFinal {
public final void finalMethod () {};
}
// END proxytest/BaseWithFinal.java

Rich Hickey

unread,
Mar 15, 2008, 10:49:11 AM3/15/08
to Clojure


On Mar 14, 10:19 pm, pmf <p...@torq.de> wrote:
> I have a problem (or possible misunderstanding) related to the proxy-
> functionality.
>
> It seems that proxies for classes that contain final methods that are
> original to the class (i.e. that are not inherited from some base)
> work as expected, whereas proxies for classes that inherit a method
> from some base and implement it (using a final-modifier) cause the
> proxy creation to fail.
>
> Is this expected behaviour? I've looked at proxy.clj and there's
> already a check 'isFinal' in place.
>
> I know that the proxy-functionality is not primarily intended for
> class-inheritance but for interface-implementation, but I just thought
> I'd let you know about this.
>

Fixed - thanks for the report!

BTW, it can make sense to make a proxy for an abstract class if you
intend to provide definitions for the abstract methods.

Rich

pmf

unread,
Mar 15, 2008, 3:46:22 PM3/15/08
to Clojure

On Mar 15, 3:49 pm, Rich Hickey <richhic...@gmail.com> wrote:
> Fixed - thanks for the report!

Nice; thank you.

> BTW, it can make sense to make a proxy for an abstract class if you
> intend to provide definitions for the abstract methods.

Yes; I was referring to my example being rather pointless, not the
general case.

#^{:note-to-myself "Now that I think of it, even if not overriding
anything, a proxy for an abstract class might prove useful for unit-
testing the non-abstract functionality of this class (without having
to create a mock derived class just to be able to get some instance)."}
Reply all
Reply to author
Forward
0 new messages