0

I have a situation where some objects come from an external Jar. There is an object X used from other objects like this:

object X {
    def foo() = ....
... more methods to follow
}

object A {
    def bar() = X.foo()
    ...few more methods using X
}

object B {
    def baz() = X.baz()
     ...few more methods using X
}

...other objects using X...

All that objects belong to the same Jar package and I need to use the classes A,B,etc but with a different reference than X (would be my local X). Is there a way to replace somehow X inside my project that is using a jar defined in that way?

3
  • it is not my code and I cannot change it. Commented Apr 22, 2020 at 13:09
  • Yeah, I supposed that. I just was ranting because you are now in a big problem because someone didn't design their code correctly. Why do you need to replace X? Commented Apr 22, 2020 at 13:26
  • I have to inject different cloud configurations that "wrap" Spark processes and the code is meant to run on a "physical" env only. Commented Apr 22, 2020 at 13:38

1 Answer 1

1

In short, no.

In long, it is possible but it's more like a hack rather than a proper solution.

You can redefine X within the same package name as original X and then rely on classloader order of jars to load your X first.

The correct solution is a redesign of your program so it allows "injection" of implementations. For example in this way

trait X {
    def foo() = ....
... more methods to follow
}

object X extends X
object Y extends X

object A {
    def bar(x: X) = x.foo()
    ...few more methods using X
}

object B {
    def baz(x: X) = x.baz()
     ...few more methods using X
}
Sign up to request clarification or add additional context in comments.

1 Comment

It is not my program, I mean I cannot change it (so not redesign it)

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.