Reputation: 11609
I want to avoid code duplication in next situation: I have two classes NewTx and GetTx, first represents new transaction, second represent a transaction, that joins current. The goal is to make CRUD code as compact as possible, so classes are usually used as:
List<Users> users = NewTx.loadList("select u from User u");
Student s = GetTx.find(Student.class, '02d7c3fe-e9cf-11e4-8ceb-c7b1b9baf140');
These classes actually differ only in a way they obtain a transaction, but all their methods are static, so it seems impossible to move logic to parent class.
Now I have
public final class NewTx extends Tx {
public static <T> List<T> loadList(String query) {
return tx().call(Tx.<T>getLoadListCallable(query));
}
static Transaction tx() {
return DataSource.createTransaction();
}
As I said before, only tx() method is different for NewTx and GetTx classes, other methods just obtain transaction and than delegate job to parent Tx class.
So the goal is to move all CRUD methods like loadList to parent class Tx.
Restriction: method calls must look like before: NewTx.load(..., not NewTx.get().load(..
Any ideas?
Upvotes: 1
Views: 81
Reputation: 5094
Your goal isn't going to happen with the current restrictions you've given. If you were willing to change the method calls, there are multiple ways, but moving common static calls into a shared class doesn't work because static methods can't be inherited in java, only shadowed. Consider the following:
public static class StaticParent
{
public static void commonMethod(){
System.out.println(getOutput());
}
public static String getOutput(){
return "Parent";
}
}
public static class StaticChildA extends StaticParent
{
public static String getOutput(){
return "ChildA";
}
}
public static class StaticChildB extends StaticParent
{
public static String getOutput(){
return "ChildB";
}
}
StaticChildA.commonMethod()
and StaticChildB.commonMethod()
will both print "Parent" because commonMethod
is being shadowed and has no way of knowing that the calling code was from StaticChildA
or StaticChildB
. If we print the stack trace inside commonMethod
, we see the following from both calls:
testpackage.UnitTester$StaticParent.commonMethod(UnitTester.java:4497)
testpackage.UnitTester.main(UnitTester.java:4526)
With no this
or difference in the stack, there's no way to even branch inside the code manually to pick an implementation of your tx()
.
Upvotes: 1