Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of using proxy to wrap a non-final java object and override one of its methods. #28

Merged
merged 1 commit into from Aug 9, 2022

Conversation

jumarko
Copy link
Owner

@jumarko jumarko commented Aug 9, 2022

This uses ZipFile just as an example:

(let [filename "empty.zip"
      original-zip (ZipFile. filename)
      proxy-zip (proxy [ZipFile] [filename]
                  ;; overriding close method
                  (close [] (println "Ignored!"))
                  ;; random method implementation passing the call to the wrapper
                  (size [] (.size original-zip)))]
  [[(.size proxy-zip) (.size original-zip)]
   ;; check the REPL - you will see "Ignored!" printed one time
   ;; - note that we cannot return "Ignored" as a value
   ;; because close method is 'void'
   [(.close proxy-zip) (.close original-zip)]])

…de one of its methods.

This uses ZipFile just as an example:

```
(let [filename "empty.zip"
      original-zip (ZipFile. filename)
      proxy-zip (proxy [ZipFile] [filename]
                  ;; overriding close method
                  (close [] (println "Ignored!"))
                  ;; random method implementation passing the call to the wrapper
                  (size [] (.size original-zip)))]
  [[(.size proxy-zip) (.size original-zip)]
   ;; check the REPL - you will see "Ignored!" printed one time
   ;; - note that we cannot return "Ignored" as a value
   ;; because close method is 'void'
   [(.close proxy-zip) (.close original-zip)]])
```
@jumarko jumarko merged commit 965b90c into master Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant