Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SBT scala猴子补丁库_Java_Scala_Sbt_Monkeypatching - Fatal编程技术网

Java SBT scala猴子补丁库

Java SBT scala猴子补丁库,java,scala,sbt,monkeypatching,Java,Scala,Sbt,Monkeypatching,我想用带有更多参数的自定义类替换库中的case类 我不想从图书馆排除任何东西。我正在做的是在我的项目上创建一个具有相同包名的类,但它在运行时引发了错误 例如: 库:mamilo.rosa.jar:/com/mamilo/rosa/CaseClassA.scala 我的项目:src/scala/com/mamilo/rosa/CaseClassA.scala 我要做的是从库中删除该类或用我的类替换它,但它仍会引发运行时错误: java.lang.NoSuchMethodError: com.m

我想用带有更多参数的自定义类替换库中的case类

我不想从图书馆排除任何东西。我正在做的是在我的项目上创建一个具有相同包名的类,但它在运行时引发了错误

例如:

  • 库:
    mamilo.rosa.jar:/com/mamilo/rosa/CaseClassA.scala
  • 我的项目:
    src/scala/com/mamilo/rosa/CaseClassA.scala
我要做的是从库中删除该类或用我的类替换它,但它仍会引发运行时错误:

java.lang.NoSuchMethodError: com.mamilo.rosa.CaseClassA.<init>(Lscala/collection/Seq;...)
java.lang.NoSuchMethodError:com.mamilo.rosa.CaseClassA(Lscala/collection/Seq;…)
编辑 我正在尝试向该案例类添加一个新参数:


它将被使用并转换为HTTP请求(我也将尝试覆盖该请求)。我想做的是在他的库中添加一些更改,当它变好时,我可以提交一份带有一些更改的PR,但我在包含此库作为依赖项的项目中这样做。

您可以定义从它们的类型到您的类型的隐式转换,反之亦然,类似这样:

// All of their existing stuff
case class TheirType(a: Int, b: String)
def theirFunction(x: TheirType) = Unit

// Your new case class
case class YourType(a: Int, b: String, c: Boolean)
def yourFunction(yourType: YourType) = Unit

// Define the implicit functions to convert from one type to another (and back)
implicit def theirsToYours(t: TheirType): YourType = YourType(t.a, t.b, true /* You will need to supply some value here */)
implicit def yoursToTheirs(y: YourType): TheirType = TheirType(y.a, y.b)

// You can now pass your type to their functions and vice-versa
val y = YourType(10, "abc", false)
theirFunction(y)

val t = TheirType(99, "123")
yourFunction(t)
object Implicits {
  implicit class RichSearchDefiniton(sd: SearchDefinition) {
    // Define methods here that you'd like to use on `SearchDefinition`
    def printSomething: Unit = println("This is an example of enriching a library.")
  }
}

这里的缺点是,您需要为新参数确定一些默认值,因为它们的现有函数无法自行创建该值。

这里有两个选项:

1) 添加一个
隐式类
转换,例如
RichSearchDefinition
,当隐式转换在范围内时,它将允许您使用自己的方法。这被称为“丰富我的库”(有时也被称为“皮条客我的库”)模式。代码大致如下所示:

// All of their existing stuff
case class TheirType(a: Int, b: String)
def theirFunction(x: TheirType) = Unit

// Your new case class
case class YourType(a: Int, b: String, c: Boolean)
def yourFunction(yourType: YourType) = Unit

// Define the implicit functions to convert from one type to another (and back)
implicit def theirsToYours(t: TheirType): YourType = YourType(t.a, t.b, true /* You will need to supply some value here */)
implicit def yoursToTheirs(y: YourType): TheirType = TheirType(y.a, y.b)

// You can now pass your type to their functions and vice-versa
val y = YourType(10, "abc", false)
theirFunction(y)

val t = TheirType(99, "123")
yourFunction(t)
object Implicits {
  implicit class RichSearchDefiniton(sd: SearchDefinition) {
    // Define methods here that you'd like to use on `SearchDefinition`
    def printSomething: Unit = println("This is an example of enriching a library.")
  }
}
无论您在何处需要此功能,都可以简单地导入隐式转换:
import mypackage.Implicits.\u


2) 在本地对库进行更改,将版本号更改为类似于
0.0.1-LOCAL
,然后使用
sbt publishLocal
发布库的本地副本。在项目中,您可以依赖此本地发布的库。当您对它的工作感到满意时,您可以提交一个包含更改的请求。这里需要注意的一点是,如果通过另一个依赖项传递包含
elastic4s
,那么您必须在
build.sbt
文件中添加它,您可以提供更多上下文吗?当一个项目有两个(二进制)不兼容的依赖项时,我见过类似的错误,例如,同一个库的两个不同版本。完成后,我添加了更多上下文(我想)