-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows users to add custom rewrites to the compiler classpath and classload them by fully qualified name.
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package scalafix.util | ||
|
||
import scala.collection.immutable.Seq | ||
import scala.reflect.ClassTag | ||
import scala.util.Failure | ||
import scala.util.Success | ||
import scala.util.Try | ||
|
||
import java.lang.reflect.InvocationTargetException | ||
|
||
// Helper to classload object or no argument class. | ||
class ClassloadObject[T](classLoader: ClassLoader)(implicit ev: ClassTag[T]) { | ||
private val t = ev.runtimeClass | ||
|
||
private def getClassFor(fqcn: String): Try[Class[_ <: T]] = | ||
Try[Class[_ <: T]]({ | ||
val c = | ||
Class.forName(fqcn, false, classLoader).asInstanceOf[Class[_ <: T]] | ||
if (t.isAssignableFrom(c)) c | ||
else throw new ClassCastException(s"$t is not assignable from $c") | ||
}) | ||
|
||
private def createInstanceFor(clazz: Class[_]): Try[T] = | ||
Try { | ||
val constructor = clazz.getDeclaredConstructor() | ||
constructor.setAccessible(true) | ||
val obj = constructor.newInstance() | ||
if (t.isInstance(obj)) obj.asInstanceOf[T] | ||
else | ||
throw new ClassCastException( | ||
s"${clazz.getName} is not a subtype of $t") | ||
} recover { | ||
case i: InvocationTargetException if i.getTargetException ne null ⇒ | ||
throw i.getTargetException | ||
} | ||
|
||
def createInstanceFor(fqcn: String): Try[T] = | ||
getClassFor(fqcn).flatMap(c => createInstanceFor(c)) | ||
} | ||
|
||
object ClassloadObject { | ||
def apply[T: ClassTag](fqn: String): Either[Throwable, T] = | ||
new ClassloadObject(this.getClass.getClassLoader) | ||
.createInstanceFor(fqn) match { | ||
case Success(e) => Right(e) | ||
case Failure(e) => Left(e) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
scalafix-nsc/src/test/resources/checkSyntax/FqnRewrite.source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
rewrites = [ | ||
_root_.scalafix.test.FqnRewrite | ||
] | ||
<<< NOWRAP add import | ||
import scala._ | ||
object FqnMe | ||
>>> | ||
import scala.meta._ | ||
object FqnMe |
13 changes: 13 additions & 0 deletions
13
scalafix-nsc/src/test/scala/scalafix/test/FqnRewrite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package scalafix.test | ||
|
||
import scala.meta._ | ||
import scala.collection.immutable.Seq | ||
import scalafix.rewrite.Rewrite | ||
import scalafix.rewrite.RewriteCtx | ||
import scalafix.util.Patch | ||
import scalafix.util.TreePatch.AddGlobalImport | ||
|
||
case object FqnRewrite extends Rewrite[Any] { | ||
override def rewrite[B <: Any](ctx: RewriteCtx[B]): Seq[Patch] = | ||
Seq(AddGlobalImport(importer"scala.meta._")) | ||
} |
d0c05db
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is super exciting!