scala-reflect

java.lang.NoSuchMethodException: scala.collection.immutable.$colon$colon

你离开我真会死。 提交于 2021-02-19 06:30:55
问题 I am trying to call a function using a variable as String type dynamically i.e. variable will contain a function name as String. So, I need to call a function using that variable. So, I am using Scala Reflection. It is working if the function is accepting datatype as Sting, But it is throwing error List[Map[String, Double]] I used below link to refer a code Is there any Scala feature that allows you to call a method whose name is stored in a string? and below is a test program which I am

How to get underlying constant type from singleton type with Scala reflection API

不想你离开。 提交于 2021-02-10 18:32:49
问题 import scala.reflect.runtime.universe._ val a: 42 = 42 val t: Type = typeOf[a.type] assert(getConstantType(t).get =:= typeOf[42]) def getConstantType(t: Type): Option[ConstantType] = ??? How could I generally implement getConstantType so that the above assertion passes? I assumed that something like this was possible since the assertion below passes: assert(t <:< typeOf[42]) t.widen goes too far as it return Int . I'm looking for something that returns Int(42) . 回答1: How about assert(t

How to get underlying constant type from singleton type with Scala reflection API

半腔热情 提交于 2021-02-10 18:32:48
问题 import scala.reflect.runtime.universe._ val a: 42 = 42 val t: Type = typeOf[a.type] assert(getConstantType(t).get =:= typeOf[42]) def getConstantType(t: Type): Option[ConstantType] = ??? How could I generally implement getConstantType so that the above assertion passes? I assumed that something like this was possible since the assertion below passes: assert(t <:< typeOf[42]) t.widen goes too far as it return Int . I'm looking for something that returns Int(42) . 回答1: How about assert(t

In Scala, why it is impossible to infer TypeTag from type alias or dependent type?

假如想象 提交于 2021-02-08 03:30:39
问题 I have a simple scala program to test the Capability of Scala to infer type classes: import scala.reflect.ClassTag object InferTypeTag { import org.apache.spark.sql.catalyst.ScalaReflection.universe._ def infer(): Unit = { type U = (Int, String) val ttg1 = implicitly[TypeTag[(Int, String)]] val ttg2 = implicitly[TypeTag[U]] val ctg = implicitly[ClassTag[U]] } } ttg1 got inferred without problem. ttg2 triggered the following compilation error: Error:(14, 26) No TypeTag available for U val ttg2

Calling a method from Annotation using reflection

大城市里の小女人 提交于 2021-02-05 11:25:26
问题 I have Sample class with Size annotation case class Sample( attr: SomeTypeA @Size(value = 50) name: SomeTypeB) This Size annotation is a class that implements AnnotationInterface trait AnnotationInterface[T] { def getValue: T } class Size(value: Int) extends StaticAnnotation with AnnotationInterface[Int] { override def getValue: Int = value } And I have Extractor which is responsible for extracting class members using reflection class Extractor[A](implicit tt: TypeTag[A], ct: ClassTag[A] ) {

Load Dataset from Dynamically generated Case Class

蹲街弑〆低调 提交于 2021-02-04 08:06:24
问题 What is Needed: number of tables in source database are changing rapidly and thus I don't want to edit case classes so I dynamically generate them through SCALA code and put in package. But now not able to read it dynamically. If this works than I would parse "com.example.datasources.fileSystemSource.schema.{}" as object schema members in loop What has already been Done: I have some case classes dynamically generated from schema of database tables as below: object schema{ case class Users

In a scala macro, how to get the full name that a class will have at runtime?

隐身守侯 提交于 2021-01-29 22:08:03
问题 The intention is to get at run-time some info of particular classes that is available only at compile-time. My approach was to generate the info at compile time with a macro that expands to a map that contains the info indexed by the runtime class name. Something like this: object macros { def subClassesOf[T]: Map[String, Info] = macro subClassesOfImpl[T]; def subClassesOfImpl[T: ctx.WeakTypeTag](ctx: blackbox.Context): ctx.Expr[Map[String, Info]] = { import ctx.universe._ val classSymbol =

scala AST Select node can't find members inherited from parent

女生的网名这么多〃 提交于 2021-01-29 19:25:05
问题 I'm writing a macro called assign whose job is to assign values from members of one instance to another with a particular prefix added to the name of the member. For example, I have an instance with members named my_prefix_data, my_prefix_rden, ... and I want to assign values to these members from another instance with corresponding members named data, rden, ... . I've made a prototype version of the macro that just handles the my_prefix_data <- data assignment. The assignments are to be made

How can I reflect on a field annotation (Java) in a Scala program?

浪子不回头ぞ 提交于 2021-01-29 14:21:35
问题 I'm using Scala 2.13 and I know there's been a lot deprecated since older versions. I've got this annotation: @Inherited @Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface Foo { int index() default 0; } (I know... I've got lots of ElementTypes there, but I'm struggling to see where this pops up in reflection so wanted to maximize my chances of a hit!) Used like this: case class Person(name: String, @Foo(index = 3) age:

How to reflect concrete return types for methods of classes defined at runtime using the Scala ToolBox?

别等时光非礼了梦想. 提交于 2021-01-29 07:07:43
问题 When reflecting the foo() method of the class Cls we can easily get the concrete return type using the following. class Cls { def foo() = List("A", "B") } val classType = ru.typeOf[Cls] val classMirror = toolbox.mirror.reflectClass(classType.typeSymbol.asClass) val ctorSymbol = classType.decl(ru.termNames.CONSTRUCTOR).asMethod val methodSymb = classType.decl(ru.TermName("foo")).asMethod val ctor = classMirror.reflectConstructor(ctorSymbol) val instance = ctor() val im = toolbox.mirror.reflect