为什么在使用混合项目时,Scala case类中的Lombok在Java类中无法访问?

问题描述 投票:1回答:1

我有一个简单的Spring引导程序,有一个Scala类......现在我试图从一个java类中访问getter和setter函数,就像这样。

case class TestThing(val name: String ){
  @Getter
  @Setter
  var value = null
  def getMap = {
    val list: List[Item] = List(Item("1", "Foo"), Item("2", "Bar"))
    val map = list.map(item => item.key -> item).toMap
    map("1")
  }
}

现在我试图从一个java类中访问getter和setter函数,就像这样...。

@GetMapping("/other")
public String index(){
    TestThing thing = new TestThing("My Name");
    thing.setValue("Test");
    return "Hello World from me "+thing.getMap().value()+"||"+thing.getValue();
}

这个 thing.getMap() 工作正常,但我的getters和setters得到以下编译错误......

  error: cannot find symbol
        return "Hello World from me "+thing.getMap().value()+"||"+thing.getValue();
                                                                       ^
  symbol:   method getValue()
  location: variable thing of type TestThing

我遗漏了什么?我发现这个问题(编译JavaScala混合项目和Lombok时出错。)但它是相反的,似乎没有帮助。

scala lombok
1个回答
2
投票

Lombok不能和Scala一起工作。就这么简单。(在你链接的问题中甚至描述了原因)。Scala类中的@Getter和@Setter注解永远不会被处理,访问器也不会被生成。

它也是完全不需要的,因为case类会生成。toString, equals, hashcode getters和setter。如果你想拥有Java Bean访问器,你可以使用 @BeanProperty 注释。

import scala.beans.BeanProperty

case class TestThing(val name: String ){
  @BeanProperty
  var value: String = null
}
val test = TestThing("test")
test.getValue // null
test.setValue("test")
test.getValue // "test"
© www.soinside.com 2019 - 2024. All rights reserved.