Scala method injector plugin - how to get the object from the type parameter?

I'm trying to  create a plugin to help Intellij to recognize my injected methods, but i hit a roadblock in trying to get a list of the properties of the annotations' type parameter.

My question is, how can i get the actual ScObject from the ScTypeElement?

This is the code my code so far:

 


class Injector extends SyntheticMembersInjector {
override def injectFunctions(source: ScTypeDefinition): Seq[String] = {

val annotationName = "com.mmagyar.macros.Crud"
source match {
case c: ScObjectImpl if c.hasAnnotation(annotationName).isDefined =>
val macroForType: ScTypeElement =
c.hasAnnotation(annotationName).get.constructor.typeArgList.get.typeArgs.head

//This obviously doesn't work, how can i get the actual Object from the type?
val macroObject = macroForType.asInstanceOf[ScObject]

macroObject.allVals.map(x=> {
//generate code based on the object properties
""
})
case _ => Seq()
}
}
}
3 comments
Comment actions Permalink
Official comment

You need to evaluate type first:

import org.jetbrains.plugins.scala.lang.psi.api.base.types.ScTypeElement
val te: ScTypeElement = ???
te.calcType.extractClass(te.getProject)(te.typeSystem) match {
case o: ScObject => //...
}
Comment actions Permalink

That does not compile for me, what i'm i missing?

calcType does not have "extractClass" method, and te is missing the "typeSystem" property

 

[info] Compiling 2 Scala sources to C:\Users\MßtΘ\code\macro-annotation-idea-plugin\target\scala-2.11\classes...
[error] C:\Users\MßtΘ\code\macro-annotation-idea-plugin\src\main\scala\org\jetbrains\example\injector\Injector.scala:22: value extractClass is not a member of org.jetbrains.plugins.scala.lang.psi.types.ScType
[error] te.calcType.extractClass(te.getProject)(te.typeSystem) match {
[error] ^
[error] C:\Users\MßtΘ\code\macro-annotation-idea-plugin\src\main\scala\org\jetbrains\example\injector\Injector.scala:22: value typeSystem is not a member of org.jetbrains.plugins.scala.lang.psi.api.base.types.ScTypeElement
[error] te.calcType.extractClass(te.getProject)(te.typeSystem) match {
[error] ^
[error] two errors found

0
Comment actions Permalink

never mind, the provided skeleton project is out of date, i updated the plugin version and the IDEA version in build.sbt and now it compiles.

0

Please sign in to leave a comment.