[英]How to access getter method in calling Java class or Java interface using lombok?
Using JDK 1.8 and the following lombok version:使用 JDK 1.8 和以下 lombok 版本:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
Have the following Java interface:有如下Java接口:
package com.myapp.bridge;
public interface MyBridge {
public void run();
}
Implementing class:实施 class:
package com.myapp.bridge;
import lombok.Getter;
import lombok.Setter;
public class MyBridgeHandler implements MyBridge {
@Getter(AccessLevel.PACKAGE)
@Setter(AccessLevel.PACKAGE)
public String value;
public void run() {
String msg = "hello";
setValue(msg);
// This works and prints hello ten times.
for (int i = 0; i < 10; i++) {
System.out.println(getValue());
}
}
}
Calling class:拨打 class:
package com.myapp.bridge;
import lombok.Getter;
public class App {
@Getter
private MyBridge bridge = new MyBridgeHandler();
public void printValue() {
// IntelliJ IDEA doesn't seem to let me access this getter?
String aValue = getBridge().getValue();
System.out.println(aValue);
}
public static void main(String args []) {
App app = new App();
app.printValue();
}
}
Question(s):问题):
How do I obtain / access the MyBridgetHandler.getValue()
method inside my calling class (IntelliJ IDEA's code completion doesn't even display / or opt for this method after I put a dot after getBridge().
inside my printValue
method)?如何在我的调用 class 中获取/访问
MyBridgetHandler.getValue()
方法(IntelliJ IDEA 的代码完成甚至不显示/或在我在getBridge().
在我的printValue
方法中)?
Should it be placed inside the MyBridge
interface?它应该放在
MyBridge
界面内吗? If so, how would the implementation look like?如果是这样,实施情况如何?
Note, this is not an IntelliJ specific question as my other POJOs have lombok-based @getter
& @setter
annotations and work inside calling classes via IntelliJ IDEA.请注意,这不是 IntelliJ 特定的问题,因为我的其他 POJO 具有基于 lombok 的
@getter
和@setter
注释,并通过 IntelliJ IDEA 在调用类中工作。
Am able to see hello
be printed to stdout 10 times so it is getting set.我能够看到
hello
被打印到 stdout 10 次,所以它正在设置。
Seems like a visibility / accessibility issue.似乎是可见性/可访问性问题。
Fixed / found the solution...固定/找到解决方案...
Inside the MyBridge
interface, I added the following method:在
MyBridge
界面中,我添加了以下方法:
package com.myapp.bridge;
public interface MyBridge {
public void run();
String getValue();
}
Implemented it like this inside MyBridgeHandler
Java class:在
MyBridgeHandler
Java class 中这样实现:
package com.myapp.bridge;
import lombok.Getter;
import lombok.Setter;
public class MyBridgeHandler implements MyBridge {
@Setter(AccessLevel.PACKAGE)
public String value;
public void run() {
String msg = "hello";
setValue(msg);
// This works and prints hello ten times.
for (int i = 0; i < 10; i++) {
System.out.println(getValue());
}
}
public String getValue() {
return value;
}
}
Voila!瞧! I am able to access it now inside the
App.printValue()
method.我现在可以在
App.printValue()
方法中访问它。
Using JDK 1.8 and the following lombok version:使用 JDK 1.8 和以下 lombok 版本:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
Have the following Java interface:有如下Java接口:
package com.myapp.bridge;
public interface MyBridge {
public void run();
}
Implementing class:实现 class:
package com.myapp.bridge;
import lombok.Getter;
import lombok.Setter;
public class MyBridgeHandler implements MyBridge {
@Getter(AccessLevel.PACKAGE)
@Setter(AccessLevel.PACKAGE)
public String value;
public void run() {
String msg = "hello";
setValue(msg);
// This works and prints hello ten times.
for (int i = 0; i < 10; i++) {
System.out.println(getValue());
}
}
}
Calling class:调用 class:
package com.myapp.bridge;
import lombok.Getter;
public class App {
@Getter
private MyBridge bridge = new MyBridgeHandler();
public void printValue() {
// IntelliJ IDEA doesn't seem to let me access this getter?
String aValue = getBridge().getValue();
System.out.println(aValue);
}
public static void main(String args []) {
App app = new App();
app.printValue();
}
}
Question(s):问题):
How do I obtain / access the MyBridgetHandler.getValue()
method inside my calling class (IntelliJ IDEA's code completion doesn't even display / or opt for this method after I put a dot after getBridge().
inside my printValue
method)?如何在我的调用 class 中获取/访问
MyBridgetHandler.getValue()
方法(在我的printValue
方法中的getBridge().
之后放置一个点后,IntelliJ IDEA 的代码完成甚至不显示/或选择此方法)?
Should it be placed inside the MyBridge
interface?是否应该放在
MyBridge
界面中? If so, how would the implementation look like?如果是这样,实施将如何?
Note, this is not an IntelliJ specific question as my other POJOs have lombok-based @getter
& @setter
annotations and work inside calling classes via IntelliJ IDEA.请注意,这不是 IntelliJ 特定的问题,因为我的其他 POJO 具有基于 lombok 的
@getter
和@setter
注释,并通过 IntelliJ IDEA 在调用类中工作。
Am able to see hello
be printed to stdout 10 times so it is getting set.我能够看到
hello
被打印到标准输出 10 次,所以它正在设置中。
Seems like a visibility / accessibility issue.似乎是可见性/可访问性问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.