0

Very silly question, I am doing a project and I can't think about any workaround I have implemented new functions in an open jar (let's call it converter), I want to modify one of these methods: ExchangeToEuro, ExchangeToDollar, CheckCurrency.

My question is: Are there any mechanism for which I can add functionality (modifying of of these methods or even adding methods to this class) to the class without modifying the jar (without modifying the classes present in the jar)?

I thought about a class decorator, but that wouldn't work as I would need to modify the jar itself.

3 Answers 3

2

You can extend classes within Converter.jar, as long as they are not final, and override the methods contained within them, as long as they are visible and not final as well. Say Converter.jar had a class like this:

public class ExchangeToDollar {
    public BigDecimal exchangeEuroToDollar(final BigDecimal euroValue) {
        ...method implementation....
    }
}

You could extend and override like this:

public class MyClass extends ExchangeToDollar {
    @Override
    public BigDecimal exchangeEuroToDollar(final BigDecimal euroValue) {
        ...your implementation....
    }
}

The MyClass.exchangeEuroToDollar(BigDecimal) method overrides the exchangeEuroToDollar(BigDecimal) implementation that's contained within the ExchangeToDollar class in Converter.jar, meaning that when you call the method on an instance of MyClass, your implementation will be executed.

The downside of this type of implementation is that any code that you do not have control over that uses ExchangeToDollar will not be able to use your overridden implementation, unless you can pass a MyClass instance in instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes that is the bummer, I want to be able to use the new implementation and not the old one...
Well, you can use the new implementation, but classes outside of your control might not be able to.
1

It's not entirely clear what kind of modifications you need to make, but if you need to modify class methods themselves, you options are limited to things like AspectJ or less-clean forms of byte-code manipulation using tools like BCEL or ASM.

You could conceivably create the additional functionality in a different JVM language like Groovy/etc., compile to .class files, and use those.

You may need to provide more and/or less-contradictory info, though.

4 Comments

ehehe Sorry Dave, neither I know how to explain it! so I have a jar (open, e.g. I can open it and modify the java/class files) This contains a list of classes, one is in this package called "org.conversion" One of these classes is called Conversion and has the method I listed ExchangeToEuro, ExchangeToDollar, CheckCurrency. I want to modify one of them, without modifying the class in the jar.
See Ioeth's answer, then; is it sufficient? If not, things will get complicated.
well, I am afraid that is half answer.. like from what I am researching it seems impossible to do what I said...
Because you said you wanted to modify classes in the jar without modifying the jar--that's pretty much impossible, yep. If you can't subclass and override, can't wrap using something like Spring AOP (which can play games with proxies), your options are (1) modify the jar through byte-code manipulation (preferably via something like AspectJ) or (2) modify the class using load-time weaving (jar unmodified; byte code manipulated during load).
1

Extend the classes, add what new functionality is required, and override that which should change.

1 Comment

Well I tried that, (see above comment), I want the old jar to use the new functionaly though :(

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.