package org.apache.spark

/**
 * Access Spark internals.
 */
object EvilSparkContext {
  def stopActiveSparkContext(): Unit = {
    // This is very slow and creates a SparkContext if one doesn't exist
    // it is less than ideal but the active context is hidden from us in 2.X
    SparkContext.getOrCreate().stop()
  }
}

package org.apache.spark

import java.util.concurrent.atomic.AtomicReference

/**
 * Access Spark internals.
 */
object EvilSparkContext {
  def stopActiveSparkContext(): Unit = {
    val declaredFields = classOf[SparkContext$].getDeclaredFields()
    declaredFields.foreach{field => field.setAccessible(true) }
    val activeContextField =  declaredFields.filter(_.getName.contains("active"))
    val activeContextValue = activeContextField.map(field => field.get(SparkContext$.MODULE$))
    val activeContextRef = activeContextValue.filter(ctx => ctx != null && ctx.isInstanceOf[AtomicReference[_]])
    activeContextRef.foreach(ctx => Option(ctx.asInstanceOf[AtomicReference[SparkContext]].get()).foreach(_.stop()))
  }
}

Use reflection internals in pre-2.2 land instead
Use reflection internals in pre-2.2 land instead
