Interface implementation in several versions

I am trying to create a class that implements an interface from a Java library. The ResultSet should be specific, although the specific interface should not be relevant. (I need to overlay a layer on top of a regular ResultSet, which provides some additional features, but I would like the “regular” functions to go through, and I have a couple of functions that should be able to accept either a regular ResultSet or my “advanced” ResultSet.)

My problem: is there a way I can do this so that the class compiles successfully in both Java 5 and Java 6?

There are a number of functions in the ResultSet interface in Java 6 that return objects that are not defined in Java 5. If I enable these functions, my class will not compile in Java 5 because I refer to undefined types, But if I do not include these functions, then my class will not compile in Java 6 because I do not fully implement the interface. I seem to be stuck in something caught-22. I really don't need any of these functions - in fact, my implementation simply throws an “not implemented” exception for all of them.

Some of our programmers work in Java 5, and some of them work in Java 6. Our production environment is Java 5. I believe that in a more advanced world, we will work with the same version. But even if I can change our environment to solve the problem in this case, of course, this problem occurs with open source projects. And if I modify my code to work with Java 5, then, sooner or later, we will switch to Java 6, the class will break, which seems rather annoying.

Update

Ok, thanks for the answers. I rather hoped that someone would tell me: "Oh, if you just add this annotation or write the letter" W "here, everything will work in a magical way." There seems to be no such luck.

( , ) , , " " , .: -)

, - ResultSet , ResultSet, . , , , , .

+3
4

, Proxy - , :

-, API, , , , , (, , , ). :

API JDBC - , , API. . , JDBC. JDBC - ., ,. , , , , / ResultSet, () . , ResultSet -; JDBC ResultSet, DAO , .

+2

, - JDK, Proxy.

1,3, .

:

ResultSet rs = Proxy.newProxyInstance(
                   ResultSet.class.getClassloader(),
                   new Class[]{ResultSet.class},
                   new ResultSetInvocationHandler()
);

ResultSetInvocationHandler InvocationHandler, .

.

+2

, , JDBC. .

, 5 6. - :

abstract class JaysBaseSuperResultSet /* does not implement ResultSet */ {
    // all needed methods for java 5
}

class JaysSuperResultSetForJava6 extends JaysBaseSuperResultSet implements ResultSet {
    // all additional java 6 methods
}

class JaysSuperResultSetForJava5 extends JaysBaseSuperResultSetForJava5 implements ResultSet {
    // class body can be empty; compile this only with java 5
}

: , - , JaysSuperResultSetForJava5, 6, -target 5, 5 6, 5 ( ).

factory, .

JAR.

, ?

EDIT: , , .

+2

, ResultSet, ResultSet .

: , ResultSet:

public class MyWrapper<T extends ResultSet> {
    T myResultSet;

    public MyWrapper(T myResultSet) {
        this.myResultSet = myResultSet;
    }

    public T getResultSet() {
        return myResultSet;
    }

    public void setResultSet(T myResultSet) {
        this.myResultSet = myResultSet;
    }

}

(, ).

Note:
Creating a new class that implements ResultSetmay seem like a good idea on the surface, but it quickly degenerates into insanity because it ResultSetalready has its own subinterface RowSet, which, in turn, has its own subinterface JdbcRowSet, which in turn has its own own subinterfaces ... and at the very end of this tree are the actual objects that are part of the JDBC driver for your database.

+1
source

Source: https://habr.com/ru/post/1782339/


All Articles