简体   繁体   English

使用sbt console / spark-shell重写类

[英]overriding classes using sbt console/spark-shell

So I've recently gotten much more interested in developing using sbt console/spark-shell and I had a question about working with existing packages/jars. 所以我最近对开发使用sbt console / spark-shell感兴趣了,我对使用现有的包/ jar有疑问。 So I know that you can import jars that that it's possible to override classes, but I'm wondering: Is it possible to override classes and force all other classes to point to that overriden class? 所以我知道你可以导入罐子,它可以覆盖类,但我想知道:是否有可能覆盖类并强制所有其他类指向该覆盖类?

So if I have 所以,如果我有

class Bar(){ 
    def a() = { (new Foo).blah()}
}

and I override Foo, is there a way I can do that so that I don't need to also override Bar? 并且我重写Foo,有没有办法可以做到这一点,所以我不需要也覆盖Bar?

Let's explain this with a timeline: 让我们用时间表解释一下:

1. class X { def t = 1 }

2. class Y {
      def x: X = new X
   }

Up to here the definition of class Y at line 2 refers to the definition of X in line 1. 到此为止,第2行的Y类定义是指第1行中X的定义。

3. class X { def t = 2 } 

Now, class Y from line 2 still refers to X from line 1. This is how the REPL works. 现在,第2行的Y类仍然从第1行引用X.这就是REPL的工作原理。 Changes are effective forward in time not backwards. 变更是有效的,而不是倒退。

4. class Y {
     def x: X = new X
   }

Now, as you expect, the new Y at line 4 will refer to the new X from line 3. 现在,正如您所期望的那样,第4行的新Y将引用第3行的新X.

Normally, you'd do that by replacing the class in your classpath. 通常,您可以通过替换类路径中的类来实现。 If the new version is binary-compatible, you could even re-run without re-compiling. 如果新版本是二进制兼容的,您甚至可以重新运行而无需重新编译。

The couple of hitches are that the REPL compiler is resident, and the class is in a specific package (eg, $line8 ). 这两个故障是REPL编译器是驻留的,并且该类位于特定的包中(例如, $line8 )。 You'd need a fresh compiler to use the refreshed package. 您需要一个新的编译器来使用刷新的包。

There are open tickets to retain or discard $line packages when resetting the compiler. 在重置编译器时,有打开的票证可以保留或丢弃$line包。 The other missing piece is to compile the new version of the class in the appropriate package, or conversely to regenerate the consuming class. 另一个缺失的部分是在适当的包中编译新版本的类,或者反过来重新生成消费类。

Note that the :require command lets you add a jar but not replace classes. 请注意:require命令允许您添加jar但不替换类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM