Calling method via reflection in Scala












0















I want to call an arbitrary public method of an arbitrary stuff via reflection. I.e. let's say, I want to write method extractMethod to be used like:



class User { def setAvatar(avatar: Avatar): Unit = …; … }

val m = extractMethod(someUser, "setAvatar")
m(someAvatar)


From the Reflection. Overview document from Scala docs, I see the following direct way to do that:



import scala.reflect.ClassTag
import scala.reflect.runtime.universe._

def extractMethod[Stuff: ClassTag: TypeTag](
stuff: Stuff,
methodName: String): MethodMirror =
{
val stuffTypeTag = typeTag[Stuff]
val mirror = stuffTypeTag.mirror
val stuffType = stuffTypeTag.tpe
val methodSymbol = stuffType
.member(TermName(methodName)).asMethod
mirror.reflect(stuff)
.reflectMethod(methodSymbol)
}


However what I'm bothered with this solution is that I need to pass implicit ClassTag[Stuff] and TypeTag[Stuff] parameters (first one is needed for calling reflect, second one — for getting stuffType). Which may be quite cumbersome, especially if extractMethod is called from generics that are called from generics and so on. I'd accept this as necessity for some languages that strongly lack runtime type information, but Scala is based on JRE, which allows to do the following:



def extractMethod[Stuff](
stuff: Stuff,
methodName: String,
parameterTypes: Array[Class[_]]): (Object*) => Object =
{
val unboundMethod = stuff.getClass()
.getMethod(methodName, parameterTypes: _*)
arguments => unboundMethod(stuff, arguments: _*)
}


I understand that Scala reflection allows to get more information that basic Java reflection. Still, here I just need to call a method. Is there a way to somehow reduce requirements (e.g. these ClassTag, TypeTag) of the Scala-reflection-based extractMethod version (without falling back to pure-Java reflection), assuming that performance doesn't matter for me?










share|improve this question



























    0















    I want to call an arbitrary public method of an arbitrary stuff via reflection. I.e. let's say, I want to write method extractMethod to be used like:



    class User { def setAvatar(avatar: Avatar): Unit = …; … }

    val m = extractMethod(someUser, "setAvatar")
    m(someAvatar)


    From the Reflection. Overview document from Scala docs, I see the following direct way to do that:



    import scala.reflect.ClassTag
    import scala.reflect.runtime.universe._

    def extractMethod[Stuff: ClassTag: TypeTag](
    stuff: Stuff,
    methodName: String): MethodMirror =
    {
    val stuffTypeTag = typeTag[Stuff]
    val mirror = stuffTypeTag.mirror
    val stuffType = stuffTypeTag.tpe
    val methodSymbol = stuffType
    .member(TermName(methodName)).asMethod
    mirror.reflect(stuff)
    .reflectMethod(methodSymbol)
    }


    However what I'm bothered with this solution is that I need to pass implicit ClassTag[Stuff] and TypeTag[Stuff] parameters (first one is needed for calling reflect, second one — for getting stuffType). Which may be quite cumbersome, especially if extractMethod is called from generics that are called from generics and so on. I'd accept this as necessity for some languages that strongly lack runtime type information, but Scala is based on JRE, which allows to do the following:



    def extractMethod[Stuff](
    stuff: Stuff,
    methodName: String,
    parameterTypes: Array[Class[_]]): (Object*) => Object =
    {
    val unboundMethod = stuff.getClass()
    .getMethod(methodName, parameterTypes: _*)
    arguments => unboundMethod(stuff, arguments: _*)
    }


    I understand that Scala reflection allows to get more information that basic Java reflection. Still, here I just need to call a method. Is there a way to somehow reduce requirements (e.g. these ClassTag, TypeTag) of the Scala-reflection-based extractMethod version (without falling back to pure-Java reflection), assuming that performance doesn't matter for me?










    share|improve this question

























      0












      0








      0








      I want to call an arbitrary public method of an arbitrary stuff via reflection. I.e. let's say, I want to write method extractMethod to be used like:



      class User { def setAvatar(avatar: Avatar): Unit = …; … }

      val m = extractMethod(someUser, "setAvatar")
      m(someAvatar)


      From the Reflection. Overview document from Scala docs, I see the following direct way to do that:



      import scala.reflect.ClassTag
      import scala.reflect.runtime.universe._

      def extractMethod[Stuff: ClassTag: TypeTag](
      stuff: Stuff,
      methodName: String): MethodMirror =
      {
      val stuffTypeTag = typeTag[Stuff]
      val mirror = stuffTypeTag.mirror
      val stuffType = stuffTypeTag.tpe
      val methodSymbol = stuffType
      .member(TermName(methodName)).asMethod
      mirror.reflect(stuff)
      .reflectMethod(methodSymbol)
      }


      However what I'm bothered with this solution is that I need to pass implicit ClassTag[Stuff] and TypeTag[Stuff] parameters (first one is needed for calling reflect, second one — for getting stuffType). Which may be quite cumbersome, especially if extractMethod is called from generics that are called from generics and so on. I'd accept this as necessity for some languages that strongly lack runtime type information, but Scala is based on JRE, which allows to do the following:



      def extractMethod[Stuff](
      stuff: Stuff,
      methodName: String,
      parameterTypes: Array[Class[_]]): (Object*) => Object =
      {
      val unboundMethod = stuff.getClass()
      .getMethod(methodName, parameterTypes: _*)
      arguments => unboundMethod(stuff, arguments: _*)
      }


      I understand that Scala reflection allows to get more information that basic Java reflection. Still, here I just need to call a method. Is there a way to somehow reduce requirements (e.g. these ClassTag, TypeTag) of the Scala-reflection-based extractMethod version (without falling back to pure-Java reflection), assuming that performance doesn't matter for me?










      share|improve this question














      I want to call an arbitrary public method of an arbitrary stuff via reflection. I.e. let's say, I want to write method extractMethod to be used like:



      class User { def setAvatar(avatar: Avatar): Unit = …; … }

      val m = extractMethod(someUser, "setAvatar")
      m(someAvatar)


      From the Reflection. Overview document from Scala docs, I see the following direct way to do that:



      import scala.reflect.ClassTag
      import scala.reflect.runtime.universe._

      def extractMethod[Stuff: ClassTag: TypeTag](
      stuff: Stuff,
      methodName: String): MethodMirror =
      {
      val stuffTypeTag = typeTag[Stuff]
      val mirror = stuffTypeTag.mirror
      val stuffType = stuffTypeTag.tpe
      val methodSymbol = stuffType
      .member(TermName(methodName)).asMethod
      mirror.reflect(stuff)
      .reflectMethod(methodSymbol)
      }


      However what I'm bothered with this solution is that I need to pass implicit ClassTag[Stuff] and TypeTag[Stuff] parameters (first one is needed for calling reflect, second one — for getting stuffType). Which may be quite cumbersome, especially if extractMethod is called from generics that are called from generics and so on. I'd accept this as necessity for some languages that strongly lack runtime type information, but Scala is based on JRE, which allows to do the following:



      def extractMethod[Stuff](
      stuff: Stuff,
      methodName: String,
      parameterTypes: Array[Class[_]]): (Object*) => Object =
      {
      val unboundMethod = stuff.getClass()
      .getMethod(methodName, parameterTypes: _*)
      arguments => unboundMethod(stuff, arguments: _*)
      }


      I understand that Scala reflection allows to get more information that basic Java reflection. Still, here I just need to call a method. Is there a way to somehow reduce requirements (e.g. these ClassTag, TypeTag) of the Scala-reflection-based extractMethod version (without falling back to pure-Java reflection), assuming that performance doesn't matter for me?







      scala reflection scala-reflect






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 12:29









      SashaSasha

      1,6431529




      1,6431529
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Yes, there is.





          1. First, according to this answer, TypeTag[Stuff] is a strictly stronger requirement than ClassTag[Stuff]. Although we don't automatically get implicit ClassTag[Stuff] from implicit TypeTag[Stuff], we can evaluate it manually as ClassTag[Stuff](stuffTypeTag.mirror.runtimeClass(stuffTypeTag.tpe)) and then implicitly or explicitly pass it to reflect that needs it:



            import scala.reflect.ClassTag
            import scala.reflect.runtime.universe._

            def extractMethod[Stuff: TypeTag](
            stuff: Stuff,
            methodName: String): MethodMirror =
            {
            val stuffTypeTag = typeTag[Stuff]
            val mirror = stuffTypeTag.mirror
            val stuffType = stuffTypeTag.tpe
            val stuffClassTag = ClassTag[Stuff](mirror.runtimeClass(stuffType))
            val methodSymbol = stuffType
            .member(TermName(methodName)).asMethod
            mirror.reflect(stuff)(stuffClassTag)
            .reflectMethod(methodSymbol)
            }



          2. Second, mirror and stuffType can be obtained from stuff.getClass():



            import scala.reflect.ClassTag
            import scala.reflect.runtime.universe._

            def extractMethod(stuff: Stuff, methodName: String): MethodMirror = {
            val stuffClass = stuff.getClass()
            val mirror = runtimeMirror(stuffClass.getClassLoader)
            val stuffType = mirror.classSymbol(stuffClass).toType
            val stuffClassTag = ClassTag[Stuff](mirror.runtimeClass(stuffType))
            val methodSymbol = stuffType
            .member(TermName(methodName)).asMethod
            mirror.reflect(stuff)(stuffClassTag)
            .reflectMethod(methodSymbol)
            }



          Therefore we obtained Scala-style reflection entities (i.e. finally MethodMirror) without requiring ClassTag and/or TypeTag to be passed explicitly or implicitly from the caller. Not sure, however, how it compares with the ways described in the question (i.e. passing tags from outside and pure Java) in the terms of performance.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431065%2fcalling-method-via-reflection-in-scala%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Yes, there is.





            1. First, according to this answer, TypeTag[Stuff] is a strictly stronger requirement than ClassTag[Stuff]. Although we don't automatically get implicit ClassTag[Stuff] from implicit TypeTag[Stuff], we can evaluate it manually as ClassTag[Stuff](stuffTypeTag.mirror.runtimeClass(stuffTypeTag.tpe)) and then implicitly or explicitly pass it to reflect that needs it:



              import scala.reflect.ClassTag
              import scala.reflect.runtime.universe._

              def extractMethod[Stuff: TypeTag](
              stuff: Stuff,
              methodName: String): MethodMirror =
              {
              val stuffTypeTag = typeTag[Stuff]
              val mirror = stuffTypeTag.mirror
              val stuffType = stuffTypeTag.tpe
              val stuffClassTag = ClassTag[Stuff](mirror.runtimeClass(stuffType))
              val methodSymbol = stuffType
              .member(TermName(methodName)).asMethod
              mirror.reflect(stuff)(stuffClassTag)
              .reflectMethod(methodSymbol)
              }



            2. Second, mirror and stuffType can be obtained from stuff.getClass():



              import scala.reflect.ClassTag
              import scala.reflect.runtime.universe._

              def extractMethod(stuff: Stuff, methodName: String): MethodMirror = {
              val stuffClass = stuff.getClass()
              val mirror = runtimeMirror(stuffClass.getClassLoader)
              val stuffType = mirror.classSymbol(stuffClass).toType
              val stuffClassTag = ClassTag[Stuff](mirror.runtimeClass(stuffType))
              val methodSymbol = stuffType
              .member(TermName(methodName)).asMethod
              mirror.reflect(stuff)(stuffClassTag)
              .reflectMethod(methodSymbol)
              }



            Therefore we obtained Scala-style reflection entities (i.e. finally MethodMirror) without requiring ClassTag and/or TypeTag to be passed explicitly or implicitly from the caller. Not sure, however, how it compares with the ways described in the question (i.e. passing tags from outside and pure Java) in the terms of performance.






            share|improve this answer




























              1














              Yes, there is.





              1. First, according to this answer, TypeTag[Stuff] is a strictly stronger requirement than ClassTag[Stuff]. Although we don't automatically get implicit ClassTag[Stuff] from implicit TypeTag[Stuff], we can evaluate it manually as ClassTag[Stuff](stuffTypeTag.mirror.runtimeClass(stuffTypeTag.tpe)) and then implicitly or explicitly pass it to reflect that needs it:



                import scala.reflect.ClassTag
                import scala.reflect.runtime.universe._

                def extractMethod[Stuff: TypeTag](
                stuff: Stuff,
                methodName: String): MethodMirror =
                {
                val stuffTypeTag = typeTag[Stuff]
                val mirror = stuffTypeTag.mirror
                val stuffType = stuffTypeTag.tpe
                val stuffClassTag = ClassTag[Stuff](mirror.runtimeClass(stuffType))
                val methodSymbol = stuffType
                .member(TermName(methodName)).asMethod
                mirror.reflect(stuff)(stuffClassTag)
                .reflectMethod(methodSymbol)
                }



              2. Second, mirror and stuffType can be obtained from stuff.getClass():



                import scala.reflect.ClassTag
                import scala.reflect.runtime.universe._

                def extractMethod(stuff: Stuff, methodName: String): MethodMirror = {
                val stuffClass = stuff.getClass()
                val mirror = runtimeMirror(stuffClass.getClassLoader)
                val stuffType = mirror.classSymbol(stuffClass).toType
                val stuffClassTag = ClassTag[Stuff](mirror.runtimeClass(stuffType))
                val methodSymbol = stuffType
                .member(TermName(methodName)).asMethod
                mirror.reflect(stuff)(stuffClassTag)
                .reflectMethod(methodSymbol)
                }



              Therefore we obtained Scala-style reflection entities (i.e. finally MethodMirror) without requiring ClassTag and/or TypeTag to be passed explicitly or implicitly from the caller. Not sure, however, how it compares with the ways described in the question (i.e. passing tags from outside and pure Java) in the terms of performance.






              share|improve this answer


























                1












                1








                1







                Yes, there is.





                1. First, according to this answer, TypeTag[Stuff] is a strictly stronger requirement than ClassTag[Stuff]. Although we don't automatically get implicit ClassTag[Stuff] from implicit TypeTag[Stuff], we can evaluate it manually as ClassTag[Stuff](stuffTypeTag.mirror.runtimeClass(stuffTypeTag.tpe)) and then implicitly or explicitly pass it to reflect that needs it:



                  import scala.reflect.ClassTag
                  import scala.reflect.runtime.universe._

                  def extractMethod[Stuff: TypeTag](
                  stuff: Stuff,
                  methodName: String): MethodMirror =
                  {
                  val stuffTypeTag = typeTag[Stuff]
                  val mirror = stuffTypeTag.mirror
                  val stuffType = stuffTypeTag.tpe
                  val stuffClassTag = ClassTag[Stuff](mirror.runtimeClass(stuffType))
                  val methodSymbol = stuffType
                  .member(TermName(methodName)).asMethod
                  mirror.reflect(stuff)(stuffClassTag)
                  .reflectMethod(methodSymbol)
                  }



                2. Second, mirror and stuffType can be obtained from stuff.getClass():



                  import scala.reflect.ClassTag
                  import scala.reflect.runtime.universe._

                  def extractMethod(stuff: Stuff, methodName: String): MethodMirror = {
                  val stuffClass = stuff.getClass()
                  val mirror = runtimeMirror(stuffClass.getClassLoader)
                  val stuffType = mirror.classSymbol(stuffClass).toType
                  val stuffClassTag = ClassTag[Stuff](mirror.runtimeClass(stuffType))
                  val methodSymbol = stuffType
                  .member(TermName(methodName)).asMethod
                  mirror.reflect(stuff)(stuffClassTag)
                  .reflectMethod(methodSymbol)
                  }



                Therefore we obtained Scala-style reflection entities (i.e. finally MethodMirror) without requiring ClassTag and/or TypeTag to be passed explicitly or implicitly from the caller. Not sure, however, how it compares with the ways described in the question (i.e. passing tags from outside and pure Java) in the terms of performance.






                share|improve this answer













                Yes, there is.





                1. First, according to this answer, TypeTag[Stuff] is a strictly stronger requirement than ClassTag[Stuff]. Although we don't automatically get implicit ClassTag[Stuff] from implicit TypeTag[Stuff], we can evaluate it manually as ClassTag[Stuff](stuffTypeTag.mirror.runtimeClass(stuffTypeTag.tpe)) and then implicitly or explicitly pass it to reflect that needs it:



                  import scala.reflect.ClassTag
                  import scala.reflect.runtime.universe._

                  def extractMethod[Stuff: TypeTag](
                  stuff: Stuff,
                  methodName: String): MethodMirror =
                  {
                  val stuffTypeTag = typeTag[Stuff]
                  val mirror = stuffTypeTag.mirror
                  val stuffType = stuffTypeTag.tpe
                  val stuffClassTag = ClassTag[Stuff](mirror.runtimeClass(stuffType))
                  val methodSymbol = stuffType
                  .member(TermName(methodName)).asMethod
                  mirror.reflect(stuff)(stuffClassTag)
                  .reflectMethod(methodSymbol)
                  }



                2. Second, mirror and stuffType can be obtained from stuff.getClass():



                  import scala.reflect.ClassTag
                  import scala.reflect.runtime.universe._

                  def extractMethod(stuff: Stuff, methodName: String): MethodMirror = {
                  val stuffClass = stuff.getClass()
                  val mirror = runtimeMirror(stuffClass.getClassLoader)
                  val stuffType = mirror.classSymbol(stuffClass).toType
                  val stuffClassTag = ClassTag[Stuff](mirror.runtimeClass(stuffType))
                  val methodSymbol = stuffType
                  .member(TermName(methodName)).asMethod
                  mirror.reflect(stuff)(stuffClassTag)
                  .reflectMethod(methodSymbol)
                  }



                Therefore we obtained Scala-style reflection entities (i.e. finally MethodMirror) without requiring ClassTag and/or TypeTag to be passed explicitly or implicitly from the caller. Not sure, however, how it compares with the ways described in the question (i.e. passing tags from outside and pure Java) in the terms of performance.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 12:29









                SashaSasha

                1,6431529




                1,6431529
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431065%2fcalling-method-via-reflection-in-scala%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith