Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier to load an implementation of scalafix.interfaces.Scalafix #1151

Closed
bjaglin opened this issue Jun 4, 2020 · 3 comments · Fixed by #1152
Closed

Make it easier to load an implementation of scalafix.interfaces.Scalafix #1151

bjaglin opened this issue Jun 4, 2020 · 3 comments · Fixed by #1152
Assignees

Comments

@bjaglin
Copy link
Collaborator

bjaglin commented Jun 4, 2020

As discussed in #1150 (comment), scalafix-interfaces should provide (Java) helpers for:

  • coursier resolution/fetch
  • classloading of the correct scalafix-cli

The current helper is pretty low-level:

static Scalafix classloadInstance(ClassLoader classLoader) throws ScalafixException {
try {
Class<?> cls = classLoader.loadClass("scalafix.internal.interfaces.ScalafixImpl");
Constructor<?> ctor = cls.getDeclaredConstructor();
ctor.setAccessible(true);
return (Scalafix) ctor.newInstance();
} catch (ClassNotFoundException | NoSuchMethodException |
IllegalAccessException | InvocationTargetException |
InstantiationException ex) {
throw new ScalafixException(
"Failed to reflectively load Scalafix with classloader " + classLoader.toString(), ex);
}
}

This will remove the need for custom code in sbt-scalafix, mill-scalafix, and all other clients.

@bjaglin bjaglin self-assigned this Jun 4, 2020
@olafurpg
Copy link
Contributor

olafurpg commented Jun 6, 2020

FWIW, in Scalafmt we have a scalafmt-dynamic_2.{11,12,13} library that is written in Scala and provides an implementation of scalafix-interfaces that dynamically loads the correct version of scalafmt-cli. Both Metals and IntelliJ use scalafmt-dynamic to interact with scalafix-interfaces. The benefit of this approach is that scalafmt-interfaces only contains Java interfaces and all the logic is implemented in Scala.

@bjaglin
Copy link
Collaborator Author

bjaglin commented Jun 6, 2020

I have a local branch almost ready with a pure Java implementation inside scalafix-interfaces (using https://github.com/coursier/interface), which I think is fine. We can discuss once I open the PR!

@bjaglin
Copy link
Collaborator Author

bjaglin commented Jun 6, 2020

Maybe another benefit of replicating that approach is to try to align classloaders so that scalameta can at some point be shared across scalafmt & scalafix somehow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants