1

I have the source code of a project developed by another group of guys. The thing is, I need to make some modifications to it, preferably not having to touch the original source file.

In ruby, using mixin & alias(alias_method_chain) should be enough in most cases. But is it possible to do the same thing in Java?

Or am I just looking at it the wrong way? The reason for trying not to touch the original source file is that the project itself is still under development, so new versions would come from time to time, and I'd like to avoid the merging/patching work every time a new version comes. Another reason is that besides the source code, I also need to modify some config files. So the idea is to let the original program load the config file itself, then mine kicks in and load my own stuff. In ruby, those kind of things are perfectly handled by alias_method_chain, but in Java, I don't know how to do it.

Edit: Working on AspectJ right now, looks like it should work

PS: How do you guys live with eclipse? Downloading a plugin is horribly slow. Took me half an hour to install the m2e plugin!!!

1 Answer 1

0

Java currently does not support mixin or alias_method_chain constructs. If you want to 'import' or use code without interacting to much with it you can extend it or use the Delegation Pattern.

Mixin example with the Delegation Pattern :

Let's say you have 2 interfaces :

InChargeOfEcology

public interface InChargeOfEcology {
    public boolean checkKyotoProtocolConformity();
}

InChargeOfEconomy

public interface InChargeOfEconomy {
    public boolean checkTreasureBalanceIsPositive();
}

These interfaces are implemented by well known classes :

MinisterOfEcology

public class MinisterOfEcology implements InChargeOfEcology {
    @Override
    public boolean checkKyotoProtocolConformity() {
        System.out.println("Doing the kyoto checks");
        return true;
    }
}

MinisterOfEconomy

public class MinisterOfEconomy implements InChargeOfEconomy {
    @Override
    public boolean checkTreasureBalanceIsPositive() {
        System.out.println("Doing the bank checks");
        return true;
    }
}

Now, you want to mix-them-in to form a single implementation :

PrimeMinister

public class PrimeMinister implements InChargeOfEcology, InChargeOfEconomy {
    private InChargeOfEcology ecologyDelegate;
    private InChargeOfEconomy economyDelegate;

    public PrimeMinister() {
        ecologyDelegate = new MinisterOfEcology();
        economyDelegate = new MinisterOfEconomy();
    }

    @Override
    public boolean checkKyotoProtocolConformity() {
        return ecologyDelegate.checkKyotoProtocolConformity();
    }

    @Override
    public boolean checkTreasureBalanceIsPositive() {
        return economyDelegate.checkTreasureBalanceIsPositive();
    }
}

This can seem heavy when you are used to lighter constructs, but Java doesn't support them.

alias_method_chain (decoration) example with the Delegation Pattern :

The 'alias_method_chain' in itself cannot be fully implemented, it depends on what you really need to do. If you want to decorate a method, that is : add some behavior before or after its true execution, you can do it this way :

Take the above example and add some code before or after calling the delegate :

public class PrimeMinister implements InChargeOfEcology, InChargeOfEconomy {
    private InChargeOfEcology ecologyDelegate;
    private InChargeOfEconomy economyDelegate;

    public PrimeMinister() {
        ecologyDelegate = new MinisterOfEcology();
        economyDelegate = new MinisterOfEconomy();
    }

    @Override
    public boolean checkKyotoProtocolConformity() {
        System.out.println("Ensure kyoto protocol is still in use");
        return ecologyDelegate.checkKyotoProtocolConformity();
    }

    @Override
    public boolean checkTreasureBalanceIsPositive() {
        boolean check = economyDelegate.checkTreasureBalanceIsPositive();
        System.out.println("Compare with the last check result");
        return check;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.