Mercurial > hg > monetdb-java
annotate src/main/java/nl/cwi/monetdb/jdbc/MonetWrapper.java @ 18:8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
They now properly return expected results instead of always return false or throw an SQLException.
Added test program Test_Wrapper.java
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 13 Oct 2016 16:31:08 +0200 (2016-10-13) |
parents | a5a898f6886c |
children | b9b35ca2eec2 |
rev | line source |
---|---|
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
1 /* |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
5 * |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V. |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
7 */ |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
8 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
9 package nl.cwi.monetdb.jdbc; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
10 |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
11 import java.sql.SQLException; |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
12 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
13 /** |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
14 * A Wrapper class which provide the ability to retrieve the delegate instance |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
15 * when the instance in question is in fact a proxy class. |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
16 * |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
17 * The wrapper pattern is employed by many JDBC driver implementations to provide |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
18 * extensions beyond the traditional JDBC API that are specific to a data source. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
19 * Developers may wish to gain access to these resources that are wrapped (the delegates) |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
20 * as proxy class instances representing the the actual resources. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
21 * This class contains a standard mechanism to access these wrapped resources |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
22 * represented by their proxy, to permit direct access to the resource delegates. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
23 * |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
24 * @author Fabian Groffen, Martin van Dinther |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
25 * @version 1.1 |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
26 */ |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
27 public class MonetWrapper implements java.sql.Wrapper { |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
28 /** |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
29 * Returns true if this either implements the interface argument or |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
30 * is directly or indirectly a wrapper for an object that does. |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
31 * Returns false otherwise. If this implements the interface then |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
32 * return true, else if this is a wrapper then return the result of |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
33 * recursively calling <code>isWrapperFor</code> on the wrapped object. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
34 * If this does not implement the interface and is not a wrapper, return |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
35 * false. This method should be implemented as a low-cost operation |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
36 * compared to <code>unwrap</code> so that callers can use this method to avoid |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
37 * expensive <code>unwrap</code> calls that may fail. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
38 * If this method returns true then calling <code>unwrap</code> with the same argument should succeed. |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
39 * |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
40 * @param iface a Class defining an interface. |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
41 * @return true if this implements the interface or directly or indirectly wraps an object that does. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
42 * @throws SQLException if an error occurs while determining whether this is a wrapper |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
43 * for an object with the given interface. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
44 * @since 1.6 |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
45 */ |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
46 @Override |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
47 public boolean isWrapperFor(Class<?> iface) throws SQLException { |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
48 return iface != null && iface.isAssignableFrom(getClass()); |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
49 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
50 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
51 /** |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
52 * Returns an object that implements the given interface to allow |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
53 * access to non-standard methods, or standard methods not exposed by the proxy. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
54 * The result may be either the object found to implement the interface |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
55 * or a proxy for that object. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
56 * If the receiver implements the interface then the result is the receiver |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
57 * or a proxy for the receiver. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
58 * If the receiver is a wrapper and the wrapped object implements the interface |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
59 * then the result is the wrapped object or a proxy for the wrapped object. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
60 * Otherwise return the result of calling <code>unwrap</code> recursively on |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
61 * the wrapped object or a proxy for that result. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
62 * If the receiver is not a wrapper and does not implement the interface, |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
63 * then an <code>SQLException</code> is thrown. |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
64 * |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
65 * @param iface A Class defining an interface that the result must implement. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
66 * @return an object that implements the interface. May be a proxy for the actual implementing object. |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
67 * @throws SQLException If no object found that implements the interface |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
68 * @since 1.6 |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
69 */ |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
70 @Override |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
71 @SuppressWarnings("unchecked") |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
72 public <T> T unwrap(Class<T> iface) throws SQLException { |
18
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
73 if (isWrapperFor(iface)) { |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
74 return (T) this; |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
75 } |
8e57d20b5e80
Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
76 throw new SQLException("Cannot unwrap to interface: " + (iface != null ? iface.getName() : ""), "0A000"); |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
77 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
78 } |