Skip to content

Instantly share code, notes, and snippets.

@zinking
Created September 26, 2021 02:30
Show Gist options
  • Save zinking/7a4d4b74d605759a3a5eee40ac49eb1c to your computer and use it in GitHub Desktop.
Save zinking/7a4d4b74d605759a3a5eee40ac49eb1c to your computer and use it in GitHub Desktop.
reflective call SCALA [object] method
import scala.reflect.runtime.universe._;
import org.apache.spark.sql.SparkSession;
val field = spark.getClass.getDeclaredField("extensions");
field.setAccessible(true);
val extensions = field.get(spark).asInstanceOf[org.apache.spark.sql.SparkSessionExtensions];
val tpe = typeOf[org.apache.spark.sql.SparkSession];
val m = scala.reflect.runtime.currentMirror;
val im = m.reflect(m.reflectModule(tpe.typeSymbol.asClass.companion.asModule).instance);
val method = tpe.companion.decl(TermName("applyExtensions")).asMethod;\n
im.reflectMethod(method)("io.delta.sql.DeltaSparkSessionExtension" :: Nil, extensions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment